Even more inline packets

This commit is contained in:
TheMode 2021-07-22 13:01:00 +02:00
parent 081266775c
commit 9db4ac06cc
7 changed files with 50 additions and 49 deletions

View File

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

View File

@ -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<Entry<?>> 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

View File

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

View File

@ -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<Metadata.Entry<?>> entries;
public EntityMetaDataPacket() {}
public EntityMetaDataPacket(int entityId, Collection<Metadata.Entry<?>> 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;
}

View File

@ -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) {

View File

@ -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

View File

@ -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