Search in sources :

Example 1 with Codec

use of com.mojang.serialization.Codec in project BudschieMorphMod by Budschie.

the class ModCodecs method getRegistryCodec.

private static <A extends IForgeRegistryEntry<A>> Codec<A> getRegistryCodec(Function<ResourceLocation, A> registryRetrieval) {
    return new Codec<A>() {

        @Override
        public <T> DataResult<T> encode(A input, DynamicOps<T> ops, T prefix) {
            return DataResult.<T>success(ops.createString(input.getRegistryName().toString()));
        }

        @Override
        public <T> DataResult<Pair<A, T>> decode(DynamicOps<T> ops, T input) {
            DataResult<String> rl = ops.getStringValue(input);
            if (rl.result().isPresent()) {
                ResourceLocation result = new ResourceLocation(rl.result().get());
                A retrieved = registryRetrieval.apply(result);
                if (retrieved == null)
                    return DataResult.error(String.format("The resource location %s did not yield any registry entry when tried to resolve into an actual instance.", result));
                else
                    return DataResult.<Pair<A, T>>success(Pair.of(retrieved, input));
            } else
                return DataResult.error(rl.error().get().message());
        }
    };
}
Also used : Codec(com.mojang.serialization.Codec) ResourceLocation(net.minecraft.util.ResourceLocation) DynamicOps(com.mojang.serialization.DynamicOps) Pair(com.mojang.datafixers.util.Pair)

Example 2 with Codec

use of com.mojang.serialization.Codec in project Insights by InsightsPlugin.

the class UnloadedChunkContainer method getChunkSections.

@Override
@SuppressWarnings("deprecation")
public LevelChunkSection[] getChunkSections() throws IOException {
    var serverLevel = ((CraftWorld) world).getHandle();
    int sectionsCount = serverLevel.getSectionsCount();
    var chunkSections = new LevelChunkSection[sectionsCount];
    var chunkMap = serverLevel.getChunkSource().chunkMap;
    var chunkPos = new ChunkPos(chunkX, chunkZ);
    CompoundTag tag = chunkMap.read(chunkPos);
    if (tag == null)
        return chunkSections;
    tag = chunkMap.upgradeChunkTag(serverLevel.getTypeKey(), () -> serverLevel.getServer().overworld().getDataStorage(), tag, chunkMap.generator.getTypeNameForDataFixer(), chunkPos, serverLevel);
    ListTag sectionsTagList = tag.getList("sections", 10);
    DataResult<PalettedContainer<BlockState>> dataResult;
    for (var i = 0; i < sectionsTagList.size(); i++) {
        CompoundTag sectionTag = sectionsTagList.getCompound(i);
        var chunkSectionPart = sectionTag.getByte("Y");
        var sectionIndex = serverLevel.getSectionIndexFromSectionY(chunkSectionPart);
        if (sectionIndex < 0 || sectionIndex >= chunkSections.length)
            continue;
        PalettedContainer<BlockState> blockStateContainer;
        if (sectionTag.contains("block_states", 10)) {
            Codec<PalettedContainer<BlockState>> blockStateCodec = ChunkSerializer.BLOCK_STATE_CODEC;
            dataResult = blockStateCodec.parse(NbtOps.INSTANCE, sectionTag.getCompound("block_states")).promotePartial(message -> logger.severe(String.format(CHUNK_ERROR, chunkX, chunkSectionPart, chunkZ, message)));
            blockStateContainer = dataResult.getOrThrow(false, logger::severe);
        } else {
            blockStateContainer = new PalettedContainer<>(Block.BLOCK_STATE_REGISTRY, Blocks.AIR.defaultBlockState(), PalettedContainer.Strategy.SECTION_STATES);
        }
        LevelChunkSection chunkSection = new LevelChunkSection(chunkSectionPart, blockStateContainer, null);
        chunkSections[sectionIndex] = chunkSection;
    }
    return chunkSections;
}
Also used : ChunkSerializer(net.minecraft.world.level.chunk.storage.ChunkSerializer) ChunkCuboid(dev.frankheijden.insights.api.objects.chunk.ChunkCuboid) BlockState(net.minecraft.world.level.block.state.BlockState) IOException(java.io.IOException) PalettedContainer(net.minecraft.world.level.chunk.PalettedContainer) Logger(java.util.logging.Logger) NbtOps(net.minecraft.nbt.NbtOps) CraftWorld(org.bukkit.craftbukkit.v1_18_R2.CraftWorld) Blocks(net.minecraft.world.level.block.Blocks) ChunkPos(net.minecraft.world.level.ChunkPos) Codec(com.mojang.serialization.Codec) ScanOptions(dev.frankheijden.insights.api.concurrent.ScanOptions) CompoundTag(net.minecraft.nbt.CompoundTag) LevelChunkSection(net.minecraft.world.level.chunk.LevelChunkSection) World(org.bukkit.World) Block(net.minecraft.world.level.block.Block) DataResult(com.mojang.serialization.DataResult) InsightsPlugin(dev.frankheijden.insights.api.InsightsPlugin) ListTag(net.minecraft.nbt.ListTag) ListTag(net.minecraft.nbt.ListTag) BlockState(net.minecraft.world.level.block.state.BlockState) LevelChunkSection(net.minecraft.world.level.chunk.LevelChunkSection) ChunkPos(net.minecraft.world.level.ChunkPos) CraftWorld(org.bukkit.craftbukkit.v1_18_R2.CraftWorld) CompoundTag(net.minecraft.nbt.CompoundTag) PalettedContainer(net.minecraft.world.level.chunk.PalettedContainer)

Aggregations

Codec (com.mojang.serialization.Codec)2 Pair (com.mojang.datafixers.util.Pair)1 DataResult (com.mojang.serialization.DataResult)1 DynamicOps (com.mojang.serialization.DynamicOps)1 InsightsPlugin (dev.frankheijden.insights.api.InsightsPlugin)1 ScanOptions (dev.frankheijden.insights.api.concurrent.ScanOptions)1 ChunkCuboid (dev.frankheijden.insights.api.objects.chunk.ChunkCuboid)1 IOException (java.io.IOException)1 Logger (java.util.logging.Logger)1 CompoundTag (net.minecraft.nbt.CompoundTag)1 ListTag (net.minecraft.nbt.ListTag)1 NbtOps (net.minecraft.nbt.NbtOps)1 ResourceLocation (net.minecraft.util.ResourceLocation)1 ChunkPos (net.minecraft.world.level.ChunkPos)1 Block (net.minecraft.world.level.block.Block)1 Blocks (net.minecraft.world.level.block.Blocks)1 BlockState (net.minecraft.world.level.block.state.BlockState)1 LevelChunkSection (net.minecraft.world.level.chunk.LevelChunkSection)1 PalettedContainer (net.minecraft.world.level.chunk.PalettedContainer)1 ChunkSerializer (net.minecraft.world.level.chunk.storage.ChunkSerializer)1