From d575de33feef2c5af4a5777a010caa890a90f434 Mon Sep 17 00:00:00 2001 From: Jason Mitchell Date: Sat, 10 Apr 2021 22:31:32 -0700 Subject: [PATCH] IMCHandler * Add IMCHandler w/ `registerHandlerInfo` & `removeHandlerInfo` (NBT) message handling --- README.md | 5 +- build.properties | 2 +- src/codechicken/nei/NEIModContainer.java | 22 ++++++ src/codechicken/nei/config/IMCHandler.java | 87 +++++++++++++++++++++ src/codechicken/nei/recipe/HandlerInfo.java | 20 +++++ 5 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 src/codechicken/nei/config/IMCHandler.java diff --git a/README.md b/README.md index c2655251..4816182e 100644 --- a/README.md +++ b/README.md @@ -19,9 +19,10 @@ We've tested this against all of the mods included in GTNH, as well as a limited * Utility/Cheat buttons line up and wrap based on GUI size * ItemList is no longer regenerated from the ItemRegistry on every inventory load * JEI (Or Creative) Style tabs [Optional] Note: Requires explicit support to be added for an ItemStack to render, otherwise falls back to the first two letters of the handler name. -* Tabs/Handlers are loaded from a CSV config in the JAR (or optionally from the config folder). IMCEvent for mod authors to register this information is planned. +* Tabs/Handlers are loaded from a CSV config in the JAR (or optionally from the config folder). NBT IMCEvents `registerHandlerInfo` and `removeHandlerInfo` are available for mod authors to add handler information, using the same fields as the CSV file * `@[Mod]->[item]` searching. ex: `@Gregtech->iron ingot` -* Cycle between Recipe, Utility, and Cheat mode by cntrl clicking on the Wrench Icon +* Cycle between Recipe, Utility, and Cheat mode by ctrl clicking on the Wrench Icon +* GT5u Tools/Items should now properly work with the Overlay Recipe Transfer ## Other items of note: diff --git a/build.properties b/build.properties index d1d8da2b..14e2c4a9 100644 --- a/build.properties +++ b/build.properties @@ -2,4 +2,4 @@ mc_version=1.7.10 forge_version=10.13.4.1614-1.7.10 ccl_version=1.1.3.138 ccc_version=1.0.7.+ -mod_version=2.1.0-GTNH-beta15 +mod_version=2.1.0-GTNH-beta16 diff --git a/src/codechicken/nei/NEIModContainer.java b/src/codechicken/nei/NEIModContainer.java index be15748e..fcc3baa6 100644 --- a/src/codechicken/nei/NEIModContainer.java +++ b/src/codechicken/nei/NEIModContainer.java @@ -4,7 +4,10 @@ import codechicken.core.CommonUtils; import codechicken.core.launch.CodeChickenCorePlugin; import codechicken.nei.api.IConfigureNEI; import codechicken.nei.asm.NEICorePlugin; +import codechicken.nei.config.IMCHandler; import codechicken.nei.recipe.GuiRecipeTab; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.ImmutableList; import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; import cpw.mods.fml.client.FMLFileResourcePack; @@ -14,6 +17,7 @@ import cpw.mods.fml.common.LoadController; import cpw.mods.fml.common.MetadataCollection; import cpw.mods.fml.common.ModMetadata; import cpw.mods.fml.common.event.FMLInitializationEvent; +import cpw.mods.fml.common.event.FMLInterModComms; import cpw.mods.fml.common.event.FMLPreInitializationEvent; import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.versioning.ArtifactVersion; @@ -22,6 +26,7 @@ import cpw.mods.fml.common.versioning.VersionRange; import net.minecraft.util.EnumChatFormatting; import java.io.File; +import java.lang.reflect.Field; import java.util.HashSet; import java.util.LinkedList; import java.util.List; @@ -97,7 +102,24 @@ public class NEIModContainer extends DummyModContainer public void postInit(FMLPostInitializationEvent event) { if (CommonUtils.isClient()) GuiRecipeTab.loadHandlerInfo(); + handleIMCMessages(); } + + @SuppressWarnings("unchecked") + private void handleIMCMessages() { + try { + /* Pay no attention to these unholy hacks: + * NEI Apparently doesn't have a registered mod container, so let's use some reflection to simulate what `fetchRuntimeMessages` does + */ + Field modMessagesField = FMLInterModComms.class.getDeclaredField("modMessages"); + modMessagesField.setAccessible(true); + IMCHandler.processIMC(ImmutableList.copyOf(((ArrayListMultimap)modMessagesField.get(FMLInterModComms.class)).removeAll("NotEnoughItems"))); + } catch (NoSuchFieldException | IllegalAccessException e) { + e.printStackTrace(); + } + } + + @Override public VersionRange acceptableMinecraftVersionRange() { return VersionParser.parseRange(CodeChickenCorePlugin.mcVersion); diff --git a/src/codechicken/nei/config/IMCHandler.java b/src/codechicken/nei/config/IMCHandler.java new file mode 100644 index 00000000..9210eb70 --- /dev/null +++ b/src/codechicken/nei/config/IMCHandler.java @@ -0,0 +1,87 @@ +package codechicken.nei.config; + +import codechicken.nei.NEIClientConfig; +import codechicken.nei.recipe.GuiRecipeTab; +import codechicken.nei.recipe.HandlerInfo; +import cpw.mods.fml.common.FMLLog; +import cpw.mods.fml.common.Loader; +import cpw.mods.fml.common.event.FMLInterModComms; +import net.minecraft.nbt.NBTTagCompound; + +import java.util.List; + +public class IMCHandler { + private IMCHandler() {} + + public static void processIMC(List messages) { + for (FMLInterModComms.IMCMessage message : messages) { + String type = message.key; + if (type == null || type.isEmpty()) continue; + + if(type.equals("registerHandlerInfo")) { + if (!message.isNBTMessage()) { + logInvalidMessage(message, "NBT"); + continue; + } + final NBTTagCompound tag = message.getNBTValue(); + final String handler = tag.getString("handler"); + NEIClientConfig.logger.info("Processing registerHandlerInfo `" + handler + "` from " + message.getSender()); + + final String modName = tag.getString("modName"); + final String modId = tag.getString("modId"); + final boolean requiresMod = tag.getBoolean("modRequired"); + final String excludedModId = tag.hasKey("excludedModId") ? tag.getString("excludedModId") : null; + + if(handler.equals("") || modName.equals("") || modId.equals("")) { + NEIClientConfig.logger.warn("Missing relevant information to registerHandlerInfo!"); + continue; + } + + if (requiresMod && !Loader.isModLoaded(modId)) continue; + if (excludedModId != null && Loader.isModLoaded(excludedModId)) continue; + + HandlerInfo info = new HandlerInfo(handler, modName, modId, requiresMod, excludedModId); + final String imageResource = tag.hasKey("imageResource") ? tag.getString("imageResource") : null; + if(imageResource != null && !imageResource.equals("")) { + info.setImage(imageResource, tag.getInteger("imageX"), tag.getInteger("imageY"), tag.getInteger("imageWidth"), tag.getInteger("imageHeight")); + } + if(!info.hasImageOrItem()) { + final String itemName = tag.getString("itemName"); + if (itemName != null && !itemName.equals("")) { + info.setItem(itemName, tag.hasKey("nbtInfo") ? tag.getString("nbtInfo") : null); + } + } + final int yShift = tag.hasKey("yShift") ? tag.getInteger("yShift") : 0; + info.setYShift(yShift); + + try { + final int imageHeight = tag.hasKey("handlerHeight") ? tag.getInteger("handlerHeight") : HandlerInfo.DEFAULT_HEIGHT; + final int imageWidth = tag.hasKey("handlerWidth") ? tag.getInteger("handlerWidth") : HandlerInfo.DEFAULT_WIDTH; + final int maxRecipesPerPage = tag.hasKey("maxRecipesPerPage") ? tag.getInteger("maxRecipesPerPage") : HandlerInfo.DEFAULT_MAX_PER_PAGE; + info.setHandlerDimensions(imageHeight, imageWidth, maxRecipesPerPage); + } catch (NumberFormatException ignored) { + NEIClientConfig.logger.info("Error setting handler dimensions for " + handler); + } + + GuiRecipeTab.handlerMap.remove(handler); + GuiRecipeTab.handlerMap.put(handler, info); + } else if(type.equals("removeHandlerInfo")) { + if (!message.isNBTMessage()) { + logInvalidMessage(message, "NBT"); + continue; + } + final NBTTagCompound tag = message.getNBTValue(); + final String handler = tag.getString("handler"); + NEIClientConfig.logger.info("Processing removeHandlerInfo `" + handler + "` from " + message.getSender()); + + GuiRecipeTab.handlerMap.remove(handler); + } + + } + } + + + private static void logInvalidMessage(FMLInterModComms.IMCMessage message, String type) { + FMLLog.bigWarning(String.format("Received invalid IMC '%s' from %s. Not a %s Message.", message.key, message.getSender(), type)); + } +} diff --git a/src/codechicken/nei/recipe/HandlerInfo.java b/src/codechicken/nei/recipe/HandlerInfo.java index e3efe9c0..60a3e3bd 100644 --- a/src/codechicken/nei/recipe/HandlerInfo.java +++ b/src/codechicken/nei/recipe/HandlerInfo.java @@ -3,8 +3,14 @@ package codechicken.nei.recipe; import codechicken.nei.NEIClientConfig; import codechicken.nei.drawable.DrawableBuilder; import codechicken.nei.drawable.DrawableResource; +import cpw.mods.fml.common.FMLLog; import cpw.mods.fml.common.registry.GameRegistry; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.JsonToNBT; +import net.minecraft.nbt.NBTBase; +import net.minecraft.nbt.NBTException; +import net.minecraft.nbt.NBTTagCompound; +import org.apache.logging.log4j.Level; public class HandlerInfo { public static int DEFAULT_HEIGHT = 65; @@ -71,6 +77,20 @@ public class HandlerInfo { itemStack = GameRegistry.makeItemStack(itemLookupId, meta, 1, nbtString == null ? "" : nbtString); } else { itemStack = GameRegistry.findItemStack(split[0], split[1], 1); + if (nbtString != null && !nbtString.equals("")) { + try { + final NBTBase nbttag = JsonToNBT.func_150315_a(nbtString); + if (!(nbttag instanceof NBTTagCompound)) { + NEIClientConfig.logger.info("Unexpected NBT string - multiple values {}", nbtString); + return false; + } + + itemStack.setTagCompound((NBTTagCompound) nbttag); + } catch (NBTException e) { + FMLLog.getLogger().log(Level.WARN, "Encountered an exception parsing ItemStack NBT string {}", nbtString, e); + return false; + } + } } if (itemStack == null) NEIClientConfig.logger.info("Couldn't find " + modName + " - " + itemLookupId);