Search in sources :

Example 36 with ListTag

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

the class PrimaryLevelDataMixin method bridge$writeSpongeLevelData.

@Override
public CompoundTag bridge$writeSpongeLevelData() {
    final CompoundTag data = new CompoundTag();
    data.putUUID(Constants.Sponge.World.UNIQUE_ID, this.bridge$uniqueId());
    // Map Storage
    final CompoundTag mapUUIDIndexTag = new CompoundTag();
    MapUtil.saveMapUUIDIndex(mapUUIDIndexTag, this.bridge$getMapUUIDIndex());
    data.put(Constants.Map.MAP_UUID_INDEX, mapUUIDIndexTag);
    final ListTag playerIdList = new ListTag();
    data.put(Constants.Sponge.SPONGE_PLAYER_UUID_TABLE, playerIdList);
    this.impl$pendingUniqueIds.forEach(uuid -> playerIdList.add(new IntArrayTag(SerializableUUID.uuidToIntArray(uuid))));
    this.impl$pendingUniqueIds.clear();
    return data;
}
Also used : IntArrayTag(net.minecraft.nbt.IntArrayTag) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 37 with ListTag

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

the class ChunkSerializerMixin_Tracker method impl$writeSpongeLevelData.

@Inject(method = "write", at = @At(value = "RETURN"))
private static void impl$writeSpongeLevelData(final ServerLevel param0, final ChunkAccess param1, final CallbackInfoReturnable<CompoundTag> cir) {
    if (!(param1 instanceof LevelChunk)) {
        return;
    }
    final LevelChunkBridge chunk = (LevelChunkBridge) param1;
    if (!chunk.bridge$getTrackedShortPlayerPositions().isEmpty() || !chunk.bridge$getTrackedIntPlayerPositions().isEmpty()) {
        final CompoundTag level = (CompoundTag) cir.getReturnValue().get("Level");
        final CompoundTag trackedNbt = new CompoundTag();
        final ListTag positions = new ListTag();
        trackedNbt.put(Constants.Sponge.SPONGE_BLOCK_POS_TABLE, positions);
        level.put(Constants.Sponge.Data.V2.SPONGE_DATA, trackedNbt);
        ChunkSerializerMixin_Tracker.impl$writeMap(positions, chunk.bridge$getTrackedShortPlayerPositions(), (nbt, pos) -> nbt.putShort("pos", pos));
        ChunkSerializerMixin_Tracker.impl$writeMap(positions, chunk.bridge$getTrackedIntPlayerPositions(), (nbt, pos) -> nbt.putInt("ipos", pos));
    }
}
Also used : LevelChunk(net.minecraft.world.level.chunk.LevelChunk) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 38 with ListTag

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

the class ChunkSerializerMixin_Tracker method impl$readSpongeLevelData.

@Redirect(method = "read", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/chunk/ChunkAccess;setLightCorrect(Z)V"))
private static void impl$readSpongeLevelData(final ChunkAccess chunkAccess, final boolean var1, final ServerLevel param0, final StructureManager param1, final PoiManager param2, final ChunkPos param3, final CompoundTag param4) {
    if (!(chunkAccess instanceof LevelChunk)) {
        return;
    }
    final CompoundTag level = (CompoundTag) param4.get("Level");
    final CompoundTag spongeData = level.getCompound(Constants.Sponge.Data.V2.SPONGE_DATA);
    if (spongeData.isEmpty()) {
        return;
    }
    final Map<Integer, PlayerTracker> trackedIntPlayerPositions = new HashMap<>();
    final Map<Short, PlayerTracker> trackedShortPlayerPositions = new HashMap<>();
    final ListTag list = spongeData.getList(Constants.Sponge.SPONGE_BLOCK_POS_TABLE, 10);
    final LevelChunkBridge chunk = (LevelChunkBridge) chunkAccess;
    for (Tag tag : list) {
        final PlayerTracker tracker = new PlayerTracker();
        final CompoundTag data = (CompoundTag) tag;
        final boolean isShortPos = data.contains("pos");
        if (data.contains("owner")) {
            tracker.creatorindex = data.getInt("owner");
        }
        if (data.contains("notifier")) {
            tracker.notifierIndex = data.getInt("notifier");
        }
        if (tracker.notifierIndex != -1 || tracker.creatorindex != -1) {
            if (isShortPos) {
                trackedShortPlayerPositions.put(data.getShort("pos"), tracker);
            } else {
                trackedIntPlayerPositions.put(data.getInt("ipos"), tracker);
            }
        }
    }
    chunk.bridge$setTrackedIntPlayerPositions(trackedIntPlayerPositions);
    chunk.bridge$setTrackedShortPlayerPositions(trackedShortPlayerPositions);
}
Also used : LevelChunk(net.minecraft.world.level.chunk.LevelChunk) HashMap(java.util.HashMap) PlayerTracker(org.spongepowered.common.entity.PlayerTracker) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) Tag(net.minecraft.nbt.Tag) CompoundTag(net.minecraft.nbt.CompoundTag) ListTag(net.minecraft.nbt.ListTag) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 39 with ListTag

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

the class ShieldItemStackData method register.

// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
    registrator.asMutable(ItemStack.class).create(Keys.DYE_COLOR).get(h -> {
        final CompoundTag tag = h.getTagElement(Constants.Item.BLOCK_ENTITY_TAG);
        if (tag == null || tag.contains(Constants.TileEntity.Banner.BANNER_PATTERNS, Constants.NBT.TAG_LIST)) {
            return DyeColors.WHITE.get();
        }
        final int id = tag.getInt(Constants.TileEntity.Banner.BANNER_BASE);
        return (DyeColor) (Object) net.minecraft.world.item.DyeColor.byId(id);
    }).set((h, v) -> {
        final CompoundTag tag = h.getOrCreateTagElement(Constants.Item.BLOCK_ENTITY_TAG);
        tag.putInt(Constants.TileEntity.Banner.BANNER_BASE, ((net.minecraft.world.item.DyeColor) (Object) v).getId());
    }).supports(h -> h.getItem() instanceof ShieldItem).create(Keys.BANNER_PATTERN_LAYERS).get(h -> {
        final CompoundTag tag = h.getTagElement(Constants.Item.BLOCK_ENTITY_TAG);
        if (tag == null || !tag.contains(Constants.TileEntity.Banner.BANNER_PATTERNS, Constants.NBT.TAG_LIST)) {
            return new ArrayList<>();
        }
        final ListTag layersList = tag.getList(Constants.TileEntity.Banner.BANNER_PATTERNS, Constants.NBT.TAG_COMPOUND);
        return layersList.stream().map(layer -> ShieldItemStackData.layerFromNbt((CompoundTag) layer)).collect(Collectors.toList());
    }).set((h, v) -> {
        final ListTag layersTag = v.stream().filter(layer -> layer.shape() != BannerPatternShapes.BASE.get()).map(ShieldItemStackData::layerToNbt).collect(NBTCollectors.toTagList());
        final CompoundTag blockEntity = h.getOrCreateTagElement(Constants.Item.BLOCK_ENTITY_TAG);
        blockEntity.put(Constants.TileEntity.Banner.BANNER_PATTERNS, layersTag);
        if (h.getItem() instanceof ShieldItem) {
            // TODO reject BannerPatternShapes.BASE for BannerItem?
            v.stream().filter(layer -> layer.shape() == BannerPatternShapes.BASE.get()).forEach(layer -> {
                blockEntity.putInt(Constants.TileEntity.Banner.BANNER_BASE, ((net.minecraft.world.item.DyeColor) (Object) layer.color()).getId());
            });
        }
    }).supports(h -> h.getItem() instanceof ShieldItem || h.getItem() instanceof BannerItem);
}
Also used : ShieldItem(net.minecraft.world.item.ShieldItem) DyeColor(org.spongepowered.api.data.type.DyeColor) Constants(org.spongepowered.common.util.Constants) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) BannerPattern(net.minecraft.world.level.block.entity.BannerPattern) ArrayList(java.util.ArrayList) BannerItem(net.minecraft.world.item.BannerItem) Keys(org.spongepowered.api.data.Keys) CompoundTag(net.minecraft.nbt.CompoundTag) BannerPatternLayer(org.spongepowered.api.data.meta.BannerPatternLayer) DyeColors(org.spongepowered.api.data.type.DyeColors) NBTCollectors(org.spongepowered.common.util.NBTCollectors) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) Map(java.util.Map) BannerPatternShape(org.spongepowered.api.data.type.BannerPatternShape) ItemStack(net.minecraft.world.item.ItemStack) BannerPatternShapes(org.spongepowered.api.data.type.BannerPatternShapes) ListTag(net.minecraft.nbt.ListTag) ShieldItem(net.minecraft.world.item.ShieldItem) BannerItem(net.minecraft.world.item.BannerItem) ArrayList(java.util.ArrayList) DyeColor(org.spongepowered.api.data.type.DyeColor) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 40 with ListTag

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

the class BookPagesItemStackData method set.

private static boolean set(final ItemStack holder, final List<Enchantment> value, final Function<List<Enchantment>, Stream<Enchantment>> filter, final String nbtKey) {
    if (value.isEmpty()) {
        return BookPagesItemStackData.delete(holder, nbtKey);
    }
    final CompoundTag tag = holder.getOrCreateTag();
    final ListTag list = filter.apply(value).map(BookPagesItemStackData::enchantmentToNbt).collect(NBTCollectors.toTagList());
    tag.put(nbtKey, list);
    return true;
}
Also used : ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

ListTag (net.minecraft.nbt.ListTag)52 CompoundTag (net.minecraft.nbt.CompoundTag)38 ItemStack (net.minecraft.world.item.ItemStack)10 TagCompound (de.keyle.knbt.TagCompound)8 TagList (de.keyle.knbt.TagList)8 List (java.util.List)7 IntArrayTag (net.minecraft.nbt.IntArrayTag)7 ResourceLocation (net.minecraft.resources.ResourceLocation)7 TagString (de.keyle.knbt.TagString)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)5 StringTag (net.minecraft.nbt.StringTag)5 ByteArrayTag (net.minecraft.nbt.ByteArrayTag)4 IntTag (net.minecraft.nbt.IntTag)4 Tag (net.minecraft.nbt.Tag)4 ByteTag (net.minecraft.nbt.ByteTag)3 DoubleTag (net.minecraft.nbt.DoubleTag)3 FloatTag (net.minecraft.nbt.FloatTag)3 LongTag (net.minecraft.nbt.LongTag)3 ShortTag (net.minecraft.nbt.ShortTag)3