Skip to content

Commit

Permalink
Improve the way catalyst info is loaded via API (GTNewHorizons#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
miozune authored Nov 8, 2023
1 parent 0c5af2b commit bc7efcf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/main/java/codechicken/nei/NEIClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,6 @@ public static void bootNEI(World world) {
RecipeInfo.load();
LayoutManager.load();
NEIController.load();
RecipeCatalysts.loadCatalystInfo();
BookmarkContainerInfo.load();
mainNEIConfigLoaded = true;

Expand All @@ -502,6 +501,8 @@ public void run() {
}
}

RecipeCatalysts.loadCatalystInfo();

// Set pluginNEIConfigLoaded here before posting the NEIConfigsLoadedEvent. This used to be the other
// way around, but apparently if your modpack includes 800 mods the event poster might not return in
// time and cause issues when loading a world for a second time as configLoaded is still false. This may
Expand Down
25 changes: 14 additions & 11 deletions src/main/java/codechicken/nei/recipe/CatalystInfoList.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@

import com.google.common.collect.ForwardingList;

import codechicken.nei.NEIClientConfig;
import codechicken.nei.NEIServerUtils;

// Do not directly extend ArrayList, see Effective Java Item 16
public class CatalystInfoList extends ForwardingList<CatalystInfo> {

private final String handlerID;
Expand All @@ -34,16 +32,21 @@ protected List<CatalystInfo> delegate() {
}

@Override
public boolean add(@Nonnull CatalystInfo catalystInfo) {
if (contains(catalystInfo)) {
NEIClientConfig.logger.info(
String.format(
"catalyst %s is already registered to handler %s",
catalystInfo.getStack().getDisplayName(),
handlerID));
return false;
public boolean add(@Nonnull CatalystInfo element) {
return doAdd(element);
}

public boolean add(@Nonnull CatalystInfo catalystInfo, boolean overwrite) {
if (overwrite || !contains(catalystInfo)) {
return add(catalystInfo);
}
super.add(catalystInfo);
return false;
}

private boolean doAdd(@Nonnull CatalystInfo catalystInfo) {
catalystInfoList
.removeIf(c -> NEIServerUtils.areStacksSameTypeCraftingWithNBT(c.getStack(), catalystInfo.getStack()));
catalystInfoList.add(catalystInfo);
return true;
}

Expand Down
26 changes: 22 additions & 4 deletions src/main/java/codechicken/nei/recipe/RecipeCatalysts.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ public static void loadCatalystInfo() {
recipeCatalystMap.clear();
URL handlerUrl = RecipeCatalysts.class.getResource("/assets/nei/csv/catalysts.csv");

resolveAdderQueue();

URL url;
if (fromJar) {
url = handlerUrl;
Expand Down Expand Up @@ -208,13 +210,23 @@ public static void loadCatalystInfo() {
handlerID = handler;
}

addOrPut(recipeCatalystMap, handlerID, catalystInfo);
// Prefer info added by API if we're using default jar config.
// If not, user config overwrites it.
addOrPut(recipeCatalystMap, handlerID, catalystInfo, !fromJar);
}
} catch (Exception e) {
NEIClientConfig.logger.warn("Error parsing CSV");
e.printStackTrace();
}

if (fromJar) {
resolveRemoverQueue();
}

updatePosition(getHeight(), true);
}

private static void resolveAdderQueue() {
for (Map.Entry<String, CatalystInfoList> entry : catalystsAdderFromAPI.entrySet()) {
String handlerID = entry.getKey();
for (CatalystInfo catalyst : entry.getValue()) {
Expand Down Expand Up @@ -243,6 +255,9 @@ public static void loadCatalystInfo() {
}
});
}
}

private static void resolveRemoverQueue() {
for (Map.Entry<String, List<ItemStack>> entry : catalystsRemoverFromAPI.entrySet()) {
String handlerID = entry.getKey();
if (recipeCatalystMap.containsKey(handlerID)) {
Expand All @@ -258,8 +273,6 @@ public static void loadCatalystInfo() {
entry.getValue().forEach(catalysts::remove);
}
}

updatePosition(getHeight(), true);
}

/**
Expand All @@ -275,8 +288,13 @@ public static String getRecipeID(IRecipeHandler handler) {
}

public static void addOrPut(Map<String, CatalystInfoList> map, String handlerID, CatalystInfo catalyst) {
addOrPut(map, handlerID, catalyst, false);
}

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

0 comments on commit bc7efcf

Please sign in to comment.