From 102697939684255827620da07266089dc20572a2 Mon Sep 17 00:00:00 2001
From: SHsuperCM <shsupercm@gmail.com>
Date: Fri, 15 Oct 2021 12:17:22 +0300
Subject: Restructured to store active effective global properties

---
 .../shsupercm/fabric/citresewn/ActiveCITs.java     | 15 +++++-
 .../citresewn/mixin/core/ModelLoaderMixin.java     |  7 ++-
 .../shsupercm/fabric/citresewn/pack/CITPack.java   | 54 +++++++++++++++-------
 .../shsupercm/fabric/citresewn/pack/CITParser.java |  3 +-
 4 files changed, 56 insertions(+), 23 deletions(-)

(limited to 'src/main/java')

diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/ActiveCITs.java b/src/main/java/shcm/shsupercm/fabric/citresewn/ActiveCITs.java
index 231c1cd..b7d326b 100644
--- a/src/main/java/shcm/shsupercm/fabric/citresewn/ActiveCITs.java
+++ b/src/main/java/shcm/shsupercm/fabric/citresewn/ActiveCITs.java
@@ -11,6 +11,7 @@ import net.minecraft.util.Identifier;
 import net.minecraft.util.registry.Registry;
 import net.minecraft.world.World;
 import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig;
+import shcm.shsupercm.fabric.citresewn.pack.CITPack;
 import shcm.shsupercm.fabric.citresewn.pack.cits.*;
 
 import java.util.*;
