Change packet listener to ConnectionManager#onPacketReceive

This commit is contained in:
Felix Cravic 2020-07-29 22:38:46 +02:00
parent da641fd54b
commit 647bb39e7c
3 changed files with 30 additions and 14 deletions

View File

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

View File

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

View File

@ -18,19 +18,26 @@ public final class ConnectionManager {
private Set<Player> players = new CopyOnWriteArraySet<>();
private Map<PlayerConnection, Player> connectionPlayerMap = Collections.synchronizedMap(new HashMap<>());
private List<PacketConsumer> packetConsumers = new CopyOnWriteArrayList<>();
// All the consumers to call once a packet is received
private List<PacketConsumer> 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<Consumer<Player>> 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<Player> getOnlinePlayers() {
@ -38,6 +45,10 @@ public final class ConnectionManager {
}
/**
* Get the first player which validate {@link String#equalsIgnoreCase(String)}
* <p>
* 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<PacketConsumer> getPacketConsumers() {
return Collections.unmodifiableList(packetConsumers);
public List<PacketConsumer> 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;