Start using ListenerHandle

This commit is contained in:
TheMode 2021-08-22 07:47:06 +02:00
parent 8acb2e292c
commit 18b0e21f21
6 changed files with 28 additions and 8 deletions

View File

@ -16,6 +16,7 @@ import net.minestom.server.coordinate.Pos;
import net.minestom.server.coordinate.Vec; import net.minestom.server.coordinate.Vec;
import net.minestom.server.entity.metadata.EntityMeta; import net.minestom.server.entity.metadata.EntityMeta;
import net.minestom.server.event.EventDispatcher; import net.minestom.server.event.EventDispatcher;
import net.minestom.server.event.ListenerHandle;
import net.minestom.server.event.entity.*; import net.minestom.server.event.entity.*;
import net.minestom.server.instance.Chunk; import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.Instance; import net.minestom.server.instance.Instance;
@ -64,6 +65,8 @@ public class Entity implements Viewable, Tickable, TagHandler, PermissionHandler
private static final Map<UUID, Entity> ENTITY_BY_UUID = new ConcurrentHashMap<>(); private static final Map<UUID, Entity> ENTITY_BY_UUID = new ConcurrentHashMap<>();
private static final AtomicInteger LAST_ENTITY_ID = new AtomicInteger(); private static final AtomicInteger LAST_ENTITY_ID = new AtomicInteger();
private static final ListenerHandle<EntityTickEvent> ENTITY_TICK_HANDLER = EventDispatcher.getHandle(EntityTickEvent.class);
protected Instance instance; protected Instance instance;
protected Chunk currentChunk; protected Chunk currentChunk;
protected Pos position; protected Pos position;
@ -445,7 +448,7 @@ public class Entity implements Viewable, Tickable, TagHandler, PermissionHandler
update(time); update(time);
ticks++; ticks++;
EventDispatcher.call(new EntityTickEvent(this)); EventDispatcher.call(new EntityTickEvent(this), ENTITY_TICK_HANDLER);
// remove expired effects // remove expired effects
effectTick(time); effectTick(time);

View File

@ -32,6 +32,7 @@ import net.minestom.server.entity.fakeplayer.FakePlayer;
import net.minestom.server.entity.metadata.PlayerMeta; import net.minestom.server.entity.metadata.PlayerMeta;
import net.minestom.server.entity.vehicle.PlayerVehicleInformation; import net.minestom.server.entity.vehicle.PlayerVehicleInformation;
import net.minestom.server.event.EventDispatcher; import net.minestom.server.event.EventDispatcher;
import net.minestom.server.event.ListenerHandle;
import net.minestom.server.event.inventory.InventoryOpenEvent; import net.minestom.server.event.inventory.InventoryOpenEvent;
import net.minestom.server.event.item.ItemDropEvent; import net.minestom.server.event.item.ItemDropEvent;
import net.minestom.server.event.item.ItemUpdateStateEvent; import net.minestom.server.event.item.ItemUpdateStateEvent;
@ -98,6 +99,8 @@ import java.util.function.UnaryOperator;
*/ */
public class Player extends LivingEntity implements CommandSender, Localizable, HoverEventSource<ShowEntity>, Identified, NamedAndIdentified { public class Player extends LivingEntity implements CommandSender, Localizable, HoverEventSource<ShowEntity>, Identified, NamedAndIdentified {
private static final ListenerHandle<PlayerTickEvent> PLAYER_TICK_HANDLER = EventDispatcher.getHandle(PlayerTickEvent.class);
private long lastKeepAlive; private long lastKeepAlive;
private boolean answerKeepAlive; private boolean answerKeepAlive;
@ -166,9 +169,6 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
// Vehicle // Vehicle
private final PlayerVehicleInformation vehicleInformation = new PlayerVehicleInformation(); private final PlayerVehicleInformation vehicleInformation = new PlayerVehicleInformation();
// Tick related
private final PlayerTickEvent playerTickEvent = new PlayerTickEvent(this);
// Adventure // Adventure
private Identity identity; private Identity identity;
private final Pointers pointers; private final Pointers pointers;
@ -353,7 +353,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
} }
// Tick event // Tick event
EventDispatcher.call(playerTickEvent); EventDispatcher.call(new PlayerTickEvent(this), PLAYER_TICK_HANDLER);
} }
@Override @Override

View File

@ -10,6 +10,14 @@ public class EventDispatcher {
MinecraftServer.getGlobalEventHandler().call(event); MinecraftServer.getGlobalEventHandler().call(event);
} }
public static <E extends Event> void call(@NotNull E event, @NotNull ListenerHandle<E> handle) {
MinecraftServer.getGlobalEventHandler().call(event, handle);
}
public static <E extends Event> ListenerHandle<E> getHandle(@NotNull Class<E> handleType) {
return MinecraftServer.getGlobalEventHandler().getHandle(handleType);
}
public static void callCancellable(@NotNull CancellableEvent event, @NotNull Runnable successCallback) { public static void callCancellable(@NotNull CancellableEvent event, @NotNull Runnable successCallback) {
MinecraftServer.getGlobalEventHandler().callCancellable(event, successCallback); MinecraftServer.getGlobalEventHandler().callCancellable(event, successCallback);
} }

View File

