|
| 1 | +package juuxel.adorn.fluid; |
| 2 | + |
| 3 | +import com.mojang.datafixers.util.Either; |
| 4 | +import com.mojang.datafixers.util.Pair; |
| 5 | +import com.mojang.serialization.Codec; |
| 6 | +import com.mojang.serialization.DataResult; |
| 7 | +import com.mojang.serialization.DynamicOps; |
| 8 | +import net.minecraft.fluid.Fluid; |
| 9 | +import net.minecraft.registry.Registries; |
| 10 | +import net.minecraft.registry.RegistryKeys; |
| 11 | +import net.minecraft.registry.entry.RegistryEntry; |
| 12 | +import net.minecraft.registry.tag.TagKey; |
| 13 | +import net.minecraft.util.Identifier; |
| 14 | +import net.minecraft.util.dynamic.Codecs; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | +import java.util.Set; |
| 18 | +import java.util.function.Function; |
| 19 | +import java.util.stream.Collectors; |
| 20 | + |
| 21 | +final class FluidKeyImpl { |
| 22 | + private static final Codec<Simple> SIMPLE_CODEC = new Codec<>() { |
| 23 | + @Override |
| 24 | + public <T> DataResult<Pair<FluidKeyImpl.Simple, T>> decode(DynamicOps<T> ops, T input) { |
| 25 | + return ops.getStringValue(input) |
| 26 | + .map(s -> { |
| 27 | + if (s.startsWith("#")) { |
| 28 | + return new OfTag(TagKey.of(RegistryKeys.FLUID, new Identifier(s.substring(1)))); |
| 29 | + } else { |
| 30 | + return new OfFluid(Registries.FLUID.get(new Identifier(s))); |
| 31 | + } |
| 32 | + }) |
| 33 | + .map(key -> Pair.of(key, ops.empty())); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public <T> DataResult<T> encode(FluidKeyImpl.Simple input, DynamicOps<T> ops, T prefix) { |
| 38 | + return ops.mergeToPrimitive(prefix, ops.createString(input.getId())); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public String toString() { |
| 43 | + return "SimpleFluidKey"; |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + // TODO: Switch to either instead of xor |
| 48 | + public static final Codec<FluidKey> CODEC = Codecs.xor( |
| 49 | + SIMPLE_CODEC, |
| 50 | + SIMPLE_CODEC.listOf().xmap(OfArray::new, OfArray::children) |
| 51 | + ).xmap( |
| 52 | + // TODO (Java 21): Use pattern matching |
| 53 | + either -> either.map(Function.identity(), Function.identity()), |
| 54 | + key -> { |
| 55 | + if (key instanceof Simple simple) { |
| 56 | + return Either.left(simple); |
| 57 | + } else if (key instanceof OfArray ofArray) { |
| 58 | + return Either.right(ofArray); |
| 59 | + } else { |
| 60 | + throw new IllegalArgumentException(); |
| 61 | + } |
| 62 | + } |
| 63 | + ); |
| 64 | + |
| 65 | + sealed interface Simple extends FluidKey { |
| 66 | + String getId(); |
| 67 | + } |
| 68 | + |
| 69 | + record OfFluid(Fluid fluid) implements Simple { |
| 70 | + @Override |
| 71 | + public String getId() { |
| 72 | + return Registries.FLUID.getId(fluid).toString(); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public Set<Fluid> getFluids() { |
| 77 | + return Set.of(fluid); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public boolean matches(Fluid fluid) { |
| 82 | + return fluid == this.fluid; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + record OfTag(TagKey<Fluid> tag) implements Simple { |
| 87 | + @Override |
| 88 | + public String getId() { |
| 89 | + return "#" + tag.id(); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public Set<Fluid> getFluids() { |
| 94 | + return Registries.FLUID.getOrCreateEntryList(tag) |
| 95 | + .stream() |
| 96 | + .map(RegistryEntry::value) |
| 97 | + .collect(Collectors.toSet()); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public boolean matches(Fluid fluid) { |
| 102 | + return fluid.isIn(tag); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + record OfArray(List<Simple> children) implements FluidKey { |
| 107 | + @Override |
| 108 | + public Set<Fluid> getFluids() { |
| 109 | + return children.stream() |
| 110 | + .flatMap(child -> child.getFluids().stream()) |
| 111 | + .collect(Collectors.toSet()); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public boolean matches(Fluid fluid) { |
| 116 | + for (var child : children) { |
| 117 | + if (child.matches(fluid)) return true; |
| 118 | + } |
| 119 | + |
| 120 | + return false; |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments