Solved How to do command when a player clicked an item in GUI?

Discussion in 'Spigot Plugin Development' started by SiderRMC, May 26, 2018.

  1. I would like to make a menu for my own server, but when I use InventoryClickEvent, it does nothing??
    Code (Java):
    @EventHandler
        public void interact(InventoryClickEvent event) {

            Player player = (Player) event.getWhoClicked();

            switch (event.getCurrentItem().getType()) {
            case BOOKSHELF:
                player.chat("/server Lobby");
                break;
            case EYE_OF_ENDER:
                player.chat("/server Skywars");
            default: // if it finds null
                player.sendMessage(ChatColor.RED + "You clicked nothing!");
                break;
            }
        }
     
    • Like Like x 1
  2. Bukkit#getServer()#dispatchCommand(player, "server Server");
     
  3. I know this but I need to check the item player clicked. How?
     
    • Friendly Friendly x 1
  4. 3rc

    3rc

    Code (Text):
        @EventHandler
        public void onEvent(InventoryClickEvent e) {
            if (e.getInventory().getTitle().equalsIgnoreCase("")) {
                e.setCancelled(true);
                if (e.getRawSlot() == 0) {
                    if (e.isRightClick()) {
                        Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "");
                    } else
                        if (e.isLeftClick()) {
                            Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "");
                        }
                }
            }
        }
    }
    @SiderRMC
    This is how I have done it in the past.
     
  5. I tried to use your code, but it's still not working.
    Code (Java):
    @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {

            Player player = (Player) event.getWhoClicked();
            Inventory inventory = event.getInventory();

            if (inventory.getTitle().equalsIgnoreCase("Game Menu")) {
                event.setCancelled(true);
                if (event.getRawSlot() == 10) {
                    player.performCommand("server Lobby");
                    return;
                }
                if (event.getRawSlot() == 12) {
                    player.performCommand("server Skywars");
                    return;
                }
            }
        }
     

    Attached Files:

  6. The title is correct?, do not you use color codes or something?
     
  7. 3rc

    3rc

    if you were using my code, you forgot to add if (e.isRightClicked())
     
  8. Since you use color codes to name the Inventory you wont be able to check it with equalsIgnoreCase, i recommend using contains.
     
  9. 3rc

    3rc

    I mean event.isRightClicked()
     
  10. It worked for me.
    But when I do either player.performCommand("server ServerName") or player.chat("/server ServerName"), it always says "Unknown command. Type '/help' for help."
    And there are some errors occurred:
    Code (Text):
    [09:15:11 ERROR]: Could not pass event InventoryClickEvent to Raypixel-Core v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1630) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:31) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:9) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_161]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_161]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
    Caused by: java.lang.NullPointerException
            at com.rmc.raypixel.Events.Player.MenuClick.onInventoryClick(MenuClick.java:21) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_161]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_161]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_161]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_161]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            ... 15 more
     
  11. 3rc

    3rc

    Hmmm... I am not sure. could be because it's a bungee command. erh.
     
  12. I just want to perform a command whether they do left click or right click.
     
  13. Then how did other plugins (like ServerSelectorX) do that?
     
  14. 3rc

    3rc

    Send current code.
     
  15. Mine:
    Code (Java):
    package com.rmc.raypixel.Events.Player;

    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;

    public class MenuClick implements Listener {

        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {

            Player player = (Player) event.getWhoClicked();
            ItemStack clicked = event.getCurrentItem();
            Inventory inventory = event.getInventory();

            if (inventory.getTitle().contains("Game Menu")) {
                if (clicked.getType() == Material.BOOKSHELF) {
                    player.chat("/server Lobby");
                    player.closeInventory();
                    return;
                }
                if (clicked.getType() == Material.EYE_OF_ENDER) {
                    player.chat("/server Skywars");
                    player.closeInventory();
                    return;
                }
            }
        }
    }
    ServerSelectorX's:
    https://github.com/ServerSelectorX/...rver-pinging/src/xyz/derkades/serverselectorx
     
  16. I think "/server ServerName" was sent to the bukkit server but not bungee proxy.
     
  17. 3rc

    3rc

    Try making the player dispatch the command
     
  18. You mean this?
    Code (Java):
    Bukkit.dispatchCommand(player, "server Lobby");
     
  19. Nope. It still says 'unknown command'.
     
  20. QUESTION SOLVED!
    My final plan is to create a command that can be sent to BungeeCord, and the player perform that command when they clicked the item!
    And it works!
    QQ截图20180526113212.jpg QQ截图20180526113232.jpg

    New codes added in the main class:
    Code (Java):
    public void onEnable() {
        getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
        }

        public void bungeeCommand(Player p, String sn) {
            ByteArrayOutputStream b = new ByteArrayOutputStream();
            DataOutputStream d = new DataOutputStream(b);

            try {
                d.writeUTF("Connect");
                d.writeUTF(sn);
                b.close();
            } catch (Exception e) {
                e.printStackTrace();
                getLogger().severe(ChatColor.RED + "Error connecting to the server " + sn);
            }
            p.sendPluginMessage(this, "BungeeCord", b.toByteArray());
        }

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String sn, String[] args) {

            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "You must be a player to use this command!");
                return true;
            }

            Player p = (Player) sender;

            if (sn.equalsIgnoreCase("lobby")) {
                bungeeCommand(p, "Lobby");
            }
            if (sn.equalsIgnoreCase("s")) {
                if (args.length == 0) {
                    p.sendMessage(ChatColor.RED + "Please specify a server");
                    return true;
                }
                if (args[0].equalsIgnoreCase("lobby"))
                    bungeeCommand(p, "Lobby");
                if (args[0].equalsIgnoreCase("skywars"))
                    bungeeCommand(p, "Skywars");
                if (args[0].equalsIgnoreCase("bedwars"))
                    bungeeCommand(p, "Bedwars");
            }
            return true;
        }
    Codes in MenuClick class:
    Code (Java):
    public class MenuClick implements Listener {

        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            Player player = (Player) event.getWhoClicked();
            ItemStack clicked = event.getCurrentItem();
            Inventory inventory = event.getInventory();

            if (inventory.getTitle().contains("Game Menu")) {
                if (clicked.getType() == Material.BOOKSHELF) {
                    Bukkit.dispatchCommand(player, "s Lobby");
                    player.closeInventory();
                    return;
                }
                if (clicked.getType() == Material.EYE_OF_ENDER) {
                    Bukkit.dispatchCommand(player, "s Skywars");
                    player.closeInventory();
                    return;
                }
            }
        }
    }