mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-29 04:28:21 +01:00
Annotations for all chat components to prevent client json exception
This commit is contained in:
parent
44c912d7ec
commit
113f4fcdb3
@ -1,5 +1,7 @@
|
||||
package net.minestom.server.chat;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* Represents a click event for a specific portion of the message.
|
||||
*/
|
||||
@ -8,7 +10,7 @@ public class ChatClickEvent {
|
||||
private final String action;
|
||||
private final String value;
|
||||
|
||||
private ChatClickEvent(String action, String value) {
|
||||
private ChatClickEvent(@NotNull String action, @NotNull String value) {
|
||||
this.action = action;
|
||||
this.value = value;
|
||||
}
|
||||
@ -19,7 +21,8 @@ public class ChatClickEvent {
|
||||
* @param url the URL to open
|
||||
* @return the chat click event
|
||||
*/
|
||||
public static ChatClickEvent openUrl(String url) {
|
||||
@NotNull
|
||||
public static ChatClickEvent openUrl(@NotNull String url) {
|
||||
return new ChatClickEvent("open_url", url);
|
||||
}
|
||||
|
||||
@ -29,7 +32,8 @@ public class ChatClickEvent {
|
||||
* @param command the command to run
|
||||
* @return the chat click event
|
||||
*/
|
||||
public static ChatClickEvent runCommand(String command) {
|
||||
@NotNull
|
||||
public static ChatClickEvent runCommand(@NotNull String command) {
|
||||
return new ChatClickEvent("run_command", command);
|
||||
}
|
||||
|
||||
@ -39,14 +43,17 @@ public class ChatClickEvent {
|
||||
* @param command the command to suggest
|
||||
* @return the chat click event
|
||||
*/
|
||||
public static ChatClickEvent suggestCommand(String command) {
|
||||
@NotNull
|
||||
public static ChatClickEvent suggestCommand(@NotNull String command) {
|
||||
return new ChatClickEvent("suggest_command", command);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected String getAction() {
|
||||
return action;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
@ -5,6 +5,8 @@ import it.unimi.dsi.fastutil.chars.Char2ObjectOpenHashMap;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||
import net.minestom.server.utils.validate.Check;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@ -148,6 +150,7 @@ public class ChatColor {
|
||||
* @param b the blue component
|
||||
* @return a chat color with the specified RGB color
|
||||
*/
|
||||
@NotNull
|
||||
public static ChatColor fromRGB(byte r, byte g, byte b) {
|
||||
return fromRGB(r, g, b, -1, null);
|
||||
}
|
||||
@ -162,6 +165,7 @@ public class ChatColor {
|
||||
* @param name the color name
|
||||
* @return the color associated with the name, {@link #NO_COLOR} if not found
|
||||
*/
|
||||
@NotNull
|
||||
public static ChatColor fromName(String name) {
|
||||
return colorCode.getOrDefault(name.toLowerCase(), NO_COLOR);
|
||||
}
|
||||
@ -172,6 +176,7 @@ public class ChatColor {
|
||||
* @param id the id of the color
|
||||
* @return the color associated with the id, {@link #NO_COLOR} if not found
|
||||
*/
|
||||
@NotNull
|
||||
public static ChatColor fromId(int id) {
|
||||
return idColorMap.getOrDefault(id, NO_COLOR);
|
||||
}
|
||||
@ -180,8 +185,9 @@ public class ChatColor {
|
||||
* Gets a color based on its legacy color code (eg: 1, 2, 3,... f).
|
||||
*
|
||||
* @param colorCode the color legacy code
|
||||
* @return the color associated with the code
|
||||
* @return the color associated with the code, {@link #NO_COLOR} if not found
|
||||
*/
|
||||
@NotNull
|
||||
public static ChatColor fromLegacyColorCodes(char colorCode) {
|
||||
return legacyColorCodesMap.getOrDefault(colorCode, NO_COLOR);
|
||||
}
|
||||
@ -227,10 +233,11 @@ public class ChatColor {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the code name is the color is "special".
|
||||
* Gets the code name if the color is "special".
|
||||
*
|
||||
* @return the special code name
|
||||
*/
|
||||
@Nullable
|
||||
protected String getCodeName() {
|
||||
return codeName;
|
||||
}
|
||||
@ -240,6 +247,7 @@ public class ChatColor {
|
||||
return id;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String toString() {
|
||||
if (empty)
|
||||
|
@ -3,6 +3,8 @@ package net.minestom.server.chat;
|
||||
import com.google.gson.JsonObject;
|
||||
import net.minestom.server.entity.Entity;
|
||||
import net.minestom.server.item.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
||||
|
||||
/**
|
||||
@ -15,26 +17,31 @@ public class ChatHoverEvent {
|
||||
private JsonObject valueObject;
|
||||
private final boolean isJson;
|
||||
|
||||
private ChatHoverEvent(String action, String value) {
|
||||
private ChatHoverEvent(@NotNull String action, @NotNull String value) {
|
||||
this.action = action;
|
||||
this.value = value;
|
||||
this.valueObject = null;
|
||||
this.isJson = false;
|
||||
}
|
||||
|
||||
private ChatHoverEvent(String action, JsonObject valueObject) {
|
||||
private ChatHoverEvent(@NotNull String action, @NotNull JsonObject valueObject) {
|
||||
this.action = action;
|
||||
this.value = null;
|
||||
this.valueObject = valueObject;
|
||||
this.isJson = true;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected String getAction() {
|
||||
return action;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected JsonObject getValueObject() {
|
||||
return valueObject;
|
||||
}
|
||||
@ -49,7 +56,8 @@ public class ChatHoverEvent {
|
||||
* @param text the text to show
|
||||
* @return the chat hover event
|
||||
*/
|
||||
public static ChatHoverEvent showText(ColoredText text) {
|
||||
@NotNull
|
||||
public static ChatHoverEvent showText(@NotNull ColoredText text) {
|
||||
return new ChatHoverEvent("show_text", text.getJsonObject());
|
||||
}
|
||||
|
||||
@ -59,7 +67,8 @@ public class ChatHoverEvent {
|
||||
* @param text the text to show
|
||||
* @return the chat hover event
|
||||
*/
|
||||
public static ChatHoverEvent showText(String text) {
|
||||
@NotNull
|
||||
public static ChatHoverEvent showText(@NotNull String text) {
|
||||
return new ChatHoverEvent("show_text", text);
|
||||
}
|
||||
|
||||
@ -69,7 +78,8 @@ public class ChatHoverEvent {
|
||||
* @param itemStack the item to show
|
||||
* @return the chat hover event
|
||||
*/
|
||||
public static ChatHoverEvent showItem(ItemStack itemStack) {
|
||||
@NotNull
|
||||
public static ChatHoverEvent showItem(@NotNull ItemStack itemStack) {
|
||||
final String json = itemStack.toNBT().toSNBT();
|
||||
return new ChatHoverEvent("show_item", json);
|
||||
}
|
||||
@ -80,7 +90,8 @@ public class ChatHoverEvent {
|
||||
* @param entity the entity to show
|
||||
* @return the chat hover event
|
||||
*/
|
||||
public static ChatHoverEvent showEntity(Entity entity) {
|
||||
@NotNull
|
||||
public static ChatHoverEvent showEntity(@NotNull Entity entity) {
|
||||
NBTCompound compound = new NBTCompound()
|
||||
.setString("id", entity.getUuid().toString())
|
||||
.setString("type", entity.getEntityType().getNamespaceID());
|
||||
|
@ -2,6 +2,7 @@ package net.minestom.server.chat;
|
||||
|
||||
|
||||
import com.google.gson.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* Class used to convert JSON string to proper chat message representation.
|
||||
@ -16,7 +17,8 @@ public final class ChatParser {
|
||||
* @param json the json containing the text and color
|
||||
* @return a {@link ColoredText} representing the text
|
||||
*/
|
||||
public static ColoredText toColoredText(String json) {
|
||||
@NotNull
|
||||
public static ColoredText toColoredText(@NotNull String json) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
try {
|
||||
@ -40,7 +42,7 @@ public final class ChatParser {
|
||||
}
|
||||
}
|
||||
|
||||
private static void appendBuilder(StringBuilder builder, JsonObject object) {
|
||||
private static void appendBuilder(@NotNull StringBuilder builder, @NotNull JsonObject object) {
|
||||
builder.append(parseText(object));
|
||||
|
||||
final boolean hasExtra = object.has("extra");
|
||||
@ -59,7 +61,8 @@ public final class ChatParser {
|
||||
* @param textObject the text component to parse
|
||||
* @return the colored text format of the text component
|
||||
*/
|
||||
private static String parseText(JsonObject textObject) {
|
||||
@NotNull
|
||||
private static String parseText(@NotNull JsonObject textObject) {
|
||||
final boolean hasText = textObject.has("text");
|
||||
if (!hasText)
|
||||
return "";
|
||||
@ -80,7 +83,7 @@ public final class ChatParser {
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private static void appendColor(JsonObject textObject, StringBuilder builder) {
|
||||
private static void appendColor(@NotNull JsonObject textObject, @NotNull StringBuilder builder) {
|
||||
if (textObject.has("color")) {
|
||||
final String colorString = textObject.get("color").getAsString();
|
||||
if (colorString.startsWith("#")) {
|
||||
@ -94,7 +97,8 @@ public final class ChatParser {
|
||||
}
|
||||
}
|
||||
|
||||
private static void appendExtra(JsonObject textObject, StringBuilder builder, String name) {
|
||||
private static void appendExtra(@NotNull JsonObject textObject, @NotNull StringBuilder builder,
|
||||
@NotNull String name) {
|
||||
if (textObject.has(name)) {
|
||||
final boolean value = textObject.get(name).getAsBoolean();
|
||||
if (value) {
|
||||
|
@ -1,5 +1,8 @@
|
||||
package net.minestom.server.chat;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* Represents a translatable component which can be used in {@link ColoredText}.
|
||||
*/
|
||||
@ -8,7 +11,7 @@ public class TranslatableText {
|
||||
private final String code;
|
||||
private final String[] arguments;
|
||||
|
||||
private TranslatableText(String code, String[] arguments) {
|
||||
private TranslatableText(@NotNull String code, @Nullable String[] arguments) {
|
||||
this.code = code;
|
||||
this.arguments = arguments;
|
||||
}
|
||||
@ -19,7 +22,8 @@ public class TranslatableText {
|
||||
* @param code the translatable code
|
||||
* @return the translatable component linked to the code
|
||||
*/
|
||||
public static TranslatableText of(String code) {
|
||||
@NotNull
|
||||
public static TranslatableText of(@NotNull String code) {
|
||||
return new TranslatableText(code, null);
|
||||
}
|
||||
|
||||
@ -30,7 +34,8 @@ public class TranslatableText {
|
||||
* @param arguments the translatable component arguments in order
|
||||
* @return the translatable component linked to the code and arguments
|
||||
*/
|
||||
public static TranslatableText of(String code, String... arguments) {
|
||||
@NotNull
|
||||
public static TranslatableText of(@NotNull String code, @NotNull String... arguments) {
|
||||
return new TranslatableText(code, arguments);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user