Interaction Handling - DevNatan/inventory-framework GitHub Wiki
Table of Contents
Configuration
TBD
Global Interaction Handler
This example will send a message whenever the player interacts with the inventory.
import me.devnatan.inventoryframework.View;
class CoolView extends View {
@Override
public void onClick(SlotClickContext slotClick) {
slotClick.getPlayer().sendMessage("Clicked on " + slotClick.getClickedSlot());
}
}
Global clicking can be canceled to prevent player interaction with an item.
import me.devnatan.inventoryframework.View;
class CoolView extends View {
@Override
public void onClick(SlotClickContext slotClick) {
slotClick.setCancelled(true);
}
}
The IF by default gives you an option for these cases of cancellation that it is quite common, so it is possible to define them in the configuration.
import me.devnatan.inventoryframework.View;
class CoolView extends View {
@Override
public void onInit(ViewConfigBuilder config) {
config.cancelOnClick();
}
}
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 Interaction Handlers
Item individual 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) {
config.cancelOnClick();
}
@Override
public void onFirstRender(RenderContext render) {
render.firstSlot(new ItemStack(Material.DIAMOND)).onClick(click -> click.setCancelled(false));
}
}
Use the global handler to de-cancel specific (numeric) slots.
import me.devnatan.inventoryframework.View;
class CoolView extends View {
@Override
public void onInit(ViewConfigBuilder config) {
config.cancelOnClick();
}
@Override
public void onClick(SlotClickContext ctx) {
// allow interactions on slot 4
if (ctx.getClickedSlot() == 4)
ctx.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.