diff options
author | SHsuperCM <shsupercm@gmail.com> | 2022-03-04 15:29:14 +0200 |
---|---|---|
committer | SHsuperCM <shsupercm@gmail.com> | 2022-03-04 15:29:14 +0200 |
commit | de66a476d01d606c3815efebc904a5c6d9bce133 (patch) | |
tree | 044e5961c8a67498284f0c38bf70215d28981a56 /src/main/java/shcm/shsupercm/fabric/citresewn/CITResewnCommand.java | |
parent | 1f82ebe89c0e98eaea141eea9a493547b9093dac (diff) | |
download | CITResewn-de66a476d01d606c3815efebc904a5c6d9bce133.tar.gz CITResewn-de66a476d01d606c3815efebc904a5c6d9bce133.tar.bz2 CITResewn-de66a476d01d606c3815efebc904a5c6d9bce133.zip |
Added analyze pack command
Diffstat (limited to 'src/main/java/shcm/shsupercm/fabric/citresewn/CITResewnCommand.java')
-rw-r--r-- | src/main/java/shcm/shsupercm/fabric/citresewn/CITResewnCommand.java | 111 |
1 files changed, 107 insertions, 4 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(); + } } } |