Cleanup Messenger, fix uuid

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2022-05-10 10:17:06 +02:00
parent 7d930ee28d
commit e4c1c9fe8d

View File

@ -15,13 +15,13 @@ import java.util.UUID;
/** /**
* Utility class to handle client chat settings. * Utility class to handle client chat settings.
*/ */
public class Messenger { public final class Messenger {
/** /**
* The message sent to the client if they send a chat message but it is rejected by the server. * The message sent to the client if they send a chat message but it is rejected by the server.
*/ */
public static final Component CANNOT_SEND_MESSAGE = Component.translatable("chat.cannotSend", NamedTextColor.RED); public static final Component CANNOT_SEND_MESSAGE = Component.translatable("chat.cannotSend", NamedTextColor.RED);
private static final UUID NO_SENDER = new UUID(0, 0);
private static final ChatMessagePacket CANNOT_SEND_PACKET = new ChatMessagePacket(CANNOT_SEND_MESSAGE, ChatPosition.SYSTEM_MESSAGE, null); private static final ChatMessagePacket CANNOT_SEND_PACKET = new ChatMessagePacket(CANNOT_SEND_MESSAGE, ChatPosition.SYSTEM_MESSAGE, NO_SENDER);
/** /**
* Sends a message to a player, respecting their chat settings. * Sends a message to a player, respecting their chat settings.
@ -34,7 +34,7 @@ public class Messenger {
*/ */
public static boolean sendMessage(@NotNull Player player, @NotNull Component message, @NotNull ChatPosition position, @Nullable UUID uuid) { public static boolean sendMessage(@NotNull Player player, @NotNull Component message, @NotNull ChatPosition position, @Nullable UUID uuid) {
if (getChatMessageType(player).accepts(position)) { if (getChatMessageType(player).accepts(position)) {
player.sendPacket(new ChatMessagePacket(message, position, uuid)); player.sendPacket(new ChatMessagePacket(message, position, Objects.requireNonNullElse(uuid, NO_SENDER)));
return true; return true;
} }
return false; return false;
@ -50,7 +50,7 @@ public class Messenger {
*/ */
public static void sendMessage(@NotNull Collection<Player> players, @NotNull Component message, public static void sendMessage(@NotNull Collection<Player> players, @NotNull Component message,
@NotNull ChatPosition position, @Nullable UUID uuid) { @NotNull ChatPosition position, @Nullable UUID uuid) {
PacketUtils.sendGroupedPacket(players, new ChatMessagePacket(message, position, uuid), PacketUtils.sendGroupedPacket(players, new ChatMessagePacket(message, position, Objects.requireNonNullElse(uuid, NO_SENDER)),
player -> getChatMessageType(player).accepts(position)); player -> getChatMessageType(player).accepts(position));
} }