cn.nukkit.item.enchantment.Enchantment Java Examples

The following examples show how to use cn.nukkit.item.enchantment.Enchantment. 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: BlockOreRedstone.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Item[] getDrops(Item item) {
    if (item.isPickaxe() && item.getTier() >= ItemTool.TIER_IRON) {
        int count = new Random().nextInt(2) + 4;

        Enchantment fortune = item.getEnchantment(Enchantment.ID_FORTUNE_DIGGING);
        if (fortune != null && fortune.getLevel() >= 1) {
            count += new Random().nextInt(fortune.getLevel() + 1);
        }

        return new Item[]{
                new ItemRedstone(0, count)
        };
    } else {
        return new Item[0];
    }
}
 
Example #2
Source File: BlockOreLapis.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Item[] getDrops(Item item) {
    if (item.isPickaxe() && item.getTier() >= ItemTool.TIER_STONE) {
        int count = 4 + ThreadLocalRandom.current().nextInt(5);
        Enchantment fortune = item.getEnchantment(Enchantment.ID_FORTUNE_DIGGING);
        if (fortune != null && fortune.getLevel() >= 1) {
            int i = ThreadLocalRandom.current().nextInt(fortune.getLevel() + 2) - 1;

            if (i < 0) {
                i = 0;
            }

            count *= (i + 1);
        }

        return new Item[]{
                new ItemDye(4, new Random().nextInt(4) + 4)
        };
    } else {
        return new Item[0];
    }
}
 
Example #3
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 #4
Source File: Block.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public double getBreakTime(Item item, Player player) {
    Objects.requireNonNull(item, "getBreakTime: Item can not be null");
    Objects.requireNonNull(player, "getBreakTime: Player can not be null");
    double blockHardness = getHardness();
    boolean correctTool = correctTool0(getToolType(), item);
    boolean canHarvestWithHand = canHarvestWithHand();
    int blockId = getId();
    int itemToolType = toolType0(item);
    int itemTier = item.getTier();
    int efficiencyLoreLevel = Optional.ofNullable(item.getEnchantment(Enchantment.ID_EFFICIENCY))
            .map(Enchantment::getLevel).orElse(0);
    int hasteEffectLevel = Optional.ofNullable(player.getEffect(Effect.HASTE))
            .map(Effect::getAmplifier).orElse(0);
    boolean insideOfWaterWithoutAquaAffinity = player.isInsideOfWater() &&
            Optional.ofNullable(player.getInventory().getHelmet().getEnchantment(Enchantment.ID_WATER_WORKER))
                    .map(Enchantment::getLevel).map(l -> l >= 1).orElse(false);
    boolean outOfWaterButNotOnGround = (!player.isInsideOfWater()) && (!player.isOnGround());
    return breakTime0(blockHardness, correctTool, canHarvestWithHand, blockId, itemToolType, itemTier,
            efficiencyLoreLevel, hasteEffectLevel, insideOfWaterWithoutAquaAffinity, outOfWaterButNotOnGround);
}
 
Example #5
Source File: EntityHumanType.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setOnFire(int seconds) {
    int level = 0;

    for (Item armor : this.inventory.getArmorContents()) {
        Enchantment fireProtection = armor.getEnchantment(Enchantment.ID_PROTECTION_FIRE);

        if (fireProtection != null && fireProtection.getLevel() > 0) {
            level = Math.max(level, fireProtection.getLevel());
        }
    }

    seconds = (int) (seconds * (1 - level * 0.15));

    super.setOnFire(seconds);
}
 
Example #6
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 #7
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 #8
Source File: BlockOreCoal.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Item[] getDrops(Item item) {
    if (item.isPickaxe() && item.getTier() >= ItemTool.TIER_WOODEN) {
        int count = 1;
        Enchantment fortune = item.getEnchantment(Enchantment.ID_FORTUNE_DIGGING);
        if (fortune != null && fortune.getLevel() >= 1) {
            int i = ThreadLocalRandom.current().nextInt(fortune.getLevel() + 2) - 1;

            if (i < 0) {
                i = 0;
            }

            count = i + 1;
        }

        return new Item[]{
                new ItemCoal(0, count)
        };
    } else {
        return new Item[0];
    }
}
 
