From df28d25685f030aae9a781f0a7aafe216d8dc8e2 Mon Sep 17 00:00:00 2001 From: themode Date: Sat, 24 Oct 2020 23:28:43 +0200 Subject: [PATCH] Allow for JsonMessage in packets --- src/main/java/net/minestom/server/chat/ColoredText.java | 2 ++ src/main/java/net/minestom/server/chat/JsonMessage.java | 3 +++ src/main/java/net/minestom/server/chat/RichMessage.java | 2 ++ .../network/packet/server/play/AdvancementsPacket.java | 6 +++--- .../server/network/packet/server/play/BossBarPacket.java | 4 ++-- .../network/packet/server/play/CombatEventPacket.java | 6 +++--- .../network/packet/server/play/DisconnectPacket.java | 4 ++-- .../server/network/packet/server/play/MapDataPacket.java | 8 ++++---- .../network/packet/server/play/PlayerInfoPacket.java | 8 ++++---- .../server/play/PlayerListHeaderAndFooterPacket.java | 6 +++--- .../packet/server/play/ScoreboardObjectivePacket.java | 4 ++-- .../network/packet/server/play/TabCompletePacket.java | 4 ++-- .../server/network/packet/server/play/TitlePacket.java | 8 ++++---- 13 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/main/java/net/minestom/server/chat/ColoredText.java b/src/main/java/net/minestom/server/chat/ColoredText.java index 7ff044eda..c15bae93a 100644 --- a/src/main/java/net/minestom/server/chat/ColoredText.java +++ b/src/main/java/net/minestom/server/chat/ColoredText.java @@ -2,6 +2,7 @@ package net.minestom.server.chat; import com.google.gson.JsonArray; import com.google.gson.JsonObject; +import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.Arrays; @@ -120,6 +121,7 @@ public class ColoredText extends JsonMessage { * * @return the Json representation of the text */ + @NotNull @Override public JsonObject getJsonObject() { final List components = getComponents(); diff --git a/src/main/java/net/minestom/server/chat/JsonMessage.java b/src/main/java/net/minestom/server/chat/JsonMessage.java index 766b1adc7..c65275695 100644 --- a/src/main/java/net/minestom/server/chat/JsonMessage.java +++ b/src/main/java/net/minestom/server/chat/JsonMessage.java @@ -1,6 +1,7 @@ package net.minestom.server.chat; import com.google.gson.JsonObject; +import org.jetbrains.annotations.NotNull; /** * Represents a json message which can be send to a player. @@ -24,6 +25,7 @@ public abstract class JsonMessage { * @return the json representation of the message * @see #toString() */ + @NotNull public abstract JsonObject getJsonObject(); /** @@ -43,6 +45,7 @@ public abstract class JsonMessage { * @see #getJsonObject() * @see #refreshUpdate() */ + @NotNull @Override public String toString() { if (!updated) { diff --git a/src/main/java/net/minestom/server/chat/RichMessage.java b/src/main/java/net/minestom/server/chat/RichMessage.java index 4201afcee..d64ac3211 100644 --- a/src/main/java/net/minestom/server/chat/RichMessage.java +++ b/src/main/java/net/minestom/server/chat/RichMessage.java @@ -3,6 +3,7 @@ package net.minestom.server.chat; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import net.minestom.server.utils.validate.Check; +import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.List; @@ -115,6 +116,7 @@ public class RichMessage extends JsonMessage { return append(coloredText, FormatRetention.ALL); } + @NotNull @Override public JsonObject getJsonObject() { List cacheComponents = new ArrayList<>(components); diff --git a/src/main/java/net/minestom/server/network/packet/server/play/AdvancementsPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/AdvancementsPacket.java index 823918e61..684daf575 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/AdvancementsPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/AdvancementsPacket.java @@ -1,7 +1,7 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.advancements.FrameType; -import net.minestom.server.chat.ColoredText; +import net.minestom.server.chat.JsonMessage; import net.minestom.server.item.ItemStack; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; @@ -80,8 +80,8 @@ public class AdvancementsPacket implements ServerPacket { } public static class DisplayData { - public ColoredText title; - public ColoredText description; + public JsonMessage title; // Only text + public JsonMessage description; // Only text public ItemStack icon; public FrameType frameType; public int flags; diff --git a/src/main/java/net/minestom/server/network/packet/server/play/BossBarPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/BossBarPacket.java index 8df51c76a..b099cf355 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/BossBarPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/BossBarPacket.java @@ -2,7 +2,7 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.bossbar.BarColor; import net.minestom.server.bossbar.BarDivision; -import net.minestom.server.chat.ColoredText; +import net.minestom.server.chat.JsonMessage; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.utils.binary.BinaryWriter; @@ -14,7 +14,7 @@ public class BossBarPacket implements ServerPacket { public UUID uuid; public Action action; - public ColoredText title; + public JsonMessage title; // Only text public float health; public BarColor color; public BarDivision division; diff --git a/src/main/java/net/minestom/server/network/packet/server/play/CombatEventPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/CombatEventPacket.java index f74a98d5c..7f91ed9d4 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/CombatEventPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/CombatEventPacket.java @@ -1,6 +1,6 @@ package net.minestom.server.network.packet.server.play; -import net.minestom.server.chat.ColoredText; +import net.minestom.server.chat.JsonMessage; import net.minestom.server.entity.Entity; import net.minestom.server.entity.Player; import net.minestom.server.network.packet.server.ServerPacket; @@ -19,7 +19,7 @@ public class CombatEventPacket implements ServerPacket { private int duration; private int opponent; private int playerID; - private ColoredText deathMessage; + private JsonMessage deathMessage; // Only text private CombatEventPacket() { } @@ -38,7 +38,7 @@ public class CombatEventPacket implements ServerPacket { return packet; } - public static CombatEventPacket death(Player player, Optional killer, ColoredText message) { + public static CombatEventPacket death(Player player, Optional killer, JsonMessage message) { CombatEventPacket packet = new CombatEventPacket(); packet.type = EventType.DEATH; packet.playerID = player.getEntityId(); diff --git a/src/main/java/net/minestom/server/network/packet/server/play/DisconnectPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/DisconnectPacket.java index af3ff4c0d..890e2fa39 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/DisconnectPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/DisconnectPacket.java @@ -1,13 +1,13 @@ package net.minestom.server.network.packet.server.play; -import net.minestom.server.chat.ColoredText; +import net.minestom.server.chat.JsonMessage; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.utils.binary.BinaryWriter; public class DisconnectPacket implements ServerPacket { - public ColoredText message; + public JsonMessage message; // Only text @Override public void write(BinaryWriter writer) { diff --git a/src/main/java/net/minestom/server/network/packet/server/play/MapDataPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/MapDataPacket.java index 4592a229b..7efb2b0f4 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/MapDataPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/MapDataPacket.java @@ -1,6 +1,6 @@ package net.minestom.server.network.packet.server.play; -import net.minestom.server.chat.ColoredText; +import net.minestom.server.chat.JsonMessage; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.utils.binary.BinaryWriter; @@ -36,12 +36,12 @@ public class MapDataPacket implements ServerPacket { writer.writeVarInt(0); } - writer.writeByte((byte)columns); + writer.writeByte((byte) columns); if (columns <= 0) { return; } - writer.writeByte((byte)rows); + writer.writeByte((byte) rows); writer.writeByte(x); writer.writeByte(z); if (data != null && data.length > 0) { @@ -62,7 +62,7 @@ public class MapDataPacket implements ServerPacket { public int type; public byte x, z; public byte direction; - public ColoredText displayName; + public JsonMessage displayName; // Only text private void write(BinaryWriter writer) { writer.writeVarInt(type); diff --git a/src/main/java/net/minestom/server/network/packet/server/play/PlayerInfoPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/PlayerInfoPacket.java index 52afbe184..912772f4b 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/PlayerInfoPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/PlayerInfoPacket.java @@ -1,6 +1,6 @@ package net.minestom.server.network.packet.server.play; -import net.minestom.server.chat.ColoredText; +import net.minestom.server.chat.JsonMessage; import net.minestom.server.entity.GameMode; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; @@ -74,7 +74,7 @@ public class PlayerInfoPacket implements ServerPacket { public ArrayList properties; public GameMode gameMode; public int ping; - public ColoredText displayName; + public JsonMessage displayName; // Only text public AddPlayer(UUID uuid, String name, GameMode gameMode, int ping) { super(uuid); @@ -160,9 +160,9 @@ public class PlayerInfoPacket implements ServerPacket { public static class UpdateDisplayName extends PlayerInfo { - public ColoredText displayName; + public JsonMessage displayName; // Only text - public UpdateDisplayName(UUID uuid, ColoredText displayName) { + public UpdateDisplayName(UUID uuid, JsonMessage displayName) { super(uuid); this.displayName = displayName; } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/PlayerListHeaderAndFooterPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/PlayerListHeaderAndFooterPacket.java index 47183eb59..9c6fdfa76 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/PlayerListHeaderAndFooterPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/PlayerListHeaderAndFooterPacket.java @@ -1,6 +1,6 @@ package net.minestom.server.network.packet.server.play; -import net.minestom.server.chat.ColoredText; +import net.minestom.server.chat.JsonMessage; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.utils.binary.BinaryWriter; @@ -12,8 +12,8 @@ public class PlayerListHeaderAndFooterPacket implements ServerPacket { public boolean emptyHeader; public boolean emptyFooter; - public ColoredText header; - public ColoredText footer; + public JsonMessage header; // Only text + public JsonMessage footer; // Only text @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/ScoreboardObjectivePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/ScoreboardObjectivePacket.java index 3be0a275c..0e20089b0 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/ScoreboardObjectivePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/ScoreboardObjectivePacket.java @@ -1,6 +1,6 @@ package net.minestom.server.network.packet.server.play; -import net.minestom.server.chat.ColoredText; +import net.minestom.server.chat.JsonMessage; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.utils.binary.BinaryWriter; @@ -20,7 +20,7 @@ public class ScoreboardObjectivePacket implements ServerPacket { /** * The text to be displayed for the score */ - public ColoredText objectiveValue; + public JsonMessage objectiveValue; // Only text /** * The type how the score is displayed */ diff --git a/src/main/java/net/minestom/server/network/packet/server/play/TabCompletePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/TabCompletePacket.java index 5fa478b0f..4778d915b 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/TabCompletePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/TabCompletePacket.java @@ -1,6 +1,6 @@ package net.minestom.server.network.packet.server.play; -import net.minestom.server.chat.ColoredText; +import net.minestom.server.chat.JsonMessage; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.utils.binary.BinaryWriter; @@ -35,7 +35,7 @@ public class TabCompletePacket implements ServerPacket { public static class Match { public String match; public boolean hasTooltip; - public ColoredText tooltip; + public JsonMessage tooltip; // Only text } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/TitlePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/TitlePacket.java index dab96275f..4a216bdc9 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/TitlePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/TitlePacket.java @@ -1,6 +1,6 @@ package net.minestom.server.network.packet.server.play; -import net.minestom.server.chat.ColoredText; +import net.minestom.server.chat.JsonMessage; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.utils.binary.BinaryWriter; @@ -9,11 +9,11 @@ public class TitlePacket implements ServerPacket { public Action action; - public ColoredText titleText; + public JsonMessage titleText; // Only text - public ColoredText subtitleText; + public JsonMessage subtitleText; // Only text - public ColoredText actionBarText; + public JsonMessage actionBarText; // Only text public int fadeIn; public int stay;