@@ -18,16 +19,26 @@ import java.util.function.Supplier;
 import java.util.stream.Collectors;
 
 public class ActiveCITs {
-    public final Collection<CIT> cits;
+    public final List<CITPack> packs;
+    public final CITPack effectiveGlobalProperties = new CITPack(null);
+
+    public final List<CIT> cits;
+
     public final Map<Item, List<CITItem>> citItems = new HashMap<>();
     public final Map<ArmorItem, List<CITArmor>> citArmor = new HashMap<>();
     public final List<CITElytra> citElytra = new ArrayList<>();
     public final List<List<CITEnchantment>> citEnchantments = new ArrayList<>();
 
 
-    public ActiveCITs(Collection<CIT> cits) {
+    public ActiveCITs(List<CITPack> packs, List<CIT> cits) {
+        this.packs = packs;
         this.cits = cits;
 
+        for (CITPack pack : packs)
+            effectiveGlobalProperties.loadGlobalProperties(pack);
+        for (CITPack pack : packs)
+            pack.loadGlobalProperties(effectiveGlobalProperties);
+
         Map<Integer, List<CITEnchantment>> citEnchantmentLayers = new TreeMap<>(); // order citEnchantments by layers
 
         for (CIT cit : cits.stream().sorted(Comparator.<CIT>comparingInt(cit -> cit.weight).reversed().thenComparing(cit -> cit.propertiesIdentifier.toString())).collect(Collectors.toList())) {
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/core/ModelLoaderMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/core/ModelLoaderMixin.java
index 4592167..d65ffb8 100644
--- a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/core/ModelLoaderMixin.java
+++ b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/core/ModelLoaderMixin.java
@@ -12,11 +12,13 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
 import shcm.shsupercm.fabric.citresewn.ActiveCITs;
 import shcm.shsupercm.fabric.citresewn.CITResewn;
 import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig;
+import shcm.shsupercm.fabric.citresewn.pack.CITPack;
 import shcm.shsupercm.fabric.citresewn.pack.CITParser;
 import shcm.shsupercm.fabric.citresewn.pack.cits.CIT;
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.List;
 import java.util.stream.Collectors;
 
 import static shcm.shsupercm.fabric.citresewn.CITResewn.info;
@@ -37,11 +39,12 @@ public abstract class ModelLoaderMixin {
             return;
 
         info("Parsing CITs...");
-        Collection<CIT> parsed = CITParser.parseCITs(resourceManager.streamResourcePacks().collect(Collectors.toCollection(ArrayList::new)));
+        List<CITPack> parsedPacks = CITParser.parseCITs(resourceManager.streamResourcePacks().collect(Collectors.toCollection(ArrayList::new)));
+        List<CIT> parsed = parsedPacks.stream().flatMap(pack -> pack.cits.stream()).collect(Collectors.toCollection(ArrayList::new));
 
         if (parsed.size() > 0) {
             info("Activating CITs...");
-            CITResewn.INSTANCE.activeCITs = new ActiveCITs(parsed);
+            CITResewn.INSTANCE.activeCITs = new ActiveCITs(parsedPacks, parsed);
         } else
             info("No cit packs found.");
     }
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/CITPack.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/CITPack.java
index 92faec0..59f1659 100644
--- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/CITPack.java
+++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/CITPack.java
@@ -9,10 +9,10 @@ public class CITPack {
     public final ResourcePack resourcePack;
     public final Collection<CIT> cits = new ArrayList<>();
 
-    private EnchantmentMergeMethod method = EnchantmentMergeMethod.AVERAGE;
-    private int cap = 8;
-    private float fade = 0.5f;
-    private boolean useGlint = true;
+    public EnchantmentMergeMethod method = EnchantmentMergeMethod.AVERAGE;
+    public Integer cap = 8;
+    public Float fade = 0.5f;
+    public Boolean useGlint = true;
 
     public CITPack(ResourcePack resourcePack) {
         this.resourcePack = resourcePack;
@@ -20,27 +20,47 @@ public class CITPack {
 
     public void loadGlobalProperties(Properties properties) throws Exception {
         try {
-            this.method = CITPack.EnchantmentMergeMethod.valueOf(properties.getProperty("method", "average").toUpperCase(Locale.ENGLISH));
-            this.cap = Integer.parseInt(properties.getProperty("cap", "8"));
-            if (this.cap < 0)
-                throw new Exception("cap cannot be negative");
-            this.fade = Float.parseFloat(properties.getProperty("fade", "0.5"));
-            if (this.fade < 0f)
-                throw new Exception("fade cannot be negative");
-            this.useGlint = switch (properties.getProperty("useGlint", "true").toLowerCase(Locale.ENGLISH)) {
+            this.method = properties.containsKey("method") ? CITPack.EnchantmentMergeMethod.valueOf(properties.getProperty("method").toUpperCase(Locale.ENGLISH)) : null;
+
+            if (properties.containsKey("cap")) {
+                this.cap = Integer.parseInt(properties.getProperty("cap"));
+                if (this.cap < 0)
+                    throw new Exception("cap cannot be negative");
+            } else
+                this.cap = null;
+
+            if (properties.containsKey("fade")) {
+                this.fade = Float.parseFloat(properties.getProperty("fade"));
+                if (this.fade < 0f)
+                    throw new Exception("fade cannot be negative");
+            } else
+                this.fade = null;
+
+            this.useGlint = properties.containsKey("useGlint") ? switch (properties.getProperty("useGlint").toLowerCase(Locale.ENGLISH)) {
                 case "true" -> true;
                 case "false" -> false;
                 default -> throw new Exception("useGlint is not a boolean");
-            };
+            } : null;
         } catch (Exception e) {
-            this.method = EnchantmentMergeMethod.AVERAGE;
-            this.cap = 8;
-            this.fade = 0.5f;
-            this.useGlint = true;
+            this.method = null;
+            this.cap = null;
+            this.fade = null;
+            this.useGlint = null;
             throw e;
         }
     }
 
+    public void loadGlobalProperties(CITPack properties) {
+        if (properties.method != null)
+            this.method = properties.method;
+        if (properties.cap != null)
+            this.cap = properties.cap;
+        if (properties.fade != null)
+            this.fade = properties.fade;
+        if (properties.useGlint != null)
+            this.useGlint = properties.useGlint;
+    }
+
     public enum EnchantmentMergeMethod {
         AVERAGE,
         LAYERED,
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/CITParser.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/CITParser.java
index 03ea512..ee97b25 100644
--- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/CITParser.java
+++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/CITParser.java
@@ -36,11 +36,10 @@ public final class CITParser { private CITParser() {}
      * @param packs packs to parse
      * @return a collection of parsed CITs
      */
-    public static Collection<CIT> parseCITs(Collection<ResourcePack> packs) {
+    public static List<CITPack> parseCITs(Collection<ResourcePack> packs) {
         return packs.stream()
                 .map(CITParser::parse)
                 .flatMap(Collection::stream)
-                .flatMap(pack -> pack.cits.stream())
                 .collect(Collectors.toList());
     }
 
-- 
cgit