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

The following examples show how to use cn.nukkit.item.enchantment.Enchantment#getLevel() . 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: 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 2
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 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: 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 5
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 6
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 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: 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 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: 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 13
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 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: ItemTool.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
private boolean canReduceDamage() {
    if (!hasEnchantments()) {
        return true;
    }

    Enchantment durability = getEnchantment(Enchantment.ID_DURABILITY);
    return durability != null && durability.getLevel() > 0 && (100 / (durability.getLevel() + 1)) <= new Random().nextInt(100);
}
 
Example 16
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 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: 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 19
Source File: ItemBow.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public boolean onReleaseUsing(Player player) {
    Item itemArrow = Item.get(Item.ARROW, 0, 1);

    if (player.isSurvival() && !player.getInventory().contains(itemArrow)) {
        player.getInventory().sendContents(player);
        return false;
    }

    double damage = 2;
    boolean flame = false;

    if (this.hasEnchantments()) {
        Enchantment bowDamage = this.getEnchantment(Enchantment.ID_BOW_POWER);

        if (bowDamage != null && bowDamage.getLevel() > 0) {
            damage += 0.25 * (bowDamage.getLevel() + 1);
        }

        Enchantment flameEnchant = this.getEnchantment(Enchantment.ID_BOW_FLAME);
        flame = flameEnchant != null && flameEnchant.getLevel() > 0;
    }

    CompoundTag nbt = new CompoundTag()
            .putList(new ListTag<DoubleTag>("Pos")
                    .add(new DoubleTag("", player.x))
                    .add(new DoubleTag("", player.y + player.getEyeHeight()))
                    .add(new DoubleTag("", player.z)))
            .putList(new ListTag<DoubleTag>("Motion")
                    .add(new DoubleTag("", -Math.sin(player.yaw / 180 * Math.PI) * Math.cos(player.pitch / 180 * Math.PI)))
                    .add(new DoubleTag("", -Math.sin(player.pitch / 180 * Math.PI)))
                    .add(new DoubleTag("", Math.cos(player.yaw / 180 * Math.PI) * Math.cos(player.pitch / 180 * Math.PI))))
            .putList(new ListTag<FloatTag>("Rotation")
                    .add(new FloatTag("", (player.yaw > 180 ? 360 : 0) - (float) player.yaw))
                    .add(new FloatTag("", (float) -player.pitch)))
            .putShort("Fire", player.isOnFire() || flame ? 45 * 60 : 0)
            .putDouble("damage", damage);

    int diff = (Server.getInstance().getTick() - player.getStartActionTick());
    double p = (double) diff / 20;

    double f = Math.min((p * p + p * 2) / 3, 1) * 2;
    EntityShootBowEvent entityShootBowEvent = new EntityShootBowEvent(player, this, new EntityArrow(player.chunk, nbt, player, f == 2), f);

    if (f < 0.1 || diff < 5) {
        entityShootBowEvent.setCancelled();
    }

    Server.getInstance().getPluginManager().callEvent(entityShootBowEvent);
    if (entityShootBowEvent.isCancelled()) {
        entityShootBowEvent.getProjectile().kill();
        player.getInventory().sendContents(player);
    } else {
        entityShootBowEvent.getProjectile().setMotion(entityShootBowEvent.getProjectile().getMotion().multiply(entityShootBowEvent.getForce()));
        if (player.isSurvival()) {
            Enchantment infinity;

            if (!this.hasEnchantments() || (infinity = this.getEnchantment(Enchantment.ID_BOW_INFINITY)) == null || infinity.getLevel() <= 0)
                player.getInventory().removeItem(itemArrow);
            if (!this.isUnbreakable()) {
                Enchantment durability = this.getEnchantment(Enchantment.ID_DURABILITY);
                if (!(durability != null && durability.getLevel() > 0 && (100 / (durability.getLevel() + 1)) <= new Random().nextInt(100))) {
                    this.setDamage(this.getDamage() + 1);
                    if (this.getDamage() >= 385) {
                        player.getInventory().setItemInHand(new ItemBlock(new BlockAir(), 0, 0));
                    } else {
                        player.getInventory().setItemInHand(this);
                    }
                }
            }
        }
        if (entityShootBowEvent.getProjectile() instanceof EntityProjectile) {
            ProjectileLaunchEvent projectev = new ProjectileLaunchEvent(entityShootBowEvent.getProjectile());
            Server.getInstance().getPluginManager().callEvent(projectev);
            if (projectev.isCancelled()) {
                entityShootBowEvent.getProjectile().kill();
            } else {
                entityShootBowEvent.getProjectile().spawnToAll();
                player.level.addSound(player, Sound.RANDOM_BOW, 1, 1, player.getViewers().values());
            }
        } else {
            entityShootBowEvent.getProjectile().spawnToAll();
        }
    }

    return true;
}
 
