Inventory comparing best way?

Discussion in 'BungeeCord Plugin Development' started by Ibas, Jul 8, 2013.

  1. How may I compare two inventories I don't like comparing title names. I saw one developer, ompared somehow in other way but I can't understand how he did that. http://www.spigotmc.org/resources/libs-hungergames.55/

    My code is:

    Code (Text):
     @EventHandler
              public void onInventoryInteract(InventoryClickEvent e)
              {
                if (e.getInventory().getTitle() == "Menu")
    }
    }
    Does anybody know any better way, when comparing? Because I want use title for other things like:
    "Hello, + player.getName()"
     
    #1 Ibas, Jul 8, 2013
    Last edited: Jul 8, 2013
  2. What do you mean comparing inventories ? You mean knowing what kind of inventory ? Then you should use
    Code (Text):
    if (e.getInventory().getType() == InventoryType.PLAYER)
    . Here is the documentation for InventoryType : http://jd.bukkit.org/rb/apidocs/org/bukkit/event/inventory/InventoryType.html.
    You could also compare the contents one by one, the holder, etc... There are many ways to do what you want.

    Also, note that in java,
    Code (Text):
    e.getInventory().getTitle() == "Menu"
    may return false even if getTitle returns "Menu" because java is a stupid language. You need to use
    Code (Text):
    e.getInventroy().getTitle().equals("Menu")
    . The same goes for MANY comparisons. The only things you can compare safely with == are primitives (int, double, float...) and enums.
     
  3. libraryaddict

    Patron

    I just compare the viewers of both inventories.
     

  4. Yes, I saw that, but I still can't understand how does that works.
     
  5. libraryaddict

    Patron

    It checks if the people looking at your inventory are the same as this.
    People can only look at a max of 2 inventories.

    Player Inventory and the top inventory.

    I don't worry about the player inventory as I do nothing in it.
     
  6. I have an inventory meta system on my server, but I think im going to modify it and submit to CB so that Inventories are Metaable

    Also, theres is another PR I will do to make CraftInventory.equals() work on comparing inventories, since bukkit has a bug that inventory passed to player.openInventory is not the object used in inventory events.