Search in sources :

Example 56 with CompoundTag

use of net.minecraft.nbt.CompoundTag in project SpongeCommon by SpongePowered.

the class BookPagesItemStackData method enchantmentToNbt.

private static CompoundTag enchantmentToNbt(final Enchantment enchantment) {
    final CompoundTag compound = new CompoundTag();
    final String enchantmentId = String.valueOf(Registry.ENCHANTMENT.getKey((net.minecraft.world.item.enchantment.Enchantment) enchantment.type()));
    compound.putString(Constants.Item.ITEM_ENCHANTMENT_ID, enchantmentId);
    compound.putShort(Constants.Item.ITEM_ENCHANTMENT_LEVEL, (short) ((byte) enchantment.level()));
    return compound;
}
Also used : Enchantment(org.spongepowered.api.item.enchantment.Enchantment) SpongeEnchantment(org.spongepowered.common.item.enchantment.SpongeEnchantment) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 57 with CompoundTag

use of net.minecraft.nbt.CompoundTag in project SpongeCommon by SpongePowered.

the class NBTTranslator method containerToCompound.

private static CompoundTag containerToCompound(final DataView container) {
    checkNotNull(container);
    CompoundTag compound = new CompoundTag();
    NBTTranslator.containerToCompound(container, compound);
    return compound;
}
Also used : CompoundTag(net.minecraft.nbt.CompoundTag)

Example 58 with CompoundTag

use of net.minecraft.nbt.CompoundTag in project SpongeCommon by SpongePowered.

the class NBTTranslator method setInternal.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static void setInternal(Tag base, byte type, DataView view, String key) {
    checkNotNull(base);
    checkNotNull(view);
    checkNotNull(key);
    checkArgument(!key.isEmpty());
    checkArgument(type > Constants.NBT.TAG_END && type <= Constants.NBT.TAG_INT_ARRAY);
    switch(type) {
        case Constants.NBT.TAG_BYTE:
            if (key.contains(NBTTranslator.BOOLEAN_IDENTIFIER)) {
                view.set(of(key.replace(NBTTranslator.BOOLEAN_IDENTIFIER, "")), (((ByteTag) base).getAsByte() != 0));
            } else {
                view.set(of(key), ((ByteTag) base).getAsByte());
            }
            break;
        case Constants.NBT.TAG_SHORT:
            view.set(of(key), ((ShortTag) base).getAsShort());
            break;
        case Constants.NBT.TAG_INT:
            view.set(of(key), ((IntTag) base).getAsInt());
            break;
        case Constants.NBT.TAG_LONG:
            view.set(of(key), ((LongTag) base).getAsLong());
            break;
        case Constants.NBT.TAG_FLOAT:
            view.set(of(key), ((FloatTag) base).getAsFloat());
            break;
        case Constants.NBT.TAG_DOUBLE:
            view.set(of(key), ((DoubleTag) base).getAsDouble());
            break;
        case Constants.NBT.TAG_BYTE_ARRAY:
            view.set(of(key), ((ByteArrayTag) base).getAsByteArray());
            break;
        case Constants.NBT.TAG_STRING:
            view.set(of(key), base.getAsString());
            break;
        case Constants.NBT.TAG_LIST:
            ListTag list = (ListTag) base;
            byte listType = list.getElementType();
            int count = list.size();
            List objectList = Lists.newArrayListWithCapacity(count);
            for (final Tag inbt : list) {
                objectList.add(NBTTranslator.fromTagBase(inbt, listType));
            }
            view.set(of(key), objectList);
            break;
        case Constants.NBT.TAG_COMPOUND:
            DataView internalView = view.createView(of(key));
            CompoundTag compound = (CompoundTag) base;
            for (String internalKey : compound.getAllKeys()) {
                Tag internalBase = compound.get(internalKey);
                byte internalType = internalBase.getId();
                // Basically.... more recursion.
                // Reasoning: This avoids creating a new DataContainer which would
                // then be copied in to the owning DataView anyways. We can internally
                // set the actual data directly to the child view instead.
                NBTTranslator.setInternal(internalBase, internalType, internalView, internalKey);
            }
            break;
        case Constants.NBT.TAG_INT_ARRAY:
            view.set(of(key), ((IntArrayTag) base).getAsIntArray());
            break;
        case Constants.NBT.TAG_LONG_ARRAY:
            view.set(of(key), ((LongArrayTag) base).getAsLongArray());
            break;
        default:
            throw new IllegalArgumentException("Unknown NBT type " + type);
    }
}
Also used : DataView(org.spongepowered.api.data.persistence.DataView) List(java.util.List) LongArrayTag(net.minecraft.nbt.LongArrayTag) DoubleTag(net.minecraft.nbt.DoubleTag) Tag(net.minecraft.nbt.Tag) StringTag(net.minecraft.nbt.StringTag) ByteTag(net.minecraft.nbt.ByteTag) IntTag(net.minecraft.nbt.IntTag) IntArrayTag(net.minecraft.nbt.IntArrayTag) FloatTag(net.minecraft.nbt.FloatTag) ByteArrayTag(net.minecraft.nbt.ByteArrayTag) LongTag(net.minecraft.nbt.LongTag) ShortTag(net.minecraft.nbt.ShortTag) CompoundTag(net.minecraft.nbt.CompoundTag) ListTag(net.minecraft.nbt.ListTag) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 59 with CompoundTag

use of net.minecraft.nbt.CompoundTag in project SpongeCommon by SpongePowered.

