Search in sources :

Example 61 with Inventory

use of org.bukkit.inventory.Inventory in project Essentials by EssentialsX.

the class EssentialsPlayerListener method onInventoryClickEvent.

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onInventoryClickEvent(final InventoryClickEvent event) {
    Player refreshPlayer = null;
    final Inventory top = event.getView().getTopInventory();
    final InventoryType type = top.getType();
    final Inventory clickedInventory;
    if (event.getRawSlot() < 0) {
        clickedInventory = null;
    } else {
        clickedInventory = event.getRawSlot() < top.getSize() ? top : event.getView().getBottomInventory();
    }
    if (type == InventoryType.PLAYER) {
        final User user = ess.getUser((Player) event.getWhoClicked());
        final InventoryHolder invHolder = top.getHolder();
        if (invHolder != null && invHolder instanceof HumanEntity) {
            final User invOwner = ess.getUser((Player) invHolder);
            if (user.isInvSee() && (!user.isAuthorized("essentials.invsee.modify") || invOwner.isAuthorized("essentials.invsee.preventmodify") || !invOwner.getBase().isOnline())) {
                event.setCancelled(true);
                refreshPlayer = user.getBase();
            }
        }
    } else if (type == InventoryType.ENDER_CHEST) {
        final User user = ess.getUser((Player) event.getWhoClicked());
        if (user.isEnderSee() && (!user.isAuthorized("essentials.enderchest.modify"))) {
            event.setCancelled(true);
            refreshPlayer = user.getBase();
        }
    } else if (type == InventoryType.WORKBENCH) {
        User user = ess.getUser((Player) event.getWhoClicked());
        if (user.isRecipeSee()) {
            event.setCancelled(true);
            refreshPlayer = user.getBase();
        }
    } else if (type == InventoryType.CHEST && top.getSize() == 9) {
        final User user = ess.getUser((Player) event.getWhoClicked());
        final InventoryHolder invHolder = top.getHolder();
        if (invHolder != null && invHolder instanceof HumanEntity && user.isInvSee()) {
            event.setCancelled(true);
            refreshPlayer = user.getBase();
        }
    } else if (clickedInventory != null && clickedInventory.getType() == InventoryType.PLAYER) {
        if (ess.getSettings().isDirectHatAllowed() && event.getClick() == ClickType.LEFT && event.getSlot() == 39 && event.getCursor().getType() != Material.AIR && event.getCursor().getType().getMaxDurability() == 0 && ess.getUser(event.getWhoClicked()).isAuthorized("essentials.hat")) {
            event.setCancelled(true);
            final PlayerInventory inv = (PlayerInventory) clickedInventory;
            final ItemStack head = inv.getHelmet();
            inv.setHelmet(event.getCursor());
            event.setCursor(head);
        }
    }
    if (refreshPlayer != null) {
        final Player player = refreshPlayer;
        ess.scheduleSyncDelayedTask(new Runnable() {

            @Override
            public void run() {
                player.updateInventory();
            }
        }, 1);
    }
}
Also used : Player(org.bukkit.entity.Player) InventoryType(org.bukkit.event.inventory.InventoryType) HumanEntity(org.bukkit.entity.HumanEntity) PlayerInventory(org.bukkit.inventory.PlayerInventory) ItemStack(org.bukkit.inventory.ItemStack) InventoryHolder(org.bukkit.inventory.InventoryHolder) Inventory(org.bukkit.inventory.Inventory) PlayerInventory(org.bukkit.inventory.PlayerInventory) EventHandler(org.bukkit.event.EventHandler)

Example 62 with Inventory

use of org.bukkit.inventory.Inventory in project Essentials by EssentialsX.

the class Commandinvsee method run.

// This method has a hidden param, which if given will display the equip slots. #easteregg
@Override
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
    if (args.length < 1) {
        throw new NotEnoughArgumentsException();
    }
    final User invUser = getPlayer(server, user, args, 0);
    Inventory inv;
    if (args.length > 1 && user.isAuthorized("essentials.invsee.equip")) {
        inv = server.createInventory(invUser.getBase(), 9, "Equipped");
        inv.setContents(invUser.getBase().getInventory().getArmorContents());
    } else {
        inv = invUser.getBase().getInventory();
    }
    user.getBase().closeInventory();
    user.getBase().openInventory(inv);
    user.setInvSee(true);
}
Also used : User(com.earth2me.essentials.User) Inventory(org.bukkit.inventory.Inventory)

Example 63 with Inventory

use of org.bukkit.inventory.Inventory in project Essentials by EssentialsX.

the class InventoryWorkaround method addOversizedItems.

