Deprecate AdventureSerializer

This commit is contained in:
Kieran Wallbanks 2021-06-11 15:11:21 +01:00
parent e532f16735
commit ee1ca5e03b
3 changed files with 87 additions and 7 deletions

View File

@ -27,17 +27,21 @@ import org.jetbrains.annotations.Nullable;
* of specific players, or other elements which implement {@link Localizable}. To add your * of specific players, or other elements which implement {@link Localizable}. To add your
* own translations, use {@link GlobalTranslator#addSource(Translator)} with a * own translations, use {@link GlobalTranslator#addSource(Translator)} with a
* {@link TranslationRegistry} or your own implementation of {@link Translator}. * {@link TranslationRegistry} or your own implementation of {@link Translator}.
*
* @deprecated Use {@link MinestomAdventure}
*/ */
@Deprecated(forRemoval = true)
public class AdventureSerializer { public class AdventureSerializer {
/** /**
* If components should be automatically translated in outgoing packets. * If components should be automatically translated in outgoing packets.
* @deprecated Use {@link MinestomAdventure#AUTOMATIC_COMPONENT_TRANSLATION}
*/ */
public static final boolean AUTOMATIC_COMPONENT_TRANSLATION = false; @Deprecated(forRemoval = true)
public static final boolean AUTOMATIC_COMPONENT_TRANSLATION = MinestomAdventure.AUTOMATIC_COMPONENT_TRANSLATION;
protected static final Localizable NULL_LOCALIZABLE = () -> null; protected static final Localizable NULL_LOCALIZABLE = () -> null;
private static Function<Component, String> serializer = component -> GsonComponentSerializer.gson().serialize(component); private static Function<Component, String> serializer = component -> GsonComponentSerializer.gson().serialize(component);
private static Locale defaultLocale = Locale.getDefault();
private AdventureSerializer() {} private AdventureSerializer() {}
@ -45,7 +49,9 @@ public class AdventureSerializer {
* Gets the root serializer that is used to convert components into strings. * Gets the root serializer that is used to convert components into strings.
* *
* @return the serializer * @return the serializer
* @deprecated The serializer is no longer in use, use the adventure-provided serializers
*/ */
@Deprecated(forRemoval = true)
public static @NotNull Function<Component, String> getSerializer() { public static @NotNull Function<Component, String> getSerializer() {
return AdventureSerializer.serializer; return AdventureSerializer.serializer;
} }
@ -54,7 +60,9 @@ public class AdventureSerializer {
* Sets the root serializer that is used to convert components into strings. * Sets the root serializer that is used to convert components into strings.
* *
* @param serializer the serializer * @param serializer the serializer
* @deprecated The serializer is no longer in use
*/ */
@Deprecated
public static void setSerializer(@NotNull Function<Component, String> serializer) { public static void setSerializer(@NotNull Function<Component, String> serializer) {
AdventureSerializer.serializer = serializer; AdventureSerializer.serializer = serializer;
} }
@ -65,9 +73,11 @@ public class AdventureSerializer {
* does not have a locale. * does not have a locale.
* *
* @return the default locale * @return the default locale
* @deprecated Use {@link MinestomAdventure#getDefaultLocale()}
*/ */
@Deprecated(forRemoval = true)
public static @NotNull Locale getDefaultLocale() { public static @NotNull Locale getDefaultLocale() {
return defaultLocale; return MinestomAdventure.getDefaultLocale();
} }
/** /**
@ -76,9 +86,11 @@ public class AdventureSerializer {
* does not have a locale. * does not have a locale.
* *
* @param defaultLocale the new default locale, or {@code null} to return to the default * @param defaultLocale the new default locale, or {@code null} to return to the default
* @deprecated Use {@link MinestomAdventure#setDefaultLocale(Locale)}}
*/ */
@Deprecated(forRemoval = true)
public static void setDefaultLocale(@Nullable Locale defaultLocale) { public static void setDefaultLocale(@Nullable Locale defaultLocale) {
AdventureSerializer.defaultLocale = Objects.requireNonNullElseGet(defaultLocale, Locale::getDefault); MinestomAdventure.setDefaultLocale(defaultLocale);
} }
/** /**
@ -86,7 +98,9 @@ public class AdventureSerializer {
* {@link GlobalTranslator#get()}. * {@link GlobalTranslator#get()}.
* *
* @return the global translator * @return the global translator
* @deprecated Use {@link GlobalTranslator#get()}
*/ */
@Deprecated(forRemoval = true)
public static @NotNull GlobalTranslator getTranslator() { public static @NotNull GlobalTranslator getTranslator() {
return GlobalTranslator.get(); return GlobalTranslator.get();
} }
@ -99,7 +113,9 @@ public class AdventureSerializer {
* @param localizable the localizable * @param localizable the localizable
* *
* @return the prepared component * @return the prepared component
* @deprecated Use {@link GlobalTranslator#translate(String, Locale)}
*/ */
@Deprecated(forRemoval = true)
public static @NotNull Component translate(@NotNull Component component, @NotNull Localizable localizable) { public static @NotNull Component translate(@NotNull Component component, @NotNull Localizable localizable) {
return GlobalTranslator.renderer().render(component, Objects.requireNonNullElse(localizable.getLocale(), AdventureSerializer.getDefaultLocale())); return GlobalTranslator.renderer().render(component, Objects.requireNonNullElse(localizable.getLocale(), AdventureSerializer.getDefaultLocale()));
} }
@ -112,7 +128,9 @@ public class AdventureSerializer {
* @param locale the locale * @param locale the locale
* *
* @return the prepared component * @return the prepared component
* @deprecated Use {@link GlobalTranslator#translate(String, Locale)}
*/ */
@Deprecated(forRemoval = true)
public static @NotNull Component translate(@NotNull Component component, @NotNull Locale locale) { public static @NotNull Component translate(@NotNull Component component, @NotNull Locale locale) {
return GlobalTranslator.renderer().render(component, locale); return GlobalTranslator.renderer().render(component, locale);
} }
@ -123,7 +141,9 @@ public class AdventureSerializer {
* @param component the component * @param component the component
* *
* @return the serialized string * @return the serialized string
* @deprecated Use the Adventure serializers directly
*/ */
@Deprecated(forRemoval = true)
public static @NotNull String serialize(@NotNull Component component) { public static @NotNull String serialize(@NotNull Component component) {
return AdventureSerializer.serializer.apply(component); return AdventureSerializer.serializer.apply(component);
} }
@ -135,7 +155,9 @@ public class AdventureSerializer {
* @param localizable the localisable * @param localizable the localisable
* *
* @return the string * @return the string
* @deprecated Use {@link GlobalTranslator#translate(String, Locale)} and the Adventure serializers
*/ */
@Deprecated(forRemoval = true)
public static String translateAndSerialize(@NotNull Component component, @NotNull Localizable localizable) { public static String translateAndSerialize(@NotNull Component component, @NotNull Localizable localizable) {
return AdventureSerializer.translateAndSerialize(component, Objects.requireNonNullElse(localizable.getLocale(), AdventureSerializer.getDefaultLocale())); return AdventureSerializer.translateAndSerialize(component, Objects.requireNonNullElse(localizable.getLocale(), AdventureSerializer.getDefaultLocale()));
} }
@ -147,7 +169,9 @@ public class AdventureSerializer {
* @param locale the locale * @param locale the locale
* *
* @return the string * @return the string
* @deprecated Use {@link GlobalTranslator#translate(String, Locale)} and the Adventure serializers
*/ */
@Deprecated(forRemoval = true)
public static String translateAndSerialize(@NotNull Component component, @NotNull Locale locale) { public static String translateAndSerialize(@NotNull Component component, @NotNull Locale locale) {
return AdventureSerializer.serialize(AdventureSerializer.translate(component, locale)); return AdventureSerializer.serialize(AdventureSerializer.translate(component, locale));
} }

View File

@ -0,0 +1,53 @@
package net.minestom.server.adventure;
import java.io.StringReader;
import net.kyori.adventure.util.Codec;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Locale;
import java.util.Objects;
import org.jglrxavpok.hephaistos.nbt.NBT;
import org.jglrxavpok.hephaistos.nbt.NBTException;
import org.jglrxavpok.hephaistos.nbt.SNBTParser;
/**
* Adventure related constants, etc.
*/
public final class MinestomAdventure {
/**
* A codec to convert between strings and NBT.
*/
public static final Codec<NBT, String, NBTException, RuntimeException> NBT_CODEC
= Codec.of(encoded -> new SNBTParser(new StringReader(encoded)).parse(), NBT::toSNBT);
/**
* If components should be automatically translated in outgoing packets.
*/
public static final boolean AUTOMATIC_COMPONENT_TRANSLATION = false;
private static Locale defaultLocale = Locale.getDefault();
private MinestomAdventure() {
}
/**
* Gets the default locale used to translate components when no overriding locale has been provided.
*
* @return the default locale
*/
public static @NotNull Locale getDefaultLocale() {
return defaultLocale;
}
/**
* Sets the default locale used to translate components when no overriding locale has been provided.
*
* @param defaultLocale the new default, or {@code null} to return to {@link Locale#getDefault()}
*/
public static void setDefaultLocale(@Nullable Locale defaultLocale) {
MinestomAdventure.defaultLocale = Objects.requireNonNullElseGet(defaultLocale, Locale::getDefault);
}
}

View File

@ -5,6 +5,7 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.util.Codec; import net.kyori.adventure.util.Codec;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import net.minestom.server.adventure.MinestomAdventure;
import net.minestom.server.attribute.Attribute; import net.minestom.server.attribute.Attribute;
import net.minestom.server.attribute.AttributeOperation; import net.minestom.server.attribute.AttributeOperation;
import net.minestom.server.instance.block.Block; import net.minestom.server.instance.block.Block;
@ -34,9 +35,11 @@ public final class NBTUtils {
/** /**
* An Adventure codec to convert between NBT and SNBT. * An Adventure codec to convert between NBT and SNBT.
*
* @deprecated Use {@link MinestomAdventure#NBT_CODEC}
*/ */
public static final Codec<NBT, String, NBTException, RuntimeException> SNBT_CODEC @Deprecated(forRemoval = true)
= Codec.of(encoded -> new SNBTParser(new StringReader(encoded)).parse(), NBT::toSNBT); public static final Codec<NBT, String, NBTException, RuntimeException> SNBT_CODEC = MinestomAdventure.NBT_CODEC;
private NBTUtils() { private NBTUtils() {
@ -54,7 +57,7 @@ public final class NBTUtils {
return null; return null;
} }
return BinaryTagHolder.encode(tag, SNBT_CODEC); return BinaryTagHolder.encode(tag, MinestomAdventure.NBT_CODEC);
} }
/** /**