Simple Chest GUI displayThe simplest of all! This code was originally written for my own server, and because it was very efficient, I decided to distribute it to everyone. What it basically does, it creates a Chest GUI where you can put items, and assign a command for each one when it gets clicked. A sample implementation. Code:java public class MenuGUI extends GUI{ public MenuGUI(Player player) { super(player, "Menu", 9); // Must be multiple of 9! } public void addItems() { newItem(Material.COMPASS, "Go back to Spawn", 1, new String[] { "Feeling lost?" }, 5); } public void createCommands() { if (getClickedItem().equals("Go back to Spawn")) { createCommand("spawn"); } }} GUI.java Code:java package <your package>; import java.util.Arrays; import org.bukkit.Bukkit;import org.bukkit.Material;import org.bukkit.Sound;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;import org.bukkit.inventory.meta.ItemMeta;import org.bukkit.plugin.PluginManager; public abstract class GUI implements Listener{ private Inventory inventory; private Player player; private String guiName = "Menu"; private int guiSlots; private String getClickedItem; /** * Creates a new GUI display. * * @param Player to show the GUI. * @param The GUI display name. * @param The GUI slots. Must be multiple of 9! */ public GUI(Player player, String guiName, int guiSlots) { this.player = player; this.guiName = guiName; this.guiSlots = guiSlots; inventory = Bukkit.getServer().createInventory(null, this.guiSlots, this.guiName); Bukkit.getServer().getPluginManager().registerEvents(this, UnderCovern.getPlugin()); addItems(); openGUI(); } /** * Adds items to the GUI. */ public abstract void addItems(); /** * Creates item commands when clicked. */ public abstract void createCommands(); /** * Creates a GUI item. * * @param The material type. * @param The item display name. * @param The item amount. * @param The item lore. * @param The GUI slot to be placed. */ public void newItem(Material type, String itemDisplayName, int amount, String[] itemLore, int guiSlot) { ItemStack item = new ItemStack(type); ItemMeta meta = item.getItemMeta(); item.setAmount(amount); meta.setDisplayName(itemDisplayName); meta.setLore(Arrays.asList(itemLore)); item.setItemMeta(meta); this.inventory.setItem(guiSlot, item); } /** * Creates a GUI command. * * @param command */ protected void createCommand(String command) { Bukkit.dispatchCommand(this.player, command); this.player.playSound(this.player.getLocation(), Sound.CHICKEN_EGG_POP, 1, 1); } @EventHandler public void onInventoryItemClick(final InventoryClickEvent e) { // Checking if the inventory is the correct one. if (!(e.getInventory().getName().equals(this.guiName))) { return; } // Checking if the clicked item exists. if (e.getCurrentItem() == null || !e.getCurrentItem().hasItemMeta() || !e.getCurrentItem().getItemMeta().hasDisplayName()) { return; } this.getClickedItem = e.getCurrentItem().getItemMeta().getDisplayName(); this.player = (Player) e.getWhoClicked(); e.setCancelled(true); closeGUI(); // Waiting for the event to get cancelled. if (e.isCancelled() == true) { Bukkit.getScheduler().scheduleSyncDelayedTask(UnderCovern.getPlugin(), new Runnable() { public void run() { try { createCommands(); } catch (Exception cmdException) { cmdException.printStackTrace(); } } }, 20); } } /** * Gets the GUI name. * * @return */ public String getGuiName() { return this.guiName; } /** * Opens the GUI. */ public void openGUI() { this.player.openInventory(inventory); } /** * Closes the GUI. */ public void closeGUI() { this.player.closeInventory(); } /** * Gets the GUI. * * @return */ public Inventory getGUI() { return this.inventory; } /** * Gets the clicked GUI item. * * @return */ public String getClickedItem() { return this.getClickedItem; }} Oops, looks like I posted it on the wrong section EDIT by Moderator: merged posts, please use the edit button instead of double posting.
This is great, sorry for coming back to this so late, but what is: Code:java Bukkit.getServer().getPluginManager().registerEvents(this, UnderCovern.getPlugin()); //AND Bukkit.getScheduler().scheduleSyncDelayedTask(UnderCovern.getPlugin(), new Runnable() The UnderCovern bit? EDIT: know its the server name, but what to replace to?