Multiple Inventories

Discussion in 'Plugin Development' started by cats000cats, Feb 7, 2016.

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

    cats000cats

    Hi, I am currently writing a plugin where one inventory needs to switch to another, for example, lets say I have an inventory where if i click on a piece of grass, it will bring me to another inventory because of the InventoryClick event. However, on the new inventory, none of the InvetoryClick events work. This is confusing as I know it is not a problem with this inventories on click event because if I open the inventory without using the other inventory to, all the Click events will happen. Does anyone know a way around this? I thought doing player.closeInventory() and opening the new inventory right after this would fix this but it still led to the same problem. Any ideas? Thanks in advance!
     
  2. Offline

    mcdorli

    Close it, wait a tick, and only then open it.
     
  3. Offline

    Go Hard

    @cats000cats Can you post some code so we see what you have.
     
  4. Offline

    cats000cats

    First of all, sorry if my code is very messy, I was more going for speed on this project instead of neatness...
    This is my event handler that calls certaint methods when it detects a click from one of the many inventories possible
    Code:
    @EventHandler
    public void onInventoryClick(InventoryClickEvent e) {
        if (e.getInventory().getName().equals("Sell Item")) {
            //if (e.getCurrentItem().getType().equals(Material.STAINED_GLASS_PANE)) e.setCancelled(true);
        }
        else if (e.getInventory().getName().equals("Buy Item")) {
            //if (e.getCurrentItem().getType().equals(Material.STAINED_GLASS_PANE)) e.setCancelled(true);
        }
        else if (e.getInventory().getName().startsWith("All Items:")) {
            handleItemClickAllItems(e.getCurrentItem(), (Player) e.getWhoClicked(),e.getInventory());
            Bukkit.getServer().broadcastMessage("All Items");
        }
        else if (e.getInventory().getName().startsWith("Group")) {
            handleItemClickGroups(e.getCurrentItem(), (Player) e.getWhoClicked());
            Bukkit.getServer().broadcastMessage("Group");
        }
        else if (e.getInventory().getName().startsWith("Buy: ")) {
            handleItemClickSetPrice(e.getCurrentItem(), (Player) e.getWhoClicked(),e.getInventory());
            Bukkit.getServer().broadcastMessage("Buy");
        }
    }
    The below method pretty much sums up all of the inventories, they each just have different items added
    Code:
    public static void openBuyItem(Player player) {
        Inventory inventory =Bukkit.createInventory(null, 45, "Buy Item");
      
        ItemStack item = new ItemStack(Material.STAINED_GLASS_PANE,1,DyeColor.RED.getData());
        //sets all items in an inventory to this item
        fillInventory(item,45,inventory);
    //also just a method for ez inventory editing
        setTradeSection(10,inventory,false);
        setTradeSection(14,inventory,false);
        setTradeSection(28,inventory,false);
        setTradeSection(32,inventory,false);
      
        player.openInventory(inventory);
    
    }
    However in response to mcdorli, is their anyway to fix this problem without waiting a tick? When this happens the screen flashes bright and then dark again. It just makes me sort of cringe. Anyway to do this with a smoothe transition between each one?

    Also sorry if I made any mistakes with posting this, this is my first post
     
  5. Offline

    mcdorli

    One, remove those if else chains, switches are made for this.
    Two if you wait one tick, then in the first one, you close the inventory, then in the one righr after that, you open a new one. There shouldn't be any effect between them
     
  6. Offline

    Go Hard

    Here are some methods you could try. You could add a player to an arraylist. If the player is in the arraylist when they click in a specific inventory then cancel the event. Then on an InventoryCloseEvent check to see if that player is in the arraylist and remove them.

    You could also try this. Instead of creating your inventories in a method make them public static assuming you have them in a different class. Now instead of using this:
    Code:
    e.getInventory().getName().equals("Sell Item") {
    use:
    Code:
    if(player.getInventory().getName().equals(CLASS_NAME.INVENTORY_NAME.getName())) {
    This way you're checking what inventory the player is clicking in.
     
  7. Offline

    mcdorli

    Make them static just because they're in different classes? That's really incorrect. Please, don't try to help uf ypu ypzrself don't have a basic understanding if what these are.
     
  8. Offline

    Go Hard

    @mcdorli I was assuming he was calling methods using statics from a different class because i saw this
    Code:
    public static void openBuyItem(Player player) {
    He may have a different way of doing it i'm not sure because i don't have the FULL class. Now with that said you should hop out this thread and take some spelling classes :)
     
  9. Offline

    mcdorli

    Saying somebody "make them static assuming ypu have them in different classes" is very bad.

    I'm on my phone, and I'm not native english.
     
  10. Offline

    Go Hard

    @mcdorli "ASSUMING"

    He may have a method that can call his methods from different classes but like i said i don't have the full code. I was figuring he was using statics to call them from a different classes because his openBuyItem method was a static.
     
Thread Status:
Not open for further replies.

Share This Page