11package top .mcfpp .mod .breakpoint .command ;
22
33import com .google .common .collect .Queues ;
4- import com .mojang .brigadier .arguments .IntegerArgumentType ;
4+ import net .minecraft .command .argument .*;
5+ import com .mojang .brigadier .arguments .*;
56import net .fabricmc .fabric .api .command .v2 .CommandRegistrationCallback ;
67import net .minecraft .command .CommandExecutionContext ;
7- import net .minecraft .server .command .CommandManager ;
8+ import static net .minecraft .server .command .CommandManager .literal ;
9+ import static net .minecraft .server .command .CommandManager .argument ;
10+
11+ import net .minecraft .nbt .NbtCompound ;
12+ import net .minecraft .nbt .NbtElement ;
13+ import net .minecraft .nbt .NbtHelper ;
814import net .minecraft .server .command .ServerCommandSource ;
915import net .minecraft .text .Text ;
1016import org .jetbrains .annotations .NotNull ;
17+ import org .jetbrains .annotations .Nullable ;
1118import top .mcfpp .mod .breakpoint .DatapackBreakpoint ;
1219
1320import java .util .Deque ;
@@ -22,26 +29,47 @@ public class BreakPointCommand {
2229
2330 public static void onInitialize () {
2431 CommandRegistrationCallback .EVENT .register ((dispatcher , registryAccess , environment ) -> {
25- dispatcher .register (CommandManager . literal ("breakpoint" )
32+ dispatcher .register (literal ("breakpoint" )
2633 .requires (source -> source .hasPermissionLevel (2 ))
2734 .executes (context -> {
2835 context .getSource ().sendFeedback (() -> Text .literal ("已触发断点" ), false );
2936 breakPoint (context .getSource ());
3037 return 1 ;
3138 })
32- .then (CommandManager . literal ("step" )
39+ .then (literal ("step" )
3340 .executes (context -> {
3441 step (1 , context .getSource ());
3542 return 1 ;
3643 })
44+ .then (argument ("lines" , IntegerArgumentType .integer ())
45+ .executes (context -> {
46+ final int lines = IntegerArgumentType .getInteger (context , "lines" );
47+ step (lines , context .getSource ());
48+ return 1 ;
49+ })
50+ )
3751 )
38- .then (CommandManager . literal ("move" )
52+ .then (literal ("move" )
3953 .executes (context -> {
4054 context .getSource ().sendFeedback (() -> Text .literal ("已恢复断点" ), false );
4155 moveOn (context .getSource ());
4256 return 1 ;
4357 })
4458 )
59+ .then (literal ("get" )
60+ .then (argument ("key" , StringArgumentType .string ())
61+ .executes (context -> {
62+ final String key = StringArgumentType .getString (context , "key" );
63+ NbtElement nbt = getNBT (key );
64+ if (nbt == null ){
65+ context .getSource ().sendError (Text .literal ("无法在当前上下文获取" + key + "的值" ));
66+ }else {
67+ context .getSource ().sendFeedback (() -> Text .literal (key + "的值是:" ).append (NbtHelper .toPrettyPrintedText (nbt )), false );
68+ }
69+ return 1 ;
70+ })
71+ )
72+ )
4573 );
4674 });
4775 }
@@ -83,7 +111,7 @@ private static void step(int steps, ServerCommandSource source) {
83111 try {
84112 context .close ();
85113 } catch (Exception e ) {
86- LOGGER .error (e .getMessage ());
114+ LOGGER .error (e .toString ());
87115 }
88116 }
89117 }
@@ -98,8 +126,25 @@ private static void moveOn(@NotNull ServerCommandSource source) {
98126 context .run ();
99127 context .close ();
100128 } catch (Exception e ) {
101- LOGGER .error (e .getMessage ());
129+ LOGGER .error (e .toString ());
102130 }
103131 }
104132 }
133+
134+ private static @ Nullable NbtElement getNBT (String key ){
135+ var context = storedCommandExecutionContext .peekFirst ();
136+ if (context == null ){
137+ return null ;
138+ }
139+ try {
140+ var cls = context .getClass ();
141+ var method = cls .getDeclaredMethod ("getKey" , String .class );
142+ method .setAccessible (true );
143+ return (NbtElement ) method .invoke (context , key );
144+ }catch (Exception e ){
145+ LOGGER .error (e .toString ());
146+ return null ;
147+ }
148+
149+ }
105150}
0 commit comments