Skip to content

Creating your own inventory widgets

PXAV edited this page Jan 1, 2021 · 1 revision

General

Kelp does provide some inventory widgets by default, but if you want to create your own one, you can do that as well. It is recommended to read the basic tutorial about inventories to have a better understanding of how inventories and widgets work.

Basically, there are two types of widgets:

  • SimpleWidget: A widget that only renders a single item to the final inventory.
  • GroupedWidget: A widget that adds multiple KelpItems to the final inventory. This is needed when creating a Pagination for example.

When creating your widget class you have to consider what your widget should do and implement one of the above interfaces.

Creating your Widget class

Knowing which widget type you want, you have to create a class implementing this widget type. In this example, we are going to use a SimpleWidget:

public class TestWidget implements SimpleWidget {

  private KelpPlayer player;
  
  @Override
  public KelpItem render() {
    return null;
  }

  @Override
  public TestWidget player(KelpPlayer player) { // return TestWidget instead of Widget
    this.player = player;
    return this;
  }

  @Override
  public KelpPlayer getPlayer() {
    return this.player;
  }

}

You might want to make your class final so that it cannot be extended by another widget class. Do not make it a Guice @Singleton.

For a more fluent builder design, you can return your widget class (TestWidget) instead of the normal Widget class in player(KelpPlayer) method.

Your widget has to specify a player who owns it, which is important for registering item listeners for example. Those player instances have to be set with the player(KelpPlayer method and can be queried with the getPlayer() method. So make sure they don't return null/are not empty.

If your class has any dependencies that have to be injected via the constructor, it is recommended to use a factory class for your widgets just like the WidgetFactory of Kelp.

Building your item

Example of GroupedWidget class

Further information

If you find your widget to be universal and useful for other developers, feel free to make a pull request with your widget, so that it is added to the Kelp main widget library and helps other developers improve their inventories.

Clone this wiki locally