diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 19bf50b24..fa1be0e16 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -1318,9 +1318,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 5918d3aaa..d5aefe60d 100644 --- a/src/main/java/net/minestom/server/instance/InstanceContainer.java +++ b/src/main/java/net/minestom/server/instance/InstanceContainer.java @@ -637,10 +637,7 @@ public class InstanceContainer extends Instance { * @param block the new block */ private void sendBlockChange(@NotNull Chunk chunk, @NotNull BlockPosition blockPosition, @NotNull Block block) { - BlockChangePacket blockChangePacket = new BlockChangePacket(); - blockChangePacket.blockPosition = blockPosition; - blockChangePacket.blockStateId = block.stateId(); - chunk.sendPacketToViewers(blockChangePacket); + chunk.sendPacketToViewers(new BlockChangePacket(blockPosition, block.stateId())); } @Override diff --git a/src/main/java/net/minestom/server/listener/BlockPlacementListener.java b/src/main/java/net/minestom/server/listener/BlockPlacementListener.java index d37d63027..a77dcb04d 100644 --- a/src/main/java/net/minestom/server/listener/BlockPlacementListener.java +++ b/src/main/java/net/minestom/server/listener/BlockPlacementListener.java @@ -89,10 +89,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.stateId(); - player.getPlayerConnection().sendPacket(blockChangePacket); + player.getPlayerConnection().sendPacket(new BlockChangePacket(blockPosition, Block.AIR.stateId())); } return; } diff --git a/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java b/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java index b1f1f0e28..cc7770921 100644 --- a/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java +++ b/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java @@ -179,13 +179,6 @@ public class PlayerDiggingListener { */ private static void sendAcknowledgePacket(@NotNull Player player, @NotNull BlockPosition blockPosition, Block block, @NotNull ClientPlayerDiggingPacket.Status status, boolean success) { - AcknowledgePlayerDiggingPacket acknowledgePlayerDiggingPacket = new AcknowledgePlayerDiggingPacket(); - acknowledgePlayerDiggingPacket.blockPosition = blockPosition; - acknowledgePlayerDiggingPacket.blockStateId = block.stateId(); - acknowledgePlayerDiggingPacket.status = status; - acknowledgePlayerDiggingPacket.successful = success; - - player.getPlayerConnection().sendPacket(acknowledgePlayerDiggingPacket); + player.getPlayerConnection().sendPacket(new AcknowledgePlayerDiggingPacket(blockPosition, block.stateId(), 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);