-
Notifications
You must be signed in to change notification settings - Fork 797
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ths commit introduces a subcommand "/tconstruct report" for "/tconstruct report modifier_priority", it also moves modifier_usage to be a subcommand of the same (which will make it nicer to tab complete "/tconstruct modifiers" Useful for determining the order things with the same hook will run. Unfortuantely will have limited benefits until 1.19 when modifier hooks are mandatory (as the report won't show overrides of non-modiifer hooks)
- Loading branch information
1 parent
50def7e
commit 563cb6b
Showing
5 changed files
with
142 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/slimeknights/tconstruct/shared/command/argument/ModifierHookArgument.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package slimeknights.tconstruct.shared.command.argument; | ||
|
||
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.DynamicCommandExceptionType; | ||
import com.mojang.brigadier.suggestion.Suggestions; | ||
import com.mojang.brigadier.suggestion.SuggestionsBuilder; | ||
import lombok.NoArgsConstructor; | ||
import net.minecraft.commands.CommandSourceStack; | ||
import net.minecraft.commands.SharedSuggestionProvider; | ||
import net.minecraft.resources.ResourceLocation; | ||
import slimeknights.tconstruct.TConstruct; | ||
import slimeknights.tconstruct.library.modifiers.ModifierHook; | ||
import slimeknights.tconstruct.library.modifiers.ModifierHooks; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** Argument type for a modifier */ | ||
@NoArgsConstructor(staticName = "modifierHook") | ||
public class ModifierHookArgument implements ArgumentType<ModifierHook<?>> { | ||
private static final Collection<String> EXAMPLES = Arrays.asList("tconstruct:tool_stats", "tconstruct:tooltip"); | ||
private static final DynamicCommandExceptionType HOOK_NOT_FOUND = new DynamicCommandExceptionType(name -> TConstruct.makeTranslation("command", "modifier_hook.not_found", name)); | ||
|
||
@Override | ||
public ModifierHook<?> parse(StringReader reader) throws CommandSyntaxException { | ||
ResourceLocation loc = ResourceLocation.read(reader); | ||
ModifierHook<?> hook = ModifierHooks.getHook(loc); | ||
if (hook == null) { | ||
throw HOOK_NOT_FOUND.create(loc); | ||
} | ||
return hook; | ||
} | ||
|
||
/** Gets a modifier from the command context */ | ||
public static ModifierHook<?> getModifier(CommandContext<CommandSourceStack> context, String name) { | ||
return context.getArgument(name, ModifierHook.class); | ||
} | ||
|
||
@Override | ||
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) { | ||
return SharedSuggestionProvider.suggestResource(ModifierHooks.listAllIDs(), builder); | ||
} | ||
|
||
@Override | ||
public Collection<String> getExamples() { | ||
return EXAMPLES; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/slimeknights/tconstruct/shared/command/subcommand/ModifierPriorityCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package slimeknights.tconstruct.shared.command.subcommand; | ||
|
||
import com.mojang.brigadier.builder.LiteralArgumentBuilder; | ||
import com.mojang.brigadier.context.CommandContext; | ||
import net.minecraft.commands.CommandSourceStack; | ||
import net.minecraft.commands.Commands; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.network.chat.TranslatableComponent; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraftforge.common.util.TablePrinter; | ||
import slimeknights.mantle.command.MantleCommand; | ||
import slimeknights.tconstruct.TConstruct; | ||
import slimeknights.tconstruct.library.modifiers.Modifier; | ||
import slimeknights.tconstruct.library.modifiers.ModifierHook; | ||
import slimeknights.tconstruct.library.modifiers.ModifierManager; | ||
import slimeknights.tconstruct.shared.command.argument.ModifierHookArgument; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
/** Command to list priorities for all modifiers */ | ||
public class ModifierPriorityCommand { | ||
private static final Component SUCCESS = new TranslatableComponent("command.tconstruct.modifier_priority"); | ||
|
||
/** | ||
* Registers this sub command with the root command | ||
* @param subCommand Command builder | ||
*/ | ||
public static void register(LiteralArgumentBuilder<CommandSourceStack> subCommand) { | ||
subCommand.requires(sender -> sender.hasPermission(MantleCommand.PERMISSION_EDIT_SPAWN)) | ||
// no argument: list all priorities | ||
.executes(context -> run(context, false)) | ||
// argument: list only priorities of modifiers using that hook | ||
.then(Commands.argument("modifier_hook", ModifierHookArgument.modifierHook()) | ||
.executes(context -> run(context, true))); | ||
} | ||
|
||
/** | ||
* Runs the command | ||
* @param context Command context | ||
* @param filtered If true, filter the modifiers based on the hook argument | ||
* @return Number of modifiers that are in the output table | ||
*/ | ||
private static int run(CommandContext<CommandSourceStack> context, boolean filtered) { | ||
// common table properties | ||
TablePrinter<Modifier> table = new TablePrinter<>(); | ||
table.header("ID", m -> m.getId().toString()); | ||
table.header("Priority", m -> Integer.toString(m.getPriority())); | ||
Stream<Modifier> modifiers = ModifierManager.INSTANCE.getAllValues(); | ||
StringBuilder builder = new StringBuilder(); | ||
builder.append("Modifier Priorities"); | ||
|
||
// if filtered, show fewer modifiers and add to the log name | ||
if (filtered) { | ||
ModifierHook<?> filter = ModifierHookArgument.getModifier(context, "modifier_hook"); | ||
modifiers = modifiers.filter(m -> m.getHooks().hasHook(filter)); | ||
builder.append(" for ").append(filter.getName()); | ||
} else { | ||
// if not filtered, include a row listing all used hooks | ||
table.header("Hooks", m -> m.getHooks().getAllModules().keySet().stream().map(ModifierHook::getName).sorted().map(ResourceLocation::toString).collect(Collectors.joining(", "))); | ||
} | ||
builder.append(":").append(System.lineSeparator()); | ||
List<Modifier> list = modifiers.sorted(Comparator.comparingInt(Modifier::getPriority).reversed().thenComparing(Modifier::getId)).toList(); | ||
table.add(list); | ||
|
||
table.build(builder); | ||
TConstruct.LOG.info(builder.toString()); | ||
context.getSource().sendSuccess(SUCCESS, true); | ||
return list.size(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters