Search in sources :

Example 46 with Inventory

use of org.bukkit.inventory.Inventory in project MassiveCore by MassiveCraft.

the class EngineMassiveCoreChestGui method onOpen.

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onOpen(InventoryOpenEvent event) {
    // Get
    final Inventory inventory = event.getInventory();
    if (inventory == null)
        return;
    final ChestGui gui = ChestGui.get(inventory);
    if (gui == null)
        return;
    // Sound
    SoundEffect sound = gui.getSoundOpen();
    if (sound != null) {
        HumanEntity human = event.getPlayer();
        sound.run(human);
    }
    // Later
    Bukkit.getScheduler().runTask(getPlugin(), new Runnable() {

        @Override
        public void run() {
            // Runnables
            for (Runnable runnable : gui.getRunnablesOpen()) {
                runnable.run();
            }
        }
    });
}
Also used : ChestGui(com.massivecraft.massivecore.chestgui.ChestGui) SoundEffect(com.massivecraft.massivecore.SoundEffect) HumanEntity(org.bukkit.entity.HumanEntity) Inventory(org.bukkit.inventory.Inventory) EventHandler(org.bukkit.event.EventHandler)

Example 47 with Inventory

use of org.bukkit.inventory.Inventory in project MassiveCore by MassiveCraft.

the class EngineMassiveCoreChestGui method onClick.

// -------------------------------------------- //
// LISTENER
// -------------------------------------------- //
@EventHandler(priority = EventPriority.LOW)
public void onClick(InventoryClickEvent event) {
    // If this inventory ...
    Inventory inventory = event.getInventory();
    if (inventory == null)
        return;
    // ... is a gui ...
    ChestGui gui = ChestGui.get(inventory);
    if (gui == null)
        return;
    // ... then cancel the event ...
    event.setCancelled(true);
    event.setResult(Result.DENY);
    // ... warn on bottom inventory ...
    if (InventoryUtil.isBottomInventory(event)) {
        // ... only if its not allowed.
        if (gui.isBottomInventoryAllowed()) {
            event.setCancelled(false);
            event.setResult(Result.DEFAULT);
        } else {
            MixinMessage.get().msgOne(event.getWhoClicked(), "<b>Exit the GUI to edit your items.");
        }
        return;
    }
    // ... and if this slot index ...
    int index = event.getSlot();
    // ... has an action ...
    ChestAction action = gui.getAction(index);
    if (action == null)
        return;
    // ... set last action ...
    gui.setLastAction(action);
    // ... then play click sound ...
    SoundEffect sound = gui.getSoundClick();
    if (sound != null)
        sound.run(event.getWhoClicked());
    // ... close the GUI ...
    if (gui.isAutoclosing())
        event.getView().close();
    // ... and use that action.
    action.onClick(event);
}
Also used : ChestGui(com.massivecraft.massivecore.chestgui.ChestGui) SoundEffect(com.massivecraft.massivecore.SoundEffect) ChestAction(com.massivecraft.massivecore.chestgui.ChestAction) Inventory(org.bukkit.inventory.Inventory) EventHandler(org.bukkit.event.EventHandler)

Example 48 with Inventory

use of org.bukkit.inventory.Inventory in project MassiveCore by MassiveCraft.

the class InventoryUtil method getAlter.

// Drag events by nature only matters when they affect the top inventory.
// What you are holding in the cursor is already yours.
// If you drag it into your own inventory you are not really taking anything.
// If you drag into the top inventory however, you may both give and take.
// You "take" by dragging over an existing item.
private static InventoryAlter getAlter(InventoryDragEvent event) {
    // Create
    boolean giving = false;
    boolean taking = false;
    // Fill
    final Inventory inventory = event.getInventory();
    for (Entry<Integer, ItemStack> entry : event.getNewItems().entrySet()) {
        int rawSlot = entry.getKey();
        if (InventoryUtil.isBottomInventory(rawSlot, inventory))
            continue;
        ItemStack take = inventory.getItem(rawSlot);
        if (isSomething(take))
            taking = true;
        ItemStack give = entry.getValue();
        if (isSomething(give))
            giving = true;
    }
    // Return
    return InventoryAlter.get(giving, taking);
}
Also used : ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory) MixinInventory(com.massivecraft.massivecore.mixin.MixinInventory) PlayerInventory(org.bukkit.inventory.PlayerInventory)

Example 49 with Inventory

use of org.bukkit.inventory.Inventory in project MassiveCore by MassiveCraft.

the class InventoryUtil method clone.

