diff --git a/src/main/java/fr/themode/demo/PlayerInit.java b/src/main/java/fr/themode/demo/PlayerInit.java index 2404ed036..36a70367f 100644 --- a/src/main/java/fr/themode/demo/PlayerInit.java +++ b/src/main/java/fr/themode/demo/PlayerInit.java @@ -85,6 +85,11 @@ public class PlayerInit { responseData.setFavicon("data:image/png;base64,"); }); + connectionManager.setPacketConsumer((player, packet) -> { + // Listen to all received packet + return false; + }); + connectionManager.setPlayerInitialization(player -> { player.setEventCallback(AttackEvent.class, event -> { Entity entity = event.getTarget(); diff --git a/src/main/java/fr/themode/minestom/listener/manager/PacketConsumer.java b/src/main/java/fr/themode/minestom/listener/manager/PacketConsumer.java new file mode 100644 index 000000000..47621c820 --- /dev/null +++ b/src/main/java/fr/themode/minestom/listener/manager/PacketConsumer.java @@ -0,0 +1,10 @@ +package fr.themode.minestom.listener.manager; + +import fr.themode.minestom.entity.Player; +import fr.themode.minestom.net.packet.client.ClientPacket; + +@FunctionalInterface +public interface PacketConsumer { + // Cancel the packet if return true + boolean accept(Player player, ClientPacket packet); +} diff --git a/src/main/java/fr/themode/minestom/listener/manager/PacketListenerManager.java b/src/main/java/fr/themode/minestom/listener/manager/PacketListenerManager.java index 95ae982a3..cebba5cd3 100644 --- a/src/main/java/fr/themode/minestom/listener/manager/PacketListenerManager.java +++ b/src/main/java/fr/themode/minestom/listener/manager/PacketListenerManager.java @@ -1,7 +1,9 @@ package fr.themode.minestom.listener.manager; +import fr.themode.minestom.MinecraftServer; import fr.themode.minestom.entity.Player; import fr.themode.minestom.listener.*; +import fr.themode.minestom.net.ConnectionManager; import fr.themode.minestom.net.packet.client.ClientPlayPacket; import fr.themode.minestom.net.packet.client.play.*; @@ -11,6 +13,8 @@ import java.util.function.BiConsumer; public class PacketListenerManager { + private static ConnectionManager connectionManager = MinecraftServer.getConnectionManager(); + private Map, BiConsumer> listeners = new ConcurrentHashMap<>(); public PacketListenerManager() { @@ -39,6 +43,13 @@ public class PacketListenerManager { } public void process(T packet, Player player) { + PacketConsumer packetConsumer = connectionManager.getPacketConsumer(); + if (packetConsumer != null) { + boolean cancel = packetConsumer.accept(player, packet); + if (cancel) + return; + } + BiConsumer biConsumer = (BiConsumer) listeners.get(packet.getClass()); if (biConsumer == null) { System.err.println("Packet " + packet.getClass() + " does not have any listener!"); diff --git a/src/main/java/fr/themode/minestom/net/ConnectionManager.java b/src/main/java/fr/themode/minestom/net/ConnectionManager.java index de62ec54b..25394329d 100644 --- a/src/main/java/fr/themode/minestom/net/ConnectionManager.java +++ b/src/main/java/fr/themode/minestom/net/ConnectionManager.java @@ -1,6 +1,7 @@ package fr.themode.minestom.net; import fr.themode.minestom.entity.Player; +import fr.themode.minestom.listener.manager.PacketConsumer; import fr.themode.minestom.net.player.PlayerConnection; import fr.themode.minestom.ping.ResponseDataConsumer; @@ -15,6 +16,7 @@ public class ConnectionManager { private Map connectionPlayerMap = Collections.synchronizedMap(new HashMap<>()); private ResponseDataConsumer responseDataConsumer; + private PacketConsumer packetConsumer; private Consumer playerInitialization; public Player getPlayer(PlayerConnection connection) { @@ -57,6 +59,14 @@ public class ConnectionManager { this.responseDataConsumer = responseDataConsumer; } + public PacketConsumer getPacketConsumer() { + return packetConsumer; + } + + public void setPacketConsumer(PacketConsumer packetConsumer) { + this.packetConsumer = packetConsumer; + } + public Consumer getPlayerInitialization() { return playerInitialization; } diff --git a/src/main/java/fr/themode/minestom/utils/SerializerUtils.java b/src/main/java/fr/themode/minestom/utils/SerializerUtils.java index 08bb5514c..01a050bd9 100644 --- a/src/main/java/fr/themode/minestom/utils/SerializerUtils.java +++ b/src/main/java/fr/themode/minestom/utils/SerializerUtils.java @@ -2,6 +2,22 @@ package fr.themode.minestom.utils; public class SerializerUtils { + public static byte[] intToBytes(int value) { + byte[] result = new byte[4]; + result[0] = (byte) (value >> 24); + result[1] = (byte) (value >> 16); + result[2] = (byte) (value >> 8); + result[3] = (byte) (value >> 0); + return result; + } + + public static int bytesToInt(byte[] value) { + return ((value[0] & 0xFF) << 24) | + ((value[1] & 0xFF) << 16) | + ((value[2] & 0xFF) << 8) | + ((value[3] & 0xFF) << 0); + } + public static int chunkCoordToIndex(byte x, byte y, byte z) { short index = (short) (x & 0x000F); index |= (y << 4) & 0x0FF0;