12
12
import net .earthcomputer .clientcommands .features .BrigadierRemover ;
13
13
import net .fabricmc .fabric .api .client .command .v1 .FabricClientCommandSource ;
14
14
import net .fabricmc .loader .api .FabricLoader ;
15
- import net .minecraft .client . MinecraftClient ;
15
+ import net .minecraft .text . Text ;
16
16
import net .minecraft .text .TranslatableText ;
17
17
import net .minecraft .util .Formatting ;
18
18
import org .slf4j .Logger ;
25
25
import java .util .regex .Pattern ;
26
26
27
27
import static com .mojang .brigadier .arguments .StringArgumentType .*;
28
- import static net .earthcomputer .clientcommands .command .ClientCommandHelper .*;
29
28
import static net .fabricmc .fabric .api .client .command .v1 .ClientCommandManager .*;
30
29
31
30
public class AliasCommand {
@@ -47,26 +46,26 @@ public static void register(CommandDispatcher<FabricClientCommandSource> dispatc
47
46
.then (literal ("add" )
48
47
.then (argument ("key" , string ())
49
48
.then (argument ("command" , greedyString ())
50
- .executes (ctx -> addAlias (getString (ctx , "key" ), getString (ctx , "command" ))))))
49
+ .executes (ctx -> addAlias (ctx . getSource (), getString (ctx , "key" ), getString (ctx , "command" ))))))
51
50
.then (literal ("list" )
52
- .executes (ctx -> listAliases ()))
51
+ .executes (ctx -> listAliases (ctx . getSource () )))
53
52
.then (literal ("remove" )
54
53
.then (argument ("key" , string ())
55
- .executes (ctx -> removeAlias (getString (ctx , "key" ))))));
54
+ .executes (ctx -> removeAlias (ctx . getSource (), getString (ctx , "key" ))))));
56
55
57
- for (String key : aliasMap .keySet ()) {
56
+ for (String key : aliasMap .keySet ()) {
58
57
if (dispatcher .getRoot ().getChildren ().stream ().map (CommandNode ::getName ).noneMatch (literal -> literal .equals (key ))) {
59
58
dispatcher .register (literal (key )
60
- .executes (ctx -> executeAliasCommand (key , null ))
59
+ .executes (ctx -> executeAliasCommand (ctx . getSource (), key , null ))
61
60
.then (argument ("arguments" , greedyString ())
62
- .executes (ctx -> executeAliasCommand (key , getString (ctx , "arguments" )))));
61
+ .executes (ctx -> executeAliasCommand (ctx . getSource (), key , getString (ctx , "arguments" )))));
63
62
} else {
64
63
LOGGER .error ("Attempted to register alias /{}, but that command already exists" , key );
65
64
}
66
65
}
67
66
}
68
67
69
- private static int executeAliasCommand (String aliasKey , String arguments ) throws CommandSyntaxException {
68
+ private static int executeAliasCommand (FabricClientCommandSource source , String aliasKey , String arguments ) throws CommandSyntaxException {
70
69
String cmd = aliasMap .get (aliasKey );
71
70
if (cmd == null ) {
72
71
throw NOT_FOUND_EXCEPTION .create (aliasKey );
@@ -87,13 +86,12 @@ private static int executeAliasCommand(String aliasKey, String arguments) throws
87
86
} else if (arguments != null ){
88
87
cmd += " " + arguments ;
89
88
}
90
- assert MinecraftClient .getInstance ().player != null ;
91
- MinecraftClient .getInstance ().player .sendChatMessage (cmd );
89
+ source .getPlayer ().sendChatMessage (cmd );
92
90
93
91
return 0 ;
94
92
}
95
93
96
- private static int addAlias (String key , String command ) throws CommandSyntaxException {
94
+ private static int addAlias (FabricClientCommandSource source , String key , String command ) throws CommandSyntaxException {
97
95
if (aliasMap .containsKey (key )) {
98
96
throw ALIAS_EXISTS_EXCEPTION .create (key );
99
97
}
@@ -105,29 +103,29 @@ private static int addAlias(String key, String command) throws CommandSyntaxExce
105
103
}
106
104
107
105
DISPATCHER .register (literal (key )
108
- .executes (ctx -> executeAliasCommand (key , null ))
106
+ .executes (ctx -> executeAliasCommand (source , key , null ))
109
107
.then (argument ("arguments" , greedyString ())
110
- .executes (ctx -> executeAliasCommand (key , getString (ctx , "arguments" )))));
108
+ .executes (ctx -> executeAliasCommand (source , key , getString (ctx , "arguments" )))));
111
109
aliasMap .put (key , command );
112
110
113
111
saveAliases ();
114
- sendFeedback (new TranslatableText ("commands.calias.addAlias.success" , key ));
112
+ source . sendFeedback (new TranslatableText ("commands.calias.addAlias.success" , key ));
115
113
return 0 ;
116
114
}
117
115
118
- private static int listAliases () {
116
+ private static int listAliases (FabricClientCommandSource source ) {
119
117
if (aliasMap .isEmpty ()) {
120
- sendFeedback (new TranslatableText ("commands.calias.listAliases.noAliasesRegistered" ));
118
+ source . sendFeedback (new TranslatableText ("commands.calias.listAliases.noAliasesRegistered" ));
121
119
} else {
122
- sendFeedback ("commands.calias.listAliases.success" , aliasMap .size ());
123
- for (String key : aliasMap .keySet ()) {
124
- sendFeedback (Formatting .BOLD + key + Formatting .RESET + ": " + aliasMap .get (key ).replace ("%" ,"%%" ));
120
+ source . sendFeedback (new TranslatableText ( "commands.calias.listAliases.success" , aliasMap .size () ));
121
+ for (String key : aliasMap .keySet ()) {
122
+ source . sendFeedback (Text . of ( Formatting .BOLD + key + Formatting .RESET + ": " + aliasMap .get (key ).replace ("%" ,"%%" ) ));
125
123
}
126
124
}
127
125
return 0 ;
128
126
}
129
127
130
- private static int removeAlias (String key ) throws CommandSyntaxException {
128
+ private static int removeAlias (FabricClientCommandSource source , String key ) throws CommandSyntaxException {
131
129
if (aliasMap .containsKey (key )) {
132
130
BrigadierRemover .of (DISPATCHER ).get (key ).remove ();
133
131
aliasMap .remove (key );
@@ -136,7 +134,7 @@ private static int removeAlias(String key) throws CommandSyntaxException {
136
134
}
137
135
138
136
saveAliases ();
139
- sendFeedback (new TranslatableText ("commands.calias.removeAlias.success" , key ));
137
+ source . sendFeedback (new TranslatableText ("commands.calias.removeAlias.success" , key ));
140
138
return 0 ;
141
139
}
142
140
0 commit comments