aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/shcm/shsupercm/fabric/citresewn/cit
diff options
context:
space:
mode:
authorSHsuperCM <shsupercm@gmail.com>2022-02-13 07:52:10 +0200
committerSHsuperCM <shsupercm@gmail.com>2022-02-13 07:52:10 +0200
commit49318a4452f629274e96001700006e0b4a1464a5 (patch)
treee887cce1cec886e17247b2a99029e79b0255b0e7 /src/main/java/shcm/shsupercm/fabric/citresewn/cit
parent8823e77b989ab1035fa8813120177dd3a95ce863 (diff)
downloadCITResewn-49318a4452f629274e96001700006e0b4a1464a5.tar.gz
CITResewn-49318a4452f629274e96001700006e0b4a1464a5.tar.bz2
CITResewn-49318a4452f629274e96001700006e0b4a1464a5.zip
Moved some stuff around
Diffstat (limited to 'src/main/java/shcm/shsupercm/fabric/citresewn/cit')
-rw-r--r--src/main/java/shcm/shsupercm/fabric/citresewn/cit/ActiveCITs.java66
-rw-r--r--src/main/java/shcm/shsupercm/fabric/citresewn/cit/CIT.java27
-rw-r--r--src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCondition.java35
-rw-r--r--src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITContext.java32
-rw-r--r--src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java73
-rw-r--r--src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java10
-rw-r--r--src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/WeightCondition.java27
7 files changed, 270 insertions, 0 deletions
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/ActiveCITs.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/ActiveCITs.java
new file mode 100644
index 0000000..d24faf8
--- /dev/null
+++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/ActiveCITs.java
@@ -0,0 +1,66 @@
+package shcm.shsupercm.fabric.citresewn.cit;
+
+import net.fabricmc.loader.api.FabricLoader;
+import net.minecraft.resource.ResourceManager;
+import net.minecraft.util.profiler.Profiler;
+import shcm.shsupercm.fabric.citresewn.api.CITDisposable;
+import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer;
+import shcm.shsupercm.fabric.citresewn.pack.GlobalProperties;
+import shcm.shsupercm.fabric.citresewn.pack.PackParser;
+
+import java.util.*;
+
+public class ActiveCITs implements CITDisposable { private ActiveCITs() {}
+ private static ActiveCITs active = null;
+
+ public static ActiveCITs getActive() {
+ return active;
+ }
+
+ public static boolean isActive() {
+ return active != null;
+ }
+
+ public final GlobalProperties globalProperties = new GlobalProperties();
+
+ public final Map<Class<? extends CITType>, List<CIT>> cits = new IdentityHashMap<>();
+
+ public static ActiveCITs load(ResourceManager resourceManager, Profiler profiler) {
+ profiler.push("citresewn:disposing");
+ if (active != null) {
+ active.dispose();
+ active = null;
+ }
+
+ ActiveCITs active = new ActiveCITs();
+
+ profiler.swap("citresewn:load_global_properties");
+ PackParser.loadGlobalProperties(resourceManager, active.globalProperties);
+ active.globalProperties.callHandlers();
+
+ profiler.swap("citresewn:load_cits");
+ for (CIT cit : PackParser.loadCITs(resourceManager))
+ active.cits.computeIfAbsent(cit.type.getClass(), type -> new ArrayList<>()).add(cit);
+ for (Map.Entry<Class<? extends CITType>, List<CIT>> entry : active.cits.entrySet()) {
+ entry.getValue().sort(Comparator.<CIT>comparingInt(cit -> cit.weight).reversed().thenComparing(cit -> cit.propertiesIdentifier.toString()));
+ for (CITTypeContainer<? extends CITType> typeContainer : CITRegistry.TYPES.values())
+ if (typeContainer.type == entry.getKey()) {
+ typeContainer.load(entry.getValue());
+ break;
+ }
+ }
+
+ profiler.pop();
+
+ return ActiveCITs.active = active;
+ }
+
+ @Override
+ public void dispose() {
+ for (CITDisposable disposable : FabricLoader.getInstance().getEntrypoints(CITDisposable.ENTRYPOINT, CITDisposable.class))
+ disposable.dispose();
+
+ for (CITTypeContainer<? extends CITType> typeContainer : CITRegistry.TYPES.values())
+ typeContainer.dispose();
+ }
+}
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CIT.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CIT.java
new file mode 100644
index 0000000..ea75c04
--- /dev/null
+++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CIT.java
@@ -0,0 +1,27 @@
+package shcm.shsupercm.fabric.citresewn.cit;
+
+import net.minecraft.util.Identifier;
+
+public class CIT {
+ public final Identifier propertiesIdentifier;
+ public final String packName;
+ public final CITType type;
+ public final CITCondition[] conditions;
+ public final int weight;
+
+ public CIT(Identifier propertiesIdentifier, String packName, CITType type, CITCondition[] conditions, int weight) {
+ this.propertiesIdentifier = propertiesIdentifier;
+ this.packName = packName;
+ this.type = type;
+ this.conditions = conditions;
+ this.weight = weight;
+ }
+
+ public boolean test(CITContext context) {
+ for (CITCondition condition : conditions)
+ if (!condition.test(context))
+ return false;
+
+ return true;
+ }
+}
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCondition.java
new file mode 100644
index 0000000..7640d3c
--- /dev/null
+++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCondition.java
@@ -0,0 +1,35 @@
+package shcm.shsupercm.fabric.citresewn.cit;
+
+import shcm.shsupercm.fabric.citresewn.CITResewn;
+import shcm.shsupercm.fabric.citresewn.ex.CITParsingException;
+import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup;
+import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue;
+
+import java.util.Collections;
+import java.util.Set;
+
+public abstract class CITCondition {
+ public abstract void load(PropertyValue value, PropertyGroup properties) throws CITParsingException;
+
+ public Set<Class<? extends CITCondition>> siblingConditions() {
+ return Collections.emptySet();
+ }
+
+ public <T extends CITCondition> T modifySibling(Class<? extends CITCondition> siblingType, T sibling) {
+ return sibling;
+ }
+
+ public abstract boolean test(CITContext context);
+
+ protected void warn(String message, PropertyValue value, PropertyGroup properties) {
+ CITResewn.logWarnLoading("Warning: " + CITParsingException.descriptionOf(message, properties, value.position()));
+ }
+
+ protected int parseInteger(PropertyValue value, PropertyGroup properties) throws CITParsingException {
+ try {
+ return Integer.parseInt(value.value());
+ } catch (NumberFormatException e) {
+ throw new CITParsingException("\"" + value.value() + "\" is not a valid integer", properties, value.position());
+ }
+ }
+}
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITContext.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITContext.java
new file mode 100644
index 0000000..99cdbf0
--- /dev/null
+++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITContext.java
@@ -0,0 +1,32 @@
+package shcm.shsupercm.fabric.citresewn.cit;
+
+import net.minecraft.entity.LivingEntity;
+import net.minecraft.item.ItemStack;
+import net.minecraft.world.World;
+
+import java.util.Objects;
+
+public class CITContext {
+ public final ItemStack stack;
+ public final World world;
+ public final LivingEntity entity;
+
+ public CITContext(ItemStack stack, World world, LivingEntity entity) {
+ this.stack = stack;
+ this.world = world;
+ this.entity = entity;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ return obj instanceof CITContext that &&
+ Objects.equals(this.stack, that.stack) &&
+ Objects.equals(this.world, that.world) &&
+ Objects.equals(this.entity, that.entity);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(stack, world, entity);
+ }
+}
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java
new file mode 100644
index 0000000..33f6367
--- /dev/null
+++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java
@@ -0,0 +1,73 @@
+package shcm.shsupercm.fabric.citresewn.cit;
+
+import net.fabricmc.loader.api.FabricLoader;
+import net.minecraft.util.Identifier;
+import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer;
+import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer;
+import shcm.shsupercm.fabric.citresewn.ex.CITParsingException;
+import shcm.shsupercm.fabric.citresewn.ex.UnknownCITTypeException;
+import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup;
+import shcm.shsupercm.fabric.citresewn.pack.format.PropertyKey;
+import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue;
+
+import java.util.*;
+
+import static shcm.shsupercm.fabric.citresewn.CITResewn.info;
+import static shcm.shsupercm.fabric.citresewn.CITResewn.logWarnLoading;
+
+public class CITRegistry {
+ public static final Map<Identifier, CITTypeContainer<? extends CITType>> TYPES = new HashMap<>();
+ public static final Map<PropertyKey, CITConditionContainer<? extends CITCondition>> CONDITIONS = new HashMap<>();
+
+ public static void registerAll() {
+ info("Registering CIT Conditions");
+ for (var entrypointContainer : FabricLoader.getInstance().getEntrypointContainers(CITConditionContainer.ENTRYPOINT, CITConditionContainer.class)) {
+ String namespace = entrypointContainer.getProvider().getMetadata().getId();
+ if (namespace.equals("citresewn-defaults"))
+ namespace = "citresewn";
+
+ for (String alias : entrypointContainer.getEntrypoint().aliases)
+ CONDITIONS.put(new PropertyKey(namespace, alias), (CITConditionContainer<? extends CITCondition>) entrypointContainer.getEntrypoint());
+ }
+
+ info("Registering CIT Types");
+ for (var entrypointContainer : FabricLoader.getInstance().getEntrypointContainers(CITTypeContainer.ENTRYPOINT, CITTypeContainer.class)) {
+ String namespace = entrypointContainer.getProvider().getMetadata().getId();
+ if (namespace.equals("citresewn-defaults"))
+ namespace = "citresewn";
+
+ TYPES.put(new Identifier(namespace, entrypointContainer.getEntrypoint().id), (CITTypeContainer<? extends CITType>) entrypointContainer.getEntrypoint());
+ }
+ }
+
+ public static CITCondition parseCondition(PropertyKey key, PropertyValue value, PropertyGroup properties) throws CITParsingException {
+ CITConditionContainer<? extends CITCondition> conditionContainer = CONDITIONS.get(key);
+ if (conditionContainer == null) {
+ logWarnLoading("Skipping condition: " + CITParsingException.descriptionOf("Unknown condition type", properties, value.position()));
+ return null;
+ }
+
+ CITCondition condition = conditionContainer.createCondition.get();
+ condition.load(value, properties);
+ return condition;
+ }
+
+ public static CITType parseType(PropertyGroup properties) throws CITParsingException {
+ Identifier type = new Identifier("citresewn", "item");
+
+ PropertyValue propertiesType = properties.getLast("citresewn", "type");
+ if (propertiesType != null) {
+ String value = propertiesType.value();
+ if (!value.contains(":"))
+ value = "citresewn:" + value;
+ type = new Identifier(value);
+ }
+
+ CITTypeContainer<? extends CITType> typeContainer = TYPES.get(type);
+ if (typeContainer == null)
+ // assert (propertiesType != null) because the default citresewn:item should always be registered
+ throw new UnknownCITTypeException(properties, propertiesType == null ? -1 : propertiesType.position());
+
+ return typeContainer.createType.get();
+ }
+}
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java
new file mode 100644
index 0000000..abbe755
--- /dev/null
+++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITType.java
@@ -0,0 +1,10 @@
+package shcm.shsupercm.fabric.citresewn.cit;
+
+import shcm.shsupercm.fabric.citresewn.ex.CITParsingException;
+import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup;
+
+import java.util.List;
+
+public abstract class CITType {
+ public abstract void load(List<? extends CITCondition> conditions, PropertyGroup properties) throws CITParsingException;
+}
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/WeightCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/WeightCondition.java
new file mode 100644
index 0000000..ec0a5d7
--- /dev/null
+++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/WeightCondition.java
@@ -0,0 +1,27 @@
+package shcm.shsupercm.fabric.citresewn.cit.builtin;
+
+import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint;
+import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer;
+import shcm.shsupercm.fabric.citresewn.ex.CITParsingException;
+import shcm.shsupercm.fabric.citresewn.cit.CITCondition;
+import shcm.shsupercm.fabric.citresewn.cit.CITContext;
+import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup;
+import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue;
+
+public class WeightCondition extends CITCondition {
+ @Entrypoint(CITConditionContainer.ENTRYPOINT)
+ public static final CITConditionContainer<WeightCondition> CONTAINER = new CITConditionContainer<>(WeightCondition.class, WeightCondition::new,
+ "weight");
+
+ public int weight = 0;
+
+ @Override
+ public void load(PropertyValue value, PropertyGroup properties) throws CITParsingException {
+ this.weight = parseInteger(value, properties);
+ }
+
+ @Override
+ public boolean test(CITContext context) {
+ return true;
+ }
+}