Added a packet listener

This commit is contained in:
Felix Cravic 2020-04-22 18:48:27 +02:00
parent bd8ccd427c
commit 26276af29e
5 changed files with 52 additions and 0 deletions

View File

@ -85,6 +85,11 @@ public class PlayerInit {
responseData.setFavicon("data:image/png;base64,<data>");
});
connectionManager.setPacketConsumer((player, packet) -> {
// Listen to all received packet
return false;
});
connectionManager.setPlayerInitialization(player -> {
player.setEventCallback(AttackEvent.class, event -> {
Entity entity = event.getTarget();

View File

@ -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);
}

View File

@ -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<Class<? extends ClientPlayPacket>, BiConsumer<? extends ClientPlayPacket, Player>> listeners = new ConcurrentHashMap<>();
public PacketListenerManager() {
@ -39,6 +43,13 @@ public class PacketListenerManager {
}
public <T extends ClientPlayPacket> void process(T packet, Player player) {
PacketConsumer packetConsumer = connectionManager.getPacketConsumer();
if (packetConsumer != null) {
boolean cancel = packetConsumer.accept(player, packet);
if (cancel)
return;
}
BiConsumer<T, Player> biConsumer = (BiConsumer<T, Player>) listeners.get(packet.getClass());
if (biConsumer == null) {
System.err.println("Packet " + packet.getClass() + " does not have any listener!");

View File

@ -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<PlayerConnection, Player> connectionPlayerMap = Collections.synchronizedMap(new HashMap<>());
private ResponseDataConsumer responseDataConsumer;
private PacketConsumer packetConsumer;
private Consumer<Player> 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<Player> getPlayerInitialization() {
return playerInitialization;
}

View File

@ -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;