Basic Usage - DevNatan/inventory-framework GitHub Wiki

Creating views in the Inventory Framework is pretty simple, but of course there are more advanced things to be learned over time. On this page you will learn only the basic functionality of a view.

Let's get started and create a simple view, just create a regular class and extend the View class.

Preview
import me.devnatan.inventoryframework.View;

class CoolView extends View {
}

Preview

Configuration is done through the onInit(ViewConfigBuilder config) function, override this function to configure your view.

class CoolView extends View {

    @Override
    public void onInit(ViewConfigBuilder config) {
        config.size(5);
    }

}
Preview
class CoolView extends View {

    @Override
    public void onInit(ViewConfigBuilder config) {
        config.title("Octopus");
    }

}
Preview

IF supports several types of inventory, it is also possible to change them in the configuration.

class CoolView extends View {

    @Override
    public void onInit(ViewConfigBuilder config) {
        config.type(ViewType.HOPPER);
    }

}
Preview

It is possible to conditionally configure inventory title, size and type via the onOpen function, which is called before the inventory is displayed or created for a player.

class CoolView extends View {

    @Override
    public void onOpen(OpenContext open) {
        open.modifyConfig().title("Hi, " + open.getPlayer().getName() + "!");
    }

}

You can also inherit pre-made configurations from other places to reuse them in your view.

static final ViewConfig defaultConfig = ViewConfigBuilder.create()
    .title("Default title")
    .cancelOnClick();
class CoolView extends View {

    @Override
    public void onInit(ViewConfigBuilder config) {
        config.inheritFrom(defaultConfig);
    }

}

Adding items

To add items to the inventory, override the onFirstRender function and use the methods suffixed with slot.

Item definition functions return a constructor used for handling interactions and other things.

Preview
class CoolView extends View {

    @Override
    public void onFirstRender(RenderContext render) {
        render.slot(4, new ItemStack(Material.EGG));
    }

}

It is also possible to define the position of the item based on row and column.

Preview
class CoolView extends View {

    @Override
    public void onFirstRender(RenderContext render) {
        render.slot(2, 5, new ItemStack(Material.EGG));
    }

}

In case you need to add an item to the first or last slot of the inventory, using the firstSlot and lastSlot.

Preview
class CoolView extends View {

    @Override
    public void onFirstRender(RenderContext render) {
        render.firstSlot(new ItemStack(Material.EGG));
	render.lastSlot(new ItemStack(Material.EGG));
    }

}

Using these functions is highly recommended as this function is based on the container's settings and is self-adaptive, i.e. if you change the type or size of the container or have a conditionally defined size this function will always set the item in the right place.

Preview
class CoolView extends View {

    @Override
    public void onInit(ViewConfigBuilder config) {
        config.type(ViewType.HOPPER);
    }

    @Override
    public void onFirstRender(RenderContext render) {
        render.firstSlot(new ItemStack(Material.EGG));
	render.lastSlot(new ItemStack(Material.EGG));
    }

}

Through the availableSlot function you can resemble the behavior of the Inventory.addItem of Bukkit that sets the item in the next available slot of the inventory.

But, this function does more than just that, it adapts to your view conditions, for example: if you have a layout it sets the item in the next available slot in the layout, now, if you have pagination it sets the item in the next available slot respecting paging limits.

Preview
class CoolView extends View {

    @Override
    public void onFirstRender(RenderContext render) {
        render.firstSlot(new ItemStack(Material.EGG));
        render.availableSlot(new ItemStack(Material.DIAMOND));
    }

}

It is especially useful in cases where there is a series of data to be iterated and defined items from this data.

Preview
class CoolView extends View {

    @Override
    public void onFirstRender(RenderContext render) {
        render.slot(1, new ItemStack(Material.EGG)); // will not be ignored in the iteration below

        for (int i = 1; i <= 5; i++) {
            render.availableSlot(new ItemStack(Material.DIAMOND, i));
    }

}

Next Topics

See also about Interaction Handling to handle player clicks and Registration, Open and Close to display a view to a player.

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