Implement native fluid-state JSON decoding and encoding

This commit is contained in:
Ludovic Marie
2026-10-06 12:32:23 +02:00
parent 7984238b3e
commit 27cbdfdbde
8 changed files with 173 additions and 31 deletions
@@ -1,5 +1,7 @@
package dev.horizonlab.parity;
import com.google.gson.*;
import com.mojang.serialization.JsonOps;
import net.minecraft.world.level.material.FluidState;
import java.io.*;
import java.lang.reflect.*;
import java.nio.file.*;
@@ -23,6 +25,16 @@ public final class VanillaFluidOracle {
var state=block.getFluidState();boolean falling=state.hasProperty(FlowingFluid.FALLING)&&state.getValue(FlowingFluid.FALLING);
out.println("state "+Block.getId(block)+" "+BuiltInRegistries.FLUID.getKey(state.getType())+" "+state.getAmount()+" "+falling+" "+state.isFull()+" "+state.isSource()+" "+Integer.toUnsignedString(Float.floatToRawIntBits(state.getOwnHeight()))+" "+Block.getId(state.createLegacyBlock()));
}
for(var line:Files.readAllLines(dir.resolve("codec-inputs.txt"))) {
var decoded=FluidState.CODEC.parse(JsonOps.INSTANCE,JsonParser.parseString(line)).result();
if(decoded.isEmpty()){out.println("codec error");continue;}
var state=decoded.get();boolean falling=state.hasProperty(FlowingFluid.FALLING)&&state.getValue(FlowingFluid.FALLING);
out.println("codec "+BuiltInRegistries.FLUID.getKey(state.getType())+" "+state.getAmount()+" "+falling+" "+state.isSource()+" "+Block.getId(state.createLegacyBlock()));
var raw=FluidState.CODEC.encodeStart(JsonOps.INSTANCE,state).getOrThrow().getAsJsonObject();
var canonical=new JsonObject();canonical.add("id",raw.get("id"));
if(raw.has("properties")){var properties=new JsonObject();new java.util.TreeMap<>(raw.getAsJsonObject("properties").asMap()).forEach(properties::add);canonical.add("properties",properties);}
out.println("encoded "+canonical);
}
for(var line:(Iterable<String>)inputs::iterator) {
var f=JsonParser.parseString(line).getAsJsonObject();var state=Block.stateById(f.get("state").getAsInt()).getFluidState();
var above=Block.stateById(f.get("above").getAsInt()).getFluidState();int[] calls={0};int fail=f.get("fail").getAsInt();