Help with inventory menu

Discussion in 'Spigot Plugin Development' started by rhunter, May 11, 2015.

  1. I made an inventory menu and it is working awesome, but when I click the items in it, I don't get the permission node added, and I can take the items out and place them in my inventory. Does anyone have a fix for this?
    Code (Text):
    package me.reece.rpgmenu;

    import java.util.Arrays;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    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.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin{
       
        private void chooseClass(Player player){
            Inventory chooseClass = Bukkit.createInventory(null,9,ChatColor.AQUA+"Choose your class!");
           
            ItemStack archer = new ItemStack (Material.BOW);
            ItemMeta archerMeta = archer.getItemMeta();
            archerMeta.setDisplayName(ChatColor.DARK_PURPLE+"Archer");
            archerMeta.setLore(Arrays.asList(ChatColor.AQUA+"Toggle wither arrows with",ChatColor.AQUA+"/togglewither"));
            archer.setItemMeta(archerMeta);
           
            ItemStack rogue = new ItemStack (Material.STONE_SWORD);
            ItemMeta rogueMeta = rogue.getItemMeta();
            rogueMeta.setDisplayName(ChatColor.DARK_PURPLE+"Rogue");
            rogueMeta.setLore(Arrays.asList(ChatColor.AQUA+"Hold shift to turn",ChatColor.AQUA+"invisible!"));
            rogue.setItemMeta(rogueMeta);
           
            ItemStack knight = new ItemStack (Material.DIAMOND_CHESTPLATE);
            ItemMeta knightMeta = knight.getItemMeta();
            knightMeta.setDisplayName(ChatColor.DARK_PURPLE+"Knight");
            knightMeta.setLore(Arrays.asList(ChatColor.AQUA+"Hold shift to get",ChatColor.AQUA+"resistance!"));
            knight.setItemMeta(knightMeta);
           
            ItemStack healer = new ItemStack (Material.STICK);
            ItemMeta healerMeta = healer.getItemMeta();
            healerMeta.setDisplayName(ChatColor.DARK_PURPLE+"Healer");
            healerMeta.setLore(Arrays.asList(ChatColor.AQUA+"Shoot snowballs from",ChatColor.AQUA+"a stick to heal people!"));
            healer.setItemMeta(healerMeta);
           
            ItemStack mage = new ItemStack (Material.BLAZE_ROD);
            ItemMeta mageMeta = mage.getItemMeta();
            mageMeta.setDisplayName(ChatColor.DARK_PURPLE+"Mage");
            mageMeta.setLore(Arrays.asList(ChatColor.AQUA+"Shoot fireballs from",ChatColor.AQUA+"a blaze rod!"));
            mage.setItemMeta(mageMeta);
           
            chooseClass.setItem(0,archer);
            chooseClass.setItem(2,rogue);
            chooseClass.setItem(4,knight);
            chooseClass.setItem(6,healer);
            chooseClass.setItem(8,mage);
           
            player.openInventory(chooseClass);
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
                if (!(sender instanceof Player)) {
                    return false;
                }
               
                Player player = (Player)sender;
                if (player.hasPermission("rpgmenu.class")){
                if(cmd.getName().equalsIgnoreCase("class")){
                    chooseClass(player);
                }
        }
                return true;
    }
       
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event){
            if(event.getCurrentItem().getItemMeta() == null) return;
            if(event.getCurrentItem().getItemMeta().getDisplayName().contains("Archer")) {
                event.getWhoClicked().addAttachment(this, "wands.archer", true);
                event.setCancelled(true);
                event.getWhoClicked().closeInventory();
            }
            if(event.getCurrentItem().getItemMeta().getDisplayName().contains("Rogue")) {
                event.getWhoClicked().addAttachment(this, "wands.rogue", true);
                event.setCancelled(true);
                event.getWhoClicked().closeInventory();
           
        }
            if(event.getCurrentItem().getItemMeta().getDisplayName().contains("Knight")) {
                event.getWhoClicked().addAttachment(this, "wands.knight", true);
                event.setCancelled(true);
                event.getWhoClicked().closeInventory();
            }
            if(event.getCurrentItem().getItemMeta().getDisplayName().contains("Healer")) {
                event.getWhoClicked().addAttachment(this, "wands.healer", true);
                event.setCancelled(true);
                event.getWhoClicked().closeInventory();
            }
            if(event.getCurrentItem().getItemMeta().getDisplayName().contains("Mage")) {
                event.getWhoClicked().addAttachment(this, "wands.mage", true);
                event.setCancelled(true);
                event.getWhoClicked().closeInventory();
            }
       
       
       
       
       
       
    }}
     
  2. You may want to strip the chat color before checking if it contains anything in the display name.
     
    • Like Like x 1
  3. I did that and it still doesn't work. Any other suggestions?
     
  4. Oh wow, I just realized. You haven't implemented listener nor have you registered it onEnable.
     
    • Like Like x 1
  5. Ok thanks. This is a problem i've been having. I don't know what to put in a listener class and how to register it on onEnable. Could you teach/show me?
     
  6. You need to add an "implements Listener" just like how you put "extends JavaPlugin".
    You will then need to make a private void onEnable(){} method and use this to register the events.
     
    • Like Like x 1
  7. Thanks! So this is what I have in my Listener class (I named it Listen)
    Code (Text):
    public class Listen implements Listener{
       
        void registerEvents(Listener listen,
                Plugin plugin){
           
        }

    }
     
    I don't know what to put in the brackets now
     
  8. No no no. You need to put something like this in your main class:
    PHP:
    public void onEnable(){
    Bukkit.getPluginManager().registerEvents(new Listen(), this);
    }
    -onEnable is the first thing called when the plugin starts up (Kind of like the main(String[] args) in any java program).
    -Bukkit.getPluginManager() is getting the PluginManager of the server.
    -registerEvents(listener, plugin) is telling the server/plugin what class you want to handle the events called in the server.
    -new Listen() is making a new instance of the Listen class and is telling registerEvents what class is the listener class.
    You should now be good to listen to events in your Listen class.
     
    • Like Like x 1
  9. @VenomEater552
    Thank you so much! So do I need to put anything in the Listen class?
     
  10. Yes, that is where you want to put your @EventHandler methods. So take the method you have from your main class and put it into the Listen class.

    Be sure to like my comments if you think I helped.
     
    • Like Like x 1
    • Funny Funny x 1
  11. wait, I thought only the onEnable thing was supposed to go in the listen class. Your saying I should move everything to the listen class and leave the onEnable in the main class?
     
  12. The onEnable is called ONLY in your Main class. Your Events go into the Listen class because that's where the events are handled.
     
    • Like Like x 1
  13. @VenomEater552
    Ok only the onEnable is in my Main class now. The main class is supposed to extend javaplugin and the listener is supposed to implement listener, correct?
    When i moved everything else to the listen class, im getting errors in this part of the code:
    Code (Text):
    @EventHandler
        public void onInventoryClick(InventoryClickEvent event){
            if(event.getCurrentItem().getItemMeta() == null) return;
            if(event.getCurrentItem().getItemMeta().getDisplayName().contains("Archer")) {
                event.getWhoClicked().addAttachment(this, "wands.archer", true);
                event.setCancelled(true);
                event.getWhoClicked().closeInventory();
            }
            if(event.getCurrentItem().getItemMeta().getDisplayName().contains("Rogue")) {
                event.getWhoClicked().addAttachment(this, "wands.rogue", true);
                event.setCancelled(true);
                event.getWhoClicked().closeInventory();
           
        }
            if(event.getCurrentItem().getItemMeta().getDisplayName().contains("Knight")) {
                event.getWhoClicked().addAttachment(this, "wands.knight", true);
                event.setCancelled(true);
                event.getWhoClicked().closeInventory();
            }
            if(event.getCurrentItem().getItemMeta().getDisplayName().contains("Healer")) {
                event.getWhoClicked().addAttachment(this, "wands.healer", true);
                event.setCancelled(true);
                event.getWhoClicked().closeInventory();
            }
            if(event.getCurrentItem().getItemMeta().getDisplayName().contains("Mage")) {
                event.getWhoClicked().addAttachment(this, "wands.mage", true);
                event.setCancelled(true);
                event.getWhoClicked().closeInventory();
            }
       
       
       
       
       
       
    }
    All of the addAttachments are underlined and the error is this:
    The method addAttachment(Plugin, String, boolean) in the type Permissible is not applicable for the arguments (Listen, String, boolean)

    Once this works i will be sure to like all of your comments! you're the most helpful person I've come across =)
     
  14. @VenomEater552
    Ok i think i've figured it out, but it is still doing the same thing as before.
    Main Class:
    Code (Text):
    package me.reece.rpgmenu;

    import java.util.Arrays;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin{
       
        public static void registerEvents(org.bukkit.plugin.Plugin plugin, Listener... listeners) {
            for (Listener listener : listeners) {
            Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
            }
            }
       
        public void onEnable(){
            Bukkit.getPluginManager().registerEvents(new Listen(), this);
            }
       
        private void chooseClass(Player player){
            Inventory chooseClass = Bukkit.createInventory(null,9,ChatColor.AQUA+"Choose your class!");
           
            ItemStack archer = new ItemStack (Material.BOW);
            ItemMeta archerMeta = archer.getItemMeta();
            archerMeta.setDisplayName("Archer");
            archerMeta.setLore(Arrays.asList(ChatColor.AQUA+"Toggle wither arrows with",ChatColor.AQUA+"/togglewither"));
            archer.setItemMeta(archerMeta);
           
            ItemStack rogue = new ItemStack (Material.STONE_SWORD);
            ItemMeta rogueMeta = rogue.getItemMeta();
            rogueMeta.setDisplayName("Rogue");
            rogueMeta.setLore(Arrays.asList(ChatColor.AQUA+"Hold shift to turn",ChatColor.AQUA+"invisible!"));
            rogue.setItemMeta(rogueMeta);
           
            ItemStack knight = new ItemStack (Material.DIAMOND_CHESTPLATE);
            ItemMeta knightMeta = knight.getItemMeta();
            knightMeta.setDisplayName("Knight");
            knightMeta.setLore(Arrays.asList(ChatColor.AQUA+"Hold shift to get",ChatColor.AQUA+"resistance!"));
            knight.setItemMeta(knightMeta);
           
            ItemStack healer = new ItemStack (Material.STICK);
            ItemMeta healerMeta = healer.getItemMeta();
            healerMeta.setDisplayName("Healer");
            healerMeta.setLore(Arrays.asList(ChatColor.AQUA+"Shoot snowballs from",ChatColor.AQUA+"a stick to heal people!"));
            healer.setItemMeta(healerMeta);
           
            ItemStack mage = new ItemStack (Material.BLAZE_ROD);
            ItemMeta mageMeta = mage.getItemMeta();
            mageMeta.setDisplayName("Mage");
            mageMeta.setLore(Arrays.asList(ChatColor.AQUA+"Shoot fireballs from",ChatColor.AQUA+"a blaze rod!"));
            mage.setItemMeta(mageMeta);
           
            chooseClass.setItem(0,archer);
            chooseClass.setItem(2,rogue);
            chooseClass.setItem(4,knight);
            chooseClass.setItem(6,healer);
            chooseClass.setItem(8,mage);
           
            player.openInventory(chooseClass);
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
                if (!(sender instanceof Player)) {
                    return false;
                }
               
                Player player = (Player)sender;
                if (player.hasPermission("rpgmenu.class")){
                if(cmd.getName().equalsIgnoreCase("class")){
                    chooseClass(player);
                }
        }
                return true;
    }
       
        }
    Listen Class:
    Code (Text):
    package me.reece.rpgmenu;

    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Listen extends JavaPlugin implements Listener{
       
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event){
            if(event.getCurrentItem().getItemMeta() == null) return;
            if(event.getCurrentItem().getItemMeta().getDisplayName().contains("Archer")) {
                event.getWhoClicked().addAttachment(this, "wands.archer", true);
                event.setCancelled(true);
                event.getWhoClicked().closeInventory();
            }
            if(event.getCurrentItem().getItemMeta().getDisplayName().contains("Rogue")) {
                event.getWhoClicked().addAttachment(this, "wands.rogue", true);
                event.setCancelled(true);
                event.getWhoClicked().closeInventory();
           
        }
            if(event.getCurrentItem().getItemMeta().getDisplayName().contains("Knight")) {
                event.getWhoClicked().addAttachment(this, "wands.knight", true);
                event.setCancelled(true);
                event.getWhoClicked().closeInventory();
            }
            if(event.getCurrentItem().getItemMeta().getDisplayName().contains("Healer")) {
                event.getWhoClicked().addAttachment(this, "wands.healer", true);
                event.setCancelled(true);
                event.getWhoClicked().closeInventory();
            }
            if(event.getCurrentItem().getItemMeta().getDisplayName().contains("Mage")) {
                event.getWhoClicked().addAttachment(this, "wands.mage", true);
                event.setCancelled(true);
                event.getWhoClicked().closeInventory();
            }
       
       
       
       
       
       
    }

    }
     
     
  15. addAttachment tells the server that your plugin is the permission-plugin that handles the permission. That way you don't modify your existing permission database.
    You either need to access the API of your Permission Plugin to store it permant (I would recommend using a wrapper like Vault)
    or you have to store your own user database, and re-enable the permission on every login of the user.

    A permission plugin works the following (simplified):
    • User logs in
    • Permission-Plugin ready its permission.yml
    • Permission-Plugin adds Attachment to new logged in user for every permission defined for the user
     
  16. You can't extend JavaPlugin in two different classes. Also, you don't create a registerEvents method.. Look at the post I gave you with code and how I explained everything. You may want to go look at a few bukkit tutorials on listeners before replying.