From 3dfd4678d5775447d2afa3fe8e957e6be22ac6d9 Mon Sep 17 00:00:00 2001 From: TheMode Date: Sat, 5 Jun 2021 01:04:51 +0200 Subject: [PATCH] Add `PlayerPacketEvent` --- .../event/player/PlayerPacketEvent.java | 38 +++++++++++++++++++ .../manager/PacketListenerManager.java | 20 ++++++++-- 2 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 src/main/java/net/minestom/server/event/player/PlayerPacketEvent.java diff --git a/src/main/java/net/minestom/server/event/player/PlayerPacketEvent.java b/src/main/java/net/minestom/server/event/player/PlayerPacketEvent.java new file mode 100644 index 000000000..16e40806f --- /dev/null +++ b/src/main/java/net/minestom/server/event/player/PlayerPacketEvent.java @@ -0,0 +1,38 @@ +package net.minestom.server.event.player; + +import net.minestom.server.entity.Player; +import net.minestom.server.event.CancellableEvent; +import net.minestom.server.event.trait.PlayerEvent; +import net.minestom.server.network.packet.client.ClientPacket; +import org.jetbrains.annotations.NotNull; + +public class PlayerPacketEvent implements PlayerEvent, CancellableEvent { + + private final Player player; + private final ClientPacket packet; + private boolean cancelled; + + public PlayerPacketEvent(Player player, ClientPacket packet) { + this.player = player; + this.packet = packet; + } + + @Override + public @NotNull Player getPlayer() { + return player; + } + + public @NotNull ClientPacket getPacket() { + return packet; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } +} diff --git a/src/main/java/net/minestom/server/listener/manager/PacketListenerManager.java b/src/main/java/net/minestom/server/listener/manager/PacketListenerManager.java index 72f944ca0..48b0bced1 100644 --- a/src/main/java/net/minestom/server/listener/manager/PacketListenerManager.java +++ b/src/main/java/net/minestom/server/listener/manager/PacketListenerManager.java @@ -2,6 +2,8 @@ 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.player.PlayerPacketEvent; import net.minestom.server.listener.*; import net.minestom.server.network.ConnectionManager; import net.minestom.server.network.packet.client.ClientPlayPacket; @@ -74,13 +76,23 @@ public final class PacketListenerManager { LOGGER.warn("Packet " + clazz + " does not have any default listener! (The issue comes from Minestom)"); } - final PacketController packetController = new PacketController(); - for (ClientPacketConsumer clientPacketConsumer : CONNECTION_MANAGER.getReceivePacketConsumers()) { - clientPacketConsumer.accept(player, packetController, packet); + // TODO remove legacy + { + final PacketController packetController = new PacketController(); + for (ClientPacketConsumer clientPacketConsumer : CONNECTION_MANAGER.getReceivePacketConsumers()) { + clientPacketConsumer.accept(player, packetController, packet); + } + + if (packetController.isCancel()) + return; } - if (packetController.isCancel()) + // Event + PlayerPacketEvent playerPacketEvent = new PlayerPacketEvent(player, packet); + EventDispatcher.call(playerPacketEvent); + if (playerPacketEvent.isCancelled()) { return; + } // Finally execute the listener if (packetListenerConsumer != null) {