From 1f2dc0cd9fed757668c03ee52e01cbe465d526b9 Mon Sep 17 00:00:00 2001 From: TheMode Date: Mon, 21 Jun 2021 15:01:50 +0200 Subject: [PATCH] Add some inline packet initialization --- .../java/net/minestom/server/entity/Player.java | 4 +--- .../server/instance/InstanceContainer.java | 5 +---- .../server/listener/BlockPlacementListener.java | 5 +---- .../server/listener/PlayerDiggingListener.java | 9 +-------- .../play/AcknowledgePlayerDiggingPacket.java | 16 +++++++++++++--- .../packet/server/play/ActionBarPacket.java | 9 +++++---- .../packet/server/play/AttachEntityPacket.java | 15 ++++++++++++++- .../packet/server/play/BlockChangePacket.java | 7 ++++++- .../network/packet/server/play/CameraPacket.java | 13 ++++++++++++- .../packet/server/play/ChatMessagePacket.java | 10 ++++------ 10 files changed, 58 insertions(+), 35 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 873decdb6..0b875b1fd 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -1379,9 +1379,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable, * @param entity the entity to spectate */ public void spectate(@NotNull Entity entity) { - CameraPacket cameraPacket = new CameraPacket(); - cameraPacket.cameraId = entity.getEntityId(); - playerConnection.sendPacket(cameraPacket); + playerConnection.sendPacket(new CameraPacket(entity)); } /** diff --git a/src/main/java/net/minestom/server/instance/InstanceContainer.java b/src/main/java/net/minestom/server/instance/InstanceContainer.java index aa3451643..d8a7d5fe2 100644 --- a/src/main/java/net/minestom/server/instance/InstanceContainer.java +++ b/src/main/java/net/minestom/server/instance/InstanceContainer.java @@ -750,10 +750,7 @@ public class InstanceContainer extends Instance { * @param blockStateId the new state of the block */ private void sendBlockChange(@NotNull Chunk chunk, @NotNull BlockPosition blockPosition, short blockStateId) { - BlockChangePacket blockChangePacket = new BlockChangePacket(); - blockChangePacket.blockPosition = blockPosition; - blockChangePacket.blockStateId = blockStateId; - chunk.sendPacketToViewers(blockChangePacket); + chunk.sendPacketToViewers(new BlockChangePacket(blockPosition, blockStateId)); } @Override diff --git a/src/main/java/net/minestom/server/listener/BlockPlacementListener.java b/src/main/java/net/minestom/server/listener/BlockPlacementListener.java index 79c2d4cd0..6d60411db 100644 --- a/src/main/java/net/minestom/server/listener/BlockPlacementListener.java +++ b/src/main/java/net/minestom/server/listener/BlockPlacementListener.java @@ -104,10 +104,7 @@ public class BlockPlacementListener { //Send a block change with AIR as block to keep the client in sync, //using refreshChunk results in the client not being in sync //after rapid invalid block placements - BlockChangePacket blockChangePacket = new BlockChangePacket(); - blockChangePacket.blockPosition = blockPosition; - blockChangePacket.blockStateId = Block.AIR.getBlockId(); - player.getPlayerConnection().sendPacket(blockChangePacket); + player.getPlayerConnection().sendPacket(new BlockChangePacket(blockPosition, Block.AIR.getBlockId())); } return; } diff --git a/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java b/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java index 3c04ee9e4..a92f6f59a 100644 --- a/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java +++ b/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java @@ -263,13 +263,6 @@ public class PlayerDiggingListener { */ private static void sendAcknowledgePacket(@NotNull Player player, @NotNull BlockPosition blockPosition, int blockStateId, @NotNull ClientPlayerDiggingPacket.Status status, boolean success) { - AcknowledgePlayerDiggingPacket acknowledgePlayerDiggingPacket = new AcknowledgePlayerDiggingPacket(); - acknowledgePlayerDiggingPacket.blockPosition = blockPosition; - acknowledgePlayerDiggingPacket.blockStateId = blockStateId; - acknowledgePlayerDiggingPacket.status = status; - acknowledgePlayerDiggingPacket.successful = success; - - player.getPlayerConnection().sendPacket(acknowledgePlayerDiggingPacket); + player.getPlayerConnection().sendPacket(new AcknowledgePlayerDiggingPacket(blockPosition, blockStateId, status, success)); } - } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/AcknowledgePlayerDiggingPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/AcknowledgePlayerDiggingPacket.java index da9cc6d82..36e25f056 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/AcknowledgePlayerDiggingPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/AcknowledgePlayerDiggingPacket.java @@ -10,12 +10,22 @@ import org.jetbrains.annotations.NotNull; public class AcknowledgePlayerDiggingPacket implements ServerPacket { - public BlockPosition blockPosition = new BlockPosition(0,0,0); + public BlockPosition blockPosition; public int blockStateId; - public ClientPlayerDiggingPacket.Status status = ClientPlayerDiggingPacket.Status.STARTED_DIGGING; + public ClientPlayerDiggingPacket.Status status; public boolean successful; - public AcknowledgePlayerDiggingPacket() {} + public AcknowledgePlayerDiggingPacket(@NotNull BlockPosition blockPosition, int blockStateId, + @NotNull ClientPlayerDiggingPacket.Status status, boolean success) { + this.blockPosition = blockPosition; + this.blockStateId = blockStateId; + this.status = status; + this.successful = success; + } + + public AcknowledgePlayerDiggingPacket() { + this(new BlockPosition(0, 0, 0), 0, ClientPlayerDiggingPacket.Status.STARTED_DIGGING, false); + } @Override public void write(@NotNull BinaryWriter writer) { diff --git a/src/main/java/net/minestom/server/network/packet/server/play/ActionBarPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/ActionBarPacket.java index e47f43d10..8c12cc9e6 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/ActionBarPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/ActionBarPacket.java @@ -9,13 +9,14 @@ import org.jetbrains.annotations.NotNull; public class ActionBarPacket implements ServerPacket { - public Component actionBarText = Component.empty(); + public Component actionBarText; - public ActionBarPacket() { + public ActionBarPacket(@NotNull Component actionBarText) { + this.actionBarText = actionBarText; } - public ActionBarPacket(Component actionBarText) { - this.actionBarText = actionBarText; + public ActionBarPacket() { + this(Component.empty()); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/AttachEntityPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/AttachEntityPacket.java index 8f1aacce4..66b6a1234 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/AttachEntityPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/AttachEntityPacket.java @@ -1,17 +1,30 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.entity.Entity; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.utils.binary.BinaryReader; import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; public class AttachEntityPacket implements ServerPacket { public int attachedEntityId; public int holdingEntityId; // Or -1 to detach - public AttachEntityPacket() {} + public AttachEntityPacket(int attachedEntityId, int holdingEntityId) { + this.attachedEntityId = attachedEntityId; + this.holdingEntityId = holdingEntityId; + } + + public AttachEntityPacket(@NotNull Entity attachedEntity, @Nullable Entity holdingEntity) { + this(attachedEntity.getEntityId(), holdingEntity != null ? holdingEntity.getEntityId() : -1); + } + + public AttachEntityPacket() { + this(0, 0); + } @Override public void write(@NotNull BinaryWriter writer) { diff --git a/src/main/java/net/minestom/server/network/packet/server/play/BlockChangePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/BlockChangePacket.java index 50ffe2215..cb786e72b 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/BlockChangePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/BlockChangePacket.java @@ -12,8 +12,13 @@ public class BlockChangePacket implements ServerPacket { public BlockPosition blockPosition; public int blockStateId; + public BlockChangePacket(BlockPosition blockPosition, int blockStateId) { + this.blockPosition = blockPosition; + this.blockStateId = blockStateId; + } + public BlockChangePacket() { - blockPosition = new BlockPosition(0,0,0); + this(new BlockPosition(0, 0, 0), 0); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/CameraPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/CameraPacket.java index 980224da2..87de08693 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/CameraPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/CameraPacket.java @@ -1,5 +1,6 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.entity.Entity; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.utils.binary.BinaryReader; @@ -10,7 +11,17 @@ public class CameraPacket implements ServerPacket { public int cameraId; - public CameraPacket() {} + public CameraPacket(int cameraId) { + this.cameraId = cameraId; + } + + public CameraPacket(@NotNull Entity camera) { + this(camera.getEntityId()); + } + + public CameraPacket() { + this(0); + } @Override public void write(@NotNull BinaryWriter writer) { diff --git a/src/main/java/net/minestom/server/network/packet/server/play/ChatMessagePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/ChatMessagePacket.java index bd9599a41..85b57f270 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/ChatMessagePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/ChatMessagePacket.java @@ -26,18 +26,16 @@ public class ChatMessagePacket implements ComponentHoldingServerPacket { public ChatPosition position; public UUID uuid; - public ChatMessagePacket() { - this.message = Component.empty(); - this.position = ChatPosition.SYSTEM_MESSAGE; - this.uuid = NULL_UUID; - } - public ChatMessagePacket(@NotNull Component message, @NotNull ChatPosition position, @Nullable UUID uuid) { this.message = message; this.position = position; this.uuid = Objects.requireNonNullElse(uuid, NULL_UUID); } + public ChatMessagePacket() { + this(Component.empty(), ChatPosition.SYSTEM_MESSAGE, NULL_UUID); + } + @Override public void write(@NotNull BinaryWriter writer) { writer.writeComponent(message);