My inventory is not working

Discussion in 'Spigot Discussion' started by Fitim-Michael, May 9, 2017.

  1. Hello, i created an inventory for my plugin, at the begin it worked, but since i added some things and commands it stopped working. can anyone help me?

    Inventory Code:
    Code (Text):
    package me.qerimicobussen.kingdomsandempires.Menus;

    import java.util.HashMap;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    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 me.qerimicobussen.kingdomsandempires.Managers.ChatColorManager;
    import me.qerimicobussen.kingdomsandempires.Managers.MenuManager;

    public class ReligionsMenu implements Listener {
       
        public HashMap<String, String> relig = new HashMap<String, String>();

        public Inventory inv;
        private ItemStack a, b;
        public ReligionsMenu menu;

        public ReligionsMenu() {
            inv = Bukkit.getServer().createInventory(null, 18,
                    ChatColorManager.CCM("                   &3&l~~{&6&lReligionMenu&3&l}~~"));

            a = MenuManager.createItem(Material.NETHER_STAR, "Herobrine");
            b = MenuManager.createItem(Material.GOLDEN_APPLE, "Notch");

            inv.setItem(3, a);
            inv.setItem(5, b);
        }

        public void ShowReligions(Player p) {
            p.openInventory(inv);
        }
       
        @EventHandler
        public void onInvClickRM(InventoryClickEvent e) {
            if (!e.getInventory().equals(inv))
                return;
           
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Herobrine")) {
                e.getWhoClicked().sendMessage(ChatColorManager.CCM("&6&lYour Faith Is Now: &5&lHerobrine&r"));
                relig.put(e.getWhoClicked().getName(), "Herobrine");
                e.setCancelled(true);
                e.getWhoClicked().closeInventory();
            } else if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Notch")) {
                e.getWhoClicked().sendMessage(ChatColorManager.CCM("&6&lYour Faith Is Now: &e&lNotch&r"));
                relig.put(e.getWhoClicked().getName(), "Notch");
                e.setCancelled(true);
                e.getWhoClicked().closeInventory();
            }
        }
    }
    Main Code
    Code (Text):
    package me.qerimicobussen.kingdomsandempires;

    import org.bukkit.plugin.java.JavaPlugin;

    import me.qerimicobussen.kingdomsandempires.Commands.ReligionsCMD;
    import me.qerimicobussen.kingdomsandempires.Commands.jobs.JobCheckCMD;
    import me.qerimicobussen.kingdomsandempires.Commands.jobs.JobsCMD;
    import me.qerimicobussen.kingdomsandempires.Menus.JobsMenu;
    import me.qerimicobussen.kingdomsandempires.Menus.ReligionsMenu;
    import net.milkbowl.vault.economy.Economy;

    public class Kingdoms_Empires extends JavaPlugin {

        public static Economy econ = null;

        @Override
        public void onEnable() {

            System.out.println("=={ Kingdoms & Empires }==");
            System.out.println("=={ Version: 0.1.5.2   }==");
            System.out.println("=={ ¬©Qerimi & Cobussen }==");
            System.out.println("=={ For Real Fun Games }==");

            getConfig().options().copyDefaults(true);
            saveConfig();

            KECmds();
            KEEvents();

            super.onEnable();
        }

        @Override
        public void onDisable() {

            super.onDisable();
        }

        public void KECmds() {
            getCommand("Jobs").setExecutor(new JobsCMD());
            getCommand("Religions").setExecutor(new ReligionsCMD());
            getCommand("Job").setExecutor(new JobCheckCMD());

        }

        public void KEEvents() {
            getServer().getPluginManager().registerEvents(new JobsMenu(), this);
            getServer().getPluginManager().registerEvents(new ReligionsMenu(), this);
        }

    }
     
    If you need more pieces of code, i will post them for u
     
  2. What exactly isn't working? It is not opening, are the events not working?
     
  3. o sorry, the inventory opens, but when i click on something i an just grab it out, so the events are not working
     
  4. Are there any errors present in the console?
     
  5. Is the event activated?
    did you test it with a syso or anything else?
     
  6. no there are no errors on launch, how do you test events with a syso, just put one of in them?
     
  7. yes i use a syso often to check if the event is fired
    so i can see if the event dont work or the code inside the event dont work
     
  8. ohh i think i get it, why do you return in the second line of the evnt? ther are no { } after the if check maby this is the reason
     
  9. Code (Text):
     @EventHandler
        public void onInvClickRM(InventoryClickEvent e) {
            if (!e.getInventory().equals(inv)){
              // This will check if the inventory is not the correct one!
              return;
            } else {
    //It is the correct inv!
            if (e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("Herobrine")) {
                e.getWhoClicked().sendMessage(ChatColorManager.CCM("&6&lYour Faith Is Now: &5&lHerobrine&r"));
                relig.put(e.getWhoClicked().getName(), "Herobrine");
                e.setCancelled(true);
                e.getWhoClicked().closeInventory();
            } else if (e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("Notch")) {
                e.getWhoClicked().sendMessage(ChatColorManager.CCM("&6&lYour Faith Is Now: &e&lNotch&r"));
                relig.put(e.getWhoClicked().getName(), "Notch");
                e.setCancelled(true);
                e.getWhoClicked().closeInventory();
            }
          }
    }
    }
    I think you messed up checking if the inventory is the correct!
     
  10. While it might look funny, it's valid Java. This is not the issue what so ever.

    All of these are valid:
    Code (Text):
    // Most common
    if (condition) {
      statement;
    }
    Code (Text):
    // Only can be used under certain circumstances
    if (condition) statement;
    Code (Text):
    // How they did it.
    if (condition)
      statement;
    Also, this probably isn't the issue, but try comparing inventory names instead of the inventories themselves. No reason why it should work, but you never know.
     
  11. tnx, i will test it later today, i can't run my mc server @ school. i will let you know if it works
     
  12. @Fitim-Michael, add the following line to check where the error is. It should go below ReligionsMenu.java:41
    e.getWhoClicked().sendMessage("test");
    If you see that message when the event is fired, it means that the inventories do not match.
     
  13. @SirGregg, yup, I get the test message, so that means the inventory's don't match. how do I solve this? and by the way, I forgot to say there are 2 inventory's, they are build the same way, but with different variables
     
  14. Code (Text):
    if (e.getInventory() == null){
                    return; //Checks if an inventory is clicked
                }
                if (e.getCurrentItem() == null){
                    return; //Checks if an item is actually clicked
                }
                if (e.getCurrentItem().getType() ==Material.AIR){
                    return; //Checks to make sure the item isn't empty
                }
    Once you've done that, cancel the event after your if statements to prevent people from removing the items from your inventory
     
  15. @Fitim-Michael, instead of checking the Inventory object, try comparing a color stripped version of the current inv's title with a color stripped version of the one it should be.
     
  16. Why not check for the inventory type or name.. The inventory will never be equal or Atleast not all the time. The name could be different, type, contents, ID, owner etc
     
  17. Typically you check the inventory name.
     
  18. Yes, you're correct, I messed up!