Remove GlobalHandles for better testability

This commit is contained in:
themode 2022-02-13 12:34:27 +01:00
parent 84d154b8db
commit 2438393ce1
11 changed files with 32 additions and 53 deletions

View File

@ -3,8 +3,8 @@ package net.minestom.server;
import net.minestom.server.advancements.AdvancementManager;
import net.minestom.server.adventure.bossbar.BossBarManager;
import net.minestom.server.command.CommandManager;
import net.minestom.server.event.EventDispatcher;
import net.minestom.server.event.GlobalEventHandler;
import net.minestom.server.event.GlobalHandles;
import net.minestom.server.event.server.ServerTickMonitorEvent;
import net.minestom.server.exception.ExceptionManager;
import net.minestom.server.extensions.ExtensionManager;
@ -276,7 +276,7 @@ final class ServerProcessImpl implements ServerProcess {
final double acquisitionTimeMs = Acquirable.getAcquiringTime() / 1e6D;
final double tickTimeMs = (System.nanoTime() - nanoTime) / 1e6D;
final TickMonitor tickMonitor = new TickMonitor(tickTimeMs, acquisitionTimeMs);
GlobalHandles.SERVER_TICK_MONITOR_HANDLE.call(new ServerTickMonitorEvent(tickMonitor));
EventDispatcher.call(new ServerTickMonitorEvent(tickMonitor));
Acquirable.resetAcquiringTime();
}
}

View File

@ -15,7 +15,6 @@ 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.GlobalHandles;
import net.minestom.server.event.entity.*;
import net.minestom.server.event.instance.AddEntityToInstanceEvent;
import net.minestom.server.event.instance.RemoveEntityFromInstanceEvent;
@ -545,7 +544,7 @@ public class Entity implements Viewable, Tickable, Schedulable, TagHandler, Perm
update(time);
ticks++;
GlobalHandles.ENTITY_TICK.call(new EntityTickEvent(this));
EventDispatcher.call(new EntityTickEvent(this));
// remove expired effects
effectTick(time);

View File

@ -32,7 +32,6 @@ 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.GlobalHandles;
import net.minestom.server.event.inventory.InventoryOpenEvent;
import net.minestom.server.event.item.ItemDropEvent;
import net.minestom.server.event.item.ItemUpdateStateEvent;
@ -125,7 +124,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
try {
if (chunk != null) {
chunk.sendChunk(this);
GlobalHandles.PLAYER_CHUNK_LOAD.call(new PlayerChunkLoadEvent(this, chunkX, chunkZ));
EventDispatcher.call(new PlayerChunkLoadEvent(this, chunkX, chunkZ));
}
} catch (Exception e) {
MinecraftServer.getExceptionManager().handleException(e);
@ -135,7 +134,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
final IntegerBiConsumer chunkRemover = (chunkX, chunkZ) -> {
// Unload old chunks
sendPacket(new UnloadChunkPacket(chunkX, chunkZ));
GlobalHandles.PLAYER_CHUNK_UNLOAD.call(new PlayerChunkUnloadEvent(this, chunkX, chunkZ));
EventDispatcher.call(new PlayerChunkUnloadEvent(this, chunkX, chunkZ));
};
private final AtomicInteger teleportId = new AtomicInteger();
@ -362,7 +361,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
}
// Tick event
GlobalHandles.PLAYER_TICK.call(new PlayerTickEvent(this));
EventDispatcher.call(new PlayerTickEvent(this));
}
@Override

View File

@ -42,10 +42,13 @@ non-sealed class EventNodeImpl<T extends Event> implements EventNode<T> {
}
@Override
@SuppressWarnings("unchecked")
public <E extends T> @NotNull ListenerHandle<E> getHandle(@NotNull Class<E> handleType) {
//noinspection unchecked
return (ListenerHandle<E>) handleMap.computeIfAbsent(handleType,
aClass -> new Handle<>(this, (Class<T>) aClass));
Handle<E> handle = (Handle<E>) handleMap.get(handleType);
if (handle != null) return handle;
var tmp = new Handle<>(this, (Class<T>) handleType);
handle = (Handle<E>) handleMap.putIfAbsent(handleType, tmp);
return handle != null ? handle : (ListenerHandle<E>) tmp;
}
@Override

