Not Getting Proper Inventory

Discussion in 'Plugin Development' started by RoboticPlayer, Oct 6, 2015.

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

    RoboticPlayer

    For some reason, when I try to use my InventoryClickEvent, it is acting as if I clicked in the wrong inventory (along with many other issues). So, I have my MainGUIListener class which is listening for click events with the inventory defined in the MainGUI class.
    Here is the MainGUIListener class: http://pastebin.com/4xx3QjVp
    Here is the MainGUI class: http://pastebin.com/Su1BES7K
    It should be the correct inventory, however when I click inside the inventory, I get the debug message saying that it isn't the correct inventory. The reason that it should be the correct inventory is because that inventory is opened with a command.
    Here is the AdminGUI class: http://pastebin.com/5DGbHu9j
    As you can see, when the command /admingui is run, it opens the inventory defined in the MainGUI via the getMainInv() method (which returns the inventory). Therefore, it should still be the same inventory (especially since I'm checking with the same method), but it isn't recognized as the same one.
    I also have a different listener for each inventory that I have (for sake of readability), and each one has a constructor for the inventory that it is listening for. Each inventory class has a constructor for the MainGUI class so that I am able to have an item to switch the inventory back to the one defined in the MainGUI class. My listeners are registered here:
    Code:Java
    1.  
    2. public void registerEvents() {
    3. PluginManager pm = this.getServer().getPluginManager();
    4. pm.registerEvents(new InventoryListener(this), this);
    5. pm.registerEvents(new ChatListener(this), this);
    6. pm.registerEvents(new ChatGUIListener(new ChatGUI(new MainGUI())), this);
    7. pm.registerEvents(new InventoryGUIListener(new InventoryGUI(new MainGUI())), this);
    8. pm.registerEvents(new MainGUIListener(new MainGUI()), this);
    9. pm.registerEvents(new OtherGUIListener(new OtherGUI(new MainGUI())), this);
    10. pm.registerEvents(new PlayerGUIListener(new PlayerGUI(new MainGUI())), this);
    11. pm.registerEvents(new WorldGUIListener(new WorldGUI(new MainGUI())), this);
    12. }

    In my onEnable method, I call the registerEvents() method. If I remove my check for the inventory, I have other issues, but I would like to take it one step at a time. If anyone could help, that would be greatly appreciated. Tahging people who might be able to help :p @mythbusterma @teej107 @Xerox262 @timtower
    PLEASE NOTE: I am NOT asking for someone to spoonfeed me code, I want to understand what I have done wrong and how to fix it (especially for if I or someone else has a similar problem in the future).
     
  2. Offline

    Xerox262

    The way I would do it is I would check the name of the inventory, then if it was the MainGUI for example then I would continue
     
  3. Offline

    Zombie_Striker

    Where is your main class?

    Please post all code related to the project. The whole main class, the class that has that method above and anything the "problem classes" references.

    Is instance null? Is mainINV null? Do you have any way of tracking the instances of the Inventories.
     
    Last edited: Oct 6, 2015
  4. Offline

    RoboticPlayer

    Why do you say that? I have done it before the way I am doing it now and it worked fine.
     
  5. Offline

    Zombie_Striker

    @henderry2019
    Actually, that way is better.
    Because instances can be "tricky" when it comes to Inventories, (If you know that no plugin on the server uses an Inventory with the same name) I find it is better to work with the names of the inventories instead of the instances themselves.

    Try it by testing if the Inventory.getTitle is the same as MainInv.getTitle.
     
  6. Offline

    boomboompower

    @henderry2019
    I would use something like
    Code:
    if (SomeInventory.getName().equals(OtherInventory.getName())) {
    // Do something
     
  7. Offline

    RoboticPlayer

  8. Online

    timtower Administrator Administrator Moderator

    @henderry2019 Use a variable instead of making multiple instances of the same object.
    Don't randomly tahg people please.
     
    DoggyCodeâ„¢ likes this.
  9. Example of a good system :

    Code:
    if(inventory.getHolder() instanceof CustomInventoryHolder) {
      event.setCancelled(true);
    
      CustomInventoryHolder holder = (CustomInventoryHolder) inventory.getHolder();
    
      ItemStack stack = event.getCurrentItem();
      if(stack == null || stack.getType() == Material.AIR) {
       return;
      }
    
      ClickAction action = holder.getClickAction(event.getRawSlot());
    
      if(action == null) {
       return;
      }
    
      action.execute((Player) event.getWhoClicked());
     
    }
    
    Try finding out the rest by yourself.
     
  10. Offline

    RoboticPlayer

    But that's not my issue. My issue is that it isn't recognizing the inventory that I am clicking as the proper inventory.
     
  11. Yeah it sure isnt, it isn't working because the way you're trying to make inventories is really bad, that's all... If even after all you wanna keep doing it wrong, don't expect get much help.
     
  12. Offline

    RoboticPlayer

    @MaTaMoR_ What is your way of creating custom inventories then?
     
  13. Create a CustomHolder and put a map in, then create a inventory with that holder and do what i posted.
     
  14. Offline

    RoboticPlayer

    @MaTaMoR_ I have never dealt with CustomHolders nor Java Maps (or Minecraft maps, but I am assuming that you are talking about Java Maps).
     
  15. You just have to create a new class and implement 'InventoryHolder' create the needed methods and go... And yeah it is a Java Map, it is really easy to use search some info about it.
     
Thread Status:
Not open for further replies.

Share This Page