Solved GUI - Item Click doesnt work

Discussion in 'Plugin Development' started by VNGC, Mar 18, 2020.

  1. Offline

    VNGC

    Hello,
    i created a gui which opens with a command, works so far. but if i click the item i want to get a message sent, but there does nothing happen, except i can drag the items, what shouldnt be possible.

    here is my main class:

    Code:
    package main;
    
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import settings.Challenges;
    
    public class main extends JavaPlugin implements CommandExecutor {
    
        private Inventory inv = null;
    
        @Override
        public void onEnable() {
    
            getServer().getPluginManager().registerEvents(new Challenges(), this);
           
           
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            Player p = (Player) sender;
    
            if (cmd.getName().equalsIgnoreCase("settings")) {
    
                inv = p.getPlayer().getServer().createInventory(null, 45, "Challenges");
    
                ItemStack stack = new ItemStack(Material.IRON_BOOTS);
                ItemMeta meta = stack.getItemMeta();
                meta.setDisplayName("§7[§6Sneak§7]");
    
                ItemStack stack1 = new ItemStack(Material.BREAD);
                ItemMeta meta1 = stack1.getItemMeta();
                meta1.setDisplayName("§7[§4Test§7]");
    
                ItemStack stack2 = new ItemStack(Material.COOKED_BEEF);
                ItemMeta meta2 = stack2.getItemMeta();
                meta2.setDisplayName("§7[§4Test§7]");
    
               
               
                inv.setItem(1, stack);
                inv.setItem(4, stack1);
                inv.setItem(7, stack2);
               
                p.getPlayer().openInventory(inv);
               
            }
    
            return false;
        }
    
        @Override
        public void onDisable() {
    
        }
    
    }
    

    my Challenges Class:

    Code:
    package settings;
    
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    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.ItemStack;
    
    public class Challenges implements Listener {
    
        @EventHandler
        public void Inventory(InventoryClickEvent event) {
            Player p = (Player) event.getWhoClicked();
    
            ItemStack diamondsword = new ItemStack(Material.DIAMOND_SWORD, 1);
            diamondsword.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 5);
    
            if (event.getInventory().getClass().getName().equalsIgnoreCase("Challenges")) {
                event.setCancelled(true);
    
                // Sneak
                if (event.getCurrentItem().getType() == Material.IRON_BOOTS) {
                    p.getInventory().setItem(4, new ItemStack(Material.DIAMOND_SWORD, 1));
                    p.sendMessage("Das ganze Funktioniert!");
                    event.getView().close();
                   
                } else if (event.getCurrentItem().getType() == Material.BREAD) {
                    p.getInventory().setItem(4, new ItemStack(Material.BREAD, 1));
                    p.sendMessage("Das funktioniert ebenfalls!");
                    event.getView().close();
                   
                } else if (event.getCurrentItem().getType() == Material.COOKED_BEEF) {
                    p.getInventory().setItem(4, new ItemStack(Material.BREAD, 1));
                    p.sendMessage("Auch dies funktioniert wie es soll!");
                    event.getView().close();
                }
            }
        }
    
    }
    
    thanks for help!
     
  2. Offline

    Tango_

    When checking inventory name on 1.15 use
    Code:
            if(event.getView().getTitle().equals("Challenges")) {
                // Code here
            }
     
  3. Offline

    VNGC

    i use 1.14, is it there needed as well?
     
  4. Offline

    Tango_

    Looks like it works on 1.14 too
     
  5. Offline

    VNGC

    Working! Much Thanks! :)

    well, now i see, there are no item Names... what did i wrong :c

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.

    edit: oops sry i forgot :(
     
  6. Offline

    Tango_

    You need to set the meta to the itemstack

    Code:
            ItemStack stack = new ItemStack(Material.IRON_BOOTS);
            ItemMeta meta = stack.getItemMeta();
            meta.setDisplayName("§7[§6Sneak§7]");
            stack.setItemMeta(meta); //HERE 
     
  7. Offline

    VNGC

    Working. now done. Again Much thanks!

    Before i start another Thread, i got another Problem. i am trying this:

    Code:
                ItemStack stack = new ItemStack(Material.IRON_BOOTS);
                ItemMeta meta = stack.getItemMeta();
                meta.setDisplayName("§7[§6Crafting§7]");
                ArrayList<String> crafting = new ArrayList<String>();
                if (noCrafting.crafting) { // if noCrafting is activated:
                    crafting.add("§eWenn §aaktiviert, stirbst du wenn jemand ein Item craftet!");// if activated, you die, if crafted an item
                    crafting.add(""); // placeholder
                    crafting.add("§7[§aaktiviert§7]"); // activated
                    meta.setLore(crafting);
                    stack.setItemMeta(meta);
                } else // if not:
                crafting.add("§eWenn §aaktiviert, stirbst du wenn jemand ein Item craftet!"); // if activated, you die, if crafted an item
                crafting.add(""); // placeholder again
                crafting.add("§7[§4deaktiviert§7]"); // deactivated
                meta.setLore(crafting);
                stack.setItemMeta(meta);


    if its disabled, it looks how planned:


    upload_2020-3-19_4-33-50.png


    but if enabled, it looks like this:


    upload_2020-3-19_4-34-12.png


    whats wrong :(
    ty for help
     

    Attached Files:

    Last edited by a moderator: Mar 19, 2020
  8. Offline

    Tango_

    You are adding to a list, so you haven't removed 'Disabled' when it gets enabled.

    You should be able to update the line of the lore using:

    // Enabled
    meta.getLore().set(2, "§7[§aaktiviert§7]");

    // Disabled
    meta.getLore().set(2, "§7[§4deaktiviert§7]");

    2 is the line number of the lore
     
    Last edited: Mar 18, 2020
  9. Offline

    VNGC

    i got fixed it. just forgot the {} at the "else". working now

    if i try yours, i am getting this error in console if i try to open the GUI

    [08:37:24 ERROR]: null org.bukkit.command.CommandException: Unhandled exception executing command 'settings' in plugin Challenges v1.0 at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[minecraft_server.jar:git-Paper-"7caed1a8"] at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:159) ~[minecraft_server.jar:git-Paper-"7caed1a8"] at org.bukkit.craftbukkit.v1_14_R1.CraftServer.dispatchCommand(CraftServer.java:739) ~[minecraft_server.jar:git-Paper-"7caed1a8"] at net.minecraft.server.v1_14_R1.PlayerConnection.handleCommand(PlayerConnection.java:1800) ~[minecraft_server.jar:git-Paper-"7caed1a8"] at net.minecraft.server.v1_14_R1.PlayerConnection.a(PlayerConnection.java:1608) ~[minecraft_server.jar:git-Paper-"7caed1a8"] at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:47) ~[minecraft_server.jar:git-Paper-"7caed1a8"] at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:5) ~[minecraft_server.jar:git-Paper-"7caed1a8"] at net.minecraft.server.v1_14_R1.PlayerConnectionUtils.lambda$ensureMainThread$0(PlayerConnectionUtils.java:23) ~[minecraft_server.jar:git-Paper-"7caed1a8"] at net.minecraft.server.v1_14_R1.TickTask.run(SourceFile:18) ~[minecraft_server.jar:git-Paper-"7caed1a8"] at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeTask(IAsyncTaskHandler.java:136) ~[minecraft_server.jar:git-Paper-"7caed1a8"] at net.minecraft.server.v1_14_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[minecraft_server.jar:git-Paper-"7caed1a8"] at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeNext(IAsyncTaskHandler.java:109) ~[minecraft_server.jar:git-Paper-"7caed1a8"] at net.minecraft.server.v1_14_R1.MinecraftServer.aX(MinecraftServer.java:1013) ~[minecraft_server.jar:git-Paper-"7caed1a8"] at net.minecraft.server.v1_14_R1.MinecraftServer.executeNext(MinecraftServer.java:1006) ~[minecraft_server.jar:git-Paper-"7caed1a8"] at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.awaitTasks(IAsyncTaskHandler.java:119) ~[minecraft_server.jar:git-Paper-"7caed1a8"] at net.minecraft.server.v1_14_R1.MinecraftServer.sleepForTick(MinecraftServer.java:990) ~[minecraft_server.jar:git-Paper-"7caed1a8"] at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:923) ~[minecraft_server.jar:git-Paper-"7caed1a8"] at java.lang.Thread.run(Thread.java:748) [?:1.8.0_232] Caused by: java.lang.NullPointerException at main.main.onCommand(main.java:56) ~[?:?] at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[minecraft_server.jar:git-Paper-"7caed1a8"]

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 19, 2020
  10. Offline

    Machine Maker

    Yeah, the reason for that error is because ItemMeta#getLore() can return null when the item doesn't have any lore. I think you did it the correct way anyways.
     
  11. Offline

    VNGC

    So, is there a better way?
     
  12. Online

    timtower Moderator Moderator

    Could you post your updated code then?
     
  13. Offline

    VNGC

    Code:
    ItemStack stack = new ItemStack(Material.CRAFTING_TABLE);
                    ItemMeta meta = stack.getItemMeta();
                    meta.setDisplayName("§7[§6Crafting§7]");
                    ArrayList<String> crafting = new ArrayList<String>();
                    if (noCrafting.crafting) {
                        crafting.add("§eWenn §aaktiviert, §estirbst du wenn jemand ein §7Item §ecraftet!");
                        crafting.add(""); // placeholder
                        meta.getLore().set(2, "§7[§aaktiviert§7]");
                        meta.setLore(crafting);
                        stack.setItemMeta(meta);
                    } else {
                     
                        crafting.add("§eWenn §aaktiviert, §estirbst du wenn jemand ein §7Item §ecraftet!");
                        crafting.add("");
                        meta.getLore().set(2, "§7[§4deaktiviert§7]");
                        meta.setLore(crafting);
                        stack.setItemMeta(meta);^
                    }
     
  14. Online

    timtower Moderator Moderator

    @VNGC Why would you get the lore, then set a value. Then set a completely different lore?
     
  15. Offline

    VNGC

    Because the one guy told me. I tried crafting.add(), but it doesnt update if i enable/disable it
     
  16. Online

    timtower Moderator Moderator

    @VNGC The line with meta.getLore().add won't work.
     
  17. Offline

    VNGC

    So what i have to use to get it updated on a click?

    i am not using getLore.add anywhere
     
  18. Online

    timtower Moderator Moderator

    @VNGC That is the point.
    getLore() returns a copy, not something that you can edit.
    crafting.add is the right one to call.
    Then you set the lore to crafting.
    Then you set the meta on the item.
    Then you update the players inventory.
     
  19. Offline

    VNGC

    still only updating if i re-open the gui manually.


    Code:
        crafting.add("§eWenn §aaktiviert, §estirbst du wenn jemand ein §7Item §ecraftet!");
                        crafting.add(""); // placeholder
                        crafting.add("§7[§aaktiviert§7]");
                        meta.setLore(crafting);
                        stack.setItemMeta(meta);
                        p.updateInventory();
                    } else {
    
                        crafting.add("§eWenn §aaktiviert, §estirbst du wenn jemand ein §7Item §ecraftet!");
                        crafting.add("");
                        crafting.add("§7[§4deaktiviert§7]");
                        meta.setLore(crafting);
                        stack.setItemMeta(meta);
                        p.updateInventory();
                    }
     
  20. Online

    timtower Moderator Moderator

    @VNGC Could you post the entire function then? Because I have no idea when this gets called.
     
  21. Offline

    VNGC

    Code:
    package main;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import challenges.noCrafting;
    import settings.Challenges;
    import utils.Data;
    
    public class main extends JavaPlugin implements CommandExecutor {
    
        public static Inventory inv = null;
    
        @Override
        public void onEnable() {
    
            System.out.println("§7Plugin [§5Challenges§7] wurde aktiviert!");
    
            PluginManager pm = Bukkit.getPluginManager();
            pm.registerEvents(new Challenges(), this);
            pm.registerEvents(new noCrafting(), this);
            pm.registerEvents(new join(), this);
    
            getCommand("crafting").setExecutor(new noCrafting());
    
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            Player p = (Player) sender;
    
            if (cmd.getName().equalsIgnoreCase("settings")) {
                if (p.hasPermission("challenges.settings")) {
                    inv = p.getPlayer().getServer().createInventory(null, 45, "Challenges");
                    // -----------------------------------------------------------------------------------------------------------
                    ItemStack stack = new ItemStack(Material.CRAFTING_TABLE);
                    ItemMeta meta = stack.getItemMeta();
                    meta.setDisplayName("§7[§6Crafting§7]");
                    ArrayList<String> crafting = new ArrayList<String>();
                    if (noCrafting.crafting) {
                        crafting.add(0, "§eWenn §aaktiviert, §estirbst du wenn jemand ein §7Item §ecraftet!");
                        crafting.add(1, ""); // placeholder
                        crafting.add(2, "§7[§aaktiviert§7]");
                        meta.setLore(crafting);
                        stack.setItemMeta(meta);
                        p.updateInventory();
                    } else {
    
                        crafting.add(0, "§eWenn §aaktiviert, §estirbst du wenn jemand ein §7Item §ecraftet!");
                        crafting.add(1, "");
                        crafting.add(2, "§7[§4deaktiviert§7]");
                        meta.setLore(crafting);
                        stack.setItemMeta(meta);
                        p.updateInventory();
                    }
                    // -----------------------------------------------------------------------------------------------------------
    
                    ItemStack stack1 = new ItemStack(Material.BREAD);
                    ItemMeta meta1 = stack1.getItemMeta();
                    meta1.setDisplayName("§7[§4Test§7]");
                    stack1.setItemMeta(meta1);
                    // -----------------------------------------------------------------------------------------------------------
                    ItemStack stack2 = new ItemStack(Material.COOKED_BEEF);
                    ItemMeta meta2 = stack2.getItemMeta();
                    meta2.setDisplayName("§7[§4Test§7]");
                    stack2.setItemMeta(meta2);
                    // -----------------------------------------------------------------------------------------------------------
                    inv.setItem(1, stack);
                    inv.setItem(4, stack1);
                    inv.setItem(7, stack2);
                    p.getPlayer().openInventory(inv);
    
                } else
                    p.sendMessage(Data.noPermission);
            }
    
            return false;
        }
    
        @Override
        public void onDisable() {
            System.out.println("§7Plugin [§5Challenges§7] wurde deaktiviert!");
        }
    
    }
    
     
  22. Online

    timtower Moderator Moderator

    @VNGC And how are you running a command with the gui open then?
     
  23. Offline

    VNGC

    ehhhhh idk, i think? maybe help me out ;c
     
  24. Online

    timtower Moderator Moderator

    It only updates when you are opening the gui.
    I don't see any events attached to it so it won't update when you click things.
     
  25. Offline

    VNGC

    so just use p.openinventory(inv); after clicking?
     
  26. Online

    timtower Moderator Moderator

    I don't know what you do when you click.
    You did not post that code.
     
  27. Offline

    VNGC

    Code:
    package settings;
    
    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.ItemStack;
    
    import challenges.noCrafting;
    
    public class Challenges implements Listener {
    
        @EventHandler
        public void Inventory(InventoryClickEvent event) {
            Player p = (Player) event.getWhoClicked();
    
    
            if (event.getView().getTitle().equals("Challenges")) {
                event.setCancelled(true);
    
            }
            if (event.getCurrentItem().getType() == Material.CRAFTING_TABLE) {
                noCrafting.crafting = !noCrafting.crafting;
              
            } else if (event.getCurrentItem().getType() == Material.COOKED_BEEF) {
                p.getInventory().setItem(4, new ItemStack(Material.COOKED_BEEF, 1));
                p.sendMessage("test");
            }
        }
    
    }
    
    just another class

    EDIT: i tried to add p.updateInventory() after the toggle-boolean, but still doesnt work
     
  28. Online

    timtower Moderator Moderator

    @VNGC You also need to update the item there, just changing variable won't tell the item to update itself.
     
  29. Offline

    VNGC

    So hoe would i do this?
     
  30. Online

    timtower Moderator Moderator

    The same way as you are doing in the onCommand
     

Share This Page