Java Code Examples for cn.nukkit.item.enchantment.Enchantment#setLevel()

The following examples show how to use cn.nukkit.item.enchantment.Enchantment#setLevel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Item.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public Enchantment getEnchantment(short id) {
    if (!this.hasEnchantments()) {
        return null;
    }

    for (CompoundTag entry : this.getNamedTag().getList("ench", CompoundTag.class).getAll()) {
        if (entry.getShort("id") == id) {
            Enchantment e = Enchantment.getEnchantment(entry.getShort("id"));
            if (e != null) {
                e.setLevel(entry.getShort("lvl"));
                return e;
            }
        }
    }

    return null;
}
 
Example 2
Source File: Item.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public Enchantment[] getEnchantments() {
    if (!this.hasEnchantments()) {
        return new Enchantment[0];
    }

    List<Enchantment> enchantments = new ArrayList<>();

    ListTag<CompoundTag> ench = this.getNamedTag().getList("ench", CompoundTag.class);
    for (CompoundTag entry : ench.getAll()) {
        Enchantment e = Enchantment.getEnchantment(entry.getShort("id"));
        if (e != null) {
            e.setLevel(entry.getShort("lvl"));
            enchantments.add(e);
        }
    }

    return enchantments.stream().toArray(Enchantment[]::new);
}
 
Example 3
Source File: Item.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public Enchantment getEnchantment(short id) {
    if (!this.hasEnchantments()) {
        return null;
    }

    for (CompoundTag entry : this.getNamedTag().getList("ench", CompoundTag.class).getAll()) {
        if (entry.getShort("id") == id) {
            Enchantment e = Enchantment.getEnchantment(entry.getShort("id"));
            if (e != null) {
                e.setLevel(entry.getShort("lvl"), false);
                return e;
            }
        }
    }

    return null;
}
 
Example 4
Source File: Item.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public Enchantment[] getEnchantments() {
    if (!this.hasEnchantments()) {
        return new Enchantment[0];
    }

    List<Enchantment> enchantments = new ArrayList<>();

    ListTag<CompoundTag> ench = this.getNamedTag().getList("ench", CompoundTag.class);
    for (CompoundTag entry : ench.getAll()) {
        Enchantment e = Enchantment.getEnchantment(entry.getShort("id"));
        if (e != null) {
            e.setLevel(entry.getShort("lvl"), false);
            enchantments.add(e);
        }
    }

    return enchantments.toArray(new Enchantment[0]);
}
 
Example 5
Source File: Item.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public Enchantment getEnchantment(short id) {
    if (!this.hasEnchantments()) {
        return null;
    }

    for (CompoundTag entry : this.getNamedTag().getList("ench", CompoundTag.class).getAll()) {
        if (entry.getShort("id") == id) {
            Enchantment e = Enchantment.getEnchantment(entry.getShort("id"));
            if (e != null) {
                e.setLevel(entry.getShort("lvl"));
                return e;
            }
        }
    }

    return null;
}
 
Example 6
Source File: Item.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public Enchantment[] getEnchantments() {
    if (!this.hasEnchantments()) {
        return new Enchantment[0];
    }

    List<Enchantment> enchantments = new ArrayList<>();

    ListTag<CompoundTag> ench = this.getNamedTag().getList("ench", CompoundTag.class);
    for (CompoundTag entry : ench.getAll()) {
        Enchantment e = Enchantment.getEnchantment(entry.getShort("id"));
        if (e != null) {
            e.setLevel(entry.getShort("lvl"));
            enchantments.add(e);
        }
    }

    return enchantments.stream().toArray(Enchantment[]::new);
}
 
