Solved Inventory Menu (GUi) Doesn't work.

Discussion in 'Plugin Development' started by BaconGamesnl, Nov 20, 2015.

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

    BaconGamesnl

    Hey everyone,

    I, am making my own kit-pvp plugin. So i decided to make a kit selector. The kit selector is GUI Menu like a lobby selector or something so i watched some tutorials. I watched 3 tutorials (and some Google) but it still doesn't work.

    This is my code for the selector:
    Code:
    //KIT SELECTOR
        private void openGUI(Player p) {
            Inventory i = Bukkit.createInventory(null, 9, ChatColor.DARK_BLUE + "Kit Selector");
    
            // Kit meta's
            ItemStack soldier = new ItemStack(Material.STONE_SWORD);
            ItemMeta soldierMeta = soldier.getItemMeta();
    
            ItemStack archer = new ItemStack(Material.BOW);
            ItemMeta archerMeta = archer.getItemMeta();
    
            // Display names
            soldierMeta.setDisplayName(ChatColor.GOLD + "Soldier");
            soldier.setItemMeta(soldierMeta);
    
            archerMeta.setDisplayName(ChatColor.GOLD + "Archer");
            archer.setItemMeta(archerMeta);
    
            // Inventory Item
            i.setItem(0, soldier);
            i.setItem(1, archer);
    
            p.openInventory(i);
        }
        //Eventhandlers
    
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if(!ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase("Kit Selector"))
            return;
            Player p = (Player) e.getWhoClicked();
            e.setCancelled(true);
           
            if(e.getCurrentItem() == null || e.getCurrentItem().getType() == Material.AIR || !e.getCurrentItem().hasItemMeta()) {
                p.closeInventory();
                return;
            }
            switch(e.getCurrentItem().getType()) {
            case STONE_SWORD:
                p.performCommand("soldier");
                p.closeInventory();
                break;
            case BOW:
                p.performCommand("Archer");
                p.closeInventory();
                break;
            default:
                p.closeInventory();
                break;
            }
        }
    
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            Action a = e.getAction();
            if (a == Action.PHYSICAL || e.getItem() == null || e.getItem().getType() == Material.AIR)
                return;
    
            if (e.getItem().getType() == Material.COMPASS) {
                openGUI(e.getPlayer());
            }
        }
    
        //END OF KIT SELECTOR
    And this is the code that gives you the kit selector (compass).
    Code:
    //WHEN PLAYER JOIN SERVER
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            String pname = p.getName();
            p.getInventory().clear();
            //Message to send the server on Join!
            Bukkit.broadcastMessage(ChatColor.AQUA + "[Kit-PVP] " + ChatColor.GOLD + pname + ChatColor.GREEN + " has entered Kit-PVP!");
           
            //Give the player the SELECTOR!
            ItemStack kitselector = new ItemStack(Material.COMPASS);
            ItemMeta ks = kitselector.getItemMeta();
            ks.setDisplayName(ChatColor.GOLD + "Kit Selector");
            kitselector.setItemMeta(ks);
            p.getInventory().setItem(4, kitselector);
        }
        //END OF PLAYER JOINING THE SERVER!
    There are no errors in the console and i don't have warnings in Eclipse.

    I have currently 2 kits you can get a kit by typing /soldier or /archer. So thats why the player automatically perform this command when they selector their kit in the selector.

    I hope someone can help!!

    Thanks :)
     
  2. Offline

    Scimiguy

    Please tell me you didn't watch TheBCBros

    Do the soldier and archer commands even exist?
     
  3. Offline

    BaconGamesnl

    Nope i didn't watched TheBCBros I,m warned :p

    And yes the /soldier and /archer commands work fine! But they are in another class as the kit selector maybe is that the problem...?
     
  4. Offline

    Creeper674

    Try debugging your code, by adding p.sendMessage("Debug #1"); for example, at certain points in the code so you can see what is and isn't being executed after events are called.
     
    Last edited: Nov 21, 2015
  5. Offline

    BaconGamesnl

  6. Offline

    Mrs. bwfctower

  7. Offline

    BaconGamesnl

    @Mrs. bwfctower I did this: I think it is right...

    Code:
    public void registerListeners() {
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this, this);
        }
    
        @Override
        public void onEnable() {
            instance = this;
            registerListeners();
     
  8. Offline

    BaconGamesnl

    I haven't fixed the problem yet. Still doesn't know whats wrong :(

    Does somebody have his code from a GUI menu he made in a plugin or a tutorial he recommend?

    It doesn't have to be a kit selector tutorial! a inventory GUI that opens when clicking on a compass is enough. I can add the "kit select" thing by meself I think thats not the problem!

    thanks :)
     
  9. Offline

    Scimiguy

    @BaconGamesnl
    You need to debug, as suggested above

    Put console print outs at certain parts in your code so that when you attempt to run the command, youll be able to see what your code gets up to and what it doesnt.

    Do this, then come back and tell us the results,cincluding showing us the code where you debugged
     
  10. Offline

    sgavster

  11. Offline

    BaconGamesnl

    @sgavster Thanks for the tip! It works :)


    Now the GUI is opening fine! I get the menu with the items that I want: ( I changed some things in the original code but the idea is the same).

    This is the "kit selector":
    https://gyazo.com/95b1c568c13ecc0a1a02eae651c2c475

    When i click a item or try to place it on another place the inventory closes. Thats good.

    But the server doesn't perform the /archer or the /soldier command and i don't get the message in chat for the debugging (I hope i do it on the right way :D).

    So the menu is opening fine but the problem is when i click in the inventory.

    Conclusion : There is something wrong in the: InventoryClickEvent.

    But I don't get what's wrong...? I think i need to "import" the /soldier and /archer command from the other class but I don't know how... (When I typ /soldier and /archer in game it works fine!)

    This is my InventoryClickEvent:
    Code:
    @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if(!ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase("Choose your kit:"))
                return;
            Player p = (Player) e.getWhoClicked();
            e.setCancelled(true);
        
            if(e.getCurrentItem()==null || e.getCurrentItem().getType() == Material.AIR || e.getCurrentItem().hasItemMeta()) {
                p.closeInventory();
                return;
            }
        
            switch(e.getCurrentItem().getType()) {
            case STONE_SWORD:        
                p.sendMessage("soldier kit #debug");
                p.performCommand("soldier");
                p.closeInventory();
                break;
            case BOW:
                p.sendMessage("Bow kit #debug");
                p.performCommand("archer");
                p.closeInventory();
                break;
            default:
                p.closeInventory();
                break;
            }
        }
    I hope someone knows a solution.

    Thanks :)

    Edit: I,m learning a lot from tutorials and the forums. Sorry for maybe "noob" questions but i,m still learning and i really appreciate the time and help i get on the forum :)
     
    Last edited: Dec 5, 2015
  12. Offline

    mythbusterma

    @BaconGamesnl

    Don't close the inventory on the same tick as which the InventoryClickEvent happens (this is one of the more obscure things with the Bukkit system).

    You don't need to "import" anything, as Player#performCommand(..) runs the same code on the server as if the player had entered "/archer" and pressed enter.

    Instead, add "debug statements" to your code, just printing a message (or messaging the player) to see if code is being reached.
     
  13. Offline

    BaconGamesnl

    Fixed it! I did something wrong:

    I had:
    Code:
     if(e.getCurrentItem()==null || e.getCurrentItem().getType() == Material.AIR || e.getCurrentItem().hasItemMeta()) {
                p.closeInventory();
                return;
            }
    So when there is nothing, air or a item with ItemMeta the inventory closes.

    But the items in the inventory have a ItemMeta! So when i clicked them the inventory closes!

    So I Removes the line: e.getCurrentItem().hasItemMeta()) And now it works :)

    Finally my kit selector works haha :p

    Thanks for help everyone :)
     
Thread Status:
Not open for further replies.

Share This Page