44import net .minestom .server .command .CommandSender ;
55import net .minestom .server .command .builder .arguments .*;
66import net .minestom .server .command .builder .suggestion .SuggestionEntry ;
7- import net .minestom .server .entity .Player ;
8- import net .minestom .server .utils .mojang .MojangUtils ;
97import net .swofty .commons .ServiceType ;
108import net .swofty .commons .StringUtility ;
119import net .swofty .commons .protocol .objects .punishment .PunishPlayerProtocolObject ;
1210import net .swofty .commons .punishment .PunishmentReason ;
13- import net .swofty .commons .punishment .PunishmentRedis ;
1411import net .swofty .commons .punishment .PunishmentTag ;
1512import net .swofty .commons .punishment .PunishmentType ;
1613import net .swofty .commons .punishment .template .BanType ;
2421import java .io .IOException ;
2522import java .util .ArrayList ;
2623import java .util .List ;
27- import java .util .Optional ;
2824import java .util .UUID ;
2925import java .util .concurrent .CompletableFuture ;
3026import java .util .concurrent .TimeUnit ;
31- import java .util .concurrent .atomic .AtomicBoolean ;
3227
3328@ CommandParameters (
3429 aliases = "ban tempban banip tempbanip" ,
@@ -52,55 +47,35 @@ public void registerUsage(MinestomCommand command) {
5247 for (PunishmentTag tag : PunishmentTag .values ()) {
5348 suggestion .addEntry (new SuggestionEntry ("-" + tag .getShortCode (), Component .text ("§e" + tag .getShortCode () + " §7| " + (tag .getDescription () != null ? tag .getDescription () : "No description" ))));
5449 }
55- }); // can be -O -U etc.
50+ });
5651
5752 command .addSyntax ((sender , context ) -> {
5853 String playerName = context .get (playerArg );
5954 String duration = context .get (durationArg );
6055 BanType type = BanType .valueOf (context .get (reasonArg ));
6156
62-
63- UUID targetUuid ;
64- try {
65- targetUuid = MojangUtils .getUUID (playerName );
66- sender .sendMessage ("§8Processing ban for player §e" + playerName + "§7... (" + targetUuid + ")" );
67- } catch (IOException e ) {
68- sender .sendMessage ("§cCould not find player: " + playerName );
69- return ;
70- }
71-
72- UUID senderUuid ;
73- if (sender instanceof Player player ) {
74- senderUuid = player .getUuid ();
75- } else {
76- senderUuid = UUID .fromString ("00000000-0000-0000-0000-000000000000" );
77- }
78-
79- long actualTime = StringUtility .parseDuration (duration );
80- long expiryTime = System .currentTimeMillis () + actualTime ;
81-
8257 CompletableFuture .runAsync (() -> {
83- banPlayer (sender , targetUuid , type , senderUuid , actualTime , expiryTime , playerName , null );
58+ try {
59+ UUID targetUuid = resolvePlayerUuid (sender , playerName , "ban" );
60+ long actualTime = StringUtility .parseDuration (duration );
61+ long expiryTime = System .currentTimeMillis () + actualTime ;
62+ banPlayer (sender , targetUuid , type , senderUuid (sender ), actualTime , expiryTime , playerName , null );
63+ } catch (IOException e ) {
64+ sender .sendMessage ("§cCould not find player: " + playerName );
65+ }
8466 });
8567 }, playerArg , durationArg , reasonArg );
8668
87- // permanent ban
8869 command .addSyntax ((sender , context ) -> {
8970 String playerName = context .get (playerArg );
9071 BanType reason = BanType .valueOf (context .get (reasonArg ));
9172
9273 CompletableFuture .runAsync (() -> {
9374 try {
94- banPlayer (sender ,
95- MojangUtils .getUUID (playerName ),
96- reason ,
97- sender instanceof Player player ? player .getUuid () : UUID .fromString ("00000000-0000-0000-0000-000000000000" ),
98- 0 ,
99- -1 ,
100- playerName , null );
75+ banPlayer (sender , resolvePlayerUuid (sender , playerName , "ban" ), reason ,
76+ senderUuid (sender ), 0 , -1 , playerName , null );
10177 } catch (IOException e ) {
10278 sender .sendMessage ("§cCould not find player: " + playerName );
103- return ;
10479 }
10580 });
10681 }, playerArg , reasonArg );
@@ -112,13 +87,8 @@ public void registerUsage(MinestomCommand command) {
11287
11388 CompletableFuture .runAsync (() -> {
11489 try {
115- banPlayer (sender ,
116- MojangUtils .getUUID (playerName ),
117- reason ,
118- sender instanceof Player player ? player .getUuid () : UUID .fromString ("00000000-0000-0000-0000-000000000000" ),
119- 0 ,
120- -1 ,
121- playerName , tags );
90+ banPlayer (sender , resolvePlayerUuid (sender , playerName , "ban" ), reason ,
91+ senderUuid (sender ), 0 , -1 , playerName , tags );
12292 } catch (IOException e ) {
12393 sender .sendMessage ("§cCould not find player: " + playerName );
12494 }
@@ -128,7 +98,6 @@ public void registerUsage(MinestomCommand command) {
12898
12999 private List <PunishmentTag > parseTags (List <String > rawTags ) {
130100 List <PunishmentTag > tags = new ArrayList <>();
131-
132101 for (String rawTag : rawTags ) {
133102 if (rawTag .startsWith ("-" )) {
134103 String tagCode = rawTag .substring (1 ).toUpperCase ();
@@ -140,26 +109,11 @@ private List<PunishmentTag> parseTags(List<String> rawTags) {
140109 }
141110 }
142111 }
143-
144112 return tags ;
145113 }
146114
147- private void banPlayer (CommandSender sender , UUID targetUuid , BanType type , UUID senderUuid , long actualTime , long expiryTime , String playerName , @ Nullable List <PunishmentTag > tags ) {
148- if (tags != null && !tags .contains (PunishmentTag .OVERWRITE )) {
149- Optional <PunishmentRedis .ActivePunishment > activePunishment = PunishmentRedis .getActive (targetUuid );
150- AtomicBoolean alreadyBanned = new AtomicBoolean (false );
151- activePunishment .ifPresent (punishment -> {
152- PunishmentType t = PunishmentType .valueOf (punishment .type ());
153- if (t == PunishmentType .BAN ) {
154- sender .sendMessage ("§cThis player is already banned. If you want to replace this ban use the tag -O, Punishment ID: §7" + punishment .banId ());
155- alreadyBanned .set (true );
156- }
157- });
158- if (alreadyBanned .get ()) {
159- return ;
160- }
161- }
162-
115+ private void banPlayer (CommandSender sender , UUID targetUuid , BanType type , UUID senderUuid ,
116+ long actualTime , long expiryTime , String playerName , @ Nullable List <PunishmentTag > tags ) {
163117 ProxyService punishmentService = new ProxyService (ServiceType .PUNISHMENT );
164118 PunishmentReason reason = new PunishmentReason (type );
165119 ArrayList <PunishmentTag > tagList = (tags != null ) ? new ArrayList <>(tags ) : new ArrayList <>();
@@ -178,6 +132,8 @@ private void banPlayer(CommandSender sender, UUID targetUuid, BanType type, UUID
178132 if (result instanceof PunishPlayerProtocolObject .PunishPlayerResponse response ) {
179133 if (response .success ()) {
180134 sender .sendMessage ("§aSuccessfully banned player §e" + playerName + "§a. §8Punishment ID: §7" + response .punishmentId ());
135+ } else if (response .errorCode () == PunishPlayerProtocolObject .ErrorCode .ALREADY_PUNISHED ) {
136+ sender .sendMessage ("§cThis player is already banned. Use the tag -O to overwrite. Punishment ID: §7" + response .errorMessage ());
181137 } else {
182138 sender .sendMessage ("§cFailed to ban player: " + response .errorMessage ());
183139 }
0 commit comments