-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Conditional mods & event bus subscribers
- Loading branch information
1 parent
ee3c2b9
commit 06b3ef0
Showing
7 changed files
with
111 additions
and
4 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
loader/src/main/java/net/neoforged/fml/common/ChainDependency.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright (c) NeoForged and contributors | ||
* SPDX-License-Identifier: LGPL-2.1-only | ||
*/ | ||
|
||
package net.neoforged.fml.common; | ||
|
||
public @interface ChainDependency { | ||
Dependency value(); | ||
|
||
Operator operator() default Operator.AND; | ||
|
||
enum Operator { | ||
AND, OR | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
loader/src/main/java/net/neoforged/fml/common/Dependency.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright (c) NeoForged and contributors | ||
* SPDX-License-Identifier: LGPL-2.1-only | ||
*/ | ||
|
||
package net.neoforged.fml.common; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Dependency { | ||
/** | ||
* @return the array of mod ids to be checked against the {@link #condition() condition} | ||
*/ | ||
String[] value(); | ||
|
||
Condition condition() default Condition.ALL_PRESENT; | ||
|
||
enum Condition { | ||
ALL_PRESENT, AT_LEAST_ONE_PRESENT, NONE_PRESENT, AT_LEAST_ONE_IS_NOT_PRESENT | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
loader/src/main/java/net/neoforged/fml/javafmlmod/DependencyUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (c) NeoForged and contributors | ||
* SPDX-License-Identifier: LGPL-2.1-only | ||
*/ | ||
|
||
package net.neoforged.fml.javafmlmod; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import net.neoforged.fml.common.ChainDependency; | ||
import net.neoforged.fml.common.Dependency; | ||
|
||
public final class DependencyUtil { | ||
public static Boolean evaluateChain(List<ChainDependency> chain, Collection<String> loadedMods) { | ||
var matches = true; | ||
ChainDependency.Operator previousOperator = null; | ||
for (ChainDependency chainDep : chain) { | ||
if (previousOperator == null) { | ||
matches = evaluateDependency(chainDep.value(), loadedMods); | ||
previousOperator = chainDep.operator(); | ||
continue; | ||
} | ||
|
||
switch (previousOperator) { | ||
case AND -> matches = matches && evaluateDependency(chainDep.value(), loadedMods); | ||
case OR -> matches = matches || evaluateDependency(chainDep.value(), loadedMods); | ||
} | ||
|
||
previousOperator = chainDep.operator(); | ||
} | ||
|
||
return matches; | ||
} | ||
|
||
public static Boolean evaluateDependency(Dependency dep, Collection<String> loadedMods) { | ||
return switch (dep.condition()) { | ||
case ALL_PRESENT -> Arrays.stream(dep.value()).allMatch(loadedMods::contains); | ||
case AT_LEAST_ONE_PRESENT -> Arrays.stream(dep.value()).anyMatch(loadedMods::contains); | ||
case NONE_PRESENT -> Arrays.stream(dep.value()).noneMatch(loadedMods::contains); | ||
case AT_LEAST_ONE_IS_NOT_PRESENT -> Arrays.stream(dep.value()).anyMatch((modId) -> !loadedMods.contains(modId)); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters