How to add an instruction to GUI

Discussion in 'Spigot Plugin Development' started by THEcarrier, May 7, 2017.

  1. Code (Text):
        @EventHandler
        public void onClickA(InventoryClickEvent CE) {
            String title = CE.getInventory().getTitle();
            if(title.contains("§5MENU")) {
                CE.setCancelled(true);
                    return;
    Hello everyone, I am a Java beginner, I would like to ask how to add a slot for the implementation of a specified command?
     
  2. Are you asking how to actually create the GUI menu?
     
  3. yes:)
     
  4. Here is an example:
    Code (Text):
     // BuilderGUI
         public static void openbuilderGUI(Player player) {
             Inventory builderGUI = Bukkit.createInventory(null, 27,
                     ChatColor.RED.toString() + ChatColor.BOLD + "Builder menu");

             ItemStack skull = new ItemStack(397, 1, (short) 3);
             SkullMeta meta = (SkullMeta) skull.getItemMeta();
             meta.setOwner(player.getName());
             meta.setDisplayName(ChatColor.AQUA + player.getName());
             skull.setItemMeta(meta);
             builderGUI.setItem(4, skull);

             ItemStack gamemode_0 = new ItemStack(Material.DIAMOND_SWORD, 1);
             ItemMeta gamemode_0meta = gamemode_0.getItemMeta();
             gamemode_0meta.setDisplayName(ChatColor.GREEN + "Sur mode");
             gamemode_0.setItemMeta(gamemode_0meta);
             builderGUI.setItem(12, gamemode_0);

             ItemStack gamemode_1 = new ItemStack(Material.GRASS, 1);
             ItemMeta gamemode_1meta = gamemode_1.getItemMeta();
             gamemode_1meta.setDisplayName(ChatColor.GREEN + "crea Mode");
             gamemode_1.setItemMeta(gamemode_1meta);
             builderGUI.setItem(14, gamemode_1);

             player.openInventory(builderGUI);
         }
     
     
  5. and if you want to detect it:
    Code (Text):
    @EventHandler
         public void onBuilderInventoryClick(InventoryClickEvent e) {
             if (!ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase("Builder menu"))
                 return;
             Player player = (Player) e.getWhoClicked();
             e.setCancelled(true);

             if (e.getCurrentItem() == null || e.getCurrentItem().getType() == Material.AIR
                     || !e.getCurrentItem().hasItemMeta()) {
                 player.closeInventory();
                 return;
             }
             switch (e.getCurrentItem().getType()) {
             case DIAMOND_SWORD:

                 this.main.getConfig().set(e.getWhoClicked().getUniqueId() + ".name", e.getWhoClicked().getName());
                 this.main.saveConfig();
                 player.closeInventory();
                 player.sendMessage(ChatColor.AQUA + "【MCCW console】》 " + ChatColor.GREEN + "turn to sur mode");
                 e.getWhoClicked().setGameMode(GameMode.SURVIVAL);

                 break;
             case GRASS:

                 this.main.getConfig().set(e.getWhoClicked().getUniqueId() + ".name", e.getWhoClicked().getName());
                 this.main.saveConfig();
                 player.closeInventory();
                 player.sendMessage(ChatColor.AQUA + "【MCCW console】》 " + ChatColor.GREEN + "turn to create mode");
                 e.getWhoClicked().setGameMode(GameMode.CREATIVE);

                 break;
             default:
                 player.closeInventory();
                 break;
             }
         }
     
  6. To create the actual inventory, do
    Code (Text):
    Bukkit.getServer().createInventory(null, 9, "This is the title");
    This will create a 9 slot inventory. REMEMBER when creating an inventory, the slot value has to be a multiple of 9. So you cannot have a 2 slot inventory.
     
    • Agree Agree x 1
    • Useful Useful x 1
  7. I don't speak English, I just didn't see it clearly. I mean,How to click inventory to execute a command
     
    #7 THEcarrier, May 7, 2017
    Last edited: May 7, 2017
  8. Code (Text):
    On InventoryClickEvent:
      Return if the item is null
      Return if the inventory's name is not the GUI's name
      Check which Item was clicked (name, type, lore...)
      Do what you need to do.
    Not gonna spoon-feed code, you should find out yourself the way to implement this.
     
  9. How can you not see it clearly? i think you should start with doing some easier plugin
     
  10. Well, I learned that, I want to learn advanced, such as implementing the menu click button to execute an instruction.I haven't learned yet. Can you help me?:p