Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move LoreHolder registration to a later init stage #118

Merged
merged 2 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -59,7 +60,7 @@ public static void registerLoreHolder(Class<?> clazz) {}
private static String getRandomLine(String keyPrefix) {
List<WeightedRandom.Item> lines = getAllLines(keyPrefix);

if (lines.size() == 0) {
if (lines.isEmpty()) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,6 +14,7 @@

import cpw.mods.fml.common.discovery.ASMDataTable;
import cpw.mods.fml.common.discovery.ASMDataTable.ASMData;
import lombok.Data;

public class LoreHolderDiscoverer {

Expand All @@ -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;
}
}