Skip to content

Commit

Permalink
Add handling for multiple suppliers to a single configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
fonnymunkey committed Mar 16, 2024
1 parent fb880a6 commit 2bb80ab
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
13 changes: 10 additions & 3 deletions src/main/java/fermiumbooter/FermiumPlugin.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fermiumbooter;

import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin;
Expand Down Expand Up @@ -44,16 +45,22 @@ public String getSetupClass()
*/
@Override
public void injectData(Map<String, Object> data) {
for(Map.Entry<String, Supplier<Boolean>> entry : FermiumRegistryAPI.getEarlyMixins().entrySet()) {
for(Map.Entry<String, List<Supplier<Boolean>>> entry : FermiumRegistryAPI.getEarlyMixins().entrySet()) {
//Check for removals
if(FermiumRegistryAPI.getRejectMixins().contains(entry.getKey())) {
LOGGER.warn("FermiumBooter received removal of \"" + entry.getKey() + "\" for early mixin application, rejecting.");
continue;
}
//Check for enabled
Boolean enabled = entry.getValue().get();
Boolean enabled = null;
for(Supplier<Boolean> supplier : entry.getValue()) {
if(Boolean.TRUE.equals(enabled)) continue;//Short circuit OR
Boolean supplied = supplier.get();
if(supplied == null) LOGGER.warn("FermiumBooter received null value for individual supplier from \"" + entry.getKey() + "\" for early mixin application.");
else enabled = supplied;
}
if(enabled == null) {
LOGGER.warn("FermiumBooter received null value for supplier from \"" + entry.getKey() + "\" for early mixin application, ignoring.");
LOGGER.warn("FermiumBooter received null value for suppliers from \"" + entry.getKey() + "\" for early mixin application, ignoring.");
continue;
}
//Add configuration
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/fermiumbooter/FermiumRegistryAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public abstract class FermiumRegistryAPI {

private static final Logger LOGGER = LogManager.getLogger("FermiumRegistryAPI");

private static HashMap<String, Supplier<Boolean>> earlyMixins = new HashMap<>();
private static HashMap<String, Supplier<Boolean>> lateMixins = new HashMap<>();
private static HashMap<String, List<Supplier<Boolean>>> earlyMixins = new HashMap<>();
private static HashMap<String, List<Supplier<Boolean>>> lateMixins = new HashMap<>();
private static List<String> rejectMixins = new ArrayList<>();

/**
Expand Down Expand Up @@ -54,6 +54,7 @@ public static void enqueueMixin(boolean late, String configuration, boolean enab

/**
* Add a mixin config resource to be applied, with a supplier to toggle application to be evaluated after all like-timed configs are registered
* Note: If multiple suppliers are given for a single configuration, it is evaluated as OR
* @param late - whether to apply the mixin late or early
* @param configuration - mixin config resource name
* @param supplier - supplier to determine whether to apply the mixin or not
Expand All @@ -70,11 +71,13 @@ public static void enqueueMixin(boolean late, String configuration, Supplier<Boo
//Process rejects prior to application
if(late) {
LOGGER.info("FermiumRegistryAPI supplied \"" + configuration + "\" for late mixin enqueue, adding.");
lateMixins.put(configuration, supplier);
lateMixins.computeIfAbsent(configuration, k -> new ArrayList<>());
lateMixins.get(configuration).add(supplier);
}
else {
LOGGER.info("FermiumRegistryAPI supplied \"" + configuration + "\" for early mixin enqueue, adding.");
earlyMixins.put(configuration, supplier);
earlyMixins.computeIfAbsent(configuration, k -> new ArrayList<>());
earlyMixins.get(configuration).add(supplier);
}
}

Expand All @@ -95,14 +98,14 @@ public static void removeMixin(String configuration) {
/**
* Internal Use; Do Not Use
*/
public static HashMap<String, Supplier<Boolean>> getEarlyMixins() {
public static HashMap<String, List<Supplier<Boolean>>> getEarlyMixins() {
return earlyMixins;
}

/**
* Internal Use; Do Not Use
*/
public static HashMap<String, Supplier<Boolean>> getLateMixins() {
public static HashMap<String, List<Supplier<Boolean>>> getLateMixins() {
return lateMixins;
}

Expand Down
10 changes: 8 additions & 2 deletions src/main/java/fermiumbooter/mixin/FermiumMixinLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,20 @@ private void beforeConstructingMods(List<String> nonMod, CallbackInfo ci) {

//Start FermiumBooter section

for(Map.Entry<String, Supplier<Boolean>> entry : FermiumRegistryAPI.getLateMixins().entrySet()) {
for(Map.Entry<String, List<Supplier<Boolean>>> entry : FermiumRegistryAPI.getLateMixins().entrySet()) {
//Check for removals
if(FermiumRegistryAPI.getRejectMixins().contains(entry.getKey())) {
FermiumPlugin.LOGGER.warn("FermiumBooter received removal of \"" + entry.getKey() + "\" for late mixin application, rejecting.");
continue;
}
//Check for enabled
Boolean enabled = entry.getValue().get();
Boolean enabled = null;
for(Supplier<Boolean> supplier : entry.getValue()) {
if(Boolean.TRUE.equals(enabled)) continue;//Short circuit OR
Boolean supplied = supplier.get();
if(supplied == null) FermiumPlugin.LOGGER.warn("FermiumBooter received null value for individual supplier from \"" + entry.getKey() + "\" for late mixin application.");
else enabled = supplied;
}
if(enabled == null) {
FermiumPlugin.LOGGER.warn("FermiumBooter received null value for supplier from \"" + entry.getKey() + "\" for late mixin application, ignoring.");
continue;
Expand Down

0 comments on commit 2bb80ab

Please sign in to comment.