3030import com .mojang .brigadier .arguments .IntegerArgumentType ;
3131import com .mojang .brigadier .arguments .StringArgumentType ;
3232import com .mojang .brigadier .context .CommandContext ;
33- import net . minecraft . server . command . ServerCommandSource ;
34- import net .minecraft .text . Text ;
35- import net .minecraft .util . Formatting ;
36- import net .minecraft .util . Pair ;
33+ import com . mojang . datafixers . util . Pair ;
34+ import net .minecraft .ChatFormatting ;
35+ import net .minecraft .commands . CommandSourceStack ;
36+ import net .minecraft .network . chat . Component ;
3737
3838import java .io .IOException ;
3939import java .net .URI ;
4343import static com .ayakacraft .authlibproxyforserver .AuthlibProxyForServer .LOGGER ;
4444import static com .ayakacraft .authlibproxyforserver .AuthlibProxyForServer .config ;
4545import static com .ayakacraft .authlibproxyforserver .AuthlibProxyForServer .proxy ;
46- import static net .minecraft .server . command . CommandManager .argument ;
47- import static net .minecraft .server . command . CommandManager .literal ;
46+ import static net .minecraft .commands . Commands .argument ;
47+ import static net .minecraft .commands . Commands .literal ;
4848
4949public final class AuthProxyCommand {
5050
5151 private static final int TCPING_TIMES = 5 ;
5252
53- private static int display (CommandContext <ServerCommandSource > context ) {
53+ private static int display (CommandContext <CommandSourceStack > context ) {
5454 sendFeedback (
5555 context .getSource (),
5656 proxyText (),
@@ -60,12 +60,12 @@ private static int display(CommandContext<ServerCommandSource> context) {
6060 return 1 ;
6161 }
6262
63- private static int enable (CommandContext <ServerCommandSource > context ) {
64- ServerCommandSource source = context .getSource ();
63+ private static int enable (CommandContext <CommandSourceStack > context ) {
64+ CommandSourceStack source = context .getSource ();
6565 if (config .enabled ) {
6666 sendFeedback (
6767 source ,
68- Text .literal ("Proxy already enabled" ),
68+ Component .literal ("Proxy already enabled" ),
6969 false
7070 );
7171 return 1 ;
@@ -76,18 +76,18 @@ private static int enable(CommandContext<ServerCommandSource> context) {
7676 }
7777 sendFeedback (
7878 source ,
79- Text .literal ("Proxy enabled, restart the server to apply" ),
79+ Component .literal ("Proxy enabled, restart the server to apply" ),
8080 true
8181 );
8282 return 1 ;
8383 }
8484
85- private static int disable (CommandContext <ServerCommandSource > context ) {
86- ServerCommandSource source = context .getSource ();
85+ private static int disable (CommandContext <CommandSourceStack > context ) {
86+ CommandSourceStack source = context .getSource ();
8787 if (!config .enabled ) {
8888 sendFeedback (
8989 source ,
90- Text .literal ("Proxy already disabled" ),
90+ Component .literal ("Proxy already disabled" ),
9191 false
9292 );
9393 return 1 ;
@@ -98,22 +98,22 @@ private static int disable(CommandContext<ServerCommandSource> context) {
9898 }
9999 sendFeedback (
100100 source ,
101- Text .literal ("Proxy disabled, restart the server to apply" ),
101+ Component .literal ("Proxy disabled, restart the server to apply" ),
102102 true
103103 );
104104 return 1 ;
105105 }
106106
107- private static int port (CommandContext <ServerCommandSource > context ) {
108- ServerCommandSource source = context .getSource ();
109- int port = IntegerArgumentType .getInteger (context , "port" );
107+ private static int port (CommandContext <CommandSourceStack > context ) {
108+ CommandSourceStack source = context .getSource ();
109+ int port = IntegerArgumentType .getInteger (context , "port" );
110110 config .port = (short ) port ;
111111 if (saveConfig (source )) {
112112 return 0 ;
113113 }
114114 sendFeedback (
115115 source ,
116- Text .literal ("Proxy port set to " + port ),
116+ Component .literal ("Proxy port set to " + port ),
117117 true
118118 );
119119 sendFeedback (
@@ -124,16 +124,16 @@ private static int port(CommandContext<ServerCommandSource> context) {
124124 return 1 ;
125125 }
126126
127- private static int host (CommandContext <ServerCommandSource > context ) {
128- ServerCommandSource source = context .getSource ();
129- String host = StringArgumentType .getString (context , "host" );
127+ private static int host (CommandContext <CommandSourceStack > context ) {
128+ CommandSourceStack source = context .getSource ();
129+ String host = StringArgumentType .getString (context , "host" );
130130 config .host = host ;
131131 if (saveConfig (source )) {
132132 return 0 ;
133133 }
134134 sendFeedback (
135135 source ,
136- Text .literal ("Proxy host set to " + host ),
136+ Component .literal ("Proxy host set to " + host ),
137137 true
138138 );
139139 sendFeedback (
@@ -144,15 +144,15 @@ private static int host(CommandContext<ServerCommandSource> context) {
144144 return 1 ;
145145 }
146146
147- private static int type (CommandContext <ServerCommandSource > context , String type ) {
148- ServerCommandSource source = context .getSource ();
147+ private static int type (CommandContext <CommandSourceStack > context , String type ) {
148+ CommandSourceStack source = context .getSource ();
149149 config .type = ProxyConfig .ProxyType .valueOf (type );
150150 if (saveConfig (source )) {
151151 return 0 ;
152152 }
153153 sendFeedback (
154154 source ,
155- Text .literal ("Proxy type set to " + type ),
155+ Component .literal ("Proxy type set to " + type ),
156156 true
157157 );
158158 sendFeedback (
@@ -163,7 +163,7 @@ private static int type(CommandContext<ServerCommandSource> context, String type
163163 return 1 ;
164164 }
165165
166- private static int ping (CommandContext <ServerCommandSource > context ) {
166+ private static int ping (CommandContext <CommandSourceStack > context ) {
167167 Set <String > hosts = Sets .newHashSet ();
168168 //#if MC>=12006
169169 com .mojang .authlib .Environment env = com .ayakacraft .authlibproxyforserver .mixin .YggdrasilAuthenticationServiceAccessor .determineEnvironment ();
@@ -187,46 +187,46 @@ private static int ping(CommandContext<ServerCommandSource> context) {
187187 /**
188188 * @return true if failed
189189 */
190- private static boolean saveConfig (ServerCommandSource source ) {
190+ private static boolean saveConfig (CommandSourceStack source ) {
191191 try {
192192 AuthlibProxyForServer .saveConfig (config .validate ());
193193 } catch (IOException e ) {
194- source .sendError ( Text .literal ("Error saving config" ));
194+ source .sendFailure ( Component .literal ("Error saving config" ));
195195 LOGGER .error ("Error saving config" , e );
196196 return true ;
197197 } catch (ProxyConfig .InvalidProxyConfigException e ) {
198- source .sendError ( Text .literal (e .getMessage ()));
198+ source .sendFailure ( Component .literal (e .getMessage ()));
199199 LOGGER .error (e );
200200 return true ;
201201 }
202202 return false ;
203203 }
204204
205- private static Text proxyText () {
206- return Text .literal ("Proxy for authlib: " + proxy .toString ());
207- }
208-
209205 @ PreprocessPattern
210- private static Text li (String str ) {
206+ private static Component li (String str ) {
211207 //#if MC>=11900
212- return Text .literal (str );
208+ return Component .literal (str );
213209 //#else
214- //$$ return new net.minecraft.text.LiteralText (str);
210+ //$$ return new net.minecraft.network.chat.TextComponent (str);
215211 //#endif
216212 }
217213
218- private static void sendFeedback (ServerCommandSource source , Text txt , boolean broadcastToOps ) {
214+ private static Component proxyText () {
215+ return Component .literal ("Proxy for authlib: " + proxy .toString ());
216+ }
217+
218+ private static void sendFeedback (CommandSourceStack source , Component txt , boolean broadcastToOps ) {
219219 //#if MC>=12000
220- source .sendFeedback (() -> txt , broadcastToOps );
220+ source .sendSuccess (() -> txt , broadcastToOps );
221221 //#else
222- //$$ source.sendFeedback (txt, broadcastToOps);
222+ //$$ source.sendSuccess (txt, broadcastToOps);
223223 //#endif
224224 }
225225
226- public static void register (CommandDispatcher <ServerCommandSource > dispatcher ) {
226+ public static void register (CommandDispatcher <CommandSourceStack > dispatcher ) {
227227 dispatcher .register (
228228 literal ("authproxy" )
229- .requires (source -> source .hasPermissionLevel (source .getServer ().getOpPermissionLevel ()))
229+ .requires (source -> source .hasPermission (source .getServer ().getOperatorUserPermissionLevel ()))
230230 .executes (AuthProxyCommand ::display )
231231 .then (literal ("enable" ).executes (AuthProxyCommand ::enable ))
232232 .then (literal ("disable" ).executes (AuthProxyCommand ::disable ))
@@ -248,45 +248,45 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
248248
249249 private static class TcpingThread extends Thread {
250250
251- private static Text packetLossStatusText (int packetsReceived ) {
252- double packetLoss = 1D - (double ) packetsReceived / AuthProxyCommand .TCPING_TIMES ;
253- Formatting colour ;
251+ private static Component packetLossStatusText (int packetsReceived ) {
252+ double packetLoss = 1D - (double ) packetsReceived / AuthProxyCommand .TCPING_TIMES ;
253+ ChatFormatting colour ;
254254 if (packetLoss > 0.8D ) {
255- colour = Formatting .RED ;
255+ colour = ChatFormatting .RED ;
256256 } else if (packetLoss > 0.2D ) {
257- colour = Formatting .YELLOW ;
257+ colour = ChatFormatting .YELLOW ;
258258 } else {
259- colour = Formatting .GREEN ;
259+ colour = ChatFormatting .GREEN ;
260260 }
261- return Text .literal (String .format ("%d packets transmitted, %d packets received, " , TCPING_TIMES , packetsReceived ))
262- .append (Text .literal (String .format ("%.1f%%" , packetLoss * 100 )).formatted (colour ))
263- .append (Text .literal (" packet loss" ));
261+ return Component .literal (String .format ("%d packets transmitted, %d packets received, " , TCPING_TIMES , packetsReceived ))
262+ .append (Component .literal (String .format ("%.1f%%" , packetLoss * 100 )).withStyle (colour ))
263+ .append (Component .literal (" packet loss" ));
264264 }
265265
266- private static Text averagePingText (long ping ) {
267- Formatting colour ;
266+ private static Component averagePingText (long ping ) {
267+ ChatFormatting colour ;
268268 if (ping > 1000 ) {
269- colour = Formatting .LIGHT_PURPLE ;
269+ colour = ChatFormatting .LIGHT_PURPLE ;
270270 } else if (ping >= 800 ) {
271- colour = Formatting .RED ;
271+ colour = ChatFormatting .RED ;
272272 } else if (ping >= 300 ) {
273- colour = Formatting .YELLOW ;
273+ colour = ChatFormatting .YELLOW ;
274274 } else {
275- colour = Formatting .GREEN ;
275+ colour = ChatFormatting .GREEN ;
276276 }
277- return Text .literal ("Average ping: " )
278- .append (Text .literal (ping + "ms" ).formatted (colour ));
277+ return Component .literal ("Average ping: " )
278+ .append (Component .literal (ping + "ms" ).withStyle (colour ));
279279 }
280280
281281 private final Set <String > hosts ;
282282
283- private final ServerCommandSource source ;
283+ private final CommandSourceStack source ;
284284
285285 private final Map <String , Pair <Long , Integer >> results ;
286286
287287 private long startTimeMillis ;
288288
289- public TcpingThread (Set <String > hosts , ServerCommandSource source ) {
289+ public TcpingThread (Set <String > hosts , CommandSourceStack source ) {
290290 super ("TCPing Thread" );
291291 this .hosts = hosts ;
292292 this .source = source ;
@@ -297,33 +297,33 @@ private synchronized void saveResult(String host, Pair<Long, Integer> res) {
297297 results .put (host , res );
298298 if (results .size () >= hosts .size ()) {
299299 results .forEach (this ::sendResult );
300- sendFeedback (source , Text .literal ("Ping finished after " + (System .currentTimeMillis () - startTimeMillis ) + "ms" ), false );
300+ sendFeedback (source , Component .literal ("Ping finished after " + (System .currentTimeMillis () - startTimeMillis ) + "ms" ), false );
301301 }
302302 }
303303
304304 private void sendResult (String h , Pair <Long , Integer > r ) {
305305 sendFeedback (
306306 source ,
307- Text .literal (String .format ("Ping for '%s':" , h )),
307+ Component .literal (String .format ("Ping for '%s':" , h )),
308308 false
309309 );
310310 sendFeedback (
311311 source ,
312- packetLossStatusText (r .getRight ()),
312+ packetLossStatusText (r .getSecond ()),
313313 false
314314 );
315- if (r .getRight () > 0 ) {
315+ if (r .getSecond () > 0 ) {
316316 sendFeedback (
317317 source ,
318- averagePingText (r .getLeft () / r .getRight ()),
318+ averagePingText (r .getFirst () / r .getSecond ()),
319319 false
320320 );
321321 }
322322 }
323323
324324 @ Override
325325 public void run () {
326- sendFeedback (source , Text .literal ("Ping started" ), false );
326+ sendFeedback (source , Component .literal ("Ping started" ), false );
327327 startTimeMillis = System .currentTimeMillis ();
328328 hosts .forEach (host -> new Thread (() ->
329329 saveResult (host , NetworkUtils .tcpingMultiple (URI .create (host ), proxy , TCPING_TIMES ))).start ()
0 commit comments