prefer minestom enums over raw packet values

This commit is contained in:
Kieran Wallbanks 2021-03-05 19:59:28 +00:00
parent 94b737812d
commit 35e057a638
8 changed files with 65 additions and 79 deletions

View File

@ -33,7 +33,7 @@ public class AdventurePacketConvertor {
return color.ordinal();
}
public static int getSoundCategoryValue(@NotNull Sound.Source source) {
public static int getSoundSourceValue(@NotNull Sound.Source source) {
return source.ordinal();
}

View File

@ -10,6 +10,7 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.title.Title;
import net.minestom.server.MinecraftServer;
import net.minestom.server.advancements.AdvancementTab;
import net.minestom.server.adventure.AdventurePacketConvertor;
import net.minestom.server.attribute.Attribute;
import net.minestom.server.adventure.AdventureUtils;
import net.minestom.server.adventure.Localizable;
@ -804,7 +805,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable {
@Override
public void sendMessage(@NotNull Identity source, @NotNull Component message, @NotNull MessageType type) {
ChatMessagePacket chatMessagePacket = new ChatMessagePacket(MinecraftServer.getSerializationManager().serialize(message, this), type, source.uuid());
ChatMessagePacket chatMessagePacket = new ChatMessagePacket(MinecraftServer.getSerializationManager().serialize(message, this), ChatMessagePacket.Position.fromMessageType(type), source.uuid());
playerConnection.sendPacket(chatMessagePacket);
}
@ -835,7 +836,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable {
public void playSound(@NotNull Sound sound, @NotNull SoundCategory soundCategory, int x, int y, int z, float volume, float pitch) {
SoundEffectPacket soundEffectPacket = new SoundEffectPacket();
soundEffectPacket.soundId = sound.getId();
soundEffectPacket.soundCategory = soundCategory.ordinal();
soundEffectPacket.soundSource = soundCategory.asSource();
soundEffectPacket.x = x;
soundEffectPacket.y = y;
soundEffectPacket.z = z;
@ -871,7 +872,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable {
public void playSound(@NotNull String identifier, @NotNull SoundCategory soundCategory, int x, int y, int z, float volume, float pitch) {
NamedSoundEffectPacket namedSoundEffectPacket = new NamedSoundEffectPacket();
namedSoundEffectPacket.soundName = identifier;
namedSoundEffectPacket.soundCategory = soundCategory.ordinal();
namedSoundEffectPacket.soundSource = soundCategory.asSource();
namedSoundEffectPacket.x = x;
namedSoundEffectPacket.y = y;
namedSoundEffectPacket.z = z;
@ -923,7 +924,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable {
if (minestomSound == null) {
NamedSoundEffectPacket packet = new NamedSoundEffectPacket();
packet.soundName = sound.name().asString();
packet.soundCategory = sound.source().ordinal();
packet.soundSource = sound.source();
packet.x = (int) x;
packet.y = (int) y;
packet.z = (int) z;
@ -933,7 +934,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable {
} else {
SoundEffectPacket packet = new SoundEffectPacket();
packet.soundId = minestomSound.getId();
packet.soundCategory = sound.source().ordinal();
packet.soundSource = sound.source();
packet.x = (int) x;
packet.y = (int) y;
packet.z = (int) z;
@ -950,7 +951,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable {
if (stop.source() != null) {
packet.flags |= 0x1;
packet.source = stop.source().ordinal();
packet.source = AdventurePacketConvertor.getSoundSourceValue(stop.source());
}
if (stop.sound() != null) {

View File

@ -58,7 +58,7 @@ public class ChatMessageListener {
// Send the message with the correct player UUID
ChatMessagePacket chatMessagePacket =
new ChatMessagePacket(jsonMessage, MessageType.CHAT, player.getUuid());
new ChatMessagePacket(jsonMessage, ChatMessagePacket.Position.CHAT, player.getUuid());
PacketUtils.sendGroupedPacket(recipients, chatMessagePacket);
}

View File

@ -172,7 +172,7 @@ public final class ConnectionManager implements ForwardingAudience {
}
private void broadcastJson(@NotNull String json, @NotNull Collection<Player> recipients) {
ChatMessagePacket chatMessagePacket = new ChatMessagePacket(json, MessageType.SYSTEM);
ChatMessagePacket chatMessagePacket = new ChatMessagePacket(json, ChatMessagePacket.Position.SYSTEM_MESSAGE);
PacketUtils.sendGroupedPacket(recipients, chatMessagePacket);
}

View File

@ -1,8 +1,6 @@
package net.minestom.server.network.packet.server.play;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.MessageType;
import net.kyori.adventure.text.Component;
import net.minestom.server.chat.JsonMessage;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
@ -13,66 +11,34 @@ import org.jetbrains.annotations.Nullable;
import java.util.UUID;
/**
* Represents an outgoing chat message packet. Do not use this to send messages above the
* hotbar (the game info position) as it is preferred to use
* {@link TitlePacket} due to <a href="https://bugs.mojang.com/browse/MC-119145">MC-119145</a>.
* Represents an outgoing chat message packet.
*/
public class ChatMessagePacket implements ServerPacket {
private static final UUID NULL_UUID = new UUID(0, 0);
public String message;
public MessageType messageType;
public Position position;
public UUID uuid;
/**
* @deprecated Use {@link #message}
*/
public @Deprecated JsonMessage jsonMessage;
/**
* @deprecated Use {@link #messageType}
*/
public @Deprecated Position position;
@Deprecated
public ChatMessagePacket(String jsonMessage, Position position, UUID uuid) {
this(jsonMessage, position.asMessageType(), uuid);
this.message = jsonMessage;
this.position = position;
this.uuid = uuid;
}
@Deprecated
public ChatMessagePacket(String jsonMessage, Position position) {
this(jsonMessage, position, NULL_UUID);
}
/**
* Constructs a new chat message packet with a zeroed UUID. To send formatted
* messages please use the respective {@link Audience#sendMessage(Component)}
* functions.
*
* @param jsonMessage the raw message payload
* @param messageType the message type
*/
public ChatMessagePacket(String jsonMessage, MessageType messageType) {
this(jsonMessage, messageType, NULL_UUID);
}
/**
* Constructs a new chat message packet. To send formatted messages please use the
* respective {@link Audience#sendMessage(Component)} functions.
*
* @param message the raw message payload
* @param messageType the message type
* @param uuid the sender of the chat message
*/
public ChatMessagePacket(String message, MessageType messageType, UUID uuid) {
this.message = message;
this.messageType = messageType;
this.uuid = uuid;
}
@Override
public void write(@NotNull BinaryWriter writer) {
writer.writeSizedString(jsonMessage != null ? jsonMessage.toString() : message);
writer.writeByte((byte) (position != null ? position.ordinal() : messageType == null ? 3 : messageType.ordinal()));
writer.writeByte((byte) position.ordinal());
writer.writeUuid(uuid);
}
@ -81,10 +47,6 @@ public class ChatMessagePacket implements ServerPacket {
return ServerPacketIdentifier.CHAT_MESSAGE;
}
/**
* @deprecated Use {@link MessageType}
*/
@Deprecated
public enum Position {
CHAT(MessageType.CHAT),
SYSTEM_MESSAGE(MessageType.SYSTEM),
@ -97,13 +59,28 @@ public class ChatMessagePacket implements ServerPacket {
}
/**
* Gets this position as an Adventure message type. Note this will return
* {@code null} for {@link #GAME_INFO} as it is preferred to use
* {@link TitlePacket} due to <a href="https://bugs.mojang.com/browse/MC-119145">MC-119145</a>.
* @return the message type
* Gets the Adventure message type from this position. Note that there is no
* message type for {@link #GAME_INFO}, as Adventure uses the title methods for this.
*
* @return the message type, if any
*/
public @Nullable MessageType asMessageType() {
public @Nullable MessageType getMessageType() {
return this.messageType;
}
/**
* Gets a position from an Adventure message type.
*
* @param messageType the message type
*
* @return the position
*/
public static @NotNull Position fromMessageType(@NotNull MessageType messageType) {
switch (messageType) {
case CHAT: return CHAT;
case SYSTEM: return SYSTEM_MESSAGE;
}
throw new IllegalArgumentException("Cannot get position from message type!");
}
}
}

View File

@ -1,5 +1,8 @@
package net.minestom.server.network.packet.server.play;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.sound.Sound.Source;
import net.minestom.server.adventure.AdventurePacketConvertor;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
import net.minestom.server.sound.SoundCategory;
@ -9,20 +12,20 @@ import org.jetbrains.annotations.NotNull;
public class NamedSoundEffectPacket implements ServerPacket {
public String soundName;
public int soundCategory;
public Source soundSource;
public int x, y, z;
public float volume;
public float pitch;
/**
* @deprecated Use {@link #soundCategory}
* @deprecated Use {@link #soundSource}
*/
@Deprecated public SoundCategory soundCategoryOld;
@Deprecated public SoundCategory soundCategory;
@Override
public void write(@NotNull BinaryWriter writer) {
writer.writeSizedString(soundName);
writer.writeVarInt(soundCategoryOld != null ? soundCategoryOld.ordinal() : soundCategory);
writer.writeVarInt(AdventurePacketConvertor.getSoundSourceValue(soundSource));
writer.writeInt(x * 8);
writer.writeInt(y * 8);
writer.writeInt(z * 8);

View File

@ -13,38 +13,29 @@ import org.jetbrains.annotations.NotNull;
public class SoundEffectPacket implements ServerPacket {
public int soundId;
public int soundCategory;
public Source soundSource;
public int x, y, z;
public float volume;
public float pitch;
/**
* @deprecated Use {@link #soundCategory}
* @deprecated Use {@link #soundSource}
*/
@Deprecated public SoundCategory soundCategoryOld;
@Deprecated public SoundCategory soundCategory;
/**
* @deprecated Use variables
*/
@Deprecated
public static SoundEffectPacket create(SoundCategory category, Sound sound, Position position, float volume, float pitch) {
SoundEffectPacket packet = new SoundEffectPacket();
packet.soundId = sound.getId();
packet.soundCategory = category.ordinal();
// *8 converts to fixed-point representation with 3 bits for fractional part
packet.x = (int) position.getX();
packet.y = (int) position.getY();
packet.z = (int) position.getZ();
packet.volume = volume;
packet.pitch = pitch;
return packet;
return create(category.asSource(), sound, position, volume, pitch);
}
@NotNull
public static SoundEffectPacket create(Source category, Sound sound, Position position, float volume, float pitch) {
SoundEffectPacket packet = new SoundEffectPacket();
packet.soundId = sound.getId();
packet.soundCategory = AdventurePacketConvertor.getSoundCategoryValue(category);
packet.soundSource = category;
// *8 converts to fixed-point representation with 3 bits for fractional part
packet.x = (int) position.getX();
packet.y = (int) position.getY();
@ -56,7 +47,7 @@ public class SoundEffectPacket implements ServerPacket {
@Override
public void write(@NotNull BinaryWriter writer) {
writer.writeVarInt(soundId);
writer.writeVarInt(soundCategoryOld != null ? soundCategoryOld.ordinal() : soundCategory);
writer.writeVarInt(AdventurePacketConvertor.getSoundSourceValue(soundSource));
writer.writeInt(x * 8);
writer.writeInt(y * 8);
writer.writeInt(z * 8);

View File

@ -1,7 +1,12 @@
package net.minestom.server.sound;
import net.kyori.adventure.sound.Sound;
import org.jetbrains.annotations.NotNull;
import static net.kyori.adventure.sound.Sound.*;
/**
* @deprecated Use {@link net.kyori.adventure.sound.Sound.Source}
* @deprecated Use {@link Source}
*/
@Deprecated
public enum SoundCategory {
@ -14,5 +19,14 @@ public enum SoundCategory {
NEUTRAL,
PLAYERS,
AMBIENT,
VOICE
VOICE;
/**
* Gets the Adventure source representing this sound category.
*
* @return the source
*/
public @NotNull Source asSource() {
return Source.values()[this.ordinal()];
}
}