Skip to content

Commit 120a9a6

Browse files
committed
Fix from 1.21.1 branch and added class so custom_name data is read correctly
1 parent 243c525 commit 120a9a6

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package timmychips.modefiteitemdefinitions.comp.util;
2+
3+
public class TextUtils {
4+
5+
// The strings to remove from input to get base string
6+
private static final String TEXT_LITERAL = "{\"text\":\"";
7+
private static final String END_LITERAL = "\"}";
8+
9+
/**
10+
*
11+
* @param text The String Minecraft text object
12+
* <p>Example text String object looks like: " {"text":"Hammer of Justice"} "
13+
* @return Removes and returns just the contents from the text literal
14+
* <p>Example return string: " Hammer of Justice "
15+
*/
16+
public static String fromLiteral(String text) {
17+
String str = text.replace(TEXT_LITERAL, "");
18+
str = str.replace(END_LITERAL, "");
19+
20+
return str;
21+
}
22+
}

src/client/java/timmychips/modefiteitemdefinitions/property/resolver/selectcase/ComponentCase.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import net.minecraft.util.Identifier;
1111
import org.slf4j.Logger;
1212
import timmychips.modefiteitemdefinitions.comp.ComponentType;
13+
import timmychips.modefiteitemdefinitions.comp.util.TextUtils;
1314
import timmychips.modefiteitemdefinitions.property.handler.SelectPropertyHandler;
1415
import timmychips.modefiteitemdefinitions.property.helper.EntityVariantHelper;
1516
import timmychips.modefiteitemdefinitions.property.resolver.ResolveRecursive;
@@ -58,11 +59,12 @@ public String getValue(ItemStack stack, LivingEntity entity, ModelTransformation
5859
String str;
5960

6061
Object componentValue = componentType.get(stack);
61-
if (componentValue instanceof Text textValue) {
62-
str = textValue.getString(); // Get the string without the surrounding literal from Text component types, and with string as is
62+
if (componentValue instanceof String textValue) {
63+
str = TextUtils.fromLiteral(textValue); // Get string text contents
64+
return str; // Return just the string for text
6365
}
64-
else str = String.valueOf(componentValue).toLowerCase(); // Convert value to lower case string
66+
else str = String.valueOf(componentValue).toLowerCase(); // Convert non-text values to lower case string
6567

66-
return String.valueOf(Identifier.tryParse(str)); // Return component value as string in identifier format (even for text)
68+
return String.valueOf(Identifier.tryParse(str)); // Return component non-text value as string in identifier format
6769
}
6870
}

src/client/java/timmychips/modefiteitemdefinitions/property/type/codec/SelectDefinition.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ public record Definition(
4141
.map(c -> new Case<String>(
4242
c.model(),
4343
c.when.stream()
44-
.map(whenCondition -> String.valueOf(Identifier.tryParse(whenCondition)))
44+
.map(whenCondition -> {
45+
// Try parse the condition string into identifier format. If it's string, "null", just return the
46+
// when condition string as is for stuff like the custom_name component
47+
String idStr = String.valueOf(Identifier.tryParse(whenCondition));
48+
return !idStr.equals("null") ? idStr : whenCondition;
49+
})
4550
.collect(Collectors.toCollection(HashSet::new))
4651
))
4752
.toList();

0 commit comments

Comments
 (0)