Search in sources :

Example 56 with Item

use of cn.nukkit.item.Item in project Nukkit by Nukkit.

the class BaseInventory method decreaseCount.

@Override
public void decreaseCount(int slot) {
    Item item = this.getItem(slot);
    if (item.getCount() > 0) {
        item.count--;
        this.setItem(slot, item);
    }
}
Also used : Item(cn.nukkit.item.Item)

Example 57 with Item

use of cn.nukkit.item.Item in project Nukkit by Nukkit.

the class NetworkInventoryAction method createInventoryAction.

public InventoryAction createInventoryAction(Player player) {
    switch(this.sourceType) {
        case SOURCE_CONTAINER:
            if (this.windowId == ContainerIds.ARMOR) {
                // TODO: HACK!
                this.inventorySlot += 36;
                this.windowId = ContainerIds.INVENTORY;
            }
            Inventory window = player.getWindowById(this.windowId);
            if (window != null) {
                return new SlotChangeAction(window, this.inventorySlot, this.oldItem, this.newItem);
            }
            player.getServer().getLogger().debug("Player " + player.getName() + " has no open container with window ID " + this.windowId);
            return null;
        case SOURCE_WORLD:
            if (this.inventorySlot != InventoryTransactionPacket.ACTION_MAGIC_SLOT_DROP_ITEM) {
                player.getServer().getLogger().debug("Only expecting drop-item world actions from the client!");
                return null;
            }
            return new DropItemAction(this.oldItem, this.newItem);
        case SOURCE_CREATIVE:
            int type;
            switch(this.inventorySlot) {
                case InventoryTransactionPacket.ACTION_MAGIC_SLOT_CREATIVE_DELETE_ITEM:
                    type = CreativeInventoryAction.TYPE_DELETE_ITEM;
                    break;
                case InventoryTransactionPacket.ACTION_MAGIC_SLOT_CREATIVE_CREATE_ITEM:
                    type = CreativeInventoryAction.TYPE_CREATE_ITEM;
                    break;
                default:
                    player.getServer().getLogger().debug("Unexpected creative action type " + this.inventorySlot);
                    return null;
            }
            return new CreativeInventoryAction(this.oldItem, this.newItem, type);
        case SOURCE_TODO:
            // These types need special handling.
            switch(this.windowId) {
                case SOURCE_TYPE_CRAFTING_ADD_INGREDIENT:
                case SOURCE_TYPE_CRAFTING_REMOVE_INGREDIENT:
                    window = player.getCraftingGrid();
                    return new SlotChangeAction(window, this.inventorySlot, this.oldItem, this.newItem);
                case SOURCE_TYPE_CRAFTING_RESULT:
                    return new CraftingTakeResultAction(this.oldItem, this.newItem);
                case SOURCE_TYPE_CRAFTING_USE_INGREDIENT:
                    return new CraftingTransferMaterialAction(this.oldItem, this.newItem, this.inventorySlot);
                case SOURCE_TYPE_CONTAINER_DROP_CONTENTS:
                    window = player.getCraftingGrid();
                    inventorySlot = window.first(this.oldItem, true);
                    if (inventorySlot == -1) {
                        return null;
                    }
                    return new SlotChangeAction(window, inventorySlot, this.oldItem, this.newItem);
            }
            if (this.windowId >= SOURCE_TYPE_ANVIL_OUTPUT && this.windowId <= SOURCE_TYPE_ANVIL_INPUT) {
                // anvil actions
                Inventory inv = player.getWindowById(Player.ANVIL_WINDOW_ID);
                if (!(inv instanceof AnvilInventory)) {
                    player.getServer().getLogger().debug("Player " + player.getName() + " has no open anvil inventory");
                    return null;
                }
                AnvilInventory anvil = (AnvilInventory) inv;
                switch(this.windowId) {
                    case SOURCE_TYPE_ANVIL_INPUT:
                        // System.out.println("action input");
                        this.inventorySlot = 0;
                        return new SlotChangeAction(anvil, this.inventorySlot, this.oldItem, this.newItem);
                    case SOURCE_TYPE_ANVIL_MATERIAL:
                        // System.out.println("material");
                        this.inventorySlot = 1;
                        return new SlotChangeAction(anvil, this.inventorySlot, this.oldItem, this.newItem);
                    case SOURCE_TYPE_ANVIL_OUTPUT:
                        // System.out.println("action output");
                        break;
                    case SOURCE_TYPE_ANVIL_RESULT:
                        this.inventorySlot = 2;
                        anvil.clear(0);
                        anvil.clear(1);
                        anvil.setItem(2, this.oldItem);
                        // System.out.println("action result");
                        return new SlotChangeAction(anvil, this.inventorySlot, this.oldItem, this.newItem);
                }
            }
            if (this.windowId >= SOURCE_TYPE_ENCHANT_OUTPUT && this.windowId <= SOURCE_TYPE_ENCHANT_INPUT) {
                Inventory inv = player.getWindowById(Player.ENCHANT_WINDOW_ID);
                if (!(inv instanceof EnchantInventory)) {
                    player.getServer().getLogger().debug("Player " + player.getName() + " has no open enchant inventory");
                    return null;
                }
                EnchantInventory enchant = (EnchantInventory) inv;
                switch(this.windowId) {
                    case SOURCE_TYPE_ENCHANT_INPUT:
                        this.inventorySlot = 0;
                        Item local = enchant.getItem(0);
                        if (local.equals(this.newItem, true, false)) {
                            enchant.setItem(0, this.newItem);
                        }
                        break;
                    case SOURCE_TYPE_ENCHANT_MATERIAL:
                        this.inventorySlot = 1;
                        break;
                    case SOURCE_TYPE_ENCHANT_OUTPUT:
                        enchant.sendSlot(0, player);
                        // ignore?
                        return null;
                }
                return new SlotChangeAction(enchant, this.inventorySlot, this.oldItem, this.newItem);
            }
            // TODO: more stuff
            player.getServer().getLogger().debug("Player " + player.getName() + " has no open container with window ID " + this.windowId);
            return null;
        default:
            player.getServer().getLogger().debug("Unknown inventory source type " + this.sourceType);
            return null;
    }
}
Also used : Item(cn.nukkit.item.Item) EnchantInventory(cn.nukkit.inventory.EnchantInventory) AnvilInventory(cn.nukkit.inventory.AnvilInventory) EnchantInventory(cn.nukkit.inventory.EnchantInventory) AnvilInventory(cn.nukkit.inventory.AnvilInventory) Inventory(cn.nukkit.inventory.Inventory)

