-
Notifications
You must be signed in to change notification settings - Fork 22
Event Bus
Natan Vieira edited this page Aug 2, 2023
·
13 revisions
This feature is not available yet.
Event Bus is available under the inventory-framework-eventbus
artifact.\
implementation 'me.devnatan:inventory-framework-eventbus:<version>'
To use the EventBus feature you need to install it on your framework. See the Integration with External Features documentation if you don't know yet how to install a feature.
import me.devnatan.inventoryframework.feature.eventbus.EventBusFeature.EventBus;
install(EventBus);
TBD
In this example we will use an item that when clicked will launch an event and from the launch of this event another item will be updated.
- Creates two items: DIAMOND and GOLD_INGOT
- Registers an "gold-digger" event to update GOLD_INGOT when it's called.
- Calls "gold-digger" event when player clicks on DIAMOND.
- When player clicks on DIAMOND it calls "gold-digger" event that updates GOLD_INGOT item.
- "Gold item was updated" message is sent to the player because GOLD_INGOT was updated (see
onUpdate
).
class EventItemUpdateView extends View {
private static final GOLD_DIGGER_EVENT = "gold-digger";
@Override
public void onFirstRender(RenderContext render) {
// Creates DIAMOND item in the first slot
render.firstSlot(new ItemStack(Material.DIAMOND))
.cancelOnClick()
.onClick(click -> render.emitEvent(GOLD_DIGGER_EVENT));
// Creates GOLD_INGOT item in the next available slot
// `asRef()` returns the reference of the item, to be able to use `#update()` later on
IFItem goldItem = render.availableSlot(new ItemStack(Material.GOLD_INGOT))
.onUpdate(update -> update.getPlayer().sendMessage("Gold item was updated"));
.asRef();
// Listens to the "gold-digger" event and then calls update on the gold item.
render.listenEvent(GOLD_DIGGER_EVENT, goldItem::update);
}
}