Example #9
Source File: BlockOreRedstone.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Item[] getDrops(Item item) {
    if (item.isPickaxe() && item.getTier() >= ItemTool.TIER_IRON) {
        int count = new Random().nextInt(2) + 4;

        Enchantment fortune = item.getEnchantment(Enchantment.ID_FORTUNE_DIGGING);
        if (fortune != null && fortune.getLevel() >= 1) {
            count += new Random().nextInt(fortune.getLevel() + 1);
        }

        return new Item[]{
                new ItemRedstone(0, count)
        };
    } else {
        return new Item[0];
    }
}
 
Example #10
Source File: Block.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public double getBreakTime(Item item, Player player) {
    Objects.requireNonNull(item, "getBreakTime: Item can not be null");
    Objects.requireNonNull(player, "getBreakTime: Player can not be null");
    double blockHardness = getHardness();

    if (blockHardness == 0) {
        return 0;
    }

    boolean correctTool = correctTool0(getToolType(), item);
    boolean canHarvestWithHand = canHarvestWithHand();
    int blockId = getId();
    int itemToolType = toolType0(item);
    int itemTier = item.getTier();
    int efficiencyLoreLevel = Optional.ofNullable(item.getEnchantment(Enchantment.ID_EFFICIENCY))
            .map(Enchantment::getLevel).orElse(0);
    int hasteEffectLevel = Optional.ofNullable(player.getEffect(Effect.HASTE))
            .map(Effect::getAmplifier).orElse(0);
    boolean insideOfWaterWithoutAquaAffinity = player.isInsideOfWater() &&
            Optional.ofNullable(player.getInventory().getHelmet().getEnchantment(Enchantment.ID_WATER_WORKER))
                    .map(Enchantment::getLevel).map(l -> l >= 1).orElse(false);
    boolean outOfWaterButNotOnGround = (!player.isInsideOfWater()) && (!player.isOnGround());
    return breakTime0(blockHardness, correctTool, canHarvestWithHand, blockId, itemToolType, itemTier,
            efficiencyLoreLevel, hasteEffectLevel, insideOfWaterWithoutAquaAffinity, outOfWaterButNotOnGround);
}
 
Example #11
Source File: BlockOreEmerald.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Item[] getDrops(Item item) {
    if (item.isPickaxe() && item.getTier() >= ItemTool.TIER_IRON) {
        int count = 1;
        Enchantment fortune = item.getEnchantment(Enchantment.ID_FORTUNE_DIGGING);
        if (fortune != null && fortune.getLevel() >= 1) {
            int i = ThreadLocalRandom.current().nextInt(fortune.getLevel() + 2) - 1;

            if (i < 0) {
                i = 0;
            }

            count = i + 1;
        }

        return new Item[]{
                new ItemEmerald(0, count)
        };
    } else {
        return new Item[0];
    }
}
 
Example #12
Source File: BlockOreEmerald.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Item[] getDrops(Item item) {
    if (item.isPickaxe() && item.getTier() >= ItemTool.TIER_IRON) {
        int count = 1;
        Enchantment fortune = item.getEnchantment(Enchantment.ID_FORTUNE_DIGGING);
        if (fortune != null && fortune.getLevel() >= 1) {
            int i = ThreadLocalRandom.current().nextInt(fortune.getLevel() + 2) - 1;

            if (i < 0) {
                i = 0;
            }

            count = i + 1;
        }

        return new Item[]{
                new ItemEmerald(0, count)
        };
    } else {
        return new Item[0];
    }
}
 
Example #13
Source File: BlockOreLapis.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Item[] getDrops(Item item) {
    if (item.isPickaxe() && item.getTier() >= ItemTool.TIER_STONE) {
        int count = 4 + ThreadLocalRandom.current().nextInt(5);
        Enchantment fortune = item.getEnchantment(Enchantment.ID_FORTUNE_DIGGING);
        if (fortune != null && fortune.getLevel() >= 1) {
            int i = ThreadLocalRandom.current().nextInt(fortune.getLevel() + 2) - 1;

            if (i < 0) {
                i = 0;
            }

            count *= (i + 1);
        }

        return new Item[]{
                new ItemDye(4, new Random().nextInt(4) + 4)
        };
    } else {
        return new Item[0];
    }
}
 
