Java Code Examples for cn.nukkit.item.Item#getEnchantment()

The following examples show how to use cn.nukkit.item.Item#getEnchantment() . 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: 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 2
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 3
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 4
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 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: 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 7
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 8
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 9
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 10
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 11
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 12
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 13
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 14
Source File: BlockMelon.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 = 3 + random.nextInt(5);

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

    return new Item[]{
            new ItemMelon(0, Math.min(9, count))
    };
}
 
Example 15
Source File: BlockMelon.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 = 3 + random.nextInt(5);

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

    return new Item[]{
            new ItemMelon(0, Math.min(9, count))
    };
}
 
Example 16
Source File: EnchantmentThorns.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void doPostAttack(Entity attacker, Entity entity) {
    if (!(entity instanceof EntityHumanType)) {
        return;
    }

    EntityHumanType human = (EntityHumanType) entity;

    int thornsDamage = 0;
    Random rnd = new Random();

    for (Item armor : human.getInventory().getArmorContents()) {
        Enchantment thorns = armor.getEnchantment(Enchantment.ID_THORNS);

        if (thorns != null && thorns.getLevel() > 0) {
            int chance = thorns.getLevel() * 15;

            if (chance > 90) {
                chance = 90;
            }

            if (rnd.nextInt(100) + 1 <= chance) {
                thornsDamage += rnd.nextInt(4) + 1;
            }
        }
    }

    if (thornsDamage > 0) {
        attacker.attack(new EntityDamageEvent(attacker, DamageCause.MAGIC, rnd.nextInt(4) + 1));
    }
}
 
Example 17
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 18
Source File: Fishing.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public static Item getFishingResult(Item rod) {
    int fortuneLevel = 0;
    int lureLevel = 0;
    if (rod != null) {
        if (rod.getEnchantment(Enchantment.ID_FORTUNE_FISHING) != null) {
            fortuneLevel = rod.getEnchantment(Enchantment.ID_FORTUNE_FISHING).getLevel();
        } else if (rod.getEnchantment(Enchantment.ID_LURE) != null) {
            lureLevel = rod.getEnchantment(Enchantment.ID_LURE).getLevel();
        }
    }
    return getFishingResult(fortuneLevel, lureLevel);
}
 
Example 19
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;
}
 
Example 20
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;
}