-
Notifications
You must be signed in to change notification settings - Fork 8
Creating your own inventory widgets
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 multipleKelpItems
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.
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 normalWidget
class inplayer(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.
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.
(c) 2019-2021 pxav.
Kelp is an open-source project maintained by multiple developers. If you have additions/questions/problems to report about the wiki, feel free to create an issue here on GitHub or join the Discord
- SQL Module coming soon
- Documentation in progress