public static Inventory clone(Inventory inventory, boolean playerSupport) {
    // Evade
    if (inventory == null)
        return null;
    // Create
    Inventory ret = null;
    if (inventory instanceof PlayerInventory && playerSupport) {
        ret = MixinInventory.get().createPlayerInventory();
    } else {
        InventoryHolder holder = inventory.getHolder();
        int size = inventory.getSize();
        if (inventory instanceof PlayerInventory)
            size = SIZE_PLAYER_STORAGE;
        String title = inventory.getTitle();
        ret = MixinInventory.get().createInventory(holder, size, title);
    }
    // Fill
    ItemStack[] all = getContentsAll(inventory);
    all = clone(all);
    setContentsAll(ret, all);
    // Return
    return ret;
}
Also used : PlayerInventory(org.bukkit.inventory.PlayerInventory) ItemStack(org.bukkit.inventory.ItemStack) InventoryHolder(org.bukkit.inventory.InventoryHolder) Inventory(org.bukkit.inventory.Inventory) MixinInventory(com.massivecraft.massivecore.mixin.MixinInventory) PlayerInventory(org.bukkit.inventory.PlayerInventory)

Example 50 with Inventory

use of org.bukkit.inventory.Inventory in project TokenManager by RealizedMC.

the class ShopListener method on.

@EventHandler
public void on(final InventoryClickEvent event) {
    final Player player = (Player) event.getWhoClicked();
    final Inventory clicked = event.getClickedInventory();
    final Inventory top = player.getOpenInventory().getTopInventory();
    if (clicked == null || top == null) {
        return;
    }
    final ItemStack item = event.getCurrentItem();
    if (item == null || item.getType() == Material.AIR) {
        return;
    }
    boolean confirmGui = false;
    Shop target = null;
    ConfirmInventory confirmInventory = inventories.get(player.getUniqueId());
    if (confirmInventory != null && confirmInventory.inventory.equals(top)) {
        target = confirmInventory.shop;
        confirmGui = true;
    } else {
        for (final Shop shop : config.getShops()) {
            if (shop.getGui().equals(top)) {
                target = shop;
                break;
            }
        }
    }
    if (target == null) {
        return;
    }
    event.setCancelled(true);
    if (!clicked.equals(top)) {
        return;
    }
    final int slot = event.getSlot();
    final Slot data;
    if (confirmGui) {
        if (slot == CANCEL_PURCHASE_SLOT) {
            player.openInventory(target.getGui());
            return;
        } else if (slot == CONFIRM_PURCHASE_SLOT) {
            data = confirmInventory.slot;
        } else {
            data = null;
        }
    } else {
        data = target.getSlot(slot);
    }
    if (data == null) {
        return;
    }
    if (data.isUsePermission() && !player.hasPermission("tokenmanager.use." + target.getName() + "-" + slot)) {
        plugin.getLang().sendMessage(player, true, "ERROR.no-permission", "permission", "tokenmanager.use." + target.getName() + "-" + slot);
        return;
    }
    final long now = System.currentTimeMillis();
    final long remaining = cooldowns.getOrDefault(player.getUniqueId(), 0L) + plugin.getConfiguration().getClickDelay() * 1000L - now;
    if (remaining > 0) {
        plugin.getLang().sendMessage(player, true, "ERROR.on-click-cooldown", "remaining", StringUtil.format(remaining / 1000 + (remaining % 1000 > 0 ? 1 : 0)));
        return;
    }
    cooldowns.put(player.getUniqueId(), now);
    final int cost = data.getCost();
    final OptionalLong cached = dataManager.get(player);
    if (!cached.isPresent()) {
        plugin.getLang().sendMessage(player, false, "&cYour data is improperly loaded, please re-log.");
        return;
    }
    final long balance = cached.getAsLong();
    if (balance - cost < 0) {
        plugin.getLang().sendMessage(player, true, "ERROR.balance-not-enough", "needed", cost - balance);
        return;
    }
    if (!confirmGui && (target.isConfirmPurchase() || data.isConfirmPurchase())) {
        cooldowns.remove(player.getUniqueId());
        if (confirmInventory == null) {
            confirmInventory = new ConfirmInventory(InventoryUtil.deepCopyOf(confirmGuiSample));
            inventories.put(player.getUniqueId(), confirmInventory);
        }
        confirmInventory.update(target, data);
        player.openInventory(confirmInventory.inventory);
        return;
    }
    if (cost > 0) {
        dataManager.set(player, balance - cost);
    }
    handlePurchase(player, target, data);
}
Also used : Player(org.bukkit.entity.Player) OptionalLong(java.util.OptionalLong) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory) 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