Resolve MaterialLib ml references in stack config entries

This commit is contained in:
Ruling-0 committed 2026-08-17 20:18:14 -05:00
1 parent eafd9ff4a3
commit a1631b0f58
4 files changed
+77 -3

No files matched your search

@@ -14,11 +14,13 @@ import com.google.common.eventbus.Subscribe;
import codechicken.core.CommonUtils;
import codechicken.core.launch.CodeChickenCorePlugin;
import codechicken.nei.api.API;
import codechicken.nei.api.IConfigureNEI;
import codechicken.nei.asm.NEICorePlugin;
import codechicken.nei.config.IMCHandler;
import codechicken.nei.guihook.HideousLinkedList;
import codechicken.nei.recipe.GuiRecipeTab;
import codechicken.nei.recipe.stackinfo.MaterialLibStackStringifyHandler;
import cpw.mods.fml.client.FMLFileResourcePack;
import cpw.mods.fml.client.FMLFolderResourcePack;
import cpw.mods.fml.common.DummyModContainer;
@@ -113,6 +115,10 @@ public class NEIModContainer extends DummyModContainer {
IMCForNEI.IMCSender();
}
ServerHandler.load();
if (Loader.isModLoaded("materiallib")) {
API.registerStackStringifyHandler(new MaterialLibStackStringifyHandler());
}
}
@Subscribe
@@ -537,14 +537,22 @@ public class SubsetWidget extends Button implements ItemFilterProvider {
return;
}
final int[] resolved = new int[1];
final int[] invalid = new int[1];
Stream.of(dir.listFiles()).filter(file -> !file.isDirectory()).forEach(file -> {
ClientHandler.loadSettingsFile(
"subsets/" + file.getName(),
lines -> parseFile(file.getName(), lines.collect(Collectors.toList())));
lines -> parseFile(file.getName(), lines.collect(Collectors.toList()), resolved, invalid));
});
if (resolved[0] + invalid[0] > 0) {
NEIClientConfig.logger
.info("NotEnoughItems: resolved {} MaterialLib entries ({} invalid)", resolved[0], invalid[0]);
}
}
private static void parseFile(String resource, List<String> itemStrings) {
private static void parseFile(String resource, List<String> itemStrings, int[] resolved, int[] invalid) {
final JsonParser parser = new JsonParser();
final int dotIndex = resource.lastIndexOf('.');
final String subsetNamespace = dotIndex > 0 ? resource.substring(0, dotIndex) : resource;
@@ -574,7 +582,17 @@ public class SubsetWidget extends Button implements ItemFilterProvider {
final NBTBase nbt = NBTJson.toNbt(parser.parse(itemStr));
if (nbt instanceof NBTTagCompound tag) {
((ItemStackSet) processedTag.filter).add(StackInfo.loadFromNBT(tag));
final ItemStack stack = StackInfo.loadFromNBT(tag);
if (tag.getString("strId").startsWith(StackInfo.MATERIALLIB_PREFIX)) {
if (stack != null) {
resolved[0]++;
} else {
invalid[0]++;
}
}
((ItemStackSet) processedTag.filter).add(stack);
} else {
throw new IllegalArgumentException(
"Expected NBTTagCompound but got " + nbt.getClass().getSimpleName());
@@ -27,6 +27,11 @@ import codechicken.nei.util.NBTHelper;
public class StackInfo {
/// Prefix of the stable MaterialLib reference form `ml:<MaterialName>:<shapeToken>`, which config entries use
/// instead of an id and metadata because MaterialLib hands out metadata afresh every session.
/// MaterialLibStackStringifyHandler resolves it, and is registered only when MaterialLib is present.
public static final String MATERIALLIB_PREFIX = "ml:";
private static final FluidStack NULL_FLUID = new FluidStack(FluidRegistry.WATER, 0);
public static final List<IStackStringifyHandler> stackStringifyHandlers = new ArrayList<>();
private static final Map<String, HashMap<String, String[]>> guidfilters = new HashMap<>();
@@ -0,0 +1,45 @@
package codechicken.nei.recipe.stackinfo;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import com.ruling_0.materiallib.api.StackResolver;
import codechicken.nei.api.IStackStringifyHandler;
import codechicken.nei.recipe.StackInfo;
/// Reads config entries written in the stable MaterialLib reference form
/// `ml:<MaterialName>:<shapeToken>` (see [StackInfo#MATERIALLIB_PREFIX]) and resolves them against the registries,
/// so an entry keeps naming the same item across sessions that hand out different metadata.
///
/// The resolved stack carries the metadata MaterialLib currently assigns, overriding any `Damage` the entry
/// carries alongside the id.
///
/// This is the only class in NotEnoughItems that references the MaterialLib API, and every reference to it is
/// gated on `Loader.isModLoaded("materiallib")` so it never classloads without MaterialLib present.
public class MaterialLibStackStringifyHandler implements IStackStringifyHandler {
@Override
public ItemStack convertNBTToItemStack(NBTTagCompound nbtTag) {
final String strId = nbtTag.getString("strId");
if (!strId.startsWith(StackInfo.MATERIALLIB_PREFIX)) {
return null;
}
final String[] parts = strId.split(":");
if (parts.length != 3) {
return null;
}
final ItemStack stack = StackResolver.getStack(parts[1], parts[2], nbtTag.getInteger("Count"));
if (stack != null && nbtTag.hasKey("tag")) {
stack.setTagCompound((NBTTagCompound) nbtTag.getCompoundTag("tag").copy());
}
return stack;
}
}