Example 58 with Item

use of cn.nukkit.item.Item in project Nukkit by Nukkit.

the class EntityHumanType method attack.

@Override
public boolean attack(EntityDamageEvent source) {
    if (!this.isAlive()) {
        return false;
    }
    if (source.getCause() != DamageCause.VOID && source.getCause() != DamageCause.CUSTOM && source.getCause() != DamageCause.MAGIC) {
        int points = 0;
        int epf = 0;
        int toughness = 0;
        for (Item armor : inventory.getArmorContents()) {
            points += armor.getArmorPoints();
            epf += calculateEnchantmentReduction(armor, source);
            toughness += armor.getToughness();
        }
        float originalDamage = source.getDamage();
        float finalDamage = (float) (originalDamage * (1 - Math.max(points / 5, points - originalDamage / (2 + toughness / 4)) / 25) * (1 - /*0.75 */
        epf * 0.04));
        source.setDamage(finalDamage - originalDamage, DamageModifier.ARMOR);
    // source.setDamage(source.getDamage(DamageModifier.ARMOR_ENCHANTMENTS) - (originalDamage - originalDamage * (1 - epf / 25)), DamageModifier.ARMOR_ENCHANTMENTS);
    }
    if (super.attack(source)) {
        Entity damager = null;
        if (source instanceof EntityDamageByEntityEvent) {
            damager = ((EntityDamageByEntityEvent) source).getDamager();
        }
        for (int slot = 0; slot < 4; slot++) {
            Item armor = this.inventory.getArmorItem(slot);
            if (armor.hasEnchantments()) {
                if (damager != null) {
                    for (Enchantment enchantment : armor.getEnchantments()) {
                        enchantment.doPostAttack(damager, this);
                    }
                }
                Enchantment durability = armor.getEnchantment(Enchantment.ID_DURABILITY);
                if (durability != null && durability.getLevel() > 0 && (100 / (durability.getLevel() + 1)) <= new Random().nextInt(100))
                    continue;
            }
            armor.setDamage(armor.getDamage() + 1);
            if (armor.getDamage() >= armor.getMaxDurability()) {
                inventory.setArmorItem(slot, new ItemBlock(new BlockAir()));
            } else {
                inventory.setArmorItem(slot, armor, true);
            }
        }
        return true;
    } else {
        return false;
    }
}
Also used : BlockAir(cn.nukkit.block.BlockAir) Item(cn.nukkit.item.Item) EntityDamageByEntityEvent(cn.nukkit.event.entity.EntityDamageByEntityEvent) Random(java.util.Random) Enchantment(cn.nukkit.item.enchantment.Enchantment) ItemBlock(cn.nukkit.item.ItemBlock)

Example 59 with Item

use of cn.nukkit.item.Item in project Nukkit by Nukkit.

the class EntityHumanType method setOnFire.

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

Example 60 with Item

use of cn.nukkit.item.Item in project Nukkit by Nukkit.

the class EntityLiving method kill.

@Override
public void kill() {
    if (!this.isAlive()) {
        return;
    }
    super.kill();
    EntityDeathEvent ev = new EntityDeathEvent(this, this.getDrops());
    this.server.getPluginManager().callEvent(ev);
    if (this.level.getGameRules().getBoolean(GameRule.DO_ENTITY_DROPS)) {
        for (cn.nukkit.item.Item item : ev.getDrops()) {
            this.getLevel().dropItem(this, item);
        }
    }
}
Also used : Item(cn.nukkit.item.Item)

Aggregations

Item (cn.nukkit.item.Item)64 Enchantment (cn.nukkit.item.enchantment.Enchantment)13 Player (cn.nukkit.Player)10 ItemBlock (cn.nukkit.item.ItemBlock)10 BlockAir (cn.nukkit.block.BlockAir)8 Random (java.util.Random)7 BlockEntity (cn.nukkit.blockentity.BlockEntity)6 Entity (cn.nukkit.entity.Entity)5 Block (cn.nukkit.block.Block)4 EntityItem (cn.nukkit.entity.item.EntityItem)4 EntityInventoryChangeEvent (cn.nukkit.event.entity.EntityInventoryChangeEvent)4 Inventory (cn.nukkit.inventory.Inventory)3 InventoryHolder (cn.nukkit.inventory.InventoryHolder)3 BlockEntityItemFrame (cn.nukkit.blockentity.BlockEntityItemFrame)2 EntityArmorChangeEvent (cn.nukkit.event.entity.EntityArmorChangeEvent)2 EntityDamageByEntityEvent (cn.nukkit.event.entity.EntityDamageByEntityEvent)2 EntityDamageEvent (cn.nukkit.event.entity.EntityDamageEvent)2 HopperInventory (cn.nukkit.inventory.HopperInventory)2 InventoryAction (cn.nukkit.inventory.transaction.action.InventoryAction)2 TranslationContainer (cn.nukkit.lang.TranslationContainer)2