Search in sources :

Example 1 with EnchantInventory

use of cn.nukkit.inventory.EnchantInventory 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 2 with EnchantInventory

use of cn.nukkit.inventory.EnchantInventory in project Nukkit by Nukkit.

the class BlockEnchantingTable method onActivate.

@Override
public boolean onActivate(Item item, Player player) {
    if (player != null) {
        BlockEntity t = this.getLevel().getBlockEntity(this);
        BlockEntityEnchantTable enchantTable;
        if (t instanceof BlockEntityEnchantTable) {
            enchantTable = (BlockEntityEnchantTable) t;
        } else {
            CompoundTag nbt = new CompoundTag().putList(new ListTag<>("Items")).putString("id", BlockEntity.ENCHANT_TABLE).putInt("x", (int) this.x).putInt("y", (int) this.y).putInt("z", (int) this.z);
            enchantTable = new BlockEntityEnchantTable(this.getLevel().getChunk((int) (this.x) >> 4, (int) (this.z) >> 4), nbt);
        }
        if (enchantTable.namedTag.contains("Lock") && enchantTable.namedTag.get("Lock") instanceof StringTag) {
            if (!enchantTable.namedTag.getString("Lock").equals(item.getCustomName())) {
                return true;
            }
        }
        player.addWindow(new EnchantInventory(this.getLocation()), Player.ENCHANT_WINDOW_ID);
    }
    return true;
}
Also used : StringTag(cn.nukkit.nbt.tag.StringTag) BlockEntityEnchantTable(cn.nukkit.blockentity.BlockEntityEnchantTable) EnchantInventory(cn.nukkit.inventory.EnchantInventory) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Aggregations

EnchantInventory (cn.nukkit.inventory.EnchantInventory)2 BlockEntity (cn.nukkit.blockentity.BlockEntity)1 BlockEntityEnchantTable (cn.nukkit.blockentity.BlockEntityEnchantTable)1 AnvilInventory (cn.nukkit.inventory.AnvilInventory)1 Inventory (cn.nukkit.inventory.Inventory)1 Item (cn.nukkit.item.Item)1 CompoundTag (cn.nukkit.nbt.tag.CompoundTag)1 StringTag (cn.nukkit.nbt.tag.StringTag)1