44
55import net .minecraft .core .Holder ;
66import net .minecraft .core .HolderSet ;
7- import net .minecraft .core .registries . BuiltInRegistries ;
7+ import net .minecraft .core .Registry ;
88import net .minecraft .core .registries .Registries ;
99import net .minecraft .resources .ResourceKey ;
1010import net .minecraft .resources .ResourceLocation ;
1111import net .minecraft .tags .TagKey ;
12- import net .minecraft .world .level .material . Fluid ;
12+ import net .minecraft .world .level .block . Blocks ;
1313import net .minecraft .world .level .material .Fluids ;
1414
1515import java .util .ArrayList ;
2020public class ConditionSerializeUtils {
2121
2222 /**
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 ,
23+ * Encode a List<HolderSet<T>> to a string encoding.
24+ * HolderSets are separated by '|', and elements of a direct HolderSet are separated by ','.
25+ *
26+ * @param items The list of HolderSet<T> to be encoded.
27+ * @return A string encoding.
2728 */
28- public static String encodeFluids (List <HolderSet <Fluid >> fluids ) {
29+ public static < T > String encodeHolderSets (List <HolderSet <T >> items ) {
2930 StringBuilder sb = new StringBuilder ();
3031 boolean first = true ;
3132
32- for (HolderSet <Fluid > holderSet : fluids ) {
33+ for (HolderSet <T > holderSet : items ) {
3334 if (!first ) {
3435 sb .append ("|" );
3536 }
@@ -41,14 +42,14 @@ public static String encodeFluids(List<HolderSet<Fluid>> fluids) {
4142 }
4243
4344 /**
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 ,
45+ * Encode a single HolderSet<T > to a string.
46+ * If it's a tag, it's encoded as #+location.
47+ * If it's a direct list, elements are separated by ','.
4748 *
48- * @param holderSet the HolderSet<Fluids > to be encoded
49- * @return the encoded string
49+ * @param holderSet The HolderSet<T > to be encoded.
50+ * @return The encoded string.
5051 */
51- public static String encodeHolderSet (HolderSet <Fluid > holderSet ) {
52+ public static < T > String encodeHolderSet (HolderSet <T > holderSet ) {
5253 return holderSet .unwrap ().map (
5354 // Case 1: Tag
5455 tagKey -> "#" + tagKey .location (),
@@ -59,78 +60,91 @@ public static String encodeHolderSet(HolderSet<Fluid> holderSet) {
5960 }
6061
6162 /**
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.
63+ * Encode a Holder<T> into a String.
64+ *
65+ * @param holder The Holder<T> to be encoded.
66+ * @return A string encoding (location or #+tag_location).
67+ * @throws RuntimeException if the holder cannot be serialized.
6668 */
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 ();
69+ public static <T > String getStringFromHolder (Holder <T > holder ) {
70+ Optional <ResourceKey <T >> keyOpt = holder .unwrapKey ();
7071 if (keyOpt .isPresent ()) {
7172 return keyOpt .get ().location ().toString ();
7273 }
7374
74- // Case 2: If the holder is tagged, return the first tag with a '#' prefix
75- Optional <TagKey <Fluid >> tagOpt = holder .tags ().findFirst ();
75+ Optional <TagKey <T >> tagOpt = holder .tags ().findFirst ();
7676 if (tagOpt .isPresent ()) {
77- return "#" + tagOpt .get ().location (). toString () ;
77+ return "#" + tagOpt .get ().location ();
7878 }
79-
80- throw new RuntimeException ("could not deserialize holder: " + holder );
79+ throw new RuntimeException ("Could not serialize holder: " + holder );
8180 }
8281
8382 /**
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
83+ * Decode a string into a List<HolderSet<T>>.
84+ *
85+ * @param encodedString The string encoding.
86+ * @param registryKey The ResourceKey of the registry.
87+ * @return The decoded list of HolderSet<T>.
9088 */
91- public static List <HolderSet <Fluid >> decodeFluids (String fluidString ) {
92- List <HolderSet <Fluid >> result = new ArrayList <>();
93- for (String token : fluidString .split ("\\ |" )) {
89+ public static <T > List <HolderSet <T >> decodeHolderSets (String encodedString ,
90+ ResourceKey <? extends Registry <T >> registryKey ) {
91+ List <HolderSet <T >> result = new ArrayList <>();
92+ for (String token : encodedString .split ("\\ |" )) {
9493 if (!token .isBlank ()) {
95- result .add (decodeHolderSet (token ));
94+ result .add (decodeSingleHolderSet (token , registryKey ));
9695 }
9796 }
9897 return result ;
9998 }
10099
101100 /**
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
101+ * Decode a string into a HolderSet<T>.
102+ *
103+ * @param encodedSet The encoded set string.
104+ * @param registryKey The ResourceKey of the registry.
105+ * @return The decoded HolderSet<T>.
106+ * @throws RuntimeException if an unknown ID is encountered.
108107 */
109- public static HolderSet <Fluid > decodeHolderSet (String encodedSet ) {
108+ public static <T > HolderSet <T > decodeSingleHolderSet (String encodedSet ,
109+ ResourceKey <? extends Registry <T >> registryKey ) {
110110 encodedSet = encodedSet .trim ();
111111 if (encodedSet .isEmpty ()) {
112112 return HolderSet .direct (List .of ());
113113 }
114114
115+ Registry <T > registry = GTRegistries .builtinRegistry ().registry (registryKey ).get (); // Use the helper
116+
115117 // Case 1: Tag-based holder set
116118 if (encodedSet .startsWith ("#" )) {
117119 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+ TagKey <T > tagKey = TagKey .create (registryKey , tagId );
121+ return registry .getOrCreateTag (tagKey );
120122 }
121123
122- // Case 2: Direct list of fluids
124+ // Case 2: Direct list of items
123125 String [] parts = encodedSet .split ("," );
124- List <Holder <Fluid >> holders = new ArrayList <>();
126+ List <Holder <T >> holders = new ArrayList <>();
125127 for (String part : parts ) {
126128 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 ));
129+ T item = registry .get (rl );
130+ if (item != null && item != getDefaultEmptyValue ( registryKey )) { // Use a generic default empty check
131+ holders .add (registry . wrapAsHolder (item ));
130132 } else {
131- throw new RuntimeException ("Unknown fluid id : " + rl );
133+ throw new RuntimeException ("Unknown ID for registry " + registryKey . location () + " : " + rl );
132134 }
133135 }
134136 return HolderSet .direct (holders );
135137 }
138+
139+ // Helper to get the default "empty" value for a registry type
140+ private static <T > T getDefaultEmptyValue (ResourceKey <? extends Registry <T >> registryKey ) {
141+ // Compare ResourceLocation directly instead of ResourceKey objects
142+ if (registryKey .location ().equals (Registries .FLUID .location ())) {
143+ return (T ) Fluids .EMPTY ;
144+ } else if (registryKey .location ().equals (Registries .BLOCK .location ())) {
145+ return (T ) Blocks .AIR ;
146+ }
147+ throw new IllegalArgumentException (
148+ "Unsupported registry type for default value lookup: " + registryKey .location ());
149+ }
136150}
0 commit comments