Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. Inventory holders can be used to register own types, handlers and data to a opened GUI for a player.
  2.  
  3. To start, we need to create our menu class that implemetns InventoryHolder and any other functions we may want.
  4. For this example, we are gonna fill the inventory and open it for player, then save the player for later ussage with our handler.
  5.  
  6. # The menu
  7. It will look something like this
  8. ```java
  9. import org.bukkit.Bukkit;
  10. import org.bukkit.Material;
  11. import org.bukkit.entity.Player;
  12. import org.bukkit.inventory.Inventory;
  13. import org.bukkit.inventory.InventoryHolder;
  14. import org.bukkit.inventory.ItemStack;
  15.  
  16. public class MyMenu implements InventoryHolder {
  17.  
  18. private Inventory inventory = Bukkit.createInventory(this, 6 * 9, "My menu");
  19. private Player target;
  20.  
  21. public MyMenu(Player player) {
  22. //set the player, for later used
  23. this.target = player;
  24. //fill it with one stone
  25. inventory.setItem(4, new ItemStack(Material.STONE, 1));
  26. //open the menu
  27. player.openInventory(inventory);
  28. }
  29.  
  30. public Boolean trigger(ItemStack itemStack) {
  31. //check if the clicked item is stone
  32. if (itemStack.getType() == Material.STONE) {
  33. target.sendMessage("No, you may not steal my fancy stone. It is my only friend.");
  34. //cancel the event
  35. return true;
  36. }
  37. //dont cancel the event
  38. return false;
  39. }
  40.  
  41. @Override
  42. public Inventory getInventory() {
  43. return this.inventory;
  44. }
  45. }
  46. ```
  47.  
  48.  
  49. # The handler
  50. Then, to handle it
  51. We use the bukkit event like normal, but this time we check if it is an instance of our menu, and cast/call it if this is the case.
  52. ```java
  53. import org.bukkit.event.EventHandler;
  54. import org.bukkit.event.Listener;
  55. import org.bukkit.event.inventory.InventoryClickEvent;
  56.  
  57. public class MyListener implements Listener {
  58.  
  59. @EventHandler
  60. public void onInventoryClick(InventoryClickEvent event) {
  61. //filter out bad events
  62. if (event.getInventory() == null
  63. || event.getCurrentItem() == null) return;
  64.  
  65. //check if the inventory is an instance of our menu
  66. if (event.getInventory().getHolder() instanceof MyMenu) {
  67. //call teh function and hold its state
  68. Boolean cancel = ((MyMenu) event.getInventory().getHolder()).trigger(event.getCurrentItem());
  69. //set the event cancelled based on the return state
  70. event.setCancelled(cancel);
  71. }
  72. }
  73.  
  74. }
  75. ```
  76.  
  77. # Opening the menu
  78. To open the menu and register the handler, all we have to do is
  79. ```java
  80. new MyMenu(Bukkit.getPlayer("Mindgamesnl");
  81. ```
  82.  
  83. And that's all, we now have
  84. - a menu that builds itself
  85. - a menu that is linked to a player
  86. - one lister that handles our menu like a callback function
  87. - a simpler and cleaner inventory system
  88. - a stone block in our gui, that the player cannot interact of based on our handler
  89.  
  90. That's all folks
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement