Add some generics to the component api, other minor component improvements

This commit is contained in:
Vankka 2022-03-01 12:28:31 +02:00
parent 1b688a329c
commit 2947717869
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
5 changed files with 112 additions and 57 deletions

View File

@ -32,7 +32,7 @@ import java.util.Optional;
/**
* A Minecraft json text component. Use {@link DiscordSRVApi#componentFactory()} to get an instance.<br/>
* <br/>
* This is designed to work with Adventure, see {@link #adventureAdapter(Class)} & {@link #adventureAdapter(MinecraftComponentAdapter)}
* This is designed to work with Adventure, see {@link #adventureAdapter(Class, Class)} & {@link #adventureAdapter(MinecraftComponentAdapter)}
* but is compatible with anything able to handle Minecraft's json format.
* Legacy is <b>not supported</b>.
*/
@ -66,11 +66,26 @@ public interface MinecraftComponent {
* Creates an Adventure adapter for convenience.
*
* @param gsonSerializerClass the gson serializer class
* @return a adapter that will convert to/from relocated or unrelocated adventure classes to/from json
* @return an adapter that will convert to/from relocated or unrelocated adventure classes to/from json
* @throws IllegalArgumentException if the provided class is not an Adventure GsonComponentSerializer
* @see #adventureAdapter(Class, Class)
*/
@NotNull
Adapter adventureAdapter(@NotNull Class<?> gsonSerializerClass);
default Adapter<Object> adventureAdapter(@NotNull Class<?> gsonSerializerClass) {
return adventureAdapter(gsonSerializerClass, null);
}
/**
* Creates an Adventure adapter for convenience.
*
* @param gsonSerializerClass the {@code GsonComponentSerializer} class
* @param componentClass the {@code Component} class that's returned by the given gson component serializer
* @return an adapter that will convert to/from relocated or unrelocated adventure classes to/from json
* @throws IllegalArgumentException if the provided class is not an Adventure {@code GsonComponentSerializer}
* or if the provided {@code Component} class isn't the one returned by the serializer
*/
@NotNull
<T> Adapter<T> adventureAdapter(@NotNull Class<?> gsonSerializerClass, Class<T> componentClass);
/**
* Creates an Adventure adapter from a {@link MinecraftComponentAdapter} for convenience.
@ -79,7 +94,7 @@ public interface MinecraftComponent {
* @return a {@link Adapter} for this component using the given {@link MinecraftComponentAdapter}
*/
@NotNull
Adapter adventureAdapter(@NotNull MinecraftComponentAdapter adapter);
<T> Adapter<T> adventureAdapter(@NotNull MinecraftComponentAdapter<T> adapter);
/**
* Creates an Adventure adapter for the unrelocated adventure.
@ -88,8 +103,8 @@ public interface MinecraftComponent {
*/
@NotNull
@ApiStatus.NonExtendable
default Optional<Adapter> unrelocatedAdapter() {
MinecraftComponentAdapter adapter = MinecraftComponentAdapter.UNRELOCATED;
default Optional<Adapter<Object>> unrelocatedAdapter() {
MinecraftComponentAdapter<Object> adapter = MinecraftComponentAdapter.UNRELOCATED;
if (adapter == null) {
return Optional.empty();
}
@ -99,21 +114,21 @@ public interface MinecraftComponent {
/**
* An Adventure adapter, converts from/to given adventure components from/to json.
*/
interface Adapter {
interface Adapter<Component> {
/**
* Returns the Adventure Component returned by the gson serializer of this adapter.
* @return the {@code net.kyori.adventure.text.Component} (or relocated), cast this to your end class
*/
@NotNull
Object getComponent();
Component getComponent();
/**
* Sets the component to the component that can be serialized by the gson serializer for this class.
* @param adventureComponent the component
* @throws IllegalArgumentException if the provided component cannot be processed by the gson serializer of this adapter
*/
void setComponent(@NotNull Object adventureComponent);
void setComponent(@NotNull Component adventureComponent);
}
}

View File

@ -30,12 +30,12 @@ import java.lang.reflect.Method;
* A persistent Adventure adapter for {@link MinecraftComponent}s, this is more efficient than using {@link MinecraftComponent#adventureAdapter(Class)}.
* @see MinecraftComponent#adventureAdapter(MinecraftComponentAdapter)
*/
public class MinecraftComponentAdapter {
public class MinecraftComponentAdapter<Component> {
public static final MinecraftComponentAdapter UNRELOCATED;
public static final MinecraftComponentAdapter<Object> UNRELOCATED;
static {
MinecraftComponentAdapter unrelocated = null;
MinecraftComponentAdapter<Object> unrelocated = null;
try {
unrelocated = MinecraftComponentAdapter.create(
Class.forName("net.ky".concat("ori.adventure.text.serializer.gson.GsonComponentSerializer"))
@ -45,14 +45,26 @@ public class MinecraftComponentAdapter {
}
/**
* Creates a {@link MinecraftComponentAdapter} that can be used with {@link MinecraftComponent}s.
* Creates a new {@link MinecraftComponentAdapter} that can be used with {@link MinecraftComponent}s.
*
* @param gsonSerializerClass a GsonComponentSerializer class
* @return a new {@link MinecraftComponentAdapter} with the provided GsonComponentSerializer
* @throws IllegalArgumentException if the provided argument is not a GsonComponentSerialize class
*/
public static MinecraftComponentAdapter create(Class<?> gsonSerializerClass) {
return new MinecraftComponentAdapter(gsonSerializerClass);
public static MinecraftComponentAdapter<Object> create(Class<?> gsonSerializerClass) {
return new MinecraftComponentAdapter<>(gsonSerializerClass, null);
}
/**
* Creates a new {@link MinecraftComponentAdapter} that can be used with {@link MinecraftComponent}s.
*
* @param gsonSerializerClass a GsonComponentSerializer class
* @param componentClass the Component class returned by the GsonComponentSerializer
* @return a new {@link MinecraftComponentAdapter} with the provided GsonComponentSerializer
* @throws IllegalArgumentException if the provided argument is not a GsonComponentSerialize class
*/
public static <Component> MinecraftComponentAdapter<Component> create(Class<?> gsonSerializerClass, Class<Component> componentClass) {
return new MinecraftComponentAdapter<>(gsonSerializerClass, componentClass);
}
private final Class<?> gsonSerializerClass;
@ -60,31 +72,48 @@ public class MinecraftComponentAdapter {
private final Method deserialize;
private final Method serialize;
private MinecraftComponentAdapter(Class<?> gsonSerializerClass) {
private MinecraftComponentAdapter(Class<?> gsonSerializerClass, Class<Component> providedComponentClass) {
try {
this.gsonSerializerClass = gsonSerializerClass;
this.instance = gsonSerializerClass.getDeclaredMethod("gson").invoke(null);
this.deserialize = gsonSerializerClass.getMethod("deserialize", Object.class);
Class<?> componentClass = deserialize.getReturnType();
checkComponentClass(providedComponentClass, componentClass);
this.serialize = gsonSerializerClass.getMethod("serialize", componentClass);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
throw new IllegalArgumentException("The provided class is not a GsonComponentSerializer", e);
}
}
public Class<?> gsonSerializerClass() {
private static void checkComponentClass(Class<?> provided, Class<?> actual) {
if (provided == null) {
// Ignore null
return;
}
String providedName = provided.getName();
String actualName = actual.getName();
if (!providedName.equals(actualName)) {
throw new IllegalArgumentException(
"The provided Component class (" + providedName
+ ") does not match the one returned by the serializer: " + actualName
);
}
}
public Class<?> serializerClass() {
return gsonSerializerClass;
}
public Object instance() {
public Object serializerInstance() {
return instance;
}
public Method deserialize() {
public Method deserializeMethod() {
return deserialize;
}
public Method serialize() {
public Method serializeMethod() {
return serialize;
}
}

View File

@ -70,12 +70,10 @@ public final class PaperComponentUtil {
}
MinecraftComponent component = discordSRV.componentFactory().empty();
MinecraftComponent.Adapter adapter = component.unrelocatedAdapter().orElse(null);
if (adapter == null) {
throw new IllegalStateException("Unrelocated adventure unavailable");
}
component.unrelocatedAdapter()
.orElseThrow(() -> new IllegalStateException("Unrelocated adventure unavailable"))
.setComponent(unrelocated);
adapter.setComponent(unrelocated);
return component;
}
}

View File

@ -44,6 +44,15 @@ public class MinecraftComponentImpl implements MinecraftComponent {
setComponent(component);
}
public Component getComponent() {
return component;
}
public void setComponent(Component component) {
this.component = component;
this.json = GsonComponentSerializer.gson().serialize(component);
}
@Override
public @NotNull String asJson() {
return json;
@ -66,43 +75,37 @@ public class MinecraftComponentImpl implements MinecraftComponent {
return PlainTextComponentSerializer.plainText().serialize(component);
}
public Component getComponent() {
return component;
}
public void setComponent(Component component) {
this.component = component;
this.json = GsonComponentSerializer.gson().serialize(component);
@Override
public <T> MinecraftComponent.@NotNull Adapter<T> adventureAdapter(
@NotNull Class<?> gsonSerializerClass, @NotNull Class<T> componentClass
) {
return new Adapter<>(gsonSerializerClass, componentClass);
}
@Override
public @NotNull MinecraftComponent.Adapter adventureAdapter(@NotNull Class<?> gsonSerializerClass) {
return new Adapter(gsonSerializerClass);
public <T> MinecraftComponent.@NotNull Adapter<T> adventureAdapter(@NotNull MinecraftComponentAdapter<T> adapter) {
return new Adapter<>(adapter);
}
@Override
public @NotNull MinecraftComponent.Adapter adventureAdapter(@NotNull MinecraftComponentAdapter adapter) {
return new Adapter(adapter);
}
@SuppressWarnings("unchecked")
public class Adapter<T> implements MinecraftComponent.Adapter<T> {
public class Adapter implements MinecraftComponent.Adapter {
private final MinecraftComponentAdapter<T> adapter;
private final MinecraftComponentAdapter adapter;
private Adapter(Class<?> gsonSerializerClass) {
this(MinecraftComponentAdapter.create(gsonSerializerClass));
private Adapter(Class<?> gsonSerializerClass, Class<T> componentClass) {
this(MinecraftComponentAdapter.create(gsonSerializerClass, componentClass));
}
private Adapter(MinecraftComponentAdapter adapter) {
private Adapter(MinecraftComponentAdapter<T> adapter) {
this.adapter = adapter;
}
@Override
public @NotNull Object getComponent() {
public @NotNull T getComponent() {
try {
return adapter.deserialize()
return (T) adapter.deserializeMethod()
.invoke(
adapter.instance(),
adapter.serializerInstance(),
json
);
} catch (IllegalAccessException | InvocationTargetException e) {
@ -113,13 +116,15 @@ public class MinecraftComponentImpl implements MinecraftComponent {
@Override
public void setComponent(@NotNull Object adventureComponent) {
try {
json = (String) adapter.serialize()
.invoke(
adapter.instance(),
adventureComponent
);
setJson(
(String) adapter.serializeMethod()
.invoke(
adapter.serializerInstance(),
adventureComponent
)
);
} catch (InvocationTargetException e) {
throw new IllegalArgumentException("The provided class is not a Component for the GsonComponentSerializer " + adapter.gsonSerializerClass().getName(), e);
throw new IllegalArgumentException("The provided class is not a Component for the GsonComponentSerializer " + adapter.serializerClass().getName(), e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Failed to convert from adventure component", e);
}

View File

@ -34,7 +34,11 @@ import java.util.Collection;
*/
public final class ComponentUtil {
public static final MinecraftComponentAdapter ADAPTER = MinecraftComponentAdapter.create(GsonComponentSerializer.class);
private static MinecraftComponentAdapter<Component> ADAPTER;
private static MinecraftComponentAdapter<Component> getAdapter() {
return ADAPTER != null ? ADAPTER : (ADAPTER = MinecraftComponentAdapter.create(GsonComponentSerializer.class, Component.class));
}
private ComponentUtil() {}
@ -43,7 +47,7 @@ public final class ComponentUtil {
}
public static boolean isEmpty(MinecraftComponent component) {
return isEmpty(fromAPI(component));
return component.asPlainString().isEmpty();
}
public static MinecraftComponent toAPI(Component component) {
@ -54,12 +58,16 @@ public final class ComponentUtil {
if (component instanceof MinecraftComponentImpl) {
return ((MinecraftComponentImpl) component).getComponent();
} else {
return (Component) component.adventureAdapter(ADAPTER).getComponent();
return component.adventureAdapter(getAdapter()).getComponent();
}
}
public static void set(MinecraftComponent minecraftComponent, Component component) {
minecraftComponent.adventureAdapter(ADAPTER).setComponent(component);
if (component instanceof MinecraftComponentImpl) {
((MinecraftComponentImpl) component).setComponent(component);
} else {
minecraftComponent.adventureAdapter(getAdapter()).setComponent(component);
}
}
public static Component fromUnrelocated(Object unrelocatedAdventure) {