SHOW:
|
|
- or go back to the newest paste.
1 | - | private static Map<String, String> aliasToCanonicalNameMap = new HashMap<>(); |
1 | + | @Mixin(CommandManager.class) |
2 | public abstract class CommandManagerExecuteMixin { | |
3 | - | private void registerCommandAliases(CommandDispatcher<ServerCommandSource> dispatcher, Map<String, List<String>> configuredAliases) { |
3 | + | |
4 | - | aliasToCanonicalNameMap.clear(); |
4 | + | @Unique |
5 | private static final ThreadLocal<Boolean> IS_ALIAS_REEXECUTION = ThreadLocal.withInitial(() -> false); | |
6 | - | for (Map.Entry<String, List<String>> entry : configuredAliases.entrySet()) { |
6 | + | |
7 | - | String canonicalCommandName = entry.getKey(); |
7 | + | @Inject(method = "execute(Lcom/mojang/brigadier/ParseResults;Ljava/lang/String;)V", at = @At("HEAD"), cancellable = true) |
8 | - | List<String> aliases = entry.getValue(); |
8 | + | private void Fantasy$interceptCommandExecution(ParseResults<ServerCommandSource> parseResults, String commandString, CallbackInfo ci) { |
9 | - | CommandNode<ServerCommandSource> canonicalNode = dispatcher.getRoot().getChild(canonicalCommandName); |
9 | + | ServerCommandSource source = parseResults.getContext().getSource(); |
10 | ServerPlayerEntity sender = source.getPlayer(); | |
11 | - | if (canonicalNode == null) { |
11 | + | |
12 | - | // Warning logged if canonical command not found |
12 | + | if (sender == null) { |
13 | - | continue; |
13 | + | return; |
14 | } | |
15 | ||
16 | - | for (String alias : aliases) { |
16 | + | boolean isRecursiveAliasCall = IS_ALIAS_REEXECUTION.get(); |
17 | - | String cleanAlias = alias.startsWith("/") ? alias.substring(1) : alias; |
17 | + | |
18 | - | dispatcher.register(CommandManager.literal(cleanAlias).redirect(canonicalNode)); |
18 | + | String enteredCommandName = commandString.split(" ")[0].toLowerCase().replaceFirst("^/", ""); |
19 | - | aliasToCanonicalNameMap.put(cleanAlias, canonicalCommandName); |
19 | + | String canonicalCommandName = CM.getAliasToCanonicalNameMap().get(enteredCommandName); |
20 | ||
21 | if (canonicalCommandName != null && !isRecursiveAliasCall) { | |
22 | String argsPart = ""; | |
23 | int firstSpace = commandString.indexOf(' '); | |
24 | - | public static Map<String, String> getAliasToCanonicalNameMap() { |
24 | + | if (firstSpace != -1) { |
25 | - | return aliasToCanonicalNameMap; |
25 | + | argsPart = commandString.substring(firstSpace); |
26 | - | } |
26 | + | |
27 | ||
28 | String commandToExecute = canonicalCommandName + argsPart; // No leading slash for dispatcher.parse | |
29 | ||
30 | try { | |
31 | CommandDispatcher<ServerCommandSource> dispatcher = CM.getDispatcher(); | |
32 | IS_ALIAS_REEXECUTION.set(true); | |
33 | ParseResults<ServerCommandSource> canonicalParseResults = dispatcher.parse(commandToExecute, source); | |
34 | dispatcher.execute(canonicalParseResults); | |
35 | } catch (Exception e) { | |
36 | // Error handling for execution failures | |
37 | } finally { | |
38 | IS_ALIAS_REEXECUTION.set(false); | |
39 | } | |
40 | ||
41 | ci.cancel(); | |
42 | return; | |
43 | } | |
44 | } | |
45 | } |