Solved Cant check Inventory name on InventoryClickEvent?

Discussion in 'Plugin Development' started by Gonmarte, May 2, 2016.

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

    Gonmarte

    Im sure that my inventory has that name, but it doesnt cancell the event and send the message....
    Im also sure that the inventory is created, once i can open it, and the title of the inventory is 1
    I dont see the error....
    I have already tested in another projet, i created an inv on a comand, and then cancelled the event, and it worked, but it doesnt work with this inv....

    Code:
    }else if(event.getInventory().getName().equals("1")){
                    event.setCancelled(true);
                    player.sendMessage("It is working??");
                }
    
     
  2. Offline

    mine-care

    @Gonmarte remember, color codes translate into Strings so if it is 'ChatColor.RED+"1"' then name.equals("1") is false.
    To solve this, use ChatColor.stripColor(String param); before comparing the two strings, or use the inventory object directly... (event#getInventory().equals(otherInventoryObject);)
     
  3. Offline

    I Al Istannen

    @Gonmarte
    You can print out the inventory name to see what it is. Print it to the console, so color codes show up. Also the standard things, like is your Listener registered and do you have the EventHandler annotation.
     
  4. Offline

    Gonmarte

    I do not have colores in the name.
    I have already printed the name and it is 1....
    In the same method, im checking another inv name and it works, it only doesnt work with this one....
     
  5. Offline

    I Al Istannen

    @Gonmarte
    It's an else if. Ìs the if condition before true?
     
  6. Offline

    Gonmarte

    It depends.
    If the inventory has that name it is true and it will run that code, if not it will just pass to this one and if it is true run this code.
    The problem is not in the else if.
     
  7. Offline

    I Al Istannen

    @Gonmarte
    If the name equals "1" and you printed that out right before, it should work. You can add a event.getInv().getName().equals("1") to verify it is indeed equals. May I see your whole method?
     
  8. Offline

    Gonmarte

    @I Al Istannen

    Code:
        @EventHandler
        public void onInventoryClickEvent(InventoryClickEvent event){
          
            if(event.getWhoClicked() instanceof Player){
                Player player = (Player) event.getWhoClicked();
               // String name = doesnt matter
               
                if(event.getInventory().getName().equals(name)){
               // doesnt matter
                }else if(event.getInventory().getName().equals("1"))){
                    event.setCancelled(true);
                    player.sendMessage("It is working??");
                }
                }
            }
     
  9. Offline

    I Al Istannen

    @Gonmarte
    Hey, I copy pasted your code (you have one more bracket than needed after "1") using this:
    Code:
        @EventHandler
        public void onInventoryClickEvent(InventoryClickEvent event) {
    
            if (event.getWhoClicked() instanceof Player) {
                Player player = (Player) event.getWhoClicked();
                String name = "Test";
    
                if (event.getInventory().getName().equals(name)) {
                    System.out.println("equal");
                } else if (event.getInventory().getName().equals("1")) {
                    event.setCancelled(true);
                    player.sendMessage("It is working??");
                }
            }
        }
       
        @EventHandler
        public void onChat(final AsyncPlayerChatEvent e) {
            new BukkitRunnable() {
               
                @Override
                public void run() {
                    if(Math.random() < 0.5) {
                        e.getPlayer().openInventory(Bukkit.createInventory(null, 9, "Test"));
                    }
                    else {
                        e.getPlayer().openInventory(Bukkit.createInventory(null, 9, "1"));           
                    }               
                }
            }.runTask(InventoryProfiles.getInstance());
        }
    It worked fine. The error must lie in how you create the inventory (= It's title) or you haven't registered the listener correctly.
     
  10. Offline

    Gonmarte

    Code:
        public DoesntMatter(//doesntmatter, String name, int slots){
            //doesnt matter
            this.name = name;
            // doesnt matter
            this.slots = slots;
            //doesnt matter
            this.inv = Bukkit.createInventory(null, slots,name);
        }
    Code:
                        String name = "1";
                       DoesntMatter dm= new DoesntMatter(//doesnt matter, name, slots);
    
     
  11. Offline

    Gonmarte

  12. Offline

    I Al Istannen

    @Gonmarte
    I have literally no idea. If the listener is registered and all, it should work. Does the code I posted work? If yes, you could try printing out inv.getName() in the DoesntMatter constructor, to see what it is. If it is also "1", I have no idea how to fix that, sorry.
     
  13. Offline

    Gonmarte

    Its printing one....
    This is a stupid bug that doesnt even make any sense.....
     
  14. Offline

    I Al Istannen

    @Gonmarte
    Now, out of desperation, could you post your entire DoesntMatter class (if it isn't too big) and the whole Listener (same as for DoesntMatter). I have honestly no idea what it can be at this point, so I want to just toy around with it for a bit :)

    Just right before, do you cancel the event in any other Listener before? Is there another listener?
     
  15. Offline

    Gonmarte

    Before that, just found out a strange thing.
    when i do the command /open it will open an inventory with a book inside, if i click in that book it will open that inventory that if i click it doesnt do anything, the one that i have problems with. So, i create a command that instead of opening first that inventory and then the other, it opens that inventory "1" and its working :-: It doesnt only work if i open the other inventory and i click the book.....
    I will try updateInventory();
    EDIT: updateInventory doesnt work....
     
  16. Offline

    I Al Istannen

    @Gonmarte
    Maybe your other code to open the inventory is flawed then? I don't know how openInventory handles a exiting inventory, but you could try calling closeInventory() before, but I think it won't change anything.
     
  17. Offline

    Gonmarte

    It didnt work.
    The other code is the same. It is in the same method, the only difference is that it has differente names and slosts... When i call that method it creates that 2 inventories
    EDIT:
    It doesnt work .-.
    Code:
    package me.gonmarte;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener{
        Inventory inv , inv2;
        @Override
        public void onEnable() {
            Bukkit.getPluginManager().registerEvents(this, this);
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equalsIgnoreCase("put") && sender instanceof Player) {
                 inv = Bukkit.createInventory(null, 9, "1");
                 inv2 = Bukkit.createInventory(null ,  27 , "2");
                 inv.addItem(new ItemStack(Material.BOOK));
                 return true;
        }else if(command.getName().equalsIgnoreCase("open") && sender instanceof Player){
            Player p = (Player) sender;
            p.openInventory(inv);
            return true;
        }
            return false;
        }
       
        @EventHandler
        public void on(InventoryClickEvent e){
            if(e.getInventory().getName().equals("1")){
                e.setCancelled(true);
                if(e.getCurrentItem().getType().equals(Material.BOOK)){
                Player player = (Player) e.getWhoClicked();
                player.openInventory(inv2);
                }
            }else if(e.getInventory().getName().equals("2")){
                //Does not work!!
                e.setCancelled(true);
                Player p = (Player)e.getWhoClicked();
                p.sendMessage("b");
            }
        }
    }
     
    Last edited: May 4, 2016
Thread Status:
Not open for further replies.

Share This Page