From 647bb39e7c75163516a2d9e834359d37a5165001 Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Wed, 29 Jul 2020 22:38:46 +0200 Subject: [PATCH] Change packet listener to ConnectionManager#onPacketReceive --- src/main/java/fr/themode/demo/PlayerInit.java | 14 ++++++---- .../manager/PacketListenerManager.java | 2 +- .../server/network/ConnectionManager.java | 28 +++++++++++++------ 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/main/java/fr/themode/demo/PlayerInit.java b/src/main/java/fr/themode/demo/PlayerInit.java index fd8ffaa1a..fdadd3b2f 100644 --- a/src/main/java/fr/themode/demo/PlayerInit.java +++ b/src/main/java/fr/themode/demo/PlayerInit.java @@ -6,7 +6,9 @@ import net.minestom.server.MinecraftServer; import net.minestom.server.benchmark.BenchmarkManager; import net.minestom.server.benchmark.ThreadResult; import net.minestom.server.chat.ChatColor; +import net.minestom.server.chat.ChatHoverEvent; import net.minestom.server.chat.ColoredText; +import net.minestom.server.chat.RichMessage; import net.minestom.server.entity.*; import net.minestom.server.entity.damage.DamageType; import net.minestom.server.entity.fakeplayer.FakePlayer; @@ -32,7 +34,6 @@ import net.minestom.server.utils.MathUtils; import net.minestom.server.utils.Position; import net.minestom.server.utils.Vector; import net.minestom.server.utils.time.TimeUnit; -import net.minestom.server.utils.time.UpdateOption; import net.minestom.server.world.DimensionType; import java.util.Map; @@ -51,7 +52,7 @@ public class PlayerInit { //instanceContainer = MinecraftServer.getInstanceManager().createInstanceContainer(storageFolder); instanceContainer = MinecraftServer.getInstanceManager().createInstanceContainer(DimensionType.OVERWORLD); instanceContainer.enableAutoChunkLoad(true); - instanceContainer.setChunkGenerator(chunkGeneratorDemo); + instanceContainer.setChunkGenerator(noiseTestGenerator); netherTest = MinecraftServer.getInstanceManager().createInstanceContainer(DimensionType.NETHER); netherTest.enableAutoChunkLoad(true); @@ -109,7 +110,7 @@ public class PlayerInit { } }).repeat(10, TimeUnit.TICK).buildTask(); - connectionManager.addPacketConsumer((player, packetController, packet) -> { + connectionManager.onPacketReceive((player, packetController, packet) -> { // Listen to all received packet //System.out.println("PACKET: "+packet.getClass().getSimpleName()); packetController.setCancel(false); @@ -230,13 +231,13 @@ public class PlayerInit { }); player.addEventCallback(PlayerSpawnEvent.class, event -> { - player.setGameMode(GameMode.SURVIVAL); + player.setGameMode(GameMode.CREATIVE); player.teleport(new Position(0, 41f, 0)); //player.setHeldItemSlot((byte) 5); player.setGlowing(true); - //player.getInventory().addItemStack(new ItemStack(Material.STONE, (byte) 127)); + player.getInventory().addItemStack(new ItemStack(Material.STONE, (byte) 127)); /*for (int i = 0; i < 9; i++) { player.getInventory().setItemStack(i, new ItemStack(Material.STONE, (byte) 127)); }*/ @@ -293,6 +294,9 @@ public class PlayerInit { WorldBorder worldBorder = instance.getWorldBorder(); worldBorder.setDiameter(30); + RichMessage richMessage = RichMessage.of(ColoredText.of(ChatColor.RED + "test item")); + richMessage.setHoverEvent(ChatHoverEvent.showItem(new ItemStack(Material.DIAMOND, (byte) 1))); + player.sendMessage(richMessage); //EntityBoat entityBoat = new EntityBoat(player.getPosition()); //entityBoat.setInstance(player.getInstance()); 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 a69932f3d..bef350286 100644 --- a/src/main/java/net/minestom/server/listener/manager/PacketListenerManager.java +++ b/src/main/java/net/minestom/server/listener/manager/PacketListenerManager.java @@ -60,7 +60,7 @@ public class PacketListenerManager { final PacketController packetController = new PacketController(packetListenerConsumer); - for (PacketConsumer packetConsumer : CONNECTION_MANAGER.getPacketConsumers()) { + for (PacketConsumer packetConsumer : CONNECTION_MANAGER.getReceivePacketConsumers()) { packetConsumer.accept(player, packetController, packet); } diff --git a/src/main/java/net/minestom/server/network/ConnectionManager.java b/src/main/java/net/minestom/server/network/ConnectionManager.java index 2e1bf7e2d..3b5d46e7b 100644 --- a/src/main/java/net/minestom/server/network/ConnectionManager.java +++ b/src/main/java/net/minestom/server/network/ConnectionManager.java @@ -18,19 +18,26 @@ public final class ConnectionManager { private Set players = new CopyOnWriteArraySet<>(); private Map connectionPlayerMap = Collections.synchronizedMap(new HashMap<>()); - private List packetConsumers = new CopyOnWriteArrayList<>(); + // All the consumers to call once a packet is received + private List receivePacketConsumers = new CopyOnWriteArrayList<>(); + // The uuid provider once a player login private UuidProvider uuidProvider; + // The consumers to call once a player connect, mostly used to init events private List> playerInitializations = new CopyOnWriteArrayList<>(); /** + * Get the {@link Player} linked to a {@link PlayerConnection} + * * @param connection the player connection - * @return the {@link Player} linked to the conneciton + * @return the player linked to the connection */ public Player getPlayer(PlayerConnection connection) { return connectionPlayerMap.get(connection); } /** + * Get all online players + * * @return an unmodifiable collection containing all the online players */ public Collection getOnlinePlayers() { @@ -38,6 +45,10 @@ public final class ConnectionManager { } /** + * Get the first player which validate {@link String#equalsIgnoreCase(String)} + *

+ * This can cause issue if two or more players have the same username + * * @param username the player username (ignoreCase) * @return the first player who validate the username condition */ @@ -122,27 +133,28 @@ public final class ConnectionManager { } /** - * Those are all the listeners which are called for each packet received + * Get all the listeners which are called for each packet received * * @return an unmodifiable list of packet's consumers */ - public List getPacketConsumers() { - return Collections.unmodifiableList(packetConsumers); + public List getReceivePacketConsumers() { + return Collections.unmodifiableList(receivePacketConsumers); } /** - * Add a new packet listener + * Add a consumer to call once a packet is received * * @param packetConsumer the packet consumer */ - public void addPacketConsumer(PacketConsumer packetConsumer) { - this.packetConsumers.add(packetConsumer); + public void onPacketReceive(PacketConsumer packetConsumer) { + this.receivePacketConsumers.add(packetConsumer); } /** * Shouldn't be override if already defined * * @param uuidProvider the new player connection uuid provider + * @see #getPlayerConnectionUuid(PlayerConnection, String) */ public void setUuidProvider(UuidProvider uuidProvider) { this.uuidProvider = uuidProvider;