Solved Opening an Inventory calls InventoryCloseEvent??

Discussion in 'Plugin Development' started by Machine Maker, Jul 1, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    Machine Maker

    Whenever I create and open an inventory, the server also runs inventory close event. Here is my code creating/opening the inventory. I am creating the inventory whenever the player clicks on a certain skull so the inventory is being created in the PlayerInteractEvent event handler. You can probably ignore most of the stuff in the middle, its just dealing with recipies.


    Code:
    public void onPlayerInteract(PlayerInteractEvent e) {
            if (!e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) return;
            BlockState bs = e.getClickedBlock().getState();
            if (!(bs instanceof Skull)) return;
            Skull skull = (Skull) bs;
            if (!skull.getSkullType().equals(SkullType.PLAYER)) return;
            if (!skull.getOwningPlayer().equals(Bukkit.getOfflinePlayer("craftingtable"))) return;
            Inventory inv = Bukkit.getServer().createInventory(null, 45, ChatColor.BLUE + "Decrafting Table");
            for (int i = 0; i < 45; i++) {
                if (!DecraftingTable.getInstance().getRS().contains(i) && i != 25) {
                    inv.setItem(i, DecraftingTable.getInstance().getBarrier());
                }
            }
            e.getPlayer().openInventory(inv);
            Bukkit.getConsoleSender().sendMessage("Running");
            DecraftingTable.getInstance().getDecrafts().add(new Decraft(e.getPlayer(), Bukkit.getScheduler().scheduleSyncRepeatingTask(DecraftingTable.getInstance(), new Runnable() {
                @Override
                public void run() {
                    Bukkit.getConsoleSender().sendMessage("HDF");
                    InventoryView invView = e.getPlayer().getOpenInventory();
                    ItemStack item = invView.getItem(25);
                    if (!item.getType().equals(Material.AIR)) {
                        List<Recipe> recipes = Bukkit.getServer().getRecipesFor(item);
                        if (!recipes.isEmpty()) {
                            ShapedRecipe withShape = null;
                            ShapelessRecipe withoutShape = null;
                            for (Recipe r : recipes) {
                                if (r instanceof ShapedRecipe) {
                                    withShape = (ShapedRecipe) r;
                                    break;
                                }
                                else if (r instanceof ShapelessRecipe) {
                                    withoutShape = (ShapelessRecipe) r;
                                    break;
                                }
                            }
                            Object[] items = new Object[] { };
                            int amount = 0;
                            int amountLeft = 0;
                            if (withoutShape == null) {
                                if (withShape.getResult().getAmount() <= item.getAmount()) {
                                    amount = item.getAmount() / withShape.getResult().getAmount();
                                    amountLeft = item.getAmount() % withShape.getResult().getAmount();
                                    Map<Character, ItemStack> i = withShape.getIngredientMap();
                                    items = i.values().toArray();
                                }
                            }
                            else {
                                if (withoutShape.getResult().getAmount() <= item.getAmount()) {
                                    amount = item.getAmount() / withoutShape.getResult().getAmount();
                                    amountLeft = item.getAmount() % withoutShape.getResult().getAmount();
                                    items = withoutShape.getIngredientList().toArray();
                                }
                            }
                            for (int i = 0; i < items.length; i++) {
                                if (items[i] == null) continue;
                                ItemStack ingredient = (ItemStack) items[i];
                                //Bukkit.getConsoleSender().sendMessage("MaxSize: " + ingredient.getMaxStackSize() + " Amount: " + amount);
                                if (ingredient.getMaxStackSize() == 1) {
                                    //Bukkit.getConsoleSender().sendMessage(""+ ingredient.getMaxStackSize());
                                    amount = 1;
                                    break;
                                }
                                else if (ingredient.getMaxStackSize() == 16) {
                                    amount = 16;
                                    break;
                                }
                            }
                            //Bukkit.getConsoleSender().sendMessage("" + amount);
                            for (int i = 0; i < items.length; i++) {
                                if (items[i] == null) continue;
                                ItemStack ingredient = (ItemStack) items[i];
                                Material iType = ingredient.getType();
                                invView.setItem(DecraftingTable.getInstance().getRS().get(i), new ItemStack(iType, amount));
                            }
                        }
                    }
                    else {
                        for (int i = 0; i < DecraftingTable.getInstance().getRS().size(); i++) {
                            if (!invView.getItem(DecraftingTable.getInstance().getRS().get(i)).getType().equals(Material.AIR))
                            invView.setItem(DecraftingTable.getInstance().getRS().get(i), new ItemStack(Material.AIR));
                        }
                    }
                }
            }, 20L, 20L)));
            //Bukkit.getConsoleSender().sendMessage("taskid: " + taskid);
    }
     
  2. I think printing the old inventory in the InventoryCloseEvent would give you some useful information about what inventory is being 'closed'.
     
  3. Offline

    Machine Maker

    @knokko Yeah i tried that. I had the InventoryCloseEvent print the Title of the Inventory. It was the same. I even used a color code to make sure the inventory name was different than everything else.
     
  4. Offline

    dutchjelly

    I'm thinking it's the player's inventory closing? Therefor you could add some cheeky timer that waits for a couple milliseconds before the inventorycloseevent is allowed to run.
    Also, if the inventory closing is before the opening, you could make it so the inventoryCloseEvent can't execute before the inventoryOpenEvent.
    I'm sorry if this is an unhelpful post, but I'm just hoping I say something you didn't think of.
     
  5. Offline

    Machine Maker

    All suggestions are helpful @dutchjelly but I already found a way around it without using CloseInvEvent
     
  6. Offline

    dutchjelly

    Ok! I'm happy that you found a solution!
     
Thread Status:
Not open for further replies.

Share This Page