View File

@ -1,31 +0,0 @@
package net.minestom.server.event;
import net.minestom.server.MinecraftServer;
import net.minestom.server.event.entity.EntityTickEvent;
import net.minestom.server.event.instance.InstanceChunkLoadEvent;
import net.minestom.server.event.instance.InstanceTickEvent;
import net.minestom.server.event.inventory.InventoryItemChangeEvent;
import net.minestom.server.event.inventory.PlayerInventoryItemChangeEvent;
import net.minestom.server.event.player.*;
import net.minestom.server.event.server.ServerTickMonitorEvent;
import org.jetbrains.annotations.ApiStatus;
/**
* Contains handles to the main node {@link MinecraftServer#getGlobalEventHandler()}
* (compatible with {@link EventDispatcher}).
*/
@ApiStatus.Internal
public final class GlobalHandles {
public static final ListenerHandle<PlayerTickEvent> PLAYER_TICK = EventDispatcher.getHandle(PlayerTickEvent.class);
public static final ListenerHandle<PlayerPacketEvent> PLAYER_PACKET = EventDispatcher.getHandle(PlayerPacketEvent.class);
public static final ListenerHandle<PlayerMoveEvent> PLAYER_MOVE = EventDispatcher.getHandle(PlayerMoveEvent.class);
public static final ListenerHandle<EntityTickEvent> ENTITY_TICK = EventDispatcher.getHandle(EntityTickEvent.class);
public static final ListenerHandle<InstanceTickEvent> INSTANCE_TICK = EventDispatcher.getHandle(InstanceTickEvent.class);
public static final ListenerHandle<PlayerChunkLoadEvent> PLAYER_CHUNK_LOAD = EventDispatcher.getHandle(PlayerChunkLoadEvent.class);
public static final ListenerHandle<PlayerChunkUnloadEvent> PLAYER_CHUNK_UNLOAD = EventDispatcher.getHandle(PlayerChunkUnloadEvent.class);
public static final ListenerHandle<InstanceChunkLoadEvent> INSTANCE_CHUNK_LOAD = EventDispatcher.getHandle(InstanceChunkLoadEvent.class);
public static final ListenerHandle<InventoryItemChangeEvent> INVENTORY_ITEM_CHANGE_EVENT = EventDispatcher.getHandle(InventoryItemChangeEvent.class);
public static final ListenerHandle<PlayerInventoryItemChangeEvent> PLAYER_INVENTORY_ITEM_CHANGE_EVENT = EventDispatcher.getHandle(PlayerInventoryItemChangeEvent.class);
public static final ListenerHandle<ServerTickMonitorEvent> SERVER_TICK_MONITOR_HANDLE = EventDispatcher.getHandle(ServerTickMonitorEvent.class);
}

View File

@ -11,7 +11,7 @@ import net.minestom.server.entity.EntityCreature;
import net.minestom.server.entity.ExperienceOrb;
import net.minestom.server.entity.Player;
import net.minestom.server.entity.pathfinding.PFInstanceSpace;
import net.minestom.server.event.GlobalHandles;
import net.minestom.server.event.EventDispatcher;
import net.minestom.server.event.instance.InstanceTickEvent;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockHandler;
@ -582,7 +582,7 @@ public abstract class Instance implements Block.Getter, Block.Setter, Tickable,
// Tick event
{
// Process tick events
GlobalHandles.INSTANCE_TICK.call(new InstanceTickEvent(this, time, lastTickAge));
EventDispatcher.call(new InstanceTickEvent(this, time, lastTickAge));
// Set last tick age
this.lastTickAge = time;
}

View File