Example 7
Source File: EnchantCommand.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }
    if (args.length < 2) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return true;
    }
    Player player = sender.getServer().getPlayer(args[0]);
    if (player == null) {
        sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.player.notFound"));
        return true;
    }
    int enchantId;
    int enchantLevel;
    try {
        enchantId = getIdByName(args[1]);
        enchantLevel = args.length == 3 ? Integer.parseInt(args[2]) : 1;
    } catch (NumberFormatException e) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return true;
    }
    Enchantment enchantment = Enchantment.getEnchantment(enchantId);
    if (enchantment == null) {
        sender.sendMessage(new TranslationContainer("commands.enchant.notFound", String.valueOf(enchantId)));
        return true;
    }
    enchantment.setLevel(enchantLevel);
    Item item = player.getInventory().getItemInHand();
    if (item.getId() <= 0) {
        sender.sendMessage(new TranslationContainer("commands.enchant.noItem"));
        return true;
    }
    item.addEnchantment(enchantment);
    player.getInventory().setItemInHand(item);
    Command.broadcastCommandMessage(sender, new TranslationContainer("%commands.enchant.success"));
    return true;
}
 
Example 8
Source File: EnchantCommand.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }
    if (args.length < 2) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return true;
    }
    Player player = sender.getServer().getPlayer(args[0]);
    if (player == null) {
        sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.player.notFound"));
        return true;
    }
    int enchantId;
    int enchantLevel;
    try {
        enchantId = getIdByName(args[1]);
        enchantLevel = args.length == 3 ? Integer.parseInt(args[2]) : 1;
    } catch (NumberFormatException e) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return true;
    }
    Enchantment enchantment = Enchantment.getEnchantment(enchantId);
    if (enchantment == null) {
        sender.sendMessage(new TranslationContainer("commands.enchant.notFound", String.valueOf(enchantId)));
        return true;
    }
    enchantment.setLevel(enchantLevel);
    Item item = player.getInventory().getItemInHand();
    if (item.getId() <= 0) {
        sender.sendMessage(new TranslationContainer("commands.enchant.noItem"));
        return true;
    }
    item.addEnchantment(enchantment);
    player.getInventory().setItemInHand(item);
    Command.broadcastCommandMessage(sender, new TranslationContainer("%commands.enchant.success"));
    return true;
}
 
Example 9
Source File: EnchantCommand.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }
    if (args.length < 2) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return true;
    }
    Player player = sender.getServer().getPlayer(args[0]);
    if (player == null) {
        sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.player.notFound"));
        return true;
    }
    int enchantId;
    int enchantLevel;
    try {
        enchantId = getIdByName(args[1]);
        enchantLevel = args.length == 3 ? Integer.parseInt(args[2]) : 1;
    } catch (NumberFormatException e) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return true;
    }
    Enchantment enchantment = Enchantment.getEnchantment(enchantId);
    if (enchantment == null) {
        sender.sendMessage(new TranslationContainer("commands.enchant.notFound", String.valueOf(enchantId)));
        return true;
    }
    enchantment.setLevel(enchantLevel);
    Item item = player.getInventory().getItemInHand();
    if (item.getId() <= 0) {
        sender.sendMessage(new TranslationContainer("commands.enchant.noItem"));
        return true;
    }
    item.addEnchantment(enchantment);
    player.getInventory().setItemInHand(item);
    Command.broadcastCommandMessage(sender, new TranslationContainer("%commands.enchant.success"));
    return true;
}
 
