Skip to content

SrBlecaute01/InventoryHelper

Repository files navigation

InventoryHelper

Jitpack Downloads Codacy FOSSA Status
Build Publish

A helper for creating inventories in Minecraft with greater ease, efficiency and flexibility.

Some features

  • Creation of simple or paginated inventories.
  • Possibility to create multiple patterns in the same inventory.
  • Properties for each inventory.
  • Integration with objects.
  • Inventory update methods.

Summary

For a better understanding of the features this project offers, please check the documentation.

Enabling InventoryHelper

Before creating inventories with InventoryHelper, it must be activated. Its activation can be done as follows:

InventoryHelper.enable(plugin)

You can and should use this method when your plugin starts, like the following example:

public class MainClass extends JavaPlugin { 
    
    @Override
    public void onEnable() {
        InventoryHelper.enable(this);
    }
}

Creating simple inventory

After starting InventoryHelper you can start building your inventories. All inventories are built from InventoryBuilder. It has a range of methods for placing items or objects depending on their use.

new InventoryBuilder(title, lines);

You can also use inventory configuration to get started with InventoryBuilder. It can provide a better amount of resources for creating the inventory.

InventoryConfiguration configuration = InventoryConfiguration.builder(title, lines).build();

After instantiating the inventory configuration you can use it as follows:

new InventoryBuilder(configuration);

NOTE: You can also use the static methods like InventoryBuilder.of(title, lines) or InventoryBuilder.of(configuration) for the builder creation.

After that, you can put the items in your inventory:

Inventory inventory = InventoryBuilder.of("Simple inventory", 3)
        .withItem(13, ItemUtil.getItem(Material.REDSTONE, "§cClick me"), click -> {
            Player player = click.getPlayer();
            player.sendMessage("§cHi " + player.getName());
        }).build();

NOTE: You can directly open the inventory for the player with the build(player...) method. This method will build the inventory and automatically open it for the player.

Creating paginated inventory

Paginated inventories are a convenient way to show various items to players. They can be easily created using InventoryBuilder.

Paginated inventories work with PaginatedConfiguration. In these settings it is possible to define the buttons, amount of objects on each page and among others.

PaginatedConfiguration paginatedConfiguration = PaginatedConfiguration.builder("identifier")
        .size(15) // Size of objets in each page.
        .start(11) // The slot to start placing of items. 
        .end(34) // The slot to stop placing of items.
        .validator(slot -> (slot > 15 && slot < 20) || (slot > 24 && slot < 29)) // Skip the slots that meet this requirement.
        .button(Button.of(ButtonType.PREVIOUS_PAGE, 48, previousButtonItem)) // Set the button to go back to the page.
        .button(Button.of(ButtonType.NEXT_PAGE, 50, nextButtonItem)) //Set the button to pass the page.
        .build(); // build configuration

InventoryBuilder.of(inventoryConfiguration)
        .withItems(paginatedConfiguration, items, click -> {
            Material type = click.getItemStack().getType();
            click.getPlayer().sendMessage("You clicked in " + type.name());
        }).build(player);

In the end you will get something like this:
Paginated

Working with objects

One of the goals of this project is to allow inventories to create a reference to objects, thereby associating them with items.

For this association to be performed, your object must implement the InventoryItem. It allows you to turn the object into an item to be placed in the inventory.

@RequiredArgsConstructor @Getter
private static final class Icon implements InventoryItem { 
    
    private final String name;
    
    @Override @NotNull 
    public ItemStack getItem(@NotNull Inventory inventory, @NotNull InventoryProperty property) {
        return Skull.getByName(name);
    }
}

NOTE: If you want the object to have a default slot, when working with collections of objects, use the InventorySlot.

Creating an inventory with objects is quite simple, just pass your object in InventoryBuilder.

new InventoryBuilder<Icon>(title, lines);

From there, you can use object methods and build your inventory.

new InventoryBuilder<Icon>("Simple object", 3)
        .withObject(13, new Icon(name), click -> {
            Icon object = click.getObject();
            Player player = click.getPlayer();
            
            player.sendMessage("§cName: " + object.getName());
        }).build(player);

The same logic used for inventories paginated with items applies to objects.

Download

Downloads Downloads


You can see the latest release here

Maven

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>com.github.SrBlecaute01</groupId>
        <artifactId>InventoryHelper</artifactId>
        <version>1.5.0</version>
    </dependency>
</dependencies>

Gradle

repositories {
    maven { url 'https://jitpack.io' }
}

dependencies {
    implementation 'com.github.SrBlecaute01:InventoryHelper:1.5.0'
}