@ -8,7 +8,6 @@ import net.minestom.server.coordinate.Vec;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.Player;
import net.minestom.server.event.EventDispatcher;
import net.minestom.server.event.GlobalHandles;
import net.minestom.server.event.instance.InstanceChunkLoadEvent;
import net.minestom.server.event.instance.InstanceChunkUnloadEvent;
import net.minestom.server.event.player.PlayerBlockBreakEvent;
@ -274,7 +273,7 @@ public class InstanceContainer extends Instance {
.whenComplete((chunk, throwable) -> {
// TODO run in the instance thread?
cacheChunk(chunk);
GlobalHandles.INSTANCE_CHUNK_LOAD.call(new InstanceChunkLoadEvent(this, chunk));
EventDispatcher.call(new InstanceChunkLoadEvent(this, chunk));
synchronized (loadingChunks) {
this.loadingChunks.remove(ChunkUtils.getChunkIndex(chunk));
}

View File

@ -1,6 +1,6 @@
package net.minestom.server.inventory;
import net.minestom.server.event.GlobalHandles;
import net.minestom.server.event.EventDispatcher;
import net.minestom.server.event.inventory.InventoryItemChangeEvent;
import net.minestom.server.event.inventory.PlayerInventoryItemChangeEvent;
import net.minestom.server.inventory.click.InventoryClickProcessor;
@ -81,9 +81,9 @@ public sealed abstract class AbstractInventory implements InventoryClickHandler,
UNSAFE_itemInsert(slot, itemStack);
}
if (this instanceof PlayerInventory inv) {
GlobalHandles.PLAYER_INVENTORY_ITEM_CHANGE_EVENT.call(new PlayerInventoryItemChangeEvent(inv.player, slot, previous, itemStack));
EventDispatcher.call(new PlayerInventoryItemChangeEvent(inv.player, slot, previous, itemStack));
} else if (this instanceof Inventory inv) {
GlobalHandles.INVENTORY_ITEM_CHANGE_EVENT.call(new InventoryItemChangeEvent(inv, slot, previous, itemStack));
EventDispatcher.call(new InventoryItemChangeEvent(inv, slot, previous, itemStack));
}
}

View File

@ -2,7 +2,7 @@ package net.minestom.server.listener;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.entity.Player;
import net.minestom.server.event.GlobalHandles;
import net.minestom.server.event.EventDispatcher;
import net.minestom.server.event.player.PlayerMoveEvent;
import net.minestom.server.instance.Instance;
import net.minestom.server.network.packet.client.play.*;
@ -55,7 +55,7 @@ public class PlayerPositionListener {
}
PlayerMoveEvent playerMoveEvent = new PlayerMoveEvent(player, packetPosition);
GlobalHandles.PLAYER_MOVE.call(playerMoveEvent);
EventDispatcher.call(playerMoveEvent);
if (!currentPosition.equals(player.getPosition())) {
// Player has been teleported in the event
return;

View File

@ -3,7 +3,7 @@ package net.minestom.server.listener.manager;
import net.minestom.server.MinecraftServer;
import net.minestom.server.ServerProcess;
import net.minestom.server.entity.Player;
import net.minestom.server.event.GlobalHandles;
import net.minestom.server.event.EventDispatcher;
import net.minestom.server.event.player.PlayerPacketEvent;
import net.minestom.server.listener.*;
import net.minestom.server.network.ConnectionManager;
@ -92,7 +92,7 @@ public final class PacketListenerManager {
// Event
PlayerPacketEvent playerPacketEvent = new PlayerPacketEvent(player, packet);
GlobalHandles.PLAYER_PACKET.call(playerPacketEvent);
EventDispatcher.call(playerPacketEvent);
if (playerPacketEvent.isCancelled()) {
return;
}

View File

@ -62,6 +62,16 @@ public class EventNodeTest {
assertFalse(result.get(), "The event should not be called after the removal");
}
@Test
public void testHandle() {
var node = EventNode.all("main");
var handle = node.getHandle(EventTest.class);
assertSame(handle, node.getHandle(EventTest.class));
var handle1 = node.getHandle(CancellableTest.class);
assertSame(handle1, node.getHandle(CancellableTest.class));
}
@Test
public void testCancellable() {
var node = EventNode.all("main");