Search in sources :

Example 1 with BrewingStand

use of org.bukkit.block.BrewingStand in project modules-extra by CubeEngine.

the class ListenerContainerItem method onInventoryClick.

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onInventoryClick(InventoryClickEvent event) {
    // TODO major refactoring needed
    if (event.getSlot() == -999) {
        return;
    }
    if (event.getWhoClicked() instanceof Player) {
        Player player = (Player) event.getWhoClicked();
        if (!this.inventoryChanges.containsKey(player.getUniqueId())) {
            return;
        }
        Inventory inventory = event.getInventory();
        InventoryHolder holder = inventory.getHolder();
        org.spongepowered.api.item.inventory.ItemStack inventoryItem = event.getCurrentItem();
        ItemStack cursorItem = event.getCursor();
        if ((inventoryItem == null || inventoryItem.getType() == AIR) && (cursorItem == null || cursorItem.getType() == AIR)) {
            // nothing to log
            return;
        }
        if (// click in top inventory
        event.getRawSlot() < event.getView().getTopInventory().getSize()) {
            if (// top & shift -> remove items
            event.isShiftClick()) {
                if (inventoryItem == null || inventoryItem.getType() == AIR) {
                    return;
                }
                int missingSpace = getMissingSpace(event.getView().getTopInventory(), inventoryItem);
                int amountTake = inventoryitem.getQuantity() - missingSpace;
                if (amountTake > 0) {
                    this.prepareForLogging(player, new ItemData(inventoryItem), -amountTake);
                }
            } else {
                if (// remove items
                cursorItem == null || cursorItem.getType() == AIR) {
                    int remove = event.isLeftClick() ? inventoryitem.getQuantity() : (inventoryitem.getQuantity() + 1) / 2;
                    this.prepareForLogging(player, new ItemData(inventoryItem), -remove);
                } else if (// put items
                inventoryItem == null || inventoryItem.getType() == AIR) {
                    int put = event.isLeftClick() ? cursoritem.getQuantity() : 1;
                    if (// handle BrewingStands separatly
                    holder instanceof BrewingStand) {
                        if (event.getRawSlot() == 3) {
                            if (// can be put
                            !BukkitUtils.canBePlacedInBrewingstand(cursorItem)) {
                                return;
                            }
                        } else if (cursorItem.getType() == POTION || // bottle slot
                        cursorItem.getType() == GLASS_BOTTLE) {
                            put = 1;
                        } else {
                            return;
                        }
                    }
                    this.prepareForLogging(player, new ItemData(cursorItem), put);
                } else {
                    if (inventoryItem.isSimilar(cursorItem)) {
                        int put = event.isLeftClick() ? inventoryitem.getQuantity() : 1;
                        if (// if stack to big
                        put > inventoryItem.getMaxStackSize() - inventoryitem.getQuantity()) {
                            put = inventoryItem.getMaxStackSize() - // set to missing to fill
                            inventoryitem.getQuantity();
                        }
                        if (put == 0) {
                            return;
                        }
                        this.prepareForLogging(player, new ItemData(inventoryItem), put);
                    } else {
                        if (// handle BrewingStands separatly
                        holder instanceof BrewingStand) {
                            if (event.getRawSlot() == 3) {
                                if (// can be put
                                !BukkitUtils.canBePlacedInBrewingstand(cursorItem)) {
                                    return;
                                }
                            } else if (cursorItem.getType() == POTION || // bottle slot
                            cursorItem.getType() == GLASS_BOTTLE) {
                                if (cursoritem.getQuantity() > 1) {
                                    // nothing happens when more than 1
                                    return;
                                }
                            // else swap items
                            } else {
                                return;
                            }
                        }
                        this.prepareForLogging(player, new ItemData(cursorItem), cursoritem.getQuantity());
                        this.prepareForLogging(player, new ItemData(inventoryItem), -inventoryitem.getQuantity());
                    }
                }
            }
        } else if (// click in bottom inventory AND shift -> put | ELSE no container change
        event.isShiftClick()) {
            if (inventoryItem == null || inventoryItem.getType() == AIR) {
                return;
            }
            if (holder instanceof BrewingStand) {
                BrewerInventory brewerInventory = (BrewerInventory) event.getView().getTopInventory();
                if (BukkitUtils.canBePlacedInBrewingstand(inventoryItem)) {
                    if (// could fit into inventory
                    inventoryItem.isSimilar(brewerInventory.getIngredient())) {
                        ItemStack brewerItem = brewerInventory.getIngredient();
                        int amountPutIn = inventoryitem.getQuantity();
                        if (breweritem.getQuantity() + inventoryitem.getQuantity() > inventoryItem.getMaxStackSize()) {
                            amountPutIn = inventoryItem.getMaxStackSize() - breweritem.getQuantity();
                            if (amountPutIn <= 0) {
                                return;
                            }
                        }
                        this.prepareForLogging(player, new ItemData(inventoryItem), amountPutIn);
                    }
                } else if (inventoryItem.getType() == POTION) {
                    for (int i = 0; i <= 2; ++i) {
                        ItemStack item = brewerInventory.getItem(i);
                        if (// space for a potion?
                        item == null) {
                            this.prepareForLogging(player, new ItemData(inventoryItem), inventoryitem.getQuantity());
                            return;
                        }
                    // else no space found
                    }
                } else if (inventoryItem.getType() == GLASS_BOTTLE) {
                    int bottlesFound = 0;
                    int bottleSlots = 0;
                    for (int i = 0; i <= 2; ++i) {
                        ItemStack item = brewerInventory.getItem(i);
                        if (// space for the stack ?
                        item == null) {
                            this.prepareForLogging(player, new ItemData(inventoryItem), inventoryitem.getQuantity());
                            return;
                        } else if (item.getType() == GLASS_BOTTLE) {
                            bottleSlots++;
                            bottlesFound += item.getQuantity();
                        }
                    }
                    if (bottleSlots > 0) {
                        int space = GLASS_BOTTLE.getMaxStackSize() * bottleSlots - bottlesFound;
                        if (space <= 0) {
                            return;
                        }
                        int putInto = inventoryitem.getQuantity();
                        if (putInto > space) {
                            putInto = space;
                        }
                        this.prepareForLogging(player, new ItemData(inventoryItem), putInto);
                    }
                }
            } else if (holder instanceof Furnace) {
                FurnaceInventory furnaceInventory = (FurnaceInventory) event.getView().getTopInventory();
                int putInto = 0;
                if (BukkitUtils.isSmeltable(inventoryItem)) {
                    ItemStack item = furnaceInventory.getSmelting();
                    if (item == null) {
                        putInto = inventoryitem.getQuantity();
                    } else if (inventoryItem.isSimilar(item)) {
                        int space = inventoryItem.getMaxStackSize() - item.getQuantity();
                        if (space <= 0) {
                            return;
                        }
                        putInto = inventoryitem.getQuantity();
                        if (putInto > space) {
                            putInto = space;
                        }
                    }
                } else if (inventoryItem.getItem().getDefaultProperty(BurningFuelProperty.class).isPresent()) {
                    ItemStack item = furnaceInventory.getFuel();
                    if (item == null) {
                        putInto = inventoryitem.getQuantity();
                    } else if (inventoryItem.isSimilar(item)) {
                        int space = inventoryItem.getMaxStackSize() - item.getQuantity();
                        if (space <= 0) {
                            return;
                        }
                        putInto = inventoryitem.getQuantity();
                        if (putInto > space) {
                            putInto = space;
                        }
                    }
                }
                if (putInto == 0) {
                    return;
                }
                this.prepareForLogging(player, new ItemData(inventoryItem), putInto);
            } else {
                event.getView().getTopInventory().getContents();
                int missingSpace = getMissingSpace(event.getView().getTopInventory(), inventoryItem);
                int amountPut = inventoryitem.getQuantity() - missingSpace;
                if (amountPut > 0) {
                    this.prepareForLogging(player, new ItemData(inventoryItem), amountPut);
                }
            }
        }
    }
}
Also used : Player(org.spongepowered.api.entity.player.Player) BrewerInventory(org.bukkit.inventory.BrewerInventory) Furnace(org.bukkit.block.Furnace) FurnaceInventory(org.bukkit.inventory.FurnaceInventory) BrewingStand(org.bukkit.block.BrewingStand) InventoryHolder(org.bukkit.inventory.InventoryHolder) BrewerInventory(org.bukkit.inventory.BrewerInventory) Inventory(org.bukkit.inventory.Inventory) FurnaceInventory(org.bukkit.inventory.FurnaceInventory) EventHandler(org.bukkit.event.EventHandler)

Aggregations

BrewingStand (org.bukkit.block.BrewingStand)1 Furnace (org.bukkit.block.Furnace)1 EventHandler (org.bukkit.event.EventHandler)1 BrewerInventory (org.bukkit.inventory.BrewerInventory)1 FurnaceInventory (org.bukkit.inventory.FurnaceInventory)1 Inventory (org.bukkit.inventory.Inventory)1 InventoryHolder (org.bukkit.inventory.InventoryHolder)1 Player (org.spongepowered.api.entity.player.Player)1