Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Mixin(CommandManager.class)
- public abstract class CommandManagerExecuteMixin {
- @Unique
- private static final ThreadLocal<Boolean> IS_ALIAS_REEXECUTION = ThreadLocal.withInitial(() -> false);
- @Inject(method = "execute(Lcom/mojang/brigadier/ParseResults;Ljava/lang/String;)V", at = @At("HEAD"), cancellable = true)
- private void Fantasy$interceptCommandExecution(ParseResults<ServerCommandSource> parseResults, String commandString, CallbackInfo ci) {
- ServerCommandSource source = parseResults.getContext().getSource();
- ServerPlayerEntity sender = source.getPlayer();
- if (sender == null) {
- return;
- }
- boolean isRecursiveAliasCall = IS_ALIAS_REEXECUTION.get();
- String enteredCommandName = commandString.split(" ")[0].toLowerCase().replaceFirst("^/", "");
- String canonicalCommandName = CM.getAliasToCanonicalNameMap().get(enteredCommandName);
- if (canonicalCommandName != null && !isRecursiveAliasCall) {
- String argsPart = "";
- int firstSpace = commandString.indexOf(' ');
- if (firstSpace != -1) {
- argsPart = commandString.substring(firstSpace);
- }
- String commandToExecute = canonicalCommandName + argsPart; // No leading slash for dispatcher.parse
- try {
- CommandDispatcher<ServerCommandSource> dispatcher = CM.getDispatcher();
- IS_ALIAS_REEXECUTION.set(true);
- ParseResults<ServerCommandSource> canonicalParseResults = dispatcher.parse(commandToExecute, source);
- dispatcher.execute(canonicalParseResults);
- } catch (Exception e) {
- // Error handling for execution failures
- } finally {
- IS_ALIAS_REEXECUTION.set(false);
- }
- ci.cancel();
- return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment