5 Events
TheMode edited this page 2020-09-27 21:43:55 +02:00

Listen to events

Events are everywhere, they are as simple as

player.addEventCallback(EVENT_CLASS, event -> {
   // Do your event things
});

for example

player.addEventCallback(PlayerBlockInteractEvent.class, event -> {
   if (event.getHand() != Player.Hand.MAIN)
      return;

   short blockId = player.getInstance().getBlockId(event.getBlockPosition());
   Block block = Block.fromId(blockId);
   player.sendMessage("You clicked at the block " + block);
});

Basics events are listed here

The recommended way of how and where to put the listeners is by using ConnectionManager#addPlayerInitialization, it is basically called just after the creation of the player object (you SHOULDN'T do anything other than registering events)

ConnectionManager connectionManager = MinecraftServer.getConnectionManager();
connectionManager.addPlayerInitialization(player -> {
   // Do what you want with the player
   // player.addEventCallback ...
});

Create a custom event

In order to do so, you need a class which extends Event or CancellableEvent and finally call it by doing

MyCustomEvent customEvent = new MyCustomEvent();
player.callEvent(MyCustomEvent.class, customEvent);

You can then retrieve data from your event object and do whatever you want with it.