Example 20
Source File: ItemBow.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
public boolean onReleaseUsing(Player player) {
    Item itemArrow = Item.get(Item.ARROW, 0, 1);

    if (player.isSurvival() && !player.getInventory().contains(itemArrow)) {
        player.getInventory().sendContents(player);
        return false;
    }

    double damage = 2;
    boolean flame = false;

    if (this.hasEnchantments()) {
        Enchantment bowDamage = this.getEnchantment(Enchantment.ID_BOW_POWER);

        if (bowDamage != null && bowDamage.getLevel() > 0) {
            damage += 0.25 * (bowDamage.getLevel() + 1);
        }

        Enchantment flameEnchant = this.getEnchantment(Enchantment.ID_BOW_FLAME);
        flame = flameEnchant != null && flameEnchant.getLevel() > 0;
    }

    CompoundTag nbt = new CompoundTag()
            .putList(new ListTag<DoubleTag>("Pos")
                    .add(new DoubleTag("", player.x))
                    .add(new DoubleTag("", player.y + player.getEyeHeight()))
                    .add(new DoubleTag("", player.z)))
            .putList(new ListTag<DoubleTag>("Motion")
                    .add(new DoubleTag("", -Math.sin(player.yaw / 180 * Math.PI) * Math.cos(player.pitch / 180 * Math.PI)))
                    .add(new DoubleTag("", -Math.sin(player.pitch / 180 * Math.PI)))
                    .add(new DoubleTag("", Math.cos(player.yaw / 180 * Math.PI) * Math.cos(player.pitch / 180 * Math.PI))))
            .putList(new ListTag<FloatTag>("Rotation")
                    .add(new FloatTag("", (player.yaw > 180 ? 360 : 0) - (float) player.yaw))
                    .add(new FloatTag("", (float) -player.pitch)))
            .putShort("Fire", player.isOnFire() || flame ? 45 * 60 : 0)
            .putDouble("damage", damage);

    int diff = (Server.getInstance().getTick() - player.getStartActionTick());
    double p = (double) diff / 20;

    double f = Math.min((p * p + p * 2) / 3, 1) * 2;
    EntityShootBowEvent entityShootBowEvent = new EntityShootBowEvent(player, this, new EntityArrow(player.chunk, nbt, player, f == 2), f);

    if (f < 0.1 || diff < 5) {
        entityShootBowEvent.setCancelled();
    }

    Server.getInstance().getPluginManager().callEvent(entityShootBowEvent);
    if (entityShootBowEvent.isCancelled()) {
        entityShootBowEvent.getProjectile().kill();
        player.getInventory().sendContents(player);
    } else {
        entityShootBowEvent.getProjectile().setMotion(entityShootBowEvent.getProjectile().getMotion().multiply(entityShootBowEvent.getForce()));
        if (player.isSurvival()) {
            Enchantment infinity;

            if (!this.hasEnchantments() || (infinity = this.getEnchantment(Enchantment.ID_BOW_INFINITY)) == null || infinity.getLevel() <= 0)
                player.getInventory().removeItem(itemArrow);
            if (!this.isUnbreakable()) {
                Enchantment durability = this.getEnchantment(Enchantment.ID_DURABILITY);
                if (!(durability != null && durability.getLevel() > 0 && (100 / (durability.getLevel() + 1)) <= new Random().nextInt(100))) {
                    this.setDamage(this.getDamage() + 1);
                    if (this.getDamage() >= 385) {
                        player.getInventory().setItemInHand(new ItemBlock(new BlockAir(), 0, 0));
                    } else {
                        player.getInventory().setItemInHand(this);
                    }
                }
            }
        }
        if (entityShootBowEvent.getProjectile() instanceof EntityProjectile) {
            ProjectileLaunchEvent projectev = new ProjectileLaunchEvent(entityShootBowEvent.getProjectile());
            Server.getInstance().getPluginManager().callEvent(projectev);
            if (projectev.isCancelled()) {
                entityShootBowEvent.getProjectile().kill();
            } else {
                entityShootBowEvent.getProjectile().spawnToAll();
                player.level.addSound(new LaunchSound(player), player.getViewers().values());
            }
        } else {
            entityShootBowEvent.getProjectile().spawnToAll();
        }
    }

    return true;
}