diff options
author | SHsuperCM <shsupercm@gmail.com> | 2022-03-26 11:03:40 +0300 |
---|---|---|
committer | SHsuperCM <shsupercm@gmail.com> | 2022-03-26 11:03:40 +0300 |
commit | acc5caebf0763915d216fb35f6e37b9ad3b7f3ff (patch) | |
tree | a124ad97a41f1ae8620451fb2ac46a2611cdfc9b /src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin | |
parent | 0484498863626dc57d55d381b7b394b5cc9234c2 (diff) | |
download | CITResewn-acc5caebf0763915d216fb35f6e37b9ad3b7f3ff.tar.gz CITResewn-acc5caebf0763915d216fb35f6e37b9ad3b7f3ff.tar.bz2 CITResewn-acc5caebf0763915d216fb35f6e37b9ad3b7f3ff.zip |
Implemented fallback behavior and fixed weight bug
Diffstat (limited to 'src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin')
2 files changed, 97 insertions, 2 deletions
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/core/FallbackCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/core/FallbackCondition.java new file mode 100644 index 0000000..5f8cdc3 --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/core/FallbackCondition.java @@ -0,0 +1,79 @@ +package shcm.shsupercm.fabric.citresewn.cit.builtin.conditions.core; + +import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; +import net.minecraft.util.Identifier; +import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; +import shcm.shsupercm.fabric.citresewn.api.CITGlobalProperties; +import shcm.shsupercm.fabric.citresewn.cit.CIT; +import shcm.shsupercm.fabric.citresewn.cit.builtin.conditions.IdentifierCondition; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; + +import java.util.*; + +/** + * Core property used to set the fallback of the CIT.<br> + * When a CIT loads successfully, its fallback CIT gets removed prior to activation.<br> + * If the main CIT fails to load(or if not loaded at all) the fallback loads as usual. + * @see #apply(List) + * @see #globalProperty(String, PropertyValue) + */ +public class FallbackCondition extends IdentifierCondition { + @Entrypoint(CITConditionContainer.ENTRYPOINT) + public static final CITConditionContainer<FallbackCondition> CONTAINER = new CITConditionContainer<>(FallbackCondition.class, FallbackCondition::new, + "cit_fallback", "citFallback"); + + public FallbackCondition() { + this.value = null; + } + + public Identifier getFallback() { + return this.value; + } + + public void setFallback(Identifier value) { + this.value = value; + } + + /** + * @see #globalProperty(String, PropertyValue) + */ + private static boolean fallbackCITResewnRoot = false; + + /** + * When the global property "citresewn:root_fallback" is set to true, all CITs in the "citresewn" + * root will automatically have the fallback to the same path in the other roots(optifine/mcpatcher).<br> + * This behavior is overridden if the CIT specifies a {@link FallbackCondition fallback} manually. + * @see #apply(List) + */ + @Entrypoint(CITGlobalProperties.ENTRYPOINT) + public static void globalProperty(String key, PropertyValue value) throws Exception { + if (key.equals("root_fallback")) + fallbackCITResewnRoot = value != null && Boolean.parseBoolean(value.value()); + } + + /** + * Removes fallback {@link CIT CITs} of all successfully loaded CITs in the given list.<br> + * @see #globalProperty(String, PropertyValue) + */ + public static void apply(List<CIT<?>> cits) { + Map<String, Set<Identifier>> removePacks = new HashMap<>(); + + for (CIT<?> cit : cits) { + Set<Identifier> remove = removePacks.computeIfAbsent(cit.packName, s -> new HashSet<>()); + if (cit.fallback == null) { + if (fallbackCITResewnRoot && cit.propertiesIdentifier.getPath().startsWith("citresewn/")) { + String subPath = cit.propertiesIdentifier.getPath().substring(10); + remove.add(new Identifier(cit.propertiesIdentifier.getNamespace(), "optifine/" + subPath)); + remove.add(new Identifier(cit.propertiesIdentifier.getNamespace(), "mcpatcher/" + subPath)); + } + } else { + remove.add(cit.fallback); + } + } + + cits.removeIf(cit -> { + Set<Identifier> remove = removePacks.get(cit.packName); + return remove != null && remove.contains(cit.propertiesIdentifier); + }); + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/core/WeightCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/core/WeightCondition.java index cb728c9..60f97c4 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/core/WeightCondition.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/core/WeightCondition.java @@ -2,12 +2,17 @@ package shcm.shsupercm.fabric.citresewn.cit.builtin.conditions.core; import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; +import shcm.shsupercm.fabric.citresewn.cit.CIT; import shcm.shsupercm.fabric.citresewn.cit.builtin.conditions.IntegerCondition; +import java.util.Comparator; +import java.util.List; + /** - * Internal condition used to determine the priority CITs get tested in.<br> + * Core property used to determine the priority CITs get tested in. * Weights default to 0 and higher weights get chosen over lower weights.<br> - * When two conflicting CITs have the same weight, their path in the resourcepack is used as a tie breaker. + * When two conflicting CITs have the same weight, their path in the + * resourcepack and the pack's name are used as a tie breaker. */ public class WeightCondition extends IntegerCondition { @Entrypoint(CITConditionContainer.ENTRYPOINT) @@ -16,6 +21,7 @@ public class WeightCondition extends IntegerCondition { public WeightCondition() { super(false, true, false); + this.min = 0; } public int getWeight() { @@ -25,4 +31,14 @@ public class WeightCondition extends IntegerCondition { public void setWeight(int weight) { this.min = weight; } + + /** + * Sorts the given {@link CIT} list by the CITs' weight and then by their path/pack name. + */ + public static void apply(List<CIT<?>> cits) { + cits.sort( + Comparator.<CIT<?>>comparingInt(cit -> cit.weight) + .reversed() + .thenComparing(cit -> cit.propertiesIdentifier.toString() + cit.packName)); + } } |