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());
}
};
}
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;
}
Aggregations