the class NBTTranslator method fromTagBase.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static Object fromTagBase(Tag base, byte type) {
    switch(type) {
        case Constants.NBT.TAG_BYTE:
            return ((ByteTag) base).getAsByte();
        case Constants.NBT.TAG_SHORT:
            return (((ShortTag) base)).getAsShort();
        case Constants.NBT.TAG_INT:
            return ((IntTag) base).getAsInt();
        case Constants.NBT.TAG_LONG:
            return ((LongTag) base).getAsLong();
        case Constants.NBT.TAG_FLOAT:
            return ((FloatTag) base).getAsFloat();
        case Constants.NBT.TAG_DOUBLE:
            return ((DoubleTag) base).getAsDouble();
        case Constants.NBT.TAG_BYTE_ARRAY:
            return ((ByteArrayTag) base).getAsByteArray();
        case Constants.NBT.TAG_STRING:
            return base.getAsString();
        case Constants.NBT.TAG_LIST:
            ListTag list = (ListTag) base;
            byte listType = list.getElementType();
            int count = list.size();
            List objectList = Lists.newArrayListWithCapacity(count);
            for (Tag inbt : list) {
                objectList.add(NBTTranslator.fromTagBase(inbt, listType));
            }
            return objectList;
        case Constants.NBT.TAG_COMPOUND:
            return NBTTranslator.getViewFromCompound((CompoundTag) base);
        case Constants.NBT.TAG_INT_ARRAY:
            return ((IntArrayTag) base).getAsIntArray();
        case Constants.NBT.TAG_LONG_ARRAY:
            return ((LongArrayTag) base).getAsLongArray();
        default:
            return null;
    }
}
Also used : IntArrayTag(net.minecraft.nbt.IntArrayTag) DoubleTag(net.minecraft.nbt.DoubleTag) ListTag(net.minecraft.nbt.ListTag) FloatTag(net.minecraft.nbt.FloatTag) ByteTag(net.minecraft.nbt.ByteTag) LongArrayTag(net.minecraft.nbt.LongArrayTag) List(java.util.List) LongArrayTag(net.minecraft.nbt.LongArrayTag) DoubleTag(net.minecraft.nbt.DoubleTag) Tag(net.minecraft.nbt.Tag) StringTag(net.minecraft.nbt.StringTag) ByteTag(net.minecraft.nbt.ByteTag) IntTag(net.minecraft.nbt.IntTag) IntArrayTag(net.minecraft.nbt.IntArrayTag) FloatTag(net.minecraft.nbt.FloatTag) ByteArrayTag(net.minecraft.nbt.ByteArrayTag) LongTag(net.minecraft.nbt.LongTag) ShortTag(net.minecraft.nbt.ShortTag) CompoundTag(net.minecraft.nbt.CompoundTag) ListTag(net.minecraft.nbt.ListTag) IntTag(net.minecraft.nbt.IntTag) LongTag(net.minecraft.nbt.LongTag) ByteArrayTag(net.minecraft.nbt.ByteArrayTag)

Example 60 with CompoundTag

use of net.minecraft.nbt.CompoundTag in project SpongeCommon by SpongePowered.

the class ServerPlayerMixin method impl$copyDataOnRespawn.

@Inject(method = "restoreFrom(Lnet/minecraft/server/level/ServerPlayer;Z)V", at = @At("HEAD"))
private void impl$copyDataOnRespawn(final net.minecraft.server.level.ServerPlayer oldPlayer, final boolean respawnFromEnd, final CallbackInfo ci) {
    // Copy Sponge data
    if (oldPlayer instanceof DataCompoundHolder) {
        final DataCompoundHolder oldEntity = (DataCompoundHolder) oldPlayer;
        DataUtil.syncDataToTag(oldEntity);
        final CompoundTag compound = oldEntity.data$getCompound();
        ((DataCompoundHolder) this).data$setCompound(compound);
        DataUtil.syncTagToData(this);
    }
    // Update boss bars
    SpongeAdventure.forEachBossBar(bar -> ((BossEventBridge) bar).bridge$replacePlayer(oldPlayer, (net.minecraft.server.level.ServerPlayer) (Object) this));
}
Also used : DataCompoundHolder(org.spongepowered.common.bridge.data.DataCompoundHolder) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) CompoundTag(net.minecraft.nbt.CompoundTag) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

CompoundTag (net.minecraft.nbt.CompoundTag)146 ListTag (net.minecraft.nbt.ListTag)41 ItemStack (net.minecraft.world.item.ItemStack)22 TagCompound (de.keyle.knbt.TagCompound)12 ResourceLocation (net.minecraft.resources.ResourceLocation)12 Nullable (org.checkerframework.checker.nullness.qual.Nullable)10 Keys (org.spongepowered.api.data.Keys)9 DataProviderRegistrator (org.spongepowered.common.data.provider.DataProviderRegistrator)9 Constants (org.spongepowered.common.util.Constants)9 TagList (de.keyle.knbt.TagList)8 ArrayList (java.util.ArrayList)8 List (java.util.List)8 IntArrayTag (net.minecraft.nbt.IntArrayTag)8 Tag (net.minecraft.nbt.Tag)8 ResourceKey (org.spongepowered.api.ResourceKey)8 DataContainer (org.spongepowered.api.data.persistence.DataContainer)8 DataView (org.spongepowered.api.data.persistence.DataView)8 ByteArrayTag (net.minecraft.nbt.ByteArrayTag)7 TagString (de.keyle.knbt.TagString)6 Block (net.minecraft.world.level.block.Block)6