finish up

This commit is contained in:
vytskalt 2024-03-02 21:00:01 +02:00
parent 8df7ed8ce8
commit ac4b148fea
2 changed files with 15 additions and 22 deletions

View File

@ -7,7 +7,7 @@ import com.comphenix.protocol.utility.MinecraftVersion;
import com.comphenix.protocol.wrappers.codecs.WrappedCodec; import com.comphenix.protocol.wrappers.codecs.WrappedCodec;
import com.comphenix.protocol.wrappers.codecs.WrappedDynamicOps; import com.comphenix.protocol.wrappers.codecs.WrappedDynamicOps;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonObject; import com.google.gson.JsonElement;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
/** /**
@ -37,16 +37,16 @@ public class WrappedComponentStyle extends AbstractWrapper {
setHandle(handle); setHandle(handle);
} }
public JsonObject getJson() { public JsonElement getJson() {
if (CODEC != null) { if (CODEC != null) {
return (JsonObject) CODEC.encode(handle, WrappedDynamicOps.json(false)) return (JsonElement) CODEC.encode(handle, WrappedDynamicOps.json(false))
.getOrThrow(JsonParseException::new); .getOrThrow(JsonParseException::new);
} else { } else {
return (JsonObject) GSON.toJsonTree(handle); return GSON.toJsonTree(handle);
} }
} }
public static WrappedComponentStyle fromJson(JsonObject json) { public static WrappedComponentStyle fromJson(JsonElement json) {
Object handle; Object handle;
if (CODEC != null) { if (CODEC != null) {
handle = CODEC.parse(json, WrappedDynamicOps.json(false)) handle = CODEC.parse(json, WrappedDynamicOps.json(false))

View File

@ -15,7 +15,6 @@ import org.jetbrains.annotations.NotNull;
*/ */
@SuppressWarnings("OptionalGetWithoutIsPresent") @SuppressWarnings("OptionalGetWithoutIsPresent")
public class WrappedNumberFormat extends AbstractWrapper { 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 Object BLANK;
private static final ConstructorAccessor FIXED_CONSTRUCTOR, STYLED_CONSTRUCTOR; private static final ConstructorAccessor FIXED_CONSTRUCTOR, STYLED_CONSTRUCTOR;
@ -49,10 +48,7 @@ public class WrappedNumberFormat extends AbstractWrapper {
} }
public static WrappedNumberFormat fromHandle(Object handle) { public static WrappedNumberFormat fromHandle(Object handle) {
if (!isSupported()) { throwIfUnsupported();
throw UNSUPPORTED;
}
if (MinecraftReflection.getBlankFormatClass().get().isInstance(handle)) { if (MinecraftReflection.getBlankFormatClass().get().isInstance(handle)) {
return new Blank(handle); return new Blank(handle);
} else if (MinecraftReflection.getFixedFormatClass().get().isInstance(handle)) { } else if (MinecraftReflection.getFixedFormatClass().get().isInstance(handle)) {
@ -65,31 +61,28 @@ public class WrappedNumberFormat extends AbstractWrapper {
} }
public static Blank blank() { public static Blank blank() {
if (!isSupported()) { throwIfUnsupported();
throw UNSUPPORTED;
}
return new Blank(WrappedNumberFormat.BLANK); return new Blank(WrappedNumberFormat.BLANK);
} }
public static Fixed fixed(@NotNull WrappedChatComponent content) { public static Fixed fixed(@NotNull WrappedChatComponent content) {
if (!isSupported()) { throwIfUnsupported();
throw UNSUPPORTED;
}
Object handle = FIXED_CONSTRUCTOR.invoke(content.getHandle()); Object handle = FIXED_CONSTRUCTOR.invoke(content.getHandle());
return new Fixed(handle); return new Fixed(handle);
} }
public static Styled styled(@NotNull WrappedComponentStyle style) { public static Styled styled(@NotNull WrappedComponentStyle style) {
if (!isSupported()) { throwIfUnsupported();
throw UNSUPPORTED;
}
Object handle = STYLED_CONSTRUCTOR.invoke(style.getHandle()); Object handle = STYLED_CONSTRUCTOR.invoke(style.getHandle());
return new Styled(handle); 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) { private WrappedNumberFormat(Class<?> handleType) {
super(handleType); super(handleType);
} }