mirror of
https://github.com/GTNewHorizons/NotEnoughItems.git
synced 2026-11-13 00:47:35 +00:00
Add way to register catalysts via IMC (#232)
This commit is contained in:
1 parent
f17bf6e8e2
commit
6f2782b06e
2 files changed
+107
-5
No files matched your search
@@ -2,17 +2,26 @@ package codechicken.nei.config;
|
||||
|
||||
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) {
|
||||
@@ -20,10 +29,19 @@ public class IMCHandler {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,6 +109,72 @@ public class IMCHandler {
|
||||
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));
|
||||
|
||||
@@ -29,6 +29,8 @@ import java.util.Map;
|
||||
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<>();
|
||||
@@ -161,6 +163,7 @@ public class RecipeCatalysts {
|
||||
// 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;
|
||||
@@ -209,6 +212,13 @@ public class RecipeCatalysts {
|
||||
}
|
||||
}
|
||||
|
||||
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)) {
|
||||
@@ -217,6 +227,14 @@ public class RecipeCatalysts {
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -234,7 +252,7 @@ public class RecipeCatalysts {
|
||||
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 {
|
||||
|
||||
Reference in new issue
Block a user