Inventories - noobmobile/GroovyBukkitAPI GitHub Wiki

Inventories

Tired of creating inventories using Bukkit.createInventory() and creating listeners to InventoryClickEvent? GroovyBukkitAPI provides you a great solution.

// or new InventoryHandler(name, size)
def player = ...
menu(name, size)
	.item(10, new ItemBuilder(Material.STONE), {player -> player.sendMessage("clicked on item STONE on slot 10")})
	.item(new ItemBuilder(Material.DIAMOND), {player -> player.sendMessage("click on item DIAMOND on slot coming from config")})
	.items([item1, item2, item3])
	.handler({InventoyClickEvent event ->
		event.getWhoClicked().sendMessage("custom event handling")
	})
	.open(player)

Inventories from config

Another powerful tool from GroovyBukkitAPI.

arenas:
  Name: "&5Arenas"
  Size: 27
  goToArena:
    Slot: 10
    Material: DIAMOND_SWORD
    Name: "&6Go to Arena"
  profile:
    Slot: 11
    Texture: "{playerName}"
    Name: "&6You"
    Lore:
      - "&7You have killed {playerKills} players."
  itemTemplate: # if there's no slot, item will be avaliable on inventory.itemTemplate
    Material: STONE
    Name: "&6I dont know what i am"
    Lore:
     - "&7how are you {playerName}?"
    FileConfiguration inventories

    def test = { Context context ->
        def player = context.player()
        def inventory = inventories.arenas.asInventory(
                goToArena: { clicker -> clicker.sendMessage("going to the arena") },
                profile: { clicker -> clicker.sendMessage("yeah this is you") },
                "{playerName}": player.name, "{playerKills}": player.getStatistic(Statistic.MOB_KILLS))
        inventory.item(16, inventory.itemTemplate.editPlaceholders("{playerName}": player.name), { clicker -> clicker.sendMessage("this is a template") })
        inventory.open(player)
}

We use ConfigurationSection.asInventory() passing as a Map<String, Object>. The map contains all the placeholders we use, like {playerName} and {playerKills} and the Closures that are called when a player clicks in a item.

All methods

item(slot, itemstack, closure)

Sets an item on the inventory and execute a closure when a player clicks it

item(slot, itemstack)

Same as above, without the closure

item(itemstack, closure)

Sets an item on the ItemStack.slot (this slot comes from the config) and execute a closure when a player clicks it. Throws an exception if there's no field slot in the ItemStack

item(itemstack)

Same as above, without the closure

items(itemstacks)

Sets a list/array of itemstacks in the center of the inventory (center slots: 10..16, 19..25, 28..34)

items(itemstacks, slots)

Same as above buth with the passed slots

items(itemstacks, closure)

Same as items(itemstacks) and execute a closure with two parameters (player and itemstack)

items(itemList, {player, item -> player.sendMessage("you clicked on $item.type")})

items(itemstacks, orElse)

If the itemstacks is empty, the ItemStack orElse is added to the list

slots(slots)

Sets the slots used by items() and scroller()

itemConsumer(closure)

Handles when a player click on ItemStack itemConsumer({item, player -> player.sendMessage("clicked on $item.type")})

handler(closure)

Handles the InventoryClickEvent with a closure.

open(player)

Opens the inventory

Scroller

You can easily create an Scroller Inventory (multiple) pages.

menu("Test Scroller", 36)
	.scroller()
	.items(itemList, {item, player-> player.sendMessage("this is a $item.type")})
	.item(4, new ItemBuilder(Material.DIAMOND).setName("Test Scroller"))
	.open(player)

Scroller from config

scrollerTest:
  Name: "&2Scroller Test"
  Size: 36
  it1:
    Slot: 31
    Material: BARRIER
    Name: "&cClose"
  itScroller: # template item
    Material: STONE # gonna be changed later
    Name: "&6I'm {materialName}"
  Scroller: # this whole section is optional, if not provided it'll be used the default settings
    Slots: 11,12,13,14,15
    PreviousPage:
      Slot: 9
      Material: STAINED_CLAY
      Data: 14
      Name: "&aPage <page>"
    NextPage:
      Slot: 17
      Material: STAINED_CLAY
      Data: 5
      Name: "&aPage <page>"
    FileConfiguration inventories

    def test = { Context context ->
        def player = context.player()
        InventoryHandler inventory = inventories.scrollerTest.asInventory(it1: { clicker -> clicker.closeInventory() })
        inventory.items(Material.values()
                .findAll { it.isSolid() }
                .collect {
                    new ItemBuilder(inventory.itScroller.clone())
                            .setType(it)
                            .editPlaceholders("{materialName}": it.name().toLowerCase())
                            .build()
                }
        )
        inventory.itemConsumer({ item, clicker -> clicker.sendMessage("clicked on $item.type") })
        inventory.open(player)
    }

Scroller methods

scroller()

Enable the scroller mode

scrollerPageSlots(previousSlot, nextSlot)

Sets the slots of the previous page and next page item

scrollerPageItems(previousItem, nextItem)

Same as above but for the previous/next page item slots() itemConsumer() also applies. Call all these before items()

⚠️ **GitHub.com Fallback** ⚠️