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.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))

View File

@ -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);
}