Interaction Handling - DevNatan/inventory-framework GitHub Wiki
Table of Contents
Global Interaction Handler
This example will send a message whenever the player interacts with the view or the player inventory itself, anywhere.
@Override
public void onClick(SlotClickContext slotClick) {
slotClick.getPlayer().sendMessage("Clicked on " + slotClick.getClickedSlot());
}
Click context can be canceled to prevent player interaction with an item.
@Override
public void onClick(SlotClickContext slotClick) {
slotClick.setCancelled(true);
}
IF by default gives you an option for these cases of cancellation that it is quite common, so it is possible to set them in the configuration.
@Override
public void onInit(ViewConfigBuilder config) {
config.cancelOnClick();
}
Item Interaction Handlers
Items have a series of options related to interaction treatment, it is possible to define them individually for each of them.
import me.devnatan.inventoryframework.View;
class CoolView extends View {
@Override
public void onFirstRender(RenderContext render) {
render.firstSlot(new ItemStack(Material.DIAMOND))
.onClick(click -> click.getPlayer().sendMessage("Clicked on diamond"));
}
}
Item click handlers can also be cancelled.
import me.devnatan.inventoryframework.View;
class CoolView extends View {
@Override
public void onFirstRender(RenderContext render) {
render.firstSlot(new ItemStack(Material.DIAMOND)).cancelOnClick();
}
}
Individual de-cancellation is also supported in cases where there is global de-cancellation but you don't want it to be cancelled for a specific item.
import me.devnatan.inventoryframework.View;
class CoolView extends View {
@Override
public void onInit(ViewConfigBuilder config) {
// Cancels click anywhere
config.cancelOnClick();
}
@Override
public void onFirstRender(RenderContext render) {
// Will de-cancel click for this specific item
render.firstSlot(new ItemStack(Material.DIAMOND)).onClick(click -> click.setCancelled(false));
}
}
Player Inventory Interactions
Interaction handlers are also called in interactions of the player's own inventory, it is possible to distinguish which inventory was clicked using isOnEntityContainer()
.
import me.devnatan.inventoryframework.View;
class CoolView extends View {
@Override
public void onClick(SlotClickContext slotClick) {
if (slotClick.isOnEntityContainer()) {
// clicked on player inventory (bottom)
} else {
// clicked on view inventory (top)
}
}
}
Next Topics
Learn about State Management to manipulate data in interaction handlers.