|
| 1 | +package com.gregtechceu.gtceu.api.recipe.condition; |
| 2 | + |
| 3 | +import com.gregtechceu.gtceu.api.registry.GTRegistries; |
| 4 | + |
| 5 | +import net.minecraft.core.Holder; |
| 6 | +import net.minecraft.core.HolderSet; |
| 7 | +import net.minecraft.core.registries.BuiltInRegistries; |
| 8 | +import net.minecraft.core.registries.Registries; |
| 9 | +import net.minecraft.resources.ResourceKey; |
| 10 | +import net.minecraft.resources.ResourceLocation; |
| 11 | +import net.minecraft.tags.TagKey; |
| 12 | +import net.minecraft.world.level.material.Fluid; |
| 13 | +import net.minecraft.world.level.material.Fluids; |
| 14 | + |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Optional; |
| 18 | +import java.util.stream.Collectors; |
| 19 | + |
| 20 | +public class ConditionSerializeUtils { |
| 21 | + |
| 22 | + /** |
| 23 | + * Encode a list of HolderSet<Fluids> to a string encoding |
| 24 | + * |
| 25 | + * @param fluids the List<HolderSet<Fluids>> to be encoded |
| 26 | + * @return A string, where HolderSets are separated by |'s and elements of the HolderSet are separated by , |
| 27 | + */ |
| 28 | + public static String encodeFluids(List<HolderSet<Fluid>> fluids) { |
| 29 | + StringBuilder sb = new StringBuilder(); |
| 30 | + boolean first = true; |
| 31 | + |
| 32 | + for (HolderSet<Fluid> holderSet : fluids) { |
| 33 | + if (!first) { |
| 34 | + sb.append("|"); |
| 35 | + } |
| 36 | + sb.append(encodeHolderSet(holderSet)); |
| 37 | + first = false; |
| 38 | + } |
| 39 | + |
| 40 | + return sb.toString(); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Encode a HolderSet<Fluids> to a string encoding |
| 45 | + * The encoding is a string, where if it's a tag, it's encoded as #+location, |
| 46 | + * and if it's a list, elements of the HolderSet are separated by , |
| 47 | + * |
| 48 | + * @param holderSet the HolderSet<Fluids> to be encoded |
| 49 | + * @return the encoded string |
| 50 | + */ |
| 51 | + public static String encodeHolderSet(HolderSet<Fluid> holderSet) { |
| 52 | + return holderSet.unwrap().map( |
| 53 | + // Case 1: Tag |
| 54 | + tagKey -> "#" + tagKey.location(), |
| 55 | + // Case 2: Direct list of holders |
| 56 | + holders -> holders.stream() |
| 57 | + .map(holder -> getStringFromHolder(holder)) |
| 58 | + .collect(Collectors.joining(","))); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Encode a Holder<Fluid> into a String. |
| 63 | + * |
| 64 | + * @param holder the Holder<Fluid> to be encoded |
| 65 | + * @return a string encoding, as # + location if it's a tagkey, or the location if it's a registry entry. |
| 66 | + */ |
| 67 | + public static String getStringFromHolder(Holder<Fluid> holder) { |
| 68 | + // Case 1: If the holder has a registry key, use it |
| 69 | + Optional<ResourceKey<Fluid>> keyOpt = holder.unwrapKey(); |
| 70 | + if (keyOpt.isPresent()) { |
| 71 | + return keyOpt.get().location().toString(); |
| 72 | + } |
| 73 | + |
| 74 | + // Case 2: If the holder is tagged, return the first tag with a '#' prefix |
| 75 | + Optional<TagKey<Fluid>> tagOpt = holder.tags().findFirst(); |
| 76 | + if (tagOpt.isPresent()) { |
| 77 | + return "#" + tagOpt.get().location().toString(); |
| 78 | + } |
| 79 | + |
| 80 | + throw new RuntimeException("could not deserialize holder: " + holder); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Decode a FluidString into a List<HolderSet<Fluid>> |
| 85 | + * The encoding is a string, where if it's a tag, it's encoded as #+location, |
| 86 | + * and if it's a list, elements of the HolderSet are separated by , |
| 87 | + * |
| 88 | + * @param fluidString the string encoding |
| 89 | + * @return The decoded list |
| 90 | + */ |
| 91 | + public static List<HolderSet<Fluid>> decodeFluids(String fluidString) { |
| 92 | + List<HolderSet<Fluid>> result = new ArrayList<>(); |
| 93 | + for (String token : fluidString.split("\\|")) { |
| 94 | + if (!token.isBlank()) { |
| 95 | + result.add(decodeHolderSet(token)); |
| 96 | + } |
| 97 | + } |
| 98 | + return result; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Decode a string into a HolderSet<Fluid> |
| 103 | + * The encoding is a string, where if it's a tag, it's encoded as #+location, |
| 104 | + * and if it's a list, elements of the HolderSet are separated by , |
| 105 | + * |
| 106 | + * @param encodedSet the encoded set |
| 107 | + * @return The decoded list |
| 108 | + */ |
| 109 | + public static HolderSet<Fluid> decodeHolderSet(String encodedSet) { |
| 110 | + encodedSet = encodedSet.trim(); |
| 111 | + if (encodedSet.isEmpty()) { |
| 112 | + return HolderSet.direct(List.of()); |
| 113 | + } |
| 114 | + |
| 115 | + // Case 1: Tag-based holder set |
| 116 | + if (encodedSet.startsWith("#")) { |
| 117 | + ResourceLocation tagId = new ResourceLocation(encodedSet.substring(1)); |
| 118 | + TagKey<Fluid> tagKey = TagKey.create(Registries.FLUID, tagId); |
| 119 | + return GTRegistries.builtinRegistry().registry(Registries.FLUID).get().getOrCreateTag(tagKey); |
| 120 | + } |
| 121 | + |
| 122 | + // Case 2: Direct list of fluids |
| 123 | + String[] parts = encodedSet.split(","); |
| 124 | + List<Holder<Fluid>> holders = new ArrayList<>(); |
| 125 | + for (String part : parts) { |
| 126 | + ResourceLocation rl = new ResourceLocation(part.trim()); |
| 127 | + Fluid fluid = GTRegistries.builtinRegistry().registry(Registries.FLUID).get().get(rl); |
| 128 | + if (fluid != null && fluid != Fluids.EMPTY) { |
| 129 | + holders.add(BuiltInRegistries.FLUID.wrapAsHolder(fluid)); |
| 130 | + } else { |
| 131 | + throw new RuntimeException("Unknown fluid id: " + rl); |
| 132 | + } |
| 133 | + } |
| 134 | + return HolderSet.direct(holders); |
| 135 | + } |
| 136 | +} |
0 commit comments