Skip to content

Commit

Permalink
Add way to register catalysts via IMC (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
miozune authored Apr 8, 2022
1 parent f17bf6e commit 6f2782b
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 5 deletions.
92 changes: 88 additions & 4 deletions src/main/java/codechicken/nei/config/IMCHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,46 @@

import codechicken.core.CommonUtils;
import codechicken.nei.NEIClientConfig;
import codechicken.nei.NEIServerUtils;
import codechicken.nei.recipe.CatalystInfo;
import codechicken.nei.recipe.GuiRecipeTab;
import codechicken.nei.recipe.HandlerInfo;
import codechicken.nei.recipe.RecipeCatalysts;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.event.FMLInterModComms;
import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class IMCHandler {
private static final Set<String> processedCatalystSenders = new HashSet<>();
private IMCHandler() {}

public static void processIMC(List<FMLInterModComms.IMCMessage> messages) {
for (FMLInterModComms.IMCMessage message : messages) {
String type = message.key;
if (type == null || type.isEmpty()) continue;
if (CommonUtils.isClient()) {
if (type.equals("registerHandlerInfo")) {
handleRegisterHandlerInfo(message);
} else if (type.equals("removeHandlerInfo")) {
handleRemoveHandlerInfo(message);
switch (type) {
case "registerHandlerInfo":
handleRegisterHandlerInfo(message);
break;
case "removeHandlerInfo":
handleRemoveHandlerInfo(message);
break;
case "registerCatalystInfo":
handleRegisterCatalystInfo(message);
break;
case "removeCatalystInfo":
handleRemoveCatalystInfo(message);
break;
}
}

Expand Down Expand Up @@ -91,6 +109,72 @@ private static void handleRemoveHandlerInfo(IMCMessage message) {
GuiRecipeTab.handlerRemoverFromIMC.add(handler);
}

private static void handleRegisterCatalystInfo(IMCMessage message) {
if (!message.isNBTMessage()) {
logInvalidMessage(message, "NBT");
return;
}

if (!processedCatalystSenders.contains(message.getSender())) {
NEIClientConfig.logger.info("Processing registerCatalystInfo from " + message.getSender());
processedCatalystSenders.add(message.getSender());
}
final NBTTagCompound tag = message.getNBTValue();
final String handlerID = tag.getString("handlerID");
if (handlerID.isEmpty()) {
NEIClientConfig.logger.warn("Missing handlerID for registerCatalystInfo!");
return;
}
final String itemName = tag.getString("itemName");
final String nbtInfo = tag.hasKey("nbtInfo") ? tag.getString("nbtInfo") : null;
if (itemName.isEmpty()) {
NEIClientConfig.logger.warn(String.format("Missing itemName for registerCatalystInfo in `%s`!", handlerID));
return;
}
final ItemStack itemStack = NEIServerUtils.getModdedItem(itemName, nbtInfo);
if (itemStack == null) {
NEIClientConfig.logger.warn(String.format("Cannot find item `%s`!", itemName));
return;
}
final int priority = tag.getInteger("priority");

RecipeCatalysts.addOrPut(RecipeCatalysts.catalystsAdderFromIMC, handlerID, new CatalystInfo(itemStack, priority));
NEIClientConfig.logger.info(String.format("Added catalyst `%s` to handler %s", itemStack.getDisplayName(), handlerID));
}

private static void handleRemoveCatalystInfo(IMCMessage message) {
if (!message.isNBTMessage()) {
logInvalidMessage(message, "NBT");
return;
}

NEIClientConfig.logger.info("Processing removeCatalystInfo from " + message.getSender());
final NBTTagCompound tag = message.getNBTValue();
final String handlerID = tag.getString("handlerID");
if (handlerID.isEmpty()) {
NEIClientConfig.logger.warn("Missing handlerID for registerCatalystInfo!");
return;
}
final String itemName = tag.getString("itemName");
final String nbtInfo = tag.hasKey("nbtInfo") ? tag.getString("nbtInfo") : null;
if (itemName.isEmpty()) {
NEIClientConfig.logger.warn(String.format("Missing itemName for registerCatalystInfo in `%s`!", handlerID));
return;
}
final ItemStack itemStack = NEIServerUtils.getModdedItem(itemName, nbtInfo);
if (itemStack == null) {
NEIClientConfig.logger.warn(String.format("Cannot find item `%s`!", itemName));
return;
}

if (RecipeCatalysts.catalystsRemoverFromIMC.containsKey(handlerID)) {
RecipeCatalysts.catalystsRemoverFromIMC.get(handlerID).add(itemStack);
} else {
RecipeCatalysts.catalystsRemoverFromIMC.put(handlerID, new ArrayList<>(Collections.singletonList(itemStack)));
}
NEIClientConfig.logger.info(String.format("Removed catalyst `%s` from handler %s", itemStack.getDisplayName(), handlerID));
}


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));
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/codechicken/nei/recipe/RecipeCatalysts.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
public class RecipeCatalysts {
private static final Map<String, CatalystInfoList> catalystsAdderFromAPI = new HashMap<>();
private static final Map<String, List<ItemStack>> catalystsRemoverFromAPI = new HashMap<>();
public static final Map<String, CatalystInfoList> catalystsAdderFromIMC = new HashMap<>();
public static final Map<String, List<ItemStack>> catalystsRemoverFromIMC = new HashMap<>();
private static final Map<String, CatalystInfoList> recipeCatalystMap = new HashMap<>();
private static Map<String, List<PositionedStack>> positionedRecipeCatalystMap = new HashMap<>();
private static final List<String> forceClassNameList = new ArrayList<>();
Expand Down Expand Up @@ -161,6 +163,7 @@ public static void loadCatalystInfo() {
// todo
final String minVersion = record.get("minVersion");
final String maxVersion = record.get("maxVersion");
//
final boolean forceClassName = Boolean.parseBoolean(record.get("forceClassName"));

if (requiresMod && !Loader.isModLoaded(modId)) continue;
Expand Down Expand Up @@ -209,6 +212,13 @@ public static void loadCatalystInfo() {
}
}

for (Map.Entry<String, CatalystInfoList> entry : catalystsAdderFromIMC.entrySet()) {
String handlerID = entry.getKey();
for (CatalystInfo catalyst : entry.getValue()) {
addOrPut(recipeCatalystMap, handlerID, catalyst);
}
}

for (Map.Entry<String, List<ItemStack>> entry : catalystsRemoverFromAPI.entrySet()) {
String handlerID = entry.getKey();
if (recipeCatalystMap.containsKey(handlerID)) {
Expand All @@ -217,6 +227,14 @@ public static void loadCatalystInfo() {
}
}

for (Map.Entry<String, List<ItemStack>> entry : catalystsRemoverFromIMC.entrySet()) {
String handlerID = entry.getKey();
if (recipeCatalystMap.containsKey(handlerID)) {
CatalystInfoList catalysts = recipeCatalystMap.get(handlerID);
entry.getValue().forEach(catalysts::remove);
}
}

updatePosition(getHeight(), true);
}

Expand All @@ -234,7 +252,7 @@ public static String getRecipeID(IRecipeHandler handler) {
handler.getHandlerId());
}

private static void addOrPut(Map<String, CatalystInfoList> map, String handlerID, CatalystInfo catalyst) {
public static void addOrPut(Map<String, CatalystInfoList> map, String handlerID, CatalystInfo catalyst) {
if (map.containsKey(handlerID)) {
map.get(handlerID).add(catalyst);
} else {
Expand Down

0 comments on commit 6f2782b

Please sign in to comment.