Example 10
Source File: AnvilInventory.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public boolean onRename(Player player, Item resultItem) {
    Item local = getItem(TARGET);
    Item second = getItem(SACRIFICE);

    if (!resultItem.equals(local, true, false) || resultItem.getCount() != local.getCount()) {
        //Item does not match target item. Everything must match except the tags.
        return false;
    }

    if (local.equals(resultItem)) {
        //just item transaction
        return true;
    }

    if (local.getId() != 0 && second.getId() == 0) { //only rename
        local.setCustomName(resultItem.getCustomName());
        setItem(RESULT, local);
        player.getInventory().addItem(local);
        clearAll();
        player.getInventory().sendContents(player);
        sendContents(player);

        player.getLevel().addLevelSoundEvent(player, LevelSoundEventPacket.SOUND_RANDOM_ANVIL_USE);
        return true;
    } else if (local.getId() != 0 && second.getId() != 0) { //enchants combining
        if (!local.equals(second, true, false)) {
            return false;
        }

        if (local.getId() != 0 && second.getId() != 0) {
            Item result = local.clone();
            int enchants = 0;

            ArrayList<Enchantment> enchantments = new ArrayList<>(Arrays.asList(second.getEnchantments()));

            ArrayList<Enchantment> baseEnchants = new ArrayList<>();

            for (Enchantment ench : local.getEnchantments()) {
                if (ench.isMajor()) {
                    baseEnchants.add(ench);
                }
            }

            for (Enchantment enchantment : enchantments) {
                if (enchantment.getLevel() < 0 || enchantment.getId() < 0) {
                    continue;
                }

                if (enchantment.isMajor()) {
                    boolean same = false;
                    boolean another = false;

                    for (Enchantment baseEnchant : baseEnchants) {
                        if (baseEnchant.getId() == enchantment.getId())
                            same = true;
                        else {
                            another = true;
                        }
                    }

                    if (!same && another) {
                        continue;
                    }
                }

                Enchantment localEnchantment = local.getEnchantment(enchantment.getId());

                if (localEnchantment != null) {
                    int level = Math.max(localEnchantment.getLevel(), enchantment.getLevel());

                    if (localEnchantment.getLevel() == enchantment.getLevel())
                        level++;

                    enchantment.setLevel(level);
                    result.addEnchantment(enchantment);
                    continue;
                }

                result.addEnchantment(enchantment);
                enchants++;
            }

            result.setCustomName(resultItem.getCustomName());

            player.getInventory().addItem(result);
            player.getInventory().sendContents(player);
            clearAll();
            sendContents(player);

            player.getLevel().addLevelSoundEvent(player, LevelSoundEventPacket.SOUND_RANDOM_ANVIL_USE);
            return true;
        }
    }

    return false;
}
 
Example 11
Source File: AnvilInventory.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public boolean onRename(Player player, Item resultItem) {
    Item local = getItem(TARGET);
    Item second = getItem(SACRIFICE);

    if (!resultItem.equals(local, true, false) || resultItem.getCount() != local.getCount()) {
        //Item does not match target item. Everything must match except the tags.
        return false;
    }

    if (local.equals(resultItem)) {
        //just item transaction
        return true;
    }

    if (local.getId() != 0 && second.getId() == 0) { //only rename
        local.setCustomName(resultItem.getCustomName());
        setItem(RESULT, local);
        player.getInventory().addItem(local);
        clearAll();
        player.getInventory().sendContents(player);
        sendContents(player);

        player.getLevel().addSound(player, Sound.RANDOM_ANVIL_USE);
        return true;
    } else if (local.getId() != 0 && second.getId() != 0) { //enchants combining
        if (!local.equals(second, true, false)) {
            return false;
        }

        if (local.getId() != 0 && second.getId() != 0) {
            Item result = local.clone();
            int enchants = 0;

            ArrayList<Enchantment> enchantments = new ArrayList<>(Arrays.asList(second.getEnchantments()));

            ArrayList<Enchantment> baseEnchants = new ArrayList<>();

            for (Enchantment ench : local.getEnchantments()) {
                if (ench.isMajor()) {
                    baseEnchants.add(ench);
                }
            }

            for (Enchantment enchantment : enchantments) {
                if (enchantment.getLevel() < 0 || enchantment.getId() < 0) {
                    continue;
                }

                if (enchantment.isMajor()) {
                    boolean same = false;
                    boolean another = false;

                    for (Enchantment baseEnchant : baseEnchants) {
                        if (baseEnchant.getId() == enchantment.getId())
                            same = true;
                        else {
                            another = true;
                        }
                    }

                    if (!same && another) {
                        continue;
                    }
                }

                Enchantment localEnchantment = local.getEnchantment(enchantment.getId());

                if (localEnchantment != null) {
                    int level = Math.max(localEnchantment.getLevel(), enchantment.getLevel());

                    if (localEnchantment.getLevel() == enchantment.getLevel())
                        level++;

                    enchantment.setLevel(level);
                    result.addEnchantment(enchantment);
                    continue;
                }

                result.addEnchantment(enchantment);
                enchants++;
            }

            result.setCustomName(resultItem.getCustomName());

            player.getInventory().addItem(result);
            player.getInventory().sendContents(player);
            clearAll();
            sendContents(player);

            player.getLevel().addSound(player, Sound.RANDOM_ANVIL_USE);
            return true;
        }
    }

    return false;
}