11package net .infstudio .goki .common ;
22
3+ import com .google .common .collect .Lists ;
4+ import com .mojang .authlib .GameProfile ;
35import net .infstudio .goki .common .config .ConfigManager ;
46import net .infstudio .goki .common .config .Configurable ;
7+ import net .infstudio .goki .common .network .GokiPacketHandler ;
8+ import net .infstudio .goki .common .network .message .S2COpenGui ;
59import net .infstudio .goki .common .stats .StatBase ;
6- import net .minecraft .command .CommandBase ;
7- import net .minecraft .command .ICommandSender ;
10+ import net .minecraft .command .*;
811import net .minecraft .entity .player .EntityPlayer ;
12+ import net .minecraft .entity .player .EntityPlayerMP ;
913import net .minecraft .server .MinecraftServer ;
10- import net .minecraft .util .text .TextComponentTranslation ;
14+ import net .minecraft .util .math .BlockPos ;
15+ import net .minecraft .util .text .TextComponentString ;
1116
1217import javax .annotation .Nonnull ;
18+ import javax .annotation .Nullable ;
19+ import java .util .Arrays ;
20+ import java .util .Collections ;
21+ import java .util .List ;
1322
1423public class StatsCommand extends CommandBase {
1524 @ Nonnull
1625 @ Override
1726 public String getName () {
18- return "reloadGokiStats " ;
27+ return "gokistats " ;
1928 }
2029
2130 @ Nonnull
2231 @ Override
2332 public String getUsage (@ Nonnull ICommandSender sender ) {
24- return "/gokistats reload " ;
33+ return "/gokistats" ;
2534 }
2635
2736 @ Override
28- public void execute (MinecraftServer server , ICommandSender icommandsender , String [] astring ) {
29- StatBase .stats .forEach (Configurable ::reloadConfig );
30- ConfigManager .INSTANCE .reloadConfig ();
31- EntityPlayer player ;
32- if ((icommandsender instanceof EntityPlayer )) {
33- player = (EntityPlayer ) icommandsender ;
34- player .sendMessage (new TextComponentTranslation ("Reloaded gokistats configuration file." ));
37+ public void execute (MinecraftServer server , ICommandSender sender , String [] args ) throws CommandException {
38+ if (args .length < 1 )
39+ throw new WrongUsageException ("Invalid usage! Valid commands: /gokistats reload, /gokistats gui" );
40+
41+ if (args [0 ].equals ("reload" )) {
42+ StatBase .stats .forEach (Configurable ::reloadConfig );
43+ ConfigManager .INSTANCE .reloadConfig ();
44+ EntityPlayer player ;
45+ if ((sender instanceof EntityPlayer )) {
46+ player = (EntityPlayer ) sender ;
47+ player .sendMessage (new TextComponentString ("Reloaded gokistats configuration file." ));
48+ } else {
49+ server .logInfo ("Reloaded gokistats configuration file." );
50+ }
51+ } else if (args [0 ].equals ("gui" )) {
52+ EntityPlayerMP player = null ;
53+ if (args .length == 1 ) {
54+ if ((sender instanceof EntityPlayerMP )) {
55+ player = (EntityPlayerMP ) sender ;
56+ } else {
57+ throw new WrongUsageException ("This command should be only used by player" );
58+ }
59+ } else if (args .length == 2 ) player = server .getPlayerList ().getPlayerByUsername (args [1 ]);
60+ if (player == null )
61+ throw new PlayerNotFoundException (args [1 ]);
62+ GokiPacketHandler .CHANNEL .sendTo (new S2COpenGui (0 ), player );
3563 } else {
36- server . logInfo ( "Reloaded gokistats configuration file." );
64+ throw new CommandNotFoundException ( "No sub-command " + args [ 0 ] );
3765 }
3866 }
3967
40- public String getCommandUsage (ICommandSender icommandsender ) {
41- return null ;
68+ @ Nonnull
69+ public List <String > getTabCompletions (MinecraftServer server , ICommandSender sender , String [] args , @ Nullable BlockPos targetPos ) {
70+ if (args .length == 1 ) {
71+ String s = args [args .length - 1 ];
72+ if (args [0 ].isEmpty ()) return Arrays .asList ("reload" , "gui" );
73+ if (doesStringStartWith (s , "reload" ))
74+ return Collections .singletonList ("reload" );
75+ else if (doesStringStartWith (s , "gui" ))
76+ return Collections .singletonList ("gui" );
77+ } else if (args .length == 2 ) {
78+ if (args [0 ].equals ("gui" )) {
79+ List <String > list = Lists .newArrayList ();
80+
81+ for (GameProfile gameprofile : server .getOnlinePlayerProfiles ())
82+ if (!server .getPlayerList ().canSendCommands (gameprofile ) && doesStringStartWith (args [1 ], gameprofile .getName ()))
83+ list .add (gameprofile .getName ());
84+
85+ return list ;
86+ }
87+ }
88+ return Arrays .asList ("reload" , "gui" );
4289 }
4390
44- }
91+ public String getCommandUsage (ICommandSender sender ) {
92+ return null ;
93+ }
94+ }
0 commit comments