Inventory Detection Help

Discussion in 'Spigot Plugin Help' started by JacobRuby, Jun 8, 2017.

  1. Heyo,
    So I've been working on a plugin, and I'm setting up a GUI. I've gotten the first started and click on that inventory works, however, when I use the same method of looking for a certain item in that inventory when a player clicks it, it doesn't work on this other inventory.
    Code (Text):
    package me.jacobruby.rubyeventmanager.mechanics.listeners;

    import me.jacobruby.rubyeventmanager.core.Database;
    import me.jacobruby.rubyeventmanager.core.EventManager;
    import me.jacobruby.rubyeventmanager.mechanics.commands.actions.*;
    import me.jacobruby.rubyeventmanager.mechanics.entities.GUI;
    import net.md_5.bungee.api.ChatColor;
    import org.bukkit.Bukkit;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.ClickType;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;

    public class InventoryClick implements Listener {

        private static EventManager em = EventManager.getInstance();
        private static Database db = Database.getInstance();

        @EventHandler
        public void click(InventoryClickEvent e) {
            Inventory inv = e.getClickedInventory();

            Player p = (Player) e.getWhoClicked();
            int slot = e.getSlot();
           
            //So, this entire if statement works, I'm testing for the "Damage Settings" item in slot 14, if it's there, run stuff.
            if (inv.getContents()[14].getItemMeta().getDisplayName().equals(EventManager.c("&e&lDamage Settings"))) {
                e.setCancelled(true);
                if (p.hasPermission("events.host") || db.isCoHost(p)) {
                    switch(slot) {
                        case (0): {
                            p.closeInventory();
                            Kill.killAllMobs(p, 25);
                            break;
                        }
                        case (1): {
                            em.broadcast(inv.getName() + "\n" + inv.getTitle());
                            break;
                        }
                        case (2): {
                            p.closeInventory();
                            Heal.all();
                            break;
                        }
                        case (4): {
                            p.closeInventory();
                            Teleport.tpall(p);
                            break;
                        }
                        case (6): {
                            if (e.getClick().equals(ClickType.SHIFT_RIGHT)) {
                                p.closeInventory();
                                ClearInventory.all();
                            } else {
                                p.sendMessage(EventManager.c("&cYou must Shift Right click to use this!", true));
                            }
                            break;
                        }

                        case (12): {
                            p.closeInventory();
                            GUI.generalSettings(p);
                            break;
                        }
                        default: {
                            p.getWorld().playSound(p.getLocation(), Sound.BLOCK_ANVIL_LAND, 2, 2);
                        }
                    }
                } else {
                    p.sendMessage(EventManager.c("&cYou do not have permission to do that!", true));
                    p.closeInventory();
                }
            }
            //Now, this module, which uses the same exact method, DOES NOT WORK! I've tried looking for an item in the previous
            //Inventory, the one up there ^^^^, and it WORKS. So there must be something with this inventory that's not
            //working properly.
            if (inv.getContents()[3].getItemMeta().getDisplayName().equals(EventManager.c("&b&lOpen Inventory"))) {
                //This is my test statement to see if it works.
                em.broadcast("&a&lIt worked!");
                e.setCancelled(true);
                Player t = Bukkit.getServer().getPlayer(ChatColor.stripColor(e.getClickedInventory().getName()));
                if (p.hasPermission("events.host") || db.isCoHost(p)) {
                    switch (slot) {
                        case(0): {
                            p.closeInventory();
                            GUI.topMenu(p);
                            break;
                        }
                        case(1): {
                            e.setCancelled(false);
                            t.getEquipment().setHelmet(inv.getItem(slot));
                            break;
                        }
                        case(3): {
                            p.closeInventory();
                            OpenInventorySpy.open(p, t);
                            break;
                        }
                    }
                } else {
                    p.sendMessage(EventManager.c("&cYou do not have permission to do that!", true));
                    p.closeInventory();
                }
            }
            if (inv.getName().equals(EventManager.c("&e&lGeneral Settings"))) {
                e.setCancelled(true);

            }
        }


    }
     
    That is my entire listener class.
    This is a picture of the first menu, and the item I'm detecting to make sure that it's the right inventory.
    [​IMG]
    https://gyazo.com/dfe550a6046a06d279eabbea3fb8a89f
    This is a picture of the second inventory and the item I'm trying to detect, but it's not working for some reason.
    [​IMG]
    https://gyazo.com/e867003250587bba2f5369b6f01abb5e
    I've concluded that testing for any item in the second inventory does not work, and I can't check for the title, because the title changes depending on what player you click on.

    If someone could help me out and tell me why the InventoryClickEvent is not picking up the second inventory, that would be greatly appreciated!
    Thanks,
    -Ruby
     
  2. You said that you can't check for the title but...
    You can use:
    Code (Text):
    if (e.getInventory().getName().equals(InvetoryClassName.inventoryname.getName())){
     
  3. But that doesn't work because the GUI class method takes 2 players, the person to open the inventory and the person who's menu you're opening.
    Using getName() won't work because the name changes, and multiple of them can be opened at the same time.
     
    • Agree Agree x 1