Move LoreHolder registration to a later init stage (#118)

Co-authored-by: Maya <[email protected]>
This commit is contained in:
LyftandMaya authored and GitHub committed 2025-03-05 15:47:22 +00:00
1 parent ec37b0068e
commit 1c66f22a4b
2 files changed
+32 -10

No files matched your search

@@ -26,6 +26,7 @@ public final class LoreHandler implements IResourceManagerReloadListener {
private static final Random RANDOM = new Random();
public static void postInit() {
LoreHolderDiscoverer.register();
((SimpleReloadableResourceManager) Minecraft.getMinecraft().getResourceManager())
.registerReloadListener(new LoreHandler());
}
@@ -59,7 +60,7 @@ public final class LoreHandler implements IResourceManagerReloadListener {
private static String getRandomLine(String keyPrefix) {
List<WeightedRandom.Item> lines = getAllLines(keyPrefix);
if (lines.size() == 0) {
if (lines.isEmpty()) {
return null;
}
@@ -1,6 +1,8 @@
package com.gtnewhorizon.gtnhlib.client.tooltip;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
@@ -12,6 +14,7 @@ import com.gtnewhorizon.gtnhlib.reflect.Fields.LookupType;
import cpw.mods.fml.common.discovery.ASMDataTable;
import cpw.mods.fml.common.discovery.ASMDataTable.ASMData;
import lombok.Data;
public class LoreHolderDiscoverer {
@@ -20,40 +23,58 @@ public class LoreHolderDiscoverer {
*/
static final Map<ClassFields<?>.Field<String>, String> LORE_HOLDERS = new HashMap<>();
static final List<LoreField> LORE_FIELDS = new ArrayList<>();
public static void harvestData(ASMDataTable table) {
for (ASMData asmData : table.getAll(LoreHolder.class.getName())) {
String className = asmData.getClassName();
String fieldName = asmData.getObjectName();
String key = (String) asmData.getAnnotationInfo().get("value");
if (StringUtils.isNotBlank(key)) {
LORE_FIELDS.add(new LoreField(className, fieldName, key));
}
}
}
static void register() {
for (LoreField loreField : LORE_FIELDS) {
String className = loreField.getClassName();
Class<?> clazz;
try {
clazz = Class.forName(className);
} catch (ClassNotFoundException e) {
GTNHLib.LOG.error("Class " + className + " could not be found!", e);
GTNHLib.LOG.error("Class {} could not be found!", className, e);
continue;
}
String fieldName = asmData.getObjectName();
String fieldName = loreField.getFieldName();
ClassFields<?>.Field<String> field;
try {
field = Fields.ofClass(clazz).getField(LookupType.DECLARED, fieldName, String.class);
} catch (ClassCastException e) {
GTNHLib.LOG.error(
"Field " + fieldName + " of class " + className + " is not of type java.lang.String!",
e);
GTNHLib.LOG.error("Field {} of class {} is not of type java.lang.String!", fieldName, className, e);
continue;
}
if (field == null) {
GTNHLib.LOG.error("Field {} of class {} could not be found!", fieldName, className);
continue;
}
if (!field.isStatic) {
GTNHLib.LOG.error("Field {} of class {} is not static!", fieldName, className);
continue;
}
String key = (String) asmData.getAnnotationInfo().get("value");
if (StringUtils.isNotBlank(key)) {
LORE_HOLDERS.put(field, key);
}
LORE_HOLDERS.put(field, loreField.getKey());
}
}
@Data
private static class LoreField {
private final String className;
private final String fieldName;
private final String key;
}
}