Simple Chest GUI Display

Discussion in 'Bukkit Tools' started by parat26, Aug 28, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    parat26

    Simple Chest GUI display
    The 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
    1. public class MenuGUI extends GUI
    2. {
    3. public MenuGUI(Player player)
    4. {
    5. super(player, "Menu", 9); // Must be multiple of 9!
    6. }
    7.  
    8. public void addItems()
    9. {
    10. newItem(Material.COMPASS, "Go back to Spawn", 1, new String[] { "Feeling lost?" }, 5);
    11. }
    12.  
    13. public void createCommands()
    14. {
    15. if (getClickedItem().equals("Go back to Spawn"))
    16. {
    17. createCommand("spawn");
    18. }
    19. }
    20. }

    GUI.java​
    Code:java
    1. package <your package>;
    2.  
    3. import java.util.Arrays;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.Material;
    7. import org.bukkit.Sound;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.inventory.InventoryClickEvent;
    12. import org.bukkit.inventory.Inventory;
    13. import org.bukkit.inventory.ItemStack;
    14. import org.bukkit.inventory.meta.ItemMeta;
    15. import org.bukkit.plugin.PluginManager;
    16.  
    17. public abstract class GUI implements Listener
    18. {
    19. private Inventory inventory;
    20.  
    21. private Player player;
    22.  
    23. private String guiName = "Menu";
    24.  
    25. private int guiSlots;
    26.  
    27. private String getClickedItem;
    28.  
    29. /**
    30.   * Creates a new GUI display.
    31.   *
    32.   * @param Player to show the GUI.
    33.   * @param The GUI display name.
    34.   * @param The GUI slots. Must be multiple of 9!
    35.   */
    36. public GUI(Player player, String guiName, int guiSlots)
    37. {
    38. this.player = player;
    39.  
    40. this.guiName = guiName;
    41.  
    42. this.guiSlots = guiSlots;
    43.  
    44. inventory = Bukkit.getServer().createInventory(null, this.guiSlots, this.guiName);
    45.  
    46. Bukkit.getServer().getPluginManager().registerEvents(this, UnderCovern.getPlugin());
    47.  
    48. addItems();
    49.  
    50. openGUI();
    51. }
    52.  
    53. /**
    54.   * Adds items to the GUI.
    55.   */
    56. public abstract void addItems();
    57.  
    58. /**
    59.   * Creates item commands when clicked.
    60.   */
    61. public abstract void createCommands();
    62.  
    63. /**
    64.   * Creates a GUI item.
    65.   *
    66.   * @param The material type.
    67.   * @param The item display name.
    68.   * @param The item amount.
    69.   * @param The item lore.
    70.   * @param The GUI slot to be placed.
    71.   */
    72. public void newItem(Material type, String itemDisplayName, int amount, String[] itemLore, int guiSlot)
    73. {
    74. ItemStack item = new ItemStack(type);
    75. ItemMeta meta = item.getItemMeta();
    76.  
    77. item.setAmount(amount);
    78. meta.setDisplayName(itemDisplayName);
    79. meta.setLore(Arrays.asList(itemLore));
    80.  
    81. item.setItemMeta(meta);
    82.  
    83. this.inventory.setItem(guiSlot, item);
    84. }
    85.  
    86. /**
    87.   * Creates a GUI command.
    88.   *
    89.   * @param command
    90.   */
    91. protected void createCommand(String command)
    92. {
    93. Bukkit.dispatchCommand(this.player, command);
    94.  
    95. this.player.playSound(this.player.getLocation(), Sound.CHICKEN_EGG_POP, 1, 1);
    96. }
    97.  
    98. @EventHandler
    99. public void onInventoryItemClick(final InventoryClickEvent e)
    100. {
    101. // Checking if the inventory is the correct one.
    102.  
    103. if (!(e.getInventory().getName().equals(this.guiName)))
    104. {
    105. return;
    106. }
    107.  
    108. // Checking if the clicked item exists.
    109.  
    110. if (e.getCurrentItem() == null || !e.getCurrentItem().hasItemMeta() || !e.getCurrentItem().getItemMeta().hasDisplayName())
    111. {
    112. return;
    113. }
    114.  
    115. this.getClickedItem = e.getCurrentItem().getItemMeta().getDisplayName();
    116. this.player = (Player) e.getWhoClicked();
    117.  
    118. e.setCancelled(true);
    119.  
    120. closeGUI();
    121.  
    122. // Waiting for the event to get cancelled.
    123.  
    124. if (e.isCancelled() == true)
    125. {
    126. Bukkit.getScheduler().scheduleSyncDelayedTask(UnderCovern.getPlugin(), new Runnable()
    127. {
    128. public void run()
    129. {
    130. try
    131. {
    132. createCommands();
    133. }
    134. catch (Exception cmdException)
    135. {
    136. cmdException.printStackTrace();
    137. }
    138. }
    139. }, 20);
    140. }
    141. }
    142.  
    143. /**
    144.   * Gets the GUI name.
    145.   *
    146.   * @return
    147.   */
    148. public String getGuiName()
    149. {
    150. return this.guiName;
    151. }
    152.  
    153. /**
    154.   * Opens the GUI.
    155.   */
    156. public void openGUI()
    157. {
    158. this.player.openInventory(inventory);
    159. }
    160.  
    161. /**
    162.   * Closes the GUI.
    163.   */
    164. public void closeGUI()
    165. {
    166. this.player.closeInventory();
    167. }
    168.  
    169. /**
    170.   * Gets the GUI.
    171.   *
    172.   * @return
    173.   */
    174. public Inventory getGUI()
    175. {
    176. return this.inventory;
    177. }
    178.  
    179. /**
    180.   * Gets the clicked GUI item.
    181.   *
    182.   * @return
    183.   */
    184. public String getClickedItem()
    185. {
    186. return this.getClickedItem;
    187. }
    188. }


    Oops, looks like I posted it on the wrong section :'(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
    awesomebing1 likes this.
  2. Offline

    Echo

    This is great, sorry for coming back to this so late, but what is:
    Code:java
    1. Bukkit.getServer().getPluginManager().registerEvents(this, UnderCovern.getPlugin());
    2.  
    3. //AND
    4.  
    5. Bukkit.getScheduler().scheduleSyncDelayedTask(UnderCovern.getPlugin(), new Runnable()

    The UnderCovern bit?


    EDIT: know its the server name, but what to replace to?
     
Thread Status:
Not open for further replies.

Share This Page