@ -17,6 +17,7 @@ import net.minestom.server.entity.ExperienceOrb;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import net.minestom.server.entity.pathfinding.PFInstanceSpace; import net.minestom.server.entity.pathfinding.PFInstanceSpace;
import net.minestom.server.event.EventDispatcher; import net.minestom.server.event.EventDispatcher;
import net.minestom.server.event.ListenerHandle;
import net.minestom.server.event.instance.AddEntityToInstanceEvent; import net.minestom.server.event.instance.AddEntityToInstanceEvent;
import net.minestom.server.event.instance.InstanceTickEvent; import net.minestom.server.event.instance.InstanceTickEvent;
import net.minestom.server.event.instance.RemoveEntityFromInstanceEvent; import net.minestom.server.event.instance.RemoveEntityFromInstanceEvent;
@ -61,6 +62,8 @@ public abstract class Instance implements BlockGetter, BlockSetter, Tickable, Ta
protected static final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager(); protected static final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager();
protected static final UpdateManager UPDATE_MANAGER = MinecraftServer.getUpdateManager(); protected static final UpdateManager UPDATE_MANAGER = MinecraftServer.getUpdateManager();
private static final ListenerHandle<InstanceTickEvent> INSTANCE_TICK_HANDLER = EventDispatcher.getHandle(InstanceTickEvent.class);
private boolean registered; private boolean registered;
private final DimensionType dimensionType; private final DimensionType dimensionType;
@ -705,7 +708,7 @@ public abstract class Instance implements BlockGetter, BlockSetter, Tickable, Ta
// Tick event // Tick event
{ {
// Process tick events // Process tick events
EventDispatcher.call(new InstanceTickEvent(this, time, lastTickAge)); EventDispatcher.call(new InstanceTickEvent(this, time, lastTickAge), INSTANCE_TICK_HANDLER);
// Set last tick age // Set last tick age
this.lastTickAge = time; this.lastTickAge = time;
} }

View File

@ -3,6 +3,7 @@ package net.minestom.server.listener;
import net.minestom.server.coordinate.Pos; import net.minestom.server.coordinate.Pos;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import net.minestom.server.event.EventDispatcher; import net.minestom.server.event.EventDispatcher;
import net.minestom.server.event.ListenerHandle;
import net.minestom.server.event.player.PlayerMoveEvent; import net.minestom.server.event.player.PlayerMoveEvent;
import net.minestom.server.instance.Instance; import net.minestom.server.instance.Instance;
import net.minestom.server.network.packet.client.play.*; import net.minestom.server.network.packet.client.play.*;
@ -11,6 +12,8 @@ import org.jetbrains.annotations.NotNull;
public class PlayerPositionListener { public class PlayerPositionListener {
private static final ListenerHandle<PlayerMoveEvent> PLAYER_MOVE_HANDLER = EventDispatcher.getHandle(PlayerMoveEvent.class);
public static void playerPacketListener(ClientPlayerPacket packet, Player player) { public static void playerPacketListener(ClientPlayerPacket packet, Player player) {
player.refreshOnGround(packet.onGround); player.refreshOnGround(packet.onGround);
} }
@ -53,7 +56,7 @@ public class PlayerPositionListener {
} }
PlayerMoveEvent playerMoveEvent = new PlayerMoveEvent(player, newPosition); PlayerMoveEvent playerMoveEvent = new PlayerMoveEvent(player, newPosition);
EventDispatcher.call(playerMoveEvent); EventDispatcher.call(playerMoveEvent, PLAYER_MOVE_HANDLER);
// True if the event call changed the player position (possibly a teleport) // True if the event call changed the player position (possibly a teleport)
if (!playerMoveEvent.isCancelled() && currentPosition.equals(player.getPosition())) { if (!playerMoveEvent.isCancelled() && currentPosition.equals(player.getPosition())) {
// Move the player // Move the player

View File

@ -3,6 +3,7 @@ package net.minestom.server.listener.manager;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import net.minestom.server.event.EventDispatcher; import net.minestom.server.event.EventDispatcher;
import net.minestom.server.event.ListenerHandle;
import net.minestom.server.event.player.PlayerPacketEvent; import net.minestom.server.event.player.PlayerPacketEvent;
import net.minestom.server.listener.*; import net.minestom.server.listener.*;
import net.minestom.server.network.ConnectionManager; import net.minestom.server.network.ConnectionManager;
@ -23,6 +24,8 @@ public final class PacketListenerManager {
public final static Logger LOGGER = LoggerFactory.getLogger(PacketListenerManager.class); public final static Logger LOGGER = LoggerFactory.getLogger(PacketListenerManager.class);
private static final ConnectionManager CONNECTION_MANAGER = MinecraftServer.getConnectionManager(); private static final ConnectionManager CONNECTION_MANAGER = MinecraftServer.getConnectionManager();
private static final ListenerHandle<PlayerPacketEvent> PLAYER_PACKET_HANDLER = EventDispatcher.getHandle(PlayerPacketEvent.class);
private final Map<Class<? extends ClientPlayPacket>, PacketListenerConsumer> listeners = new ConcurrentHashMap<>(); private final Map<Class<? extends ClientPlayPacket>, PacketListenerConsumer> listeners = new ConcurrentHashMap<>();
public PacketListenerManager() { public PacketListenerManager() {
@ -89,7 +92,7 @@ public final class PacketListenerManager {
// Event // Event
PlayerPacketEvent playerPacketEvent = new PlayerPacketEvent(player, packet); PlayerPacketEvent playerPacketEvent = new PlayerPacketEvent(player, packet);
EventDispatcher.call(playerPacketEvent); EventDispatcher.call(playerPacketEvent, PLAYER_PACKET_HANDLER);
if (playerPacketEvent.isCancelled()) { if (playerPacketEvent.isCancelled()) {
return; return;
} }