// Returns what it couldn't store
// Set oversizedStack to below normal stack size to disable oversized stacks
public static Map<Integer, ItemStack> addOversizedItems(final Inventory inventory, final int oversizedStacks, final ItemStack... items) {
    if (isCombinedInventory(inventory)) {
        Inventory fakeInventory = makeTruncatedPlayerInventory((PlayerInventory) inventory);
        Map<Integer, ItemStack> overflow = addOversizedItems(fakeInventory, oversizedStacks, items);
        for (int i = 0; i < fakeInventory.getContents().length; i++) {
            inventory.setItem(i, fakeInventory.getContents()[i]);
        }
        return overflow;
    }
    final Map<Integer, ItemStack> leftover = new HashMap<>();
    /*
         * TODO: some optimization - Create a 'firstPartial' with a 'fromIndex' - Record the lastPartial per Material -
		 * Cache firstEmpty result
		 */
    // combine items
    final ItemStack[] combined = new ItemStack[items.length];
    for (ItemStack item : items) {
        if (item == null || item.getAmount() < 1) {
            continue;
        }
        for (int j = 0; j < combined.length; j++) {
            if (combined[j] == null) {
                combined[j] = item.clone();
                break;
            }
            if (combined[j].isSimilar(item)) {
                combined[j].setAmount(combined[j].getAmount() + item.getAmount());
                break;
            }
        }
    }
    for (int i = 0; i < combined.length; i++) {
        final ItemStack item = combined[i];
        if (item == null || item.getType() == Material.AIR) {
            continue;
        }
        while (true) {
            // Do we already have a stack of it?
            final int maxAmount = oversizedStacks > item.getType().getMaxStackSize() ? oversizedStacks : item.getType().getMaxStackSize();
            final int firstPartial = firstPartial(inventory, item, maxAmount);
            // Drat! no partial stack
            if (firstPartial == -1) {
                // Find a free spot!
                final int firstFree = inventory.firstEmpty();
                if (firstFree == -1) {
                    // No space at all!
                    leftover.put(i, item);
                    break;
                } else {
                    // More than a single stack!
                    if (item.getAmount() > maxAmount) {
                        final ItemStack stack = item.clone();
                        stack.setAmount(maxAmount);
                        inventory.setItem(firstFree, stack);
                        item.setAmount(item.getAmount() - maxAmount);
                    } else {
                        // Just store it
                        inventory.setItem(firstFree, item);
                        break;
                    }
                }
            } else {
                // So, apparently it might only partially fit, well lets do just that
                final ItemStack partialItem = inventory.getItem(firstPartial);
                final int amount = item.getAmount();
                final int partialAmount = partialItem.getAmount();
                // Check if it fully fits
                if (amount + partialAmount <= maxAmount) {
                    partialItem.setAmount(amount + partialAmount);
                    break;
                }
                // It fits partially
                partialItem.setAmount(maxAmount);
                item.setAmount(amount + partialAmount - maxAmount);
            }
        }
    }
    return leftover;
}
Also used : HashMap(java.util.HashMap) ItemStack(org.bukkit.inventory.ItemStack) PlayerInventory(org.bukkit.inventory.PlayerInventory) Inventory(org.bukkit.inventory.Inventory)

Example 64 with Inventory

use of org.bukkit.inventory.Inventory in project Ublisk by Derkades.

the class Voting method onVoteBoxOpen.

@EventHandler(priority = EventPriority.HIGH)
public void onVoteBoxOpen(PlayerInteractEvent event) {
    if (event.getAction() != Action.RIGHT_CLICK_BLOCK) {
        return;
    }
    UPlayer player = new UPlayer(event);
    if (player.isSneaking()) {
        return;
    }
    Block block = event.getClickedBlock();
    if (Voting.isVotingChest(block)) {
        Chest chest = (Chest) block.getState();
        Inventory inv = chest.getInventory();
        int gold = Voting.getRandomGold();
        int xp = Voting.getRandomXP();
        int life = Voting.getRandomLife();
        Item goldItem = new Item(Material.GOLD_NUGGET).setName(ChatColor.GOLD + "" + ChatColor.BOLD + "Gold: " + gold).setAmount(gold);
        Item xpItem = new Item(Material.EXP_BOTTLE).setName(ChatColor.GREEN + "" + ChatColor.BOLD + "XP: " + xp).setAmount(xp);
        Item lifeItem = new Item(Material.NETHER_STAR).setName(ChatColor.BOLD + "Life Crystals: " + life).setAmount(life);
        inv.setItem(12, goldItem.getItemStack());
        inv.setItem(13, xpItem.getItemStack());
        inv.setItem(14, lifeItem.getItemStack());
        if (gold != 0) {
            player.getInventory().addItem(Material.GOLD_NUGGET, gold);
        }
        if (xp != 0) {
            player.addXP(xp);
        }
        if (life != 0) {
            player.setLifeCrystals(player.getLifeCrystals() + life);
        }
        player.tracker(PlayerInfo.VOTE_BOX);
        Logger.log(LogLevel.DEBUG, "Voting", "Gold: " + gold + " | XP: " + xp + " | Life: " + life);
    }
}
Also used : Chest(org.bukkit.block.Chest) Item(xyz.derkades.ublisk.utils.inventory.Item) UPlayer(xyz.derkades.ublisk.utils.UPlayer) Block(org.bukkit.block.Block) Inventory(org.bukkit.inventory.Inventory) EventHandler(org.bukkit.event.EventHandler)

Example 65 with Inventory

use of org.bukkit.inventory.Inventory 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

Inventory (org.bukkit.inventory.Inventory)224 ItemStack (org.bukkit.inventory.ItemStack)132 Player (org.bukkit.entity.Player)80 EventHandler (org.bukkit.event.EventHandler)58 PlayerInventory (org.bukkit.inventory.PlayerInventory)40 ArrayList (java.util.ArrayList)37 ItemMeta (org.bukkit.inventory.meta.ItemMeta)34 InventoryHolder (org.bukkit.inventory.InventoryHolder)25 Block (org.bukkit.block.Block)18 Mage (com.elmakers.mine.bukkit.api.magic.Mage)16 Location (org.bukkit.Location)16 UUID (java.util.UUID)13 HashMap (java.util.HashMap)12 Entity (org.bukkit.entity.Entity)12 Material (org.bukkit.Material)11 HumanEntity (org.bukkit.entity.HumanEntity)10 Vector (org.bukkit.util.Vector)9 Sign (org.bukkit.block.Sign)8 InventoryType (org.bukkit.event.inventory.InventoryType)8 MageController (com.elmakers.mine.bukkit.api.magic.MageController)7