Example #14
Source File: BlockOreQuartz.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Item[] getDrops(Item item) {
    if (item.isPickaxe() && item.getTier() >= ItemTool.TIER_WOODEN) {
        int count = 1;
        Enchantment fortune = item.getEnchantment(Enchantment.ID_FORTUNE_DIGGING);
        if (fortune != null && fortune.getLevel() >= 1) {
            int i = ThreadLocalRandom.current().nextInt(fortune.getLevel() + 2) - 1;

            if (i < 0) {
                i = 0;
            }

            count = i + 1;
        }

        return new Item[]{
                new ItemQuartz(0, count)
        };
    } else {
        return new Item[0];
    }
}
 
Example #15
Source File: EntityHumanType.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setOnFire(int seconds) {
    int level = 0;

    for (Item armor : this.inventory.getArmorContents()) {
        Enchantment fireProtection = armor.getEnchantment(Enchantment.ID_PROTECTION_FIRE);

        if (fireProtection != null && fireProtection.getLevel() > 0) {
            level = Math.max(level, fireProtection.getLevel());
        }
    }

    seconds = (int) (seconds * (1 - level * 0.15));

    super.setOnFire(seconds);
}
 
Example #16
Source File: BlockOreDiamond.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Item[] getDrops(Item item) {
    if (item.isPickaxe() && item.getTier() >= ItemTool.TIER_IRON) {
        int count = 1;
        Enchantment fortune = item.getEnchantment(Enchantment.ID_FORTUNE_DIGGING);
        if (fortune != null && fortune.getLevel() >= 1) {
            int i = ThreadLocalRandom.current().nextInt(fortune.getLevel() + 2) - 1;

            if (i < 0) {
                i = 0;
            }

            count = i + 1;
        }

        return new Item[]{
                new ItemDiamond(0, count)
        };
    } else {
        return new Item[0];
    }
}
 
Example #17
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 #18
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 #19
Source File: BlockOreDiamond.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Item[] getDrops(Item item) {
    if (item.isPickaxe() && item.getTier() >= ItemTool.TIER_IRON) {
        int count = 1;
        Enchantment fortune = item.getEnchantment(Enchantment.ID_FORTUNE_DIGGING);
        if (fortune != null && fortune.getLevel() >= 1) {
            int i = ThreadLocalRandom.current().nextInt(fortune.getLevel() + 2) - 1;

            if (i < 0) {
                i = 0;
            }

            count = i + 1;
        }

        return new Item[]{
                new ItemDiamond(0, count)
        };
    } else {
        return new Item[0];
    }
}
 
Example #20
Source File: EntityHumanType.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setOnFire(int seconds) {
    int level = 0;

    for (Item armor : this.inventory.getArmorContents()) {
        Enchantment fireProtection = armor.getEnchantment(Enchantment.ID_PROTECTION_FIRE);

        if (fireProtection != null && fireProtection.getLevel() > 0) {
            level = Math.max(level, fireProtection.getLevel());
        }
    }

    seconds = (int) (seconds * (1 - level * 0.15));

    super.setOnFire(seconds);
}
 
