From 9db4ac06ccc5e99823c08b49081db80f74a67317 Mon Sep 17 00:00:00 2001 From: TheMode Date: Thu, 22 Jul 2021 13:01:00 +0200 Subject: [PATCH] Even more inline packets --- .../net/minestom/server/entity/Entity.java | 8 ++---- .../net/minestom/server/entity/Metadata.java | 16 ++--------- .../net/minestom/server/entity/Player.java | 27 ++++++------------- .../server/play/EntityMetaDataPacket.java | 16 ++++++++--- .../server/play/PluginMessagePacket.java | 13 +++++++-- .../server/play/ServerDifficultyPacket.java | 7 ++++- .../server/play/SpawnPositionPacket.java | 12 ++++++--- 7 files changed, 50 insertions(+), 49 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index 92dd013b9..3b35d5311 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -1367,12 +1367,8 @@ public class Entity implements Viewable, Tickable, TagHandler, PermissionHandler * * @return The {@link EntityMetaDataPacket} related to this entity */ - @NotNull - public EntityMetaDataPacket getMetadataPacket() { - EntityMetaDataPacket metaDataPacket = new EntityMetaDataPacket(); - metaDataPacket.entityId = getEntityId(); - metaDataPacket.entries = metadata.getEntries(); - return metaDataPacket; + public @NotNull EntityMetaDataPacket getMetadataPacket() { + return new EntityMetaDataPacket(getEntityId(), metadata.getEntries()); } /** diff --git a/src/main/java/net/minestom/server/entity/Metadata.java b/src/main/java/net/minestom/server/entity/Metadata.java index 0a930886e..1153eaa28 100644 --- a/src/main/java/net/minestom/server/entity/Metadata.java +++ b/src/main/java/net/minestom/server/entity/Metadata.java @@ -2,7 +2,6 @@ package net.minestom.server.entity; import net.kyori.adventure.text.Component; import net.minestom.server.MinecraftServer; -import net.minestom.server.chat.ColoredText; import net.minestom.server.chat.JsonMessage; import net.minestom.server.coordinate.Point; import net.minestom.server.coordinate.Vec; @@ -254,11 +253,7 @@ public class Metadata { } return; } - EntityMetaDataPacket metaDataPacket = new EntityMetaDataPacket(); - metaDataPacket.entityId = this.entity.getEntityId(); - metaDataPacket.entries = Collections.singleton(entry); - - this.entity.sendPacketToViewersAndSelf(metaDataPacket); + this.entity.sendPacketToViewersAndSelf(new EntityMetaDataPacket(entity.getEntityId(), Collections.singleton(entry))); } } @@ -266,7 +261,6 @@ public class Metadata { if (this.notifyAboutChanges == notifyAboutChanges) { return; } - Collection> entries = null; synchronized (this.notNotifiedChanges) { this.notifyAboutChanges = notifyAboutChanges; @@ -278,16 +272,10 @@ public class Metadata { this.notNotifiedChanges.clear(); } } - if (entries == null || this.entity == null || !this.entity.isActive()) { return; } - - EntityMetaDataPacket metaDataPacket = new EntityMetaDataPacket(); - metaDataPacket.entityId = this.entity.getEntityId(); - metaDataPacket.entries = entries; - - this.entity.sendPacketToViewersAndSelf(metaDataPacket); + this.entity.sendPacketToViewersAndSelf(new EntityMetaDataPacket(entity.getEntityId(), entries)); } @NotNull diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index d86f5cc9e..70dbe1d86 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -232,18 +232,11 @@ public class Player extends LivingEntity implements CommandSender, Localizable, playerConnection.sendPacket(joinGamePacket); // Server brand name - { - playerConnection.sendPacket(PluginMessagePacket.getBrandPacket()); - } + playerConnection.sendPacket(PluginMessagePacket.getBrandPacket()); + // Difficulty + playerConnection.sendPacket(new ServerDifficultyPacket(MinecraftServer.getDifficulty(), true)); - ServerDifficultyPacket serverDifficultyPacket = new ServerDifficultyPacket(); - serverDifficultyPacket.difficulty = MinecraftServer.getDifficulty(); - serverDifficultyPacket.locked = true; - playerConnection.sendPacket(serverDifficultyPacket); - - SpawnPositionPacket spawnPositionPacket = new SpawnPositionPacket(); - spawnPositionPacket.position = respawnPoint; - playerConnection.sendPacket(spawnPositionPacket); + playerConnection.sendPacket(new SpawnPositionPacket(respawnPoint, 0)); // Add player to list with spawning skin PlayerSkinInitEvent skinInitEvent = new PlayerSkinInitEvent(this, skin); @@ -291,7 +284,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable, // Tags this.playerConnection.sendPacket(TagsPacket.getRequiredTagsPacket()); - // Some client update + // Some client updates this.playerConnection.sendPacket(getPropertiesPacket()); // Send default properties refreshHealth(); // Heal and send health packet refreshAbilities(); // Send abilities packet @@ -606,11 +599,8 @@ public class Player extends LivingEntity implements CommandSender, Localizable, * @param channel the message channel * @param data the message data */ - public void sendPluginMessage(@NotNull String channel, @NotNull byte[] data) { - PluginMessagePacket pluginMessagePacket = new PluginMessagePacket(); - pluginMessagePacket.channel = channel; - pluginMessagePacket.data = data; - playerConnection.sendPacket(pluginMessagePacket); + public void sendPluginMessage(@NotNull String channel, byte @NotNull [] data) { + playerConnection.sendPacket(new PluginMessagePacket(channel, data)); } /** @@ -622,8 +612,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable, * @param message the message */ public void sendPluginMessage(@NotNull String channel, @NotNull String message) { - final byte[] bytes = message.getBytes(StandardCharsets.UTF_8); - sendPluginMessage(channel, bytes); + sendPluginMessage(channel, message.getBytes(StandardCharsets.UTF_8)); } /** diff --git a/src/main/java/net/minestom/server/network/packet/server/play/EntityMetaDataPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/EntityMetaDataPacket.java index 158ee1baf..57fa2eeda 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/EntityMetaDataPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/EntityMetaDataPacket.java @@ -8,6 +8,7 @@ import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.Collection; +import java.util.Collections; import java.util.LinkedList; public class EntityMetaDataPacket implements ServerPacket { @@ -15,13 +16,20 @@ public class EntityMetaDataPacket implements ServerPacket { public int entityId; public Collection> entries; - public EntityMetaDataPacket() {} + public EntityMetaDataPacket(int entityId, Collection> entries) { + this.entityId = entityId; + this.entries = entries; + } + + public EntityMetaDataPacket() { + this(0, Collections.emptyList()); + } @Override public void write(@NotNull BinaryWriter writer) { writer.writeVarInt(entityId); - if(entries != null) { + if (entries != null) { // Write all the fields for (Metadata.Entry entry : entries) { entry.write(writer); @@ -36,10 +44,10 @@ public class EntityMetaDataPacket implements ServerPacket { entityId = reader.readVarInt(); entries = new LinkedList<>(); - while(true) { + while (true) { byte index = reader.readByte(); - if(index == (byte) 0xFF) { // reached the end + if (index == (byte) 0xFF) { // reached the end break; } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/PluginMessagePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/PluginMessagePacket.java index 634371142..32efda9f9 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/PluginMessagePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/PluginMessagePacket.java @@ -9,8 +9,17 @@ import org.jetbrains.annotations.NotNull; public class PluginMessagePacket implements ServerPacket { - public String channel = "none"; - public byte[] data = new byte[0]; + public String channel; + public byte[] data; + + public PluginMessagePacket(String channel, byte[] data) { + this.channel = channel; + this.data = data; + } + + public PluginMessagePacket() { + this("none", new byte[0]); + } @Override public void write(@NotNull BinaryWriter writer) { diff --git a/src/main/java/net/minestom/server/network/packet/server/play/ServerDifficultyPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/ServerDifficultyPacket.java index 2f288881e..1752e686b 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/ServerDifficultyPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/ServerDifficultyPacket.java @@ -12,8 +12,13 @@ public class ServerDifficultyPacket implements ServerPacket { public Difficulty difficulty; public boolean locked; + public ServerDifficultyPacket(Difficulty difficulty, boolean locked) { + this.difficulty = difficulty; + this.locked = locked; + } + public ServerDifficultyPacket() { - difficulty = Difficulty.NORMAL; + this(Difficulty.NORMAL, false); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/SpawnPositionPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/SpawnPositionPacket.java index 286eb6c72..a26e3748d 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/SpawnPositionPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/SpawnPositionPacket.java @@ -1,19 +1,25 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.coordinate.Point; +import net.minestom.server.coordinate.Vec; 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 net.minestom.server.coordinate.Point; -import net.minestom.server.coordinate.Vec; import org.jetbrains.annotations.NotNull; public class SpawnPositionPacket implements ServerPacket { - public Point position = Vec.ZERO; + public Point position; public float angle; + public SpawnPositionPacket(Point position, float angle) { + this.position = position; + this.angle = angle; + } + public SpawnPositionPacket() { + this(Vec.ZERO, 0); } @Override