Search in sources :

Example 6 with Enchantment

use of cn.nukkit.item.enchantment.Enchantment in project Nukkit by Nukkit.

the class EnchantCommand method execute.

@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;
}
Also used : Item(cn.nukkit.item.Item) Player(cn.nukkit.Player) TranslationContainer(cn.nukkit.lang.TranslationContainer) Enchantment(cn.nukkit.item.enchantment.Enchantment)

Example 7 with Enchantment

use of cn.nukkit.item.enchantment.Enchantment in project Nukkit by Nukkit.

the class AnvilInventory method onRename.

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;
}
Also used : Item(cn.nukkit.item.Item) ArrayList(java.util.ArrayList) Enchantment(cn.nukkit.item.enchantment.Enchantment)

Example 8 with Enchantment

use of cn.nukkit.item.enchantment.Enchantment in project Nukkit by Nukkit.

the class ItemBow method onReleaseUsing.

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;
}
Also used : BlockAir(cn.nukkit.block.BlockAir) EntityArrow(cn.nukkit.entity.projectile.EntityArrow) EntityShootBowEvent(cn.nukkit.event.entity.EntityShootBowEvent) ProjectileLaunchEvent(cn.nukkit.event.entity.ProjectileLaunchEvent) DoubleTag(cn.nukkit.nbt.tag.DoubleTag) ListTag(cn.nukkit.nbt.tag.ListTag) FloatTag(cn.nukkit.nbt.tag.FloatTag) Random(java.util.Random) Enchantment(cn.nukkit.item.enchantment.Enchantment) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) EntityProjectile(cn.nukkit.entity.projectile.EntityProjectile)

Example 9 with Enchantment

use of cn.nukkit.item.enchantment.Enchantment in project Nukkit by Nukkit.

the class Item method addEnchantment.

public void addEnchantment(Enchantment... enchantments) {
    CompoundTag tag;
    if (!this.hasCompoundTag()) {
        tag = new CompoundTag();
    } else {
        tag = this.getNamedTag();
    }
    ListTag<CompoundTag> ench;
    if (!tag.contains("ench")) {
        ench = new ListTag<>("ench");
        tag.putList(ench);
    } else {
        ench = tag.getList("ench", CompoundTag.class);
    }
    for (Enchantment enchantment : enchantments) {
        boolean found = false;
        for (int k = 0; k < ench.size(); k++) {
            CompoundTag entry = ench.get(k);
            if (entry.getShort("id") == enchantment.getId()) {
                ench.add(k, new CompoundTag().putShort("id", enchantment.getId()).putShort("lvl", enchantment.getLevel()));
                found = true;
                break;
            }
        }
        if (!found) {
            ench.add(new CompoundTag().putShort("id", enchantment.getId()).putShort("lvl", enchantment.getLevel()));
        }
    }
    this.setNamedTag(tag);
}
Also used : Enchantment(cn.nukkit.item.enchantment.Enchantment) CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Example 10 with Enchantment

use of cn.nukkit.item.enchantment.Enchantment in project Nukkit by Nukkit.

the class Item method getEnchantments.

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);
}
Also used : ArrayList(java.util.ArrayList) Enchantment(cn.nukkit.item.enchantment.Enchantment) CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Aggregations

Enchantment (cn.nukkit.item.enchantment.Enchantment)18 Item (cn.nukkit.item.Item)13 Random (java.util.Random)6 BlockAir (cn.nukkit.block.BlockAir)3 CompoundTag (cn.nukkit.nbt.tag.CompoundTag)3 Player (cn.nukkit.Player)2 BlockEntity (cn.nukkit.blockentity.BlockEntity)2 EntityArrow (cn.nukkit.entity.projectile.EntityArrow)2 EntityDamageByEntityEvent (cn.nukkit.event.entity.EntityDamageByEntityEvent)2 ItemBlock (cn.nukkit.item.ItemBlock)2 TranslationContainer (cn.nukkit.lang.TranslationContainer)2 Type (cn.nukkit.AdventureSettings.Type)1 cn.nukkit.block (cn.nukkit.block)1 Block (cn.nukkit.block.Block)1 BlockEntityChest (cn.nukkit.blockentity.BlockEntityChest)1 BlockEntityItemFrame (cn.nukkit.blockentity.BlockEntityItemFrame)1 BlockEntitySpawnable (cn.nukkit.blockentity.BlockEntitySpawnable)1 Command (cn.nukkit.command.Command)1 CommandSender (cn.nukkit.command.CommandSender)1 CommandDataVersions (cn.nukkit.command.data.CommandDataVersions)1