Example #21
Source File: Block.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public double getBreakTime(Item item, Player player) {
    Objects.requireNonNull(item, "getBreakTime: Item can not be null");
    Objects.requireNonNull(player, "getBreakTime: Player can not be null");
    double blockHardness = getHardness();
    boolean correctTool = correctTool0(getToolType(), item);
    boolean canHarvestWithHand = canHarvestWithHand();
    int blockId = getId();
    int itemToolType = toolType0(item);
    int itemTier = item.getTier();
    int efficiencyLoreLevel = Optional.ofNullable(item.getEnchantment(Enchantment.ID_EFFICIENCY))
            .map(Enchantment::getLevel).orElse(0);
    int hasteEffectLevel = Optional.ofNullable(player.getEffect(Effect.HASTE))
            .map(Effect::getAmplifier).orElse(0);
    boolean insideOfWaterWithoutAquaAffinity = player.isInsideOfWater() &&
            Optional.ofNullable(player.getInventory().getHelmet().getEnchantment(Enchantment.ID_WATER_WORKER))
                    .map(Enchantment::getLevel).map(l -> l >= 1).orElse(false);
    boolean outOfWaterButNotOnGround = (!player.isInsideOfWater()) && (!player.isOnGround());
    return breakTime0(blockHardness, correctTool, canHarvestWithHand, blockId, itemToolType, itemTier,
            efficiencyLoreLevel, hasteEffectLevel, insideOfWaterWithoutAquaAffinity, outOfWaterButNotOnGround);
}
 
Example #22
Source File: CraftingDataPacket.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
private static int writeEnchantList(EnchantmentList list, BinaryStream stream) {
    stream.putByte((byte) list.getSize());
    for (int i = 0; i < list.getSize(); ++i) {
        EnchantmentEntry entry = list.getSlot(i);
        stream.putUnsignedVarInt(entry.getCost());
        stream.putUnsignedVarInt(entry.getEnchantments().length);
        for (Enchantment enchantment : entry.getEnchantments()) {
            stream.putUnsignedVarInt(enchantment.getId());
            stream.putUnsignedVarInt(enchantment.getLevel());
        }
        stream.putString(entry.getRandomName());
    }
    return CraftingDataPacket.ENTRY_ENCHANT_LIST;
}
 
Example #23
Source File: EntityHumanType.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected double calculateEnchantmentReduction(Item item, EntityDamageEvent source) {
    if (!item.hasEnchantments()) {
        return 0;
    }

    double reduction = 0;

    for (Enchantment ench : item.getEnchantments()) {
        reduction += ench.getDamageProtection(source);
    }

    return reduction;
}
 
Example #24
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 #25
Source File: EnchantmentProtection.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isCompatibleWith(Enchantment enchantment) {
    if (enchantment instanceof EnchantmentProtection) {
        if (((EnchantmentProtection) enchantment).protectionType == this.protectionType) {
            return false;
        }
        return ((EnchantmentProtection) enchantment).protectionType == TYPE.FALL || this.protectionType == TYPE.FALL;
    }
    return super.isCompatibleWith(enchantment);
}
 
Example #26
Source File: BlockGlowstone.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Item[] getDrops(Item item) {
    Random random = new Random();
    int count = 2 + random.nextInt(3);

    Enchantment fortune = item.getEnchantment(Enchantment.ID_FORTUNE_DIGGING);
    if (fortune != null && fortune.getLevel() >= 1) {
        count += random.nextInt(fortune.getLevel() + 1);
    }

    return new Item[]{
            new ItemGlowstoneDust(0, MathHelper.clamp(count, 1, 4))
    };
}
 
Example #27
Source File: ItemTool.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
private boolean isDurable() {
    if (!hasEnchantments()) {
        return false;
    }

    Enchantment durability = getEnchantment(Enchantment.ID_DURABILITY);
    return durability != null && durability.getLevel() > 0 && (100 / (durability.getLevel() + 1)) <= new Random().nextInt(100);
}
 
Example #28
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 #29
Source File: ItemTool.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isSilkTouch() {
    if (!hasEnchantments()) {
        return false;
    }

    Enchantment silkTouch = getEnchantment(Enchantment.ID_SILK_TOUCH);
    return silkTouch != null;
}
 
Example #30
Source File: EnchantmentProtection.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isCompatibleWith(Enchantment enchantment) {
    if (enchantment instanceof EnchantmentProtection) {
        if (((EnchantmentProtection) enchantment).protectionType == this.protectionType) {
            return false;
        }
        return ((EnchantmentProtection) enchantment).protectionType == TYPE.FALL || this.protectionType == TYPE.FALL;
    }
    return super.isCompatibleWith(enchantment);
}