[Request] Chest GUI Example

Discussion in 'Plugin Development' started by TheDiamondGuy, Oct 15, 2014.

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

    TheDiamondGuy

    Hey,

    Basically, the title explains it all. Does anybody have an example of a chest gui?:)

    Thanks.
     
  2. Offline

    JjPwN1

    I'm not completely sure of what you mean by "Chest GUI." I'm going to assume you mean an inventory menu in which players may click items in the inventory to perform actions. Here's how that would work:

    - You create a new inventory and populate it with items
    Code:java
    1. Inventory inv = Bukkit.createInventory(null, "My Inventory", 9);
    2. inv.setItem(0, new ItemStack(Material.AIR));
    3. player.openInventory(inv);

    "My Inventory" would be the title of the inventory, and 9 is how many slots are in the inventory. The amount of slots must be evenly divisible by 9, or you will get errors.

    - You then listen for an InventoryClickEvent, check if the event.getInventory().getTitle() is equal to your GUI's inventory title, and perform actions according to the item the player clicked with event.getCurrentItem().
    Code:java
    1. @EventHandler
    2. public void inventoryClick(InventoryClickEvent event){
    3. if(event.getInventory().getTitle() != null && event.getInventory().getTitle().equalsIgnoreCase("My Inventory")){
    4. if(event.getCurrentItem() != null){
    5. if(event.getCurrentItem().getType() == Material.AIR){
    6. event.getWhoClicked().getInventory().addItem(new ItemStack(Material.AIR));
    7. }
    8. }
    9. }
    10. }
     
  3. Offline

    Konato_K

    Also titles can't be longer than 32 characters and don't forget you check for null-pointers if they click an empty slot :)
     
  4. Offline

    Laxer21117

    Hmm seems it has been resolved I was too late. Good luck with the future of the project!
     
Thread Status:
Not open for further replies.

Share This Page