adventure: Expose component serializers in a common location
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
+import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
+import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
+import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandCause;
@@ -50,7 +53,92 @@ public static ClickEvent executeCallback(final Consumer<CommandCause> callback)
return Sponge.getGame().getFactoryProvider().provide(Factory.class).callbackClickEvent(callback);
}
+ /**
+ * Get a serializer for {@link Component}s that will convert to and from the
+ * legacy component format used by the Vanilla client.
+ *
+ * <p>This legacy serializer uses the standard section symbol to mark
+ * formatting characters.</p>
+ *
+ * <p>Implementations may provide a serializer capable of processing any
+ * information that requires access to implementation details.</p>
+ *
+ * @return a section serializer
+ */
+ public static LegacyComponentSerializer legacySectionSerializer() {
+ return Sponge.getGame().getFactoryProvider().provide(Factory.class).legacySectionSerializer();
+ }
+
+ /**
+ * Get a serializer for {@link Component}s that will convert to and from the
+ * legacy component format used by the Vanilla client.
+ *
+ * <p>This legacy serializer uses the alternate ampersand symbol ({@code &})
+ * to mark formatting characters.</p>
+ *
+ * <p>Implementations may provide a serializer capable of processing any
+ * information that requires access to implementation details.</p>
+ *
+ * @return a legacy serializer using ampersands
+ */
+ public static LegacyComponentSerializer legacyAmpersandSerializer() {
+ return Sponge.getGame().getFactoryProvider().provide(Factory.class).legacyAmpersandSerializer();
+ }
+
+ /**
+ * Get a serializer for {@link Component}s that will convert to and from the
+ * legacy component format used by the Vanilla client.
+ *
+ * <p>Implementations may provide a serializer capable of processing any
+ * information that requires access to implementation details.</p>
+ *
+ * @param formatChar the format character to use in place of the standard
+ * section symbol
+ * @return a legacy serializer using ampersands
+ */
+ public static LegacyComponentSerializer legacySerializer(final char formatChar) {
+ return Sponge.getGame().getFactoryProvider().provide(Factory.class).legacySerializer(formatChar);
+ }
+
+ /**
+ * Get a serializer for {@link Component}s that will convert to and from the
+ * standard JSON serialization format using Gson.
+ *
+ * <p>Implementations may provide a serializer capable of processing any
+ * information that requires implementation details, such as legacy
+ * (pre-1.16) hover events.</p>
+ *
+ * @return a json component serializer
+ */
+ public static GsonComponentSerializer gsonSerializer() {
+ return Sponge.getGame().getFactoryProvider().provide(Factory.class).gsonSerializer();
+ }
+
+ /**
+ * Get a serializer for {@link Component}s that will convert components to
+ * a plain-text string.
+ *
+ * <p>Implementations may provide a serializer capable of processing any
+ * information that requires access to implementation details.</p>
+ *
+ * @return a serializer to plain text
+ */
+ public static PlainComponentSerializer plainSerializer() {
+ return Sponge.getGame().getFactoryProvider().provide(Factory.class).plainSerializer();
+ }
+
public interface Factory {
ClickEvent callbackClickEvent(final Consumer<CommandCause> callback);
+
+ LegacyComponentSerializer legacySectionSerializer();
+
+ LegacyComponentSerializer legacyAmpersandSerializer();
+
+ LegacyComponentSerializer legacySerializer(final char formatChar);
+
+ GsonComponentSerializer gsonSerializer();
+
+ PlainComponentSerializer plainSerializer();
+
}
}
