Search in sources :

Example 51 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 52 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 53 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 54 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)

Example 55 with CompoundTag

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

the class BookPagesItemStackData method get.

// @formatter:on
private static List<Enchantment> get(final ItemStack holder, final String nbtKey) {
    final CompoundTag tag = holder.getTag();
    if (tag == null || !tag.contains(nbtKey, Constants.NBT.TAG_LIST)) {
        return new ArrayList<>();
    }
    final ListTag list = tag.getList(nbtKey, Constants.NBT.TAG_COMPOUND);
    return NBTStreams.toCompounds(list).map(BookPagesItemStackData::enchantmentFromNbt).filter(Objects::nonNull).collect(Collectors.toList());
}
Also used : ArrayList(java.util.ArrayList) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

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