2828import com .mojang .brigadier .context .CommandContext ;
2929import com .mojang .brigadier .context .ContextChain ;
3030import com .mojang .brigadier .exceptions .CommandSyntaxException ;
31+ import net .minecraft .commands .CommandSourceStack ;
3132import net .minecraft .commands .execution .tasks .ExecuteCommand ;
33+ import net .minecraft .server .level .ServerPlayer ;
34+ import org .checkerframework .checker .nullness .qual .Nullable ;
35+ import org .spongepowered .api .Sponge ;
36+ import org .spongepowered .api .command .CommandCause ;
3237import org .spongepowered .api .command .CommandResult ;
38+ import org .spongepowered .api .command .manager .CommandMapping ;
3339import org .spongepowered .api .event .Cause ;
40+ import org .spongepowered .api .event .CauseStackManager ;
3441import org .spongepowered .api .event .EventContextKeys ;
3542import org .spongepowered .api .event .SpongeEventFactory ;
3643import org .spongepowered .asm .mixin .Final ;
4047import org .spongepowered .asm .mixin .injection .Redirect ;
4148import org .spongepowered .common .SpongeCommon ;
4249import org .spongepowered .common .bridge .commands .CommandSourceStackBridge ;
43- import org .spongepowered .common .command .brigadier .context .SpongeCommandContext ;
50+ import org .spongepowered .common .event .tracking .PhaseTracker ;
51+ import org .spongepowered .common .event .tracking .phase .general .CommandPhaseContext ;
52+ import org .spongepowered .common .event .tracking .phase .general .GeneralPhase ;
4453
4554@ Mixin (ExecuteCommand .class )
4655public abstract class ExecuteCommandMixin {
@@ -52,26 +61,49 @@ public abstract class ExecuteCommandMixin {
5261 @ Redirect (method = "execute(Lnet/minecraft/commands/ExecutionCommandSource;Lnet/minecraft/commands/execution/ExecutionContext;Lnet/minecraft/commands/execution/Frame;)V" ,
5362 at = @ At (value = "INVOKE" , target = "Lcom/mojang/brigadier/context/ContextChain;runExecutable(Lcom/mojang/brigadier/context/CommandContext;Ljava/lang/Object;Lcom/mojang/brigadier/ResultConsumer;Z)I" ))
5463 private <S > int impl$onRunExecutable (final CommandContext <S > executable , final S source , final ResultConsumer <S > resultConsumer , final boolean forkedMode ) throws CommandSyntaxException {
55- final int result = ContextChain . runExecutable ( executable , source , resultConsumer , forkedMode );
64+ final @ Nullable ServerPlayer player = (( CommandSourceStack ) source ). getPlayer ( );
5665
5766 final Cause cause = ((CommandSourceStackBridge ) source ).bridge$getCause ();
5867 final String originalRawCommand = cause .context ().get (EventContextKeys .COMMAND ).orElse (this .commandInput );
5968 final String [] origSplitArg = originalRawCommand .split (" " , 2 );
6069 final String originalCommand = origSplitArg [0 ];
6170 final String originalArgs = origSplitArg .length == 2 ? origSplitArg [1 ] : "" ;
6271
63- final String [] splitArg = this .commandInput .split (" " , 2 );
64- final String baseCommand = splitArg [0 ];
65- final String args = splitArg .length == 2 ? splitArg [1 ] : "" ;
66- SpongeCommon .post (SpongeEventFactory .createExecuteCommandEventPost (
67- cause ,
68- originalArgs ,
69- args ,
70- originalCommand ,
71- baseCommand ,
72- ((SpongeCommandContext ) executable ).cause (),
73- CommandResult .builder ().result (result ).build ()
74- ));
75- return result ;
72+ try (final CauseStackManager .StackFrame frame = PhaseTracker .getCauseStackManager ().pushCauseFrame ()) {
73+ final CommandSourceStackBridge sourceBridge = (CommandSourceStackBridge ) source ;
74+ frame .addContext (EventContextKeys .COMMAND , this .commandInput );
75+ sourceBridge .bridge$updateFrameFromCommandSource (frame );
76+
77+ final CommandCause commandCause = sourceBridge .bridge$withCurrentCause ();
78+
79+ final String [] splitArg = this .commandInput .split (" " , 2 );
80+ final String baseCommand = splitArg [0 ];
81+ final String args = splitArg .length == 2 ? splitArg [1 ] : "" ;
82+ final CommandMapping mapping = Sponge .server ().commandManager ().commandMapping (baseCommand ).orElse (null );
83+
84+ try (final CommandPhaseContext context = GeneralPhase .State .COMMAND
85+ .createPhaseContext (PhaseTracker .getInstance ())
86+ .source (source )
87+ .command (this .commandInput )
88+ .commandMapping (mapping )) {
89+ if (player != null ) {
90+ context .creator (player .getUUID ());
91+ context .notifier (player .getUUID ());
92+ }
93+ context .buildAndSwitch ();
94+
95+ final int result = ContextChain .runExecutable (executable .copyFor ((S ) commandCause ), source , resultConsumer , forkedMode );
96+ SpongeCommon .post (SpongeEventFactory .createExecuteCommandEventPost (
97+ commandCause .cause (),
98+ originalArgs ,
99+ args ,
100+ originalCommand ,
101+ baseCommand ,
102+ commandCause ,
103+ CommandResult .builder ().result (result ).build ()
104+ ));
105+ return result ;
106+ }
107+ }
76108 }
77109}
0 commit comments