1
1
package world .bentobox .bentobox .api .commands .admin .team ;
2
2
3
3
import java .util .List ;
4
+ import java .util .Objects ;
5
+ import java .util .Optional ;
4
6
import java .util .UUID ;
5
7
8
+ import org .bukkit .Bukkit ;
9
+ import org .bukkit .entity .Player ;
10
+ import org .eclipse .jdt .annotation .Nullable ;
11
+
12
+ import world .bentobox .bentobox .api .addons .GameModeAddon ;
6
13
import world .bentobox .bentobox .api .commands .CompositeCommand ;
14
+ import world .bentobox .bentobox .api .commands .ConfirmableCommand ;
7
15
import world .bentobox .bentobox .api .events .island .IslandEvent ;
8
16
import world .bentobox .bentobox .api .events .team .TeamEvent ;
9
17
import world .bentobox .bentobox .api .localization .TextVariables ;
17
25
*
18
26
* @author tastybento
19
27
*/
20
- public class AdminTeamSetownerCommand extends CompositeCommand {
28
+ public class AdminTeamSetownerCommand extends ConfirmableCommand {
29
+
30
+ private @ Nullable UUID targetUUID ;
31
+ private Island island ;
32
+ private @ Nullable UUID previousOwnerUUID ;
21
33
22
34
public AdminTeamSetownerCommand (CompositeCommand parent ) {
23
35
super (parent , "setowner" );
@@ -28,35 +40,50 @@ public void setup() {
28
40
setPermission ("mod.team.setowner" );
29
41
setParametersHelp ("commands.admin.team.setowner.parameters" );
30
42
setDescription ("commands.admin.team.setowner.description" );
43
+ this .setOnlyPlayer (true );
31
44
}
32
45
33
46
@ Override
34
- public boolean execute (User user , String label , List <String > args ) {
47
+ public boolean canExecute (User user , String label , List <String > args ) {
35
48
// If args are not right, show help
36
49
if (args .size () != 1 ) {
37
50
showHelp (this , user );
38
51
return false ;
39
52
}
53
+
40
54
// Get target
41
- UUID targetUUID = Util .getUUID (args .get (0 ));
55
+ targetUUID = Util .getUUID (args .get (0 ));
42
56
if (targetUUID == null ) {
43
57
user .sendMessage ("general.errors.unknown-player" , TextVariables .NAME , args .get (0 ));
44
58
return false ;
45
59
}
46
- if (!getIslands ().inTeam (getWorld (), targetUUID )) {
47
- user .sendMessage ("general.errors.not-in-team" );
60
+ // Check that user is on an island
61
+ Optional <Island > opIsland = getIslands ().getIslandAt (user .getLocation ());
62
+ if (opIsland .isEmpty ()) {
63
+ user .sendMessage ("commands.admin.team.setowner.must-be-on-island" );
48
64
return false ;
49
65
}
50
- Island island = getIslands (). getPrimaryIsland ( getWorld (), targetUUID );
51
- UUID previousOwnerUUID = island .getOwner ();
66
+ island = opIsland . get ( );
67
+ previousOwnerUUID = island .getOwner ();
52
68
if (targetUUID .equals (previousOwnerUUID )) {
53
69
user .sendMessage ("commands.admin.team.setowner.already-owner" , TextVariables .NAME , args .get (0 ));
54
70
return false ;
55
71
}
72
+ return true ;
73
+ }
56
74
57
- // Get the User corresponding to the current owner
58
- User target = User .getInstance (targetUUID );
75
+ public boolean execute (User user , String label , List <String > args ) {
76
+ Objects .requireNonNull (island );
77
+ Objects .requireNonNull (targetUUID );
78
+
79
+ this .askConfirmation (user , user .getTranslation ("commands.admin.team.setowner.confirmation" , TextVariables .NAME ,
80
+ args .get (0 ), TextVariables .XYZ , Util .xyz (island .getCenter ().toVector ())), () -> changeOwner (user ));
81
+ return true ;
59
82
83
+ }
84
+
85
+ protected void changeOwner (User user ) {
86
+ User target = User .getInstance (targetUUID );
60
87
// Fire event so add-ons know
61
88
// Call the setowner event
62
89
TeamEvent .builder ().island (island ).reason (TeamEvent .Reason .SETOWNER ).involvedPlayer (targetUUID ).admin (true )
@@ -70,8 +97,20 @@ public boolean execute(User user, String label, List<String> args) {
70
97
.build ();
71
98
72
99
// Make new owner
73
- getIslands ().setOwner (getWorld (), user , targetUUID );
74
- user .sendMessage ("commands.admin.team.setowner.success" , TextVariables .NAME , args .get (0 ));
100
+ getIslands ().setOwner (user , targetUUID , island , RanksManager .MEMBER_RANK );
101
+ user .sendMessage ("commands.admin.team.setowner.success" , TextVariables .NAME , target .getName ());
102
+
103
+ // Report if this made player have more islands than expected
104
+ // Get how many islands this player has
105
+ int num = this .getIslands ().getNumberOfConcurrentIslands (targetUUID , getWorld ());
106
+ int max = target .getPermissionValue (
107
+ this .getIWM ().getAddon (getWorld ()).map (GameModeAddon ::getPermissionPrefix ).orElse ("" ) + "island.number" ,
108
+ this .getIWM ().getWorldSettings (getWorld ()).getConcurrentIslands ());
109
+ if (num > max ) {
110
+ // You cannot make an island
111
+ user .sendMessage ("commands.admin.team.setowner.extra-islands" , TextVariables .NUMBER , String .valueOf (num ),
112
+ "[max]" , String .valueOf (max ));
113
+ }
75
114
76
115
// Call the rank change event for the old island owner
77
116
if (previousOwnerUUID != null ) {
@@ -80,6 +119,13 @@ public boolean execute(User user, String label, List<String> args) {
80
119
.reason (IslandEvent .Reason .RANK_CHANGE )
81
120
.rankChange (RanksManager .OWNER_RANK , island .getRank (previousOwnerUUID )).build ();
82
121
}
83
- return true ;
122
+
123
+ }
124
+
125
+ @ Override
126
+ public Optional <List <String >> tabComplete (User user , String alias , List <String > args ) {
127
+ String lastArg = !args .isEmpty () ? args .get (args .size () - 1 ) : "" ;
128
+ List <String > options = Bukkit .getOnlinePlayers ().stream ().map (Player ::getName ).toList ();
129
+ return Optional .of (Util .tabLimit (options , lastArg ));
84
130
}
85
131
}
0 commit comments