aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/shcm
diff options
context:
space:
mode:
authorSHsuperCM <shsupercm@gmail.com>2022-03-04 15:29:14 +0200
committerSHsuperCM <shsupercm@gmail.com>2022-03-04 15:29:14 +0200
commitde66a476d01d606c3815efebc904a5c6d9bce133 (patch)
tree044e5961c8a67498284f0c38bf70215d28981a56 /src/main/java/shcm
parent1f82ebe89c0e98eaea141eea9a493547b9093dac (diff)
downloadCITResewn-de66a476d01d606c3815efebc904a5c6d9bce133.tar.gz
CITResewn-de66a476d01d606c3815efebc904a5c6d9bce133.tar.bz2
CITResewn-de66a476d01d606c3815efebc904a5c6d9bce133.zip
Added analyze pack command
Diffstat (limited to 'src/main/java/shcm')
-rw-r--r--src/main/java/shcm/shsupercm/fabric/citresewn/CITResewnCommand.java111
-rw-r--r--src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java26
2 files changed, 130 insertions, 7 deletions
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewnCommand.java b/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewnCommand.java
index b06a8ba..01c08bf 100644
--- a/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewnCommand.java
+++ b/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewnCommand.java
@@ -1,13 +1,26 @@
package shcm.shsupercm.fabric.citresewn;
+import com.mojang.brigadier.LiteralMessage;
+import com.mojang.brigadier.StringReader;
+import com.mojang.brigadier.arguments.ArgumentType;
+import com.mojang.brigadier.context.CommandContext;
+import com.mojang.brigadier.exceptions.CommandSyntaxException;
+import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
+import com.mojang.brigadier.suggestion.Suggestions;
+import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.fabricmc.fabric.api.client.command.v1.ClientCommandManager;
import net.fabricmc.loader.api.FabricLoader;
-import shcm.shsupercm.fabric.citresewn.cit.ActiveCITs;
-import shcm.shsupercm.fabric.citresewn.cit.CITRegistry;
+import net.minecraft.text.Text;
+import shcm.shsupercm.fabric.citresewn.cit.*;
import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig;
+import shcm.shsupercm.fabric.citresewn.pack.format.PropertyKey;
+import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue;
-import java.util.Collection;
+import java.util.*;
+import java.util.concurrent.CompletableFuture;
+import java.util.stream.Collectors;
+import static net.fabricmc.fabric.api.client.command.v1.ClientCommandManager.argument;
import static net.fabricmc.fabric.api.client.command.v1.ClientCommandManager.literal;
import static net.minecraft.text.Text.of;
@@ -34,6 +47,96 @@ public class CITResewnCommand {
openConfig = true;
return 1;
- })));
+ }))
+ .then(literal("analyze")
+ .then(literal("pack")
+ .then(argument("pack", new CITPackArgument())
+ .executes(context -> {
+ final String pack = context.getArgument("pack", String.class);
+ if (ActiveCITs.isActive()) {
+ context.getSource().sendFeedback(of("Analyzed CIT data of \"" + pack + "\u00a7r\":"));
+
+ List<Text> builder = new ArrayList<>();
+
+ for (Map.Entry<PropertyKey, Set<PropertyValue>> entry : ActiveCITs.getActive().globalProperties.properties.entrySet())
+ for (PropertyValue value : entry.getValue())
+ if (value.packName().equals(pack))
+ builder.add(of(" " + entry.getKey().toString() + (value.keyMetadata() == null ? "" : "." + value.keyMetadata()) + " = " + value.value()));
+ if (!builder.isEmpty()) {
+ context.getSource().sendFeedback(of(" Global Properties:"));
+ for (Text text : builder)
+ context.getSource().sendFeedback(text);
+
+ builder.clear();
+ }
+
+ for (Map.Entry<Class<? extends CITType>, List<CIT<?>>> entry : ActiveCITs.getActive().cits.entrySet())
+ if (!entry.getValue().isEmpty()) {
+ long count = entry.getValue().stream().filter(cit -> cit.packName.equals(pack)).count();
+ if (count > 0)
+ builder.add(of(" " + CITRegistry.idOfType(entry.getKey()).toString() + " = " + count));
+ }
+ if (!builder.isEmpty()) {
+ context.getSource().sendFeedback(of(" Types:"));
+ for (Text text : builder)
+ context.getSource().sendFeedback(text);
+
+ builder.clear();
+ }
+
+ List<CITCondition> conditions = ActiveCITs.getActive().cits.values().stream()
+ .flatMap(Collection::stream)
+ .filter(cit -> cit.packName.equals(pack))
+ .flatMap(cit -> Arrays.stream(cit.conditions))
+ .toList();
+ if (!conditions.isEmpty())
+ context.getSource().sendFeedback(of(" Utilizing " + conditions.size() + " conditions(" + conditions.stream().map(Object::getClass).distinct().count() + " unique condition types)"));
+ } else
+ context.getSource().sendFeedback(of("Not active"));
+
+ return 1;
+ })
+ )
+ )
+ )
+ );
+ }
+
+ private static class CITPackArgument implements ArgumentType<String> {
+ @Override
+ public String parse(StringReader reader) throws CommandSyntaxException {
+ StringBuilder builder = new StringBuilder();
+ while (reader.canRead())
+ builder.append(reader.read());
+
+ String pack = builder.toString().trim();
+
+ if (!getPacks().contains(pack)) {
+ LiteralMessage message = new LiteralMessage("Could not find CIT pack");
+ throw new CommandSyntaxException(new SimpleCommandExceptionType(message), message);
+ }
+
+ return pack;
+ }
+
+ @Override
+ public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
+ return CompletableFuture.supplyAsync(() -> {
+ for (String pack : getPacks()) {
+ builder.suggest(pack);
+ }
+ return builder.build();
+ });
+ }
+
+ private static Set<String> getPacks() {
+ if (ActiveCITs.isActive())
+ return ActiveCITs.getActive().cits.values().stream()
+ .flatMap(Collection::stream)
+ .map(cit -> cit.packName)
+ .collect(Collectors.toSet());
+ else
+ return Collections.emptySet();
+ }
}
}
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java
index ff38ef8..47f79fe 100644
--- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java
+++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java
@@ -20,6 +20,9 @@ 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<>();
+ private static final Map<Class<? extends CITType>, Identifier> TYPE_TO_ID = new IdentityHashMap<>();
+ private static final Map<Class<? extends CITCondition>, PropertyKey> CONDITION_TO_ID = new IdentityHashMap<>();
+
public static void registerAll() {
info("Registering CIT Conditions");
for (var entrypointContainer : FabricLoader.getInstance().getEntrypointContainers(CITConditionContainer.ENTRYPOINT, CITConditionContainer.class)) {
@@ -27,8 +30,13 @@ public class CITRegistry {
if (namespace.equals("citresewn-defaults"))
namespace = "citresewn";
- for (String alias : entrypointContainer.getEntrypoint().aliases)
- CONDITIONS.put(new PropertyKey(namespace, alias), (CITConditionContainer<? extends CITCondition>) entrypointContainer.getEntrypoint());
+ for (String alias : entrypointContainer.getEntrypoint().aliases) {
+ final PropertyKey key = new PropertyKey(namespace, alias);
+ CITConditionContainer<?> container = entrypointContainer.getEntrypoint();
+
+ CONDITIONS.put(key, container);
+ CONDITION_TO_ID.putIfAbsent(container.createCondition.get().getClass(), key);
+ }
}
info("Registering CIT Types");
@@ -37,7 +45,11 @@ public class CITRegistry {
if (namespace.equals("citresewn-defaults"))
namespace = "citresewn";
- TYPES.put(new Identifier(namespace, entrypointContainer.getEntrypoint().id), (CITTypeContainer<? extends CITType>) entrypointContainer.getEntrypoint());
+ final Identifier id = new Identifier(namespace, entrypointContainer.getEntrypoint().id);
+ CITTypeContainer<?> container = entrypointContainer.getEntrypoint();
+
+ TYPES.put(id, container);
+ TYPE_TO_ID.putIfAbsent(container.createType.get().getClass(), id);
}
}
@@ -71,4 +83,12 @@ public class CITRegistry {
return typeContainer.createType.get();
}
+
+ public static Identifier idOfType(Class<? extends CITType> clazz) {
+ return TYPE_TO_ID.get(clazz);
+ }
+
+ public static PropertyKey idOfCondition(Class<? extends CITCondition> clazz) {
+ return CONDITION_TO_ID.get(clazz);
+ }
}