From ac4b148fea9d4c67520e19111f99b260a08ea64a Mon Sep 17 00:00:00 2001 From: vytskalt Date: Sat, 2 Mar 2024 21:00:01 +0200 Subject: [PATCH] finish up --- .../wrappers/WrappedComponentStyle.java | 10 +++---- .../wrappers/WrappedNumberFormat.java | 27 +++++++------------ 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/comphenix/protocol/wrappers/WrappedComponentStyle.java b/src/main/java/com/comphenix/protocol/wrappers/WrappedComponentStyle.java index 0f4ff69b..f463a86f 100644 --- a/src/main/java/com/comphenix/protocol/wrappers/WrappedComponentStyle.java +++ b/src/main/java/com/comphenix/protocol/wrappers/WrappedComponentStyle.java @@ -7,7 +7,7 @@ import com.comphenix.protocol.utility.MinecraftVersion; import com.comphenix.protocol.wrappers.codecs.WrappedCodec; import com.comphenix.protocol.wrappers.codecs.WrappedDynamicOps; import com.google.gson.Gson; -import com.google.gson.JsonObject; +import com.google.gson.JsonElement; import com.google.gson.JsonParseException; /** @@ -37,16 +37,16 @@ public class WrappedComponentStyle extends AbstractWrapper { setHandle(handle); } - public JsonObject getJson() { + public JsonElement getJson() { if (CODEC != null) { - return (JsonObject) CODEC.encode(handle, WrappedDynamicOps.json(false)) + return (JsonElement) CODEC.encode(handle, WrappedDynamicOps.json(false)) .getOrThrow(JsonParseException::new); } else { - return (JsonObject) GSON.toJsonTree(handle); + return GSON.toJsonTree(handle); } } - public static WrappedComponentStyle fromJson(JsonObject json) { + public static WrappedComponentStyle fromJson(JsonElement json) { Object handle; if (CODEC != null) { handle = CODEC.parse(json, WrappedDynamicOps.json(false)) diff --git a/src/main/java/com/comphenix/protocol/wrappers/WrappedNumberFormat.java b/src/main/java/com/comphenix/protocol/wrappers/WrappedNumberFormat.java index bef76a79..3caa13df 100644 --- a/src/main/java/com/comphenix/protocol/wrappers/WrappedNumberFormat.java +++ b/src/main/java/com/comphenix/protocol/wrappers/WrappedNumberFormat.java @@ -15,7 +15,6 @@ import org.jetbrains.annotations.NotNull; */ @SuppressWarnings("OptionalGetWithoutIsPresent") public class WrappedNumberFormat extends AbstractWrapper { - private static final IllegalStateException UNSUPPORTED = new IllegalStateException("NumberFormat classes don't exist on this server version"); private static final Object BLANK; private static final ConstructorAccessor FIXED_CONSTRUCTOR, STYLED_CONSTRUCTOR; @@ -49,10 +48,7 @@ public class WrappedNumberFormat extends AbstractWrapper { } public static WrappedNumberFormat fromHandle(Object handle) { - if (!isSupported()) { - throw UNSUPPORTED; - } - + throwIfUnsupported(); if (MinecraftReflection.getBlankFormatClass().get().isInstance(handle)) { return new Blank(handle); } else if (MinecraftReflection.getFixedFormatClass().get().isInstance(handle)) { @@ -65,31 +61,28 @@ public class WrappedNumberFormat extends AbstractWrapper { } public static Blank blank() { - if (!isSupported()) { - throw UNSUPPORTED; - } - + throwIfUnsupported(); return new Blank(WrappedNumberFormat.BLANK); } public static Fixed fixed(@NotNull WrappedChatComponent content) { - if (!isSupported()) { - throw UNSUPPORTED; - } - + throwIfUnsupported(); Object handle = FIXED_CONSTRUCTOR.invoke(content.getHandle()); return new Fixed(handle); } public static Styled styled(@NotNull WrappedComponentStyle style) { - if (!isSupported()) { - throw UNSUPPORTED; - } - + throwIfUnsupported(); Object handle = STYLED_CONSTRUCTOR.invoke(style.getHandle()); return new Styled(handle); } + private static void throwIfUnsupported() { + if (!isSupported()) { + throw new IllegalStateException("NumberFormat classes don't exist on this server version"); + } + } + private WrappedNumberFormat(Class handleType) { super(handleType); }