Search in sources :

Example 1 with MiniShopEvent

use of com.wasteofplastic.acidisland.events.MiniShopEvent in project acidisland by tastybento.

the class ControlPanel method onInventoryClick.

@SuppressWarnings("deprecation")
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onInventoryClick(InventoryClickEvent event) {
    if (DEBUG)
        plugin.getLogger().info("DEBUG:" + event.getEventName());
    // The player that
    Player player = (Player) event.getWhoClicked();
    // clicked the item
    // The item that was clicked
    ItemStack clicked = event.getCurrentItem();
    // The inventory that was clicked in
    Inventory inventory = event.getInventory();
    if (inventory.getName() == null) {
        if (DEBUG)
            plugin.getLogger().info("DEBUG: inventory name is null");
        return;
    }
    // ASkyBlock plugin = ASkyBlock.getPlugin();
    int slot = event.getRawSlot();
    // Challenges
    if (inventory.getName().equals(plugin.myLocale(player.getUniqueId()).challengesguiTitle)) {
        event.setCancelled(true);
        if (event.getClick().equals(ClickType.SHIFT_RIGHT)) {
            if (DEBUG)
                plugin.getLogger().info("DEBUG: click type shift Right");
            inventory.clear();
            player.closeInventory();
            player.updateInventory();
            return;
        }
        if (event.getSlotType() == SlotType.OUTSIDE) {
            if (DEBUG)
                plugin.getLogger().info("DEBUG: slot type outside");
            inventory.clear();
            player.closeInventory();
            return;
        }
        // Get the list of items in this inventory
        // plugin.getLogger().info("DEBUG: You clicked on slot " + slot);
        List<CPItem> challenges = plugin.getChallenges().getCP(player);
        if (challenges == null) {
            plugin.getLogger().warning("Player was accessing Challenge Inventory, but it had lost state - was server restarted?");
            inventory.clear();
            player.closeInventory();
            Util.runCommand(player, Settings.CHALLENGECOMMAND);
            return;
        }
        // challenges.size());
        if (slot >= 0 && slot < challenges.size()) {
            if (DEBUG)
                plugin.getLogger().info("DEBUG: slot within challenges");
            CPItem item = challenges.get(slot);
            // END TEST
            if (DEBUG) {
                plugin.getLogger().info("DEBUG: CP Item is " + item.getItem().toString());
                plugin.getLogger().info("DEBUG: Clicked is " + clicked.toString());
            }
            // These two should be identical because it is made before
            if (clicked.equals(item.getItem())) {
                if (DEBUG)
                    plugin.getLogger().info("DEBUG: You clicked on a challenge item");
                // Next section indicates the level of panel to open
                if (item.getNextSection() != null) {
                    inventory.clear();
                    player.closeInventory();
                    player.openInventory(plugin.getChallenges().challengePanel(player, item.getNextSection()));
                } else if (item.getCommand() != null) {
                    Util.runCommand(player, item.getCommand());
                    inventory.clear();
                    player.closeInventory();
                    player.openInventory(plugin.getChallenges().challengePanel(player));
                }
            }
        }
        return;
    }
    /*
         * Minishop section
         */
    if (miniShop != null && inventory.getName().equals(miniShop.getName())) {
        // The
        // inventory
        // is
        // our
        // custom
        // Inventory
        String message = "";
        // plugin.getLogger().info("You clicked on slot " + slot);
        // Don't let them pick it up
        event.setCancelled(true);
        if (!Settings.useEconomy || slot == -999) {
            player.closeInventory();
            return;
        }
        if (event.getClick().equals(ClickType.SHIFT_RIGHT)) {
            player.closeInventory();
            player.updateInventory();
            return;
        }
        if (store.containsKey(slot)) {
            // We have a winner!
            MiniShopItem item = store.get(slot);
            if (clicked.equals(item.getItem())) {
                // Check what type of click - LEFT = BUY, RIGHT = sell
                if (event.getClick().equals(ClickType.LEFT)) {
                    // Check if item is for sale
                    if (item.getPrice() > 0D) {
                        // Check they can afford it
                        if (!VaultHelper.econ.has(player, Settings.worldName, item.getPrice())) {
                            // message = "You cannot afford that item!";
                            message = (plugin.myLocale().minishopYouCannotAfford).replace("[description]", item.getDescription());
                        } else {
                            EconomyResponse r = VaultHelper.econ.withdrawPlayer(player, Settings.worldName, item.getPrice());
                            if (r.transactionSuccess()) {
                                // message = "You bought " +
                                // item.getQuantity() + " " +
                                // item.getDescription() + " for " +
                                // VaultHelper.econ.format(item.getPrice());
                                message = plugin.myLocale().minishopYouBought.replace("[number]", Integer.toString(item.getQuantity()));
                                message = message.replace("[description]", item.getDescription());
                                message = message.replace("[price]", VaultHelper.econ.format(item.getPrice()));
                                Map<Integer, ItemStack> items = player.getInventory().addItem(item.getItemClean());
                                if (!items.isEmpty()) {
                                    for (ItemStack i : items.values()) {
                                        player.getWorld().dropItem(player.getLocation(), i);
                                    }
                                }
                                // Fire event
                                MiniShopEvent shopEvent = new MiniShopEvent(player.getUniqueId(), item, TransactionType.BUY);
                                plugin.getServer().getPluginManager().callEvent(shopEvent);
                            } else {
                                // message =
                                // "There was a problem puchasing that item: "
                                // + r.errorMessage;
                                message = (plugin.myLocale().minishopBuyProblem).replace("[description]", item.getDescription());
                            }
                        }
                    }
                } else if (event.getClick().equals(ClickType.RIGHT) && allowSelling && item.getSellPrice() > 0D) {
                    // Check if they have the item
                    if (player.getInventory().containsAtLeast(item.getItemClean(), item.getQuantity())) {
                        player.getInventory().removeItem(item.getItemClean());
                        VaultHelper.econ.depositPlayer(player, Settings.worldName, item.getSellPrice());
                        // message = "You sold " + item.getQuantity() + " "
                        // + item.getDescription() + " for " +
                        // VaultHelper.econ.format(item.getSellPrice());
                        message = plugin.myLocale().minishopYouSold.replace("[number]", Integer.toString(item.getQuantity()));
                        message = message.replace("[description]", item.getDescription());
                        message = message.replace("[price]", VaultHelper.econ.format(item.getSellPrice()));
                        // Fire event
                        MiniShopEvent shopEvent = new MiniShopEvent(player.getUniqueId(), item, TransactionType.SELL);
                        plugin.getServer().getPluginManager().callEvent(shopEvent);
                    } else {
                        // message =
                        // "You do not have enough of that item to sell it.";
                        message = (plugin.myLocale().minishopSellProblem).replace("[description]", item.getDescription());
                        ;
                    }
                }
                // player.closeInventory(); // Closes the inventory
                if (!message.isEmpty()) {
                    Util.sendMessage(player, message);
                }
            }
        }
        return;
    }
    // Check control panels
    for (String panelName : controlPanel.keySet()) {
        if (inventory.getName().equals(panelName)) {
            event.setCancelled(true);
            // plugin.getLogger().info("DEBUG: panel name " + panelName);
            if (slot == -999) {
                player.closeInventory();
                return;
            }
            if (event.getClick().equals(ClickType.SHIFT_RIGHT)) {
                player.closeInventory();
                player.updateInventory();
                return;
            }
            HashMap<Integer, CPItem> thisPanel = panels.get(panelName);
            if (slot >= 0 && slot < thisPanel.size()) {
                // plugin.getLogger().info("DEBUG: slot is " + slot);
                // Do something
                String command = thisPanel.get(slot).getCommand();
                String nextSection = ChatColor.translateAlternateColorCodes('&', thisPanel.get(slot).getNextSection());
                if (!command.isEmpty()) {
                    // Closes the inventory
                    player.closeInventory();
                    event.setCancelled(true);
                    // plugin.getLogger().info("DEBUG: performing command "
                    // + command);
                    Util.runCommand(player, command);
                    return;
                }
                if (!nextSection.isEmpty()) {
                    // Closes the inventory
                    player.closeInventory();
                    Inventory next = controlPanel.get(nextSection);
                    if (next == null) {
                    // plugin.getLogger().info("DEBUG: next panel is null");
                    }
                    // plugin.getLogger().info("DEBUG: opening next cp "+nextSection);
                    player.openInventory(next);
                    event.setCancelled(true);
                    return;
                }
                // Closes the inventory
                player.closeInventory();
                event.setCancelled(true);
                return;
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) MiniShopEvent(com.wasteofplastic.acidisland.events.MiniShopEvent) EconomyResponse(net.milkbowl.vault.economy.EconomyResponse) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory) EventHandler(org.bukkit.event.EventHandler)

Aggregations

MiniShopEvent (com.wasteofplastic.acidisland.events.MiniShopEvent)1 EconomyResponse (net.milkbowl.vault.economy.EconomyResponse)1 Player (org.bukkit.entity.Player)1 EventHandler (org.bukkit.event.EventHandler)1 Inventory (org.bukkit.inventory.Inventory)1 ItemStack (org.bukkit.inventory.ItemStack)1