Search in sources :

Example 1 with GsonComponentSerializer

use of net.kyori.adventure.text.serializer.gson.GsonComponentSerializer in project SpongeCommon by SpongePowered.

the class SpongeAdventure method json.

// Horrible-ness
public static List<Component> json(final List<String> strings) {
    final GsonComponentSerializer gcs = GsonComponentSerializer.gson();
    final List<Component> components = new ArrayList<>();
    for (final String string : strings) {
        components.add(gcs.deserialize(string));
    }
    return components;
}
Also used : GsonComponentSerializer(net.kyori.adventure.text.serializer.gson.GsonComponentSerializer) ArrayList(java.util.ArrayList) Component(net.kyori.adventure.text.Component)

Example 2 with GsonComponentSerializer

use of net.kyori.adventure.text.serializer.gson.GsonComponentSerializer in project SpongeCommon by SpongePowered.

the class SpongeAdventure method listTagJson.

public static ListTag listTagJson(final List<Component> components) {
    final GsonComponentSerializer gcs = GsonComponentSerializer.gson();
    final ListTag nbt = new ListTag();
    for (final Component component : components) {
        nbt.add(StringTag.valueOf(gcs.serialize(component)));
    }
    return nbt;
}
Also used : GsonComponentSerializer(net.kyori.adventure.text.serializer.gson.GsonComponentSerializer) Component(net.kyori.adventure.text.Component) ListTag(net.minecraft.nbt.ListTag)

Example 3 with GsonComponentSerializer

use of net.kyori.adventure.text.serializer.gson.GsonComponentSerializer in project SpongeCommon by SpongePowered.

the class SignItemStackData method register.

// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
    registrator.asMutable(ItemStack.class).create(Keys.SIGN_LINES).get(h -> {
        final CompoundTag tag = h.getTagElement(Constants.Item.BLOCK_ENTITY_TAG);
        if (tag == null) {
            return null;
        }
        final String id = tag.getString(Constants.Item.BLOCK_ENTITY_ID);
        if (!id.equalsIgnoreCase(Constants.TileEntity.SIGN)) {
            return null;
        }
        final GsonComponentSerializer gcs = GsonComponentSerializer.gson();
        final List<Component> texts = Lists.newArrayListWithCapacity(4);
        for (int i = 0; i < 4; i++) {
            texts.add(gcs.deserialize(tag.getString("Text" + (i + 1))));
        }
        return texts;
    }).set((h, v) -> {
        final GsonComponentSerializer gcs = GsonComponentSerializer.gson();
        final CompoundTag tag = h.getOrCreateTagElement(Constants.Item.BLOCK_ENTITY_TAG);
        tag.putString(Constants.Item.BLOCK_ENTITY_ID, Constants.TileEntity.SIGN);
        for (int i = 0; i < 4; i++) {
            final Component line = v.size() > i ? v.get(i) : Component.empty();
            if (line == null) {
                throw new IllegalArgumentException("A null line was given at index " + i);
            }
            tag.putString("Text" + (i + 1), gcs.serialize(line));
        }
    }).delete(h -> h.removeTagKey(Constants.Item.BLOCK_ENTITY_TAG));
}
Also used : GsonComponentSerializer(net.kyori.adventure.text.serializer.gson.GsonComponentSerializer) Keys(org.spongepowered.api.data.Keys) List(java.util.List) Lists(com.google.common.collect.Lists) CompoundTag(net.minecraft.nbt.CompoundTag) Constants(org.spongepowered.common.util.Constants) Component(net.kyori.adventure.text.Component) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) ItemStack(net.minecraft.world.item.ItemStack) SpongeAdventure(org.spongepowered.common.adventure.SpongeAdventure) GsonComponentSerializer(net.kyori.adventure.text.serializer.gson.GsonComponentSerializer) Component(net.kyori.adventure.text.Component) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

Component (net.kyori.adventure.text.Component)3 GsonComponentSerializer (net.kyori.adventure.text.serializer.gson.GsonComponentSerializer)3 Lists (com.google.common.collect.Lists)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CompoundTag (net.minecraft.nbt.CompoundTag)1 ListTag (net.minecraft.nbt.ListTag)1 ItemStack (net.minecraft.world.item.ItemStack)1 Keys (org.spongepowered.api.data.Keys)1 SpongeAdventure (org.spongepowered.common.adventure.SpongeAdventure)1 DataProviderRegistrator (org.spongepowered.common.data.provider.DataProviderRegistrator)1 Constants (org.spongepowered.common.util.Constants)1