Search in sources :

Example 41 with CompoundTag

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

the class SpongeEntitySnapshotBuilder method build.

@Override
public EntitySnapshot build() {
    Objects.requireNonNull(this.worldKey);
    Objects.requireNonNull(this.position);
    Objects.requireNonNull(this.rotation);
    Objects.requireNonNull(this.scale);
    Objects.requireNonNull(this.entityType);
    final EntitySnapshot snapshot = new SpongeEntitySnapshot(this);
    // Write the the manipulator values to NBT
    if (this.manipulator != null && !this.manipulator.getKeys().isEmpty()) {
        if (this.compound == null) {
            this.compound = new CompoundTag();
        }
        final SimpleNBTDataHolder dataHolder = new SimpleNBTDataHolder(this.compound, NBTDataTypes.ENTITY);
        dataHolder.copyFrom(this.manipulator);
        this.compound = dataHolder.data$getCompound();
        // If there was no data remove the compound again
        if (this.compound.isEmpty()) {
            this.compound = null;
        }
    }
    return snapshot;
}
Also used : EntitySnapshot(org.spongepowered.api.entity.EntitySnapshot) SimpleNBTDataHolder(org.spongepowered.common.data.holder.SimpleNBTDataHolder) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 42 with CompoundTag

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

the class SpongeEntitySnapshotBuilder method from.

public SpongeEntitySnapshotBuilder from(final net.minecraft.world.entity.Entity minecraftEntity) {
    this.entityType = ((Entity) minecraftEntity).type();
    this.worldKey = ((Entity) minecraftEntity).serverLocation().worldKey();
    this.uniqueId = minecraftEntity.getUUID();
    final Transform transform = ((Entity) minecraftEntity).transform();
    this.position = transform.position();
    this.rotation = transform.rotation();
    this.scale = transform.scale();
    this.manipulator = DataManipulator.mutableOf((Entity) minecraftEntity);
    this.compound = new CompoundTag();
    minecraftEntity.saveWithoutId(this.compound);
    return this;
}
Also used : Entity(org.spongepowered.api.entity.Entity) Transform(org.spongepowered.api.util.Transform) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 43 with CompoundTag

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

the class BlockTypeItemStackData method set.

private static boolean set(final ItemStack stack, final String nbtKey, final Set<? extends BlockType> value) {
    if (value.isEmpty()) {
        stack.removeTagKey(nbtKey);
        return true;
    }
    final CompoundTag tag = stack.getOrCreateTag();
    final ListTag list = value.stream().map(type -> Registry.BLOCK.getKey((Block) type).toString()).collect(NBTCollectors.toStringTagList());
    tag.put(nbtKey, list);
    return true;
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) Constants(org.spongepowered.common.util.Constants) Set(java.util.Set) NBTStreams(org.spongepowered.common.util.NBTStreams) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Registry(net.minecraft.core.Registry) Keys(org.spongepowered.api.data.Keys) CompoundTag(net.minecraft.nbt.CompoundTag) NBTCollectors(org.spongepowered.common.util.NBTCollectors) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) BlockType(org.spongepowered.api.block.BlockType) Block(net.minecraft.world.level.block.Block) ItemStack(net.minecraft.world.item.ItemStack) ListTag(net.minecraft.nbt.ListTag) Block(net.minecraft.world.level.block.Block) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 44 with CompoundTag

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

the class CompassItemData method register.

static void register(final DataProviderRegistrator registrator) {
    registrator.asMutable(ItemStack.class).create(Keys.LODESTONE).get(stack -> {
        if (CompassItem.isLodestoneCompass(stack)) {
            final CompoundTag tag = stack.getOrCreateTag();
            final Optional<ResourceKey<Level>> dimension = CompassItem.getLodestoneDimension(tag);
            if (dimension.isPresent()) {
                return ServerLocation.of((ServerWorld) SpongeCommon.server().getLevel(dimension.get()), VecHelper.toVector3d(NbtUtils.readBlockPos(tag.getCompound("LodestonePos"))));
            }
        }
        return null;
    }).set((stack, location) -> {
        final CompoundTag tag = stack.getOrCreateTag();
        tag.put("LodestonePos", NbtUtils.writeBlockPos(VecHelper.toBlockPos(location)));
        Level.RESOURCE_KEY_CODEC.encodeStart(NbtOps.INSTANCE, ((net.minecraft.server.level.ServerLevel) location.world()).dimension()).resultOrPartial(SpongeCommon.logger()::error).ifPresent(dimension -> tag.put("LodestoneDimension", dimension));
        tag.putBoolean("LodestoneTracked", true);
    }).delete(stack -> {
        final CompoundTag tag = stack.getTag();
        if (tag != null) {
            tag.remove("LodestoneDimension");
            tag.remove("LodestonePos");
            tag.remove("LodestoneTracked");
        }
    });
}
Also used : ServerWorld(org.spongepowered.api.world.server.ServerWorld) NbtUtils(net.minecraft.nbt.NbtUtils) SpongeCommon(org.spongepowered.common.SpongeCommon) NbtOps(net.minecraft.nbt.NbtOps) ResourceKey(net.minecraft.resources.ResourceKey) CompassItem(net.minecraft.world.item.CompassItem) Keys(org.spongepowered.api.data.Keys) CompoundTag(net.minecraft.nbt.CompoundTag) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) VecHelper(org.spongepowered.common.util.VecHelper) Optional(java.util.Optional) ItemStack(net.minecraft.world.item.ItemStack) Level(net.minecraft.world.level.Level) ServerLocation(org.spongepowered.api.world.server.ServerLocation) CompoundTag(net.minecraft.nbt.CompoundTag) ResourceKey(net.minecraft.resources.ResourceKey)

Example 45 with CompoundTag

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

the class ShieldItemStackData method layerToNbt.

public static CompoundTag layerToNbt(final BannerPatternLayer layer) {
    final CompoundTag nbt = new CompoundTag();
    nbt.putString(Constants.TileEntity.Banner.BANNER_PATTERN_ID, ((BannerPattern) (Object) layer.shape()).getHashname());
    nbt.putInt(Constants.TileEntity.Banner.BANNER_PATTERN_COLOR, ((net.minecraft.world.item.DyeColor) (Object) layer.color()).getId());
    return nbt;
}
Also used : CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

CompoundTag (net.minecraft.nbt.CompoundTag)476 ListTag (net.minecraft.nbt.ListTag)145 ItemStack (net.minecraft.world.item.ItemStack)63 Tag (net.minecraft.nbt.Tag)53 ArrayList (java.util.ArrayList)43 StringTag (net.minecraft.nbt.StringTag)39 ItemStack (org.bukkit.inventory.ItemStack)37 IntTag (net.minecraft.nbt.IntTag)36 IntArrayTag (net.minecraft.nbt.IntArrayTag)34 ByteArrayTag (net.minecraft.nbt.ByteArrayTag)32 HashMap (java.util.HashMap)25 ByteTag (net.minecraft.nbt.ByteTag)25 DoubleTag (net.minecraft.nbt.DoubleTag)25 FloatTag (net.minecraft.nbt.FloatTag)25 LongTag (net.minecraft.nbt.LongTag)25 ShortTag (net.minecraft.nbt.ShortTag)25 Map (java.util.Map)24 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)24 LongArrayTag (net.minecraft.nbt.LongArrayTag)23 ResourceLocation (net.minecraft.resources.ResourceLocation)21