Skip to content

Creating Custom Inventories

Leonardo Luiz Gava edited this page Jan 8, 2023 · 4 revisions

Neelix allows the creation of inventories in a practical way with functionalities according to the type. It is possible to create simple or paginated inventories.

Creating your first Simple Inventory

Parameter Type Description
size int The inventory size.
title String The inventory title.
lockedSlots List<Integer> Slots that must be blocked.
Items cannot be set into locked slots.
items List<NeelixInventoryItem> The inventory items.

main plugin class

public class MyPlugin extends JavaPlugin {

  @Override
  public void onEnable() {
    Neelix.init(this);

    NeelixSimpleInventory inventory = new NeelixSimpleInventory(54, "Minecraft Items (Simple)", null, items);
  }
}

Creating your first Paginated Inventory

Parameter Type Description
size int The inventory size.
title String The inventory title.
lockedSlots List<Integer> Slots that must be blocked. Items cannot be set into locked slots.
items List<NeelixInventoryItem> The inventory items.
navigation NeelixPaginatedNavigation Responsible for navigation between pages.

main plugin class

public class MyPlugin extends JavaPlugin {

  @Override
  public void onEnable() {
    Neelix.init(this);

    NeelixPaginatedInventory inventory = new NeelixPaginatedInventory(54, "Minecraft Items (Paginated)", null, items, new NeelixPaginatedNavigation());
  }
}

You can create a default paginated navigation passing no parameters to the NeelixPaginatedNavigation constructor.

What is the locked slots parameter?

The lockedSlots parameter allows the inventory to block the addition of items to certain slots, allowing for better aesthetics in inventory creation.

It is recommended to save locked slots in a configuration file, for example:

# avoid the inventory border
locked-slots: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 17, 18, 26, 27, 35, 36, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53]

Remember: Using locked slots will decrease the amount of items per page.

Creating Inventory Items

Creating custom items allows the onClick event to be fired as soon as clicked on it.

BasicItem class

public class BasicItem extends NeelixInventoryItem {
  public BasicItem(@Nullable Integer slot, ItemStack item) {
    super(slot, item);
  }

  @Override
  public void onClick(NeelixInventory inventory, Player whoClicked, int clickedSlot, ItemStack clickedItem) {
    whoClicked.sendMessage("You have received the item you clicked!")
    whoClicked.getInventory().addItem(clickedItem);
  }
}

When the slot parameter is null, Neelix will selected the next available inventory for the item automatically. This is applied for simple and paginated inventories.

Creating Custom Navigation

The navigation of a paginated inventory consists of two main interactions:

When creating a new navigation, you need to create a new item responsible to go to next page and another to go to previous page.

main plugin class

public class MyPlugin extends JavaPlugin {

  @Override
  public void onEnable() {
    Neelix.init(this);

    NeelixPaginatedNavigation navigation = new NeelixPaginatedNavigation(
      new NextInventoryPageItem(45, buildNavigationItem(Material.SPECTRAL_ARROW, "Next ->")),
      new PreviousInventoryPageItem(53, buildNavigationItem(Material.SPECTRAL_ARROW, "<- Back"))
    );

    NeelixPaginatedInventory inventory = new NeelixPaginatedInventory(54, "Minecraft Items", null, items, navigation);
  }

  /** Build a new ItemStack to be used as Icon for navigation items. */
  private static ItemStack buildNavigationItem(Material material, String name) {
    ItemStack item = new ItemStack(material);
    ItemMeta meta = item.getItemMeta();

    meta.setDisplayName(ChatColor.RESET + name);
    item.setItemMeta(meta);

    return item;
  }
}

Remember: The chosen slot for the navigation items will be automatically added to the locked slots.

Registering your inventories

main plugin class

public class MyPlugin extends JavaPlugin {

  @Override
  public void onEnable() {
    Neelix.init(this);

    NeelixSimpleInventory simpleInventory = new NeelixSimpleInventory(54, "Minecraft Items (Simple)", null, items);
    NeelixPaginatedInventory paginatedInventory = new NeelixPaginatedInventory(54, "Minecraft Items", null, items, new NeelixPaginatedNavigation());

    Neelix.getInventoryManager().registerInventory(simpleInventory);
    Neelix.getInventoryManager().registerInventory(paginatedInventory);
  }
}