mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-02 14:38:26 +01:00
Start using ListenerHandle
This commit is contained in:
parent
8acb2e292c
commit
18b0e21f21
@ -16,6 +16,7 @@ import net.minestom.server.coordinate.Pos;
|
||||
import net.minestom.server.coordinate.Vec;
|
||||
import net.minestom.server.entity.metadata.EntityMeta;
|
||||
import net.minestom.server.event.EventDispatcher;
|
||||
import net.minestom.server.event.ListenerHandle;
|
||||
import net.minestom.server.event.entity.*;
|
||||
import net.minestom.server.instance.Chunk;
|
||||
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 AtomicInteger LAST_ENTITY_ID = new AtomicInteger();
|
||||
|
||||
private static final ListenerHandle<EntityTickEvent> ENTITY_TICK_HANDLER = EventDispatcher.getHandle(EntityTickEvent.class);
|
||||
|
||||
protected Instance instance;
|
||||
protected Chunk currentChunk;
|
||||
protected Pos position;
|
||||
@ -445,7 +448,7 @@ public class Entity implements Viewable, Tickable, TagHandler, PermissionHandler
|
||||
update(time);
|
||||
|
||||
ticks++;
|
||||
EventDispatcher.call(new EntityTickEvent(this));
|
||||
EventDispatcher.call(new EntityTickEvent(this), ENTITY_TICK_HANDLER);
|
||||
|
||||
// remove expired effects
|
||||
effectTick(time);
|
||||
|
@ -32,6 +32,7 @@ import net.minestom.server.entity.fakeplayer.FakePlayer;
|
||||
import net.minestom.server.entity.metadata.PlayerMeta;
|
||||
import net.minestom.server.entity.vehicle.PlayerVehicleInformation;
|
||||
import net.minestom.server.event.EventDispatcher;
|
||||
import net.minestom.server.event.ListenerHandle;
|
||||
import net.minestom.server.event.inventory.InventoryOpenEvent;
|
||||
import net.minestom.server.event.item.ItemDropEvent;
|
||||
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 {
|
||||
|
||||
private static final ListenerHandle<PlayerTickEvent> PLAYER_TICK_HANDLER = EventDispatcher.getHandle(PlayerTickEvent.class);
|
||||
|
||||
private long lastKeepAlive;
|
||||
private boolean answerKeepAlive;
|
||||
|
||||
@ -166,9 +169,6 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
||||
// Vehicle
|
||||
private final PlayerVehicleInformation vehicleInformation = new PlayerVehicleInformation();
|
||||
|
||||
// Tick related
|
||||
private final PlayerTickEvent playerTickEvent = new PlayerTickEvent(this);
|
||||
|
||||
// Adventure
|
||||
private Identity identity;
|
||||
private final Pointers pointers;
|
||||
@ -353,7 +353,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
||||
}
|
||||
|
||||
// Tick event
|
||||
EventDispatcher.call(playerTickEvent);
|
||||
EventDispatcher.call(new PlayerTickEvent(this), PLAYER_TICK_HANDLER);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -10,6 +10,14 @@ public class EventDispatcher {
|
||||
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) {
|
||||
MinecraftServer.getGlobalEventHandler().callCancellable(event, successCallback);
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ import net.minestom.server.entity.ExperienceOrb;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.entity.pathfinding.PFInstanceSpace;
|
||||
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.InstanceTickEvent;
|
||||
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 UpdateManager UPDATE_MANAGER = MinecraftServer.getUpdateManager();
|
||||
|
||||
private static final ListenerHandle<InstanceTickEvent> INSTANCE_TICK_HANDLER = EventDispatcher.getHandle(InstanceTickEvent.class);
|
||||
|
||||
private boolean registered;
|
||||
|
||||
private final DimensionType dimensionType;
|
||||
@ -705,7 +708,7 @@ public abstract class Instance implements BlockGetter, BlockSetter, Tickable, Ta
|
||||
// Tick event
|
||||
{
|
||||
// Process tick events
|
||||
EventDispatcher.call(new InstanceTickEvent(this, time, lastTickAge));
|
||||
EventDispatcher.call(new InstanceTickEvent(this, time, lastTickAge), INSTANCE_TICK_HANDLER);
|
||||
// Set last tick age
|
||||
this.lastTickAge = time;
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package net.minestom.server.listener;
|
||||
import net.minestom.server.coordinate.Pos;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.event.EventDispatcher;
|
||||
import net.minestom.server.event.ListenerHandle;
|
||||
import net.minestom.server.event.player.PlayerMoveEvent;
|
||||
import net.minestom.server.instance.Instance;
|
||||
import net.minestom.server.network.packet.client.play.*;
|
||||
@ -11,6 +12,8 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class PlayerPositionListener {
|
||||
|
||||
private static final ListenerHandle<PlayerMoveEvent> PLAYER_MOVE_HANDLER = EventDispatcher.getHandle(PlayerMoveEvent.class);
|
||||
|
||||
public static void playerPacketListener(ClientPlayerPacket packet, Player player) {
|
||||
player.refreshOnGround(packet.onGround);
|
||||
}
|
||||
@ -53,7 +56,7 @@ public class PlayerPositionListener {
|
||||
}
|
||||
|
||||
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)
|
||||
if (!playerMoveEvent.isCancelled() && currentPosition.equals(player.getPosition())) {
|
||||
// Move the player
|
||||
|
@ -3,6 +3,7 @@ package net.minestom.server.listener.manager;
|
||||
import net.minestom.server.MinecraftServer;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.event.EventDispatcher;
|
||||
import net.minestom.server.event.ListenerHandle;
|
||||
import net.minestom.server.event.player.PlayerPacketEvent;
|
||||
import net.minestom.server.listener.*;
|
||||
import net.minestom.server.network.ConnectionManager;
|
||||
@ -23,6 +24,8 @@ public final class PacketListenerManager {
|
||||
public final static Logger LOGGER = LoggerFactory.getLogger(PacketListenerManager.class);
|
||||
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<>();
|
||||
|
||||
public PacketListenerManager() {
|
||||
@ -89,7 +92,7 @@ public final class PacketListenerManager {
|
||||
|
||||
// Event
|
||||
PlayerPacketEvent playerPacketEvent = new PlayerPacketEvent(player, packet);
|
||||
EventDispatcher.call(playerPacketEvent);
|
||||
EventDispatcher.call(playerPacketEvent, PLAYER_PACKET_HANDLER);
|
||||
if (playerPacketEvent.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user