Skip to content

Commit 6d8ac15

Browse files
authored
Merge pull request #2523 from BentoBoxWorld/2522_admin_command_to_reset_player's_home
Add AdminResetHome command #2522
2 parents 83b7c66 + 80e1063 commit 6d8ac15

File tree

6 files changed

+531
-4
lines changed

6 files changed

+531
-4
lines changed

src/main/java/world/bentobox/bentobox/api/commands/admin/AdminMaxHomesCommand.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,12 @@ public Optional<List<String>> tabComplete(User user, String alias, List<String>
140140
return Optional.of(Util.getOnlinePlayerList(user));
141141
}
142142
if (args.size() > 3) {
143-
return Optional.of(Util.tabLimit(new ArrayList<>(getNameIslandMap(user).keySet()), lastArg));
143+
// Work out who is in arg 2
144+
UUID targetUUID = getPlayers().getUUID(args.get(1));
145+
if (targetUUID != null) {
146+
User target = User.getInstance(targetUUID);
147+
return Optional.of(Util.tabLimit(new ArrayList<>(getNameIslandMap(target).keySet()), lastArg));
148+
}
144149
}
145150
return Optional.of(List.of("1"));
146151

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package world.bentobox.bentobox.api.commands.admin;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.Optional;
8+
import java.util.UUID;
9+
10+
import world.bentobox.bentobox.api.commands.CompositeCommand;
11+
import world.bentobox.bentobox.api.localization.TextVariables;
12+
import world.bentobox.bentobox.api.user.User;
13+
import world.bentobox.bentobox.database.objects.Island;
14+
import world.bentobox.bentobox.util.Util;
15+
16+
17+
/**
18+
* This command resets players island name.
19+
* @author BONNe
20+
*/
21+
public class AdminResetHomeCommand extends CompositeCommand
22+
{
23+
Map<String, Island> islands = new HashMap<>();
24+
25+
/**
26+
* Default constructor.
27+
* @param command Parent command.
28+
*/
29+
public AdminResetHomeCommand(CompositeCommand command)
30+
{
31+
super(command, "resethome");
32+
}
33+
34+
35+
/**
36+
* {@inheritDoc}
37+
*/
38+
@Override
39+
public void setup()
40+
{
41+
this.setPermission("mod.resethome");
42+
this.setDescription("commands.admin.resethome.description");
43+
this.setParametersHelp("commands.admin.resethome.parameters");
44+
}
45+
46+
47+
/**
48+
* @param user the {@link User} who is executing this command.
49+
* @param label the label which has been used to execute this command.
50+
* It can be {@link CompositeCommand#getLabel()} or an alias.
51+
* @param args the command arguments.
52+
* @return {@code true} if name can be reset, {@code false} otherwise.
53+
*/
54+
@Override
55+
public boolean canExecute(User user, String label, List<String> args)
56+
{
57+
islands.clear();
58+
if (args.isEmpty()) {
59+
this.showHelp(this, user);
60+
return false;
61+
}
62+
// First arg must be a valid player name
63+
UUID targetUUID = getPlayers().getUUID(args.get(0));
64+
if (targetUUID == null) {
65+
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0));
66+
return false;
67+
}
68+
// Get islands
69+
islands = this.getNameIslandMap(User.getInstance(targetUUID));
70+
if (islands.isEmpty()) {
71+
user.sendMessage("general.errors.player-has-no-island");
72+
return false;
73+
}
74+
75+
// Second optional arg must be the name of the island
76+
if (args.size() == 1) {
77+
return true;
78+
}
79+
80+
// A specific island is mentioned. Parse which one it is and remove the others
81+
final String name = String.join(" ", args.subList(1, args.size())); // Join all the args from here with spaces
82+
83+
islands.keySet().removeIf(n -> !name.equalsIgnoreCase(n));
84+
85+
if (islands.isEmpty()) {
86+
// Failed name check - there are either
87+
user.sendMessage("commands.admin.maxhomes.errors.unknown-island", TextVariables.NAME, name);
88+
return false;
89+
}
90+
91+
return true;
92+
}
93+
94+
95+
/**
96+
* @param user the {@link User} who is executing this command.
97+
* @param label the label which has been used to execute this command.
98+
* It can be {@link CompositeCommand#getLabel()} or an alias.
99+
* @param args the command arguments.
100+
* @return {@code true}
101+
*/
102+
@Override
103+
public boolean execute(User user, String label, List<String> args)
104+
{
105+
if (islands.isEmpty()) {
106+
// Sanity check
107+
return false;
108+
}
109+
islands.forEach((name, island) -> {
110+
island.getHomes().keySet().removeIf(String::isEmpty); // Remove the default home
111+
user.sendMessage("commands.admin.resethome.cleared", TextVariables.NAME, name);
112+
});
113+
114+
user.sendMessage("general.success");
115+
return true;
116+
}
117+
118+
119+
@Override
120+
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
121+
String lastArg = !args.isEmpty() ? args.get(args.size() - 1) : "";
122+
if (args.size() == 2) {
123+
// Suggest player names
124+
return Optional.of(Util.getOnlinePlayerList(user));
125+
}
126+
if (args.size() > 2) {
127+
// Work out who is in arg 2
128+
UUID targetUUID = getPlayers().getUUID(args.get(0));
129+
if (targetUUID != null) {
130+
User target = User.getInstance(targetUUID);
131+
return Optional.of(Util.tabLimit(new ArrayList<>(getNameIslandMap(target).keySet()), lastArg));
132+
}
133+
}
134+
return Optional.empty();
135+
136+
}
137+
138+
Map<String, Island> getNameIslandMap(User user) {
139+
Map<String, Island> islandMap = new HashMap<>();
140+
int index = 0;
141+
System.out.println("Getting for " + user.getName());
142+
for (Island island : getIslands().getIslands(getWorld(), user.getUniqueId())) {
143+
System.out.println("Island - " + island);
144+
index++;
145+
if (island.getName() != null && !island.getName().isBlank()) {
146+
// Name has been set
147+
islandMap.put(island.getName(), island);
148+
} else {
149+
// Name has not been set
150+
String text = user.getTranslation("protection.flags.ENTER_EXIT_MESSAGES.island", TextVariables.NAME,
151+
user.getName(), TextVariables.DISPLAY_NAME, user.getDisplayName()) + " " + index;
152+
islandMap.put(text, island);
153+
}
154+
}
155+
156+
return islandMap;
157+
158+
}
159+
160+
}

src/main/java/world/bentobox/bentobox/api/commands/admin/DefaultAdminCommand.java

+2
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ public void setup() {
104104
new AdminResetNameCommand(this);
105105
// Max homes
106106
new AdminMaxHomesCommand(this);
107+
// Reset Home
108+
new AdminResetHomeCommand(this);
107109
}
108110

109111
/**

src/main/resources/locales/en-US.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,14 @@ commands:
6161
description: admin command
6262
maxhomes:
6363
description: change the number of homes allowed on this island or player's island
64-
parameters: <number / player> <number> <island name>
64+
parameters: <number / player> <number> [island name]
6565
max-homes-set: '&a [name] - Set island max homes to [number]'
6666
errors:
6767
unknown-island: &c Unknown island! [name]
68-
68+
resethome:
69+
description: Reset the player's home to default
70+
parameters: <player> [island name]
71+
cleared: '&b Home reset. [name]'
6972
resets:
7073
description: edit player reset values
7174
set:

src/test/java/world/bentobox/bentobox/api/commands/admin/AdminMaxHomesCommandTest.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.junit.Assert.assertFalse;
55
import static org.junit.Assert.assertTrue;
66
import static org.mockito.ArgumentMatchers.any;
7+
import static org.mockito.ArgumentMatchers.anyString;
78
import static org.mockito.ArgumentMatchers.eq;
89
import static org.mockito.Mockito.doReturn;
910
import static org.mockito.Mockito.mock;
@@ -135,6 +136,8 @@ public void setUp() throws Exception {
135136
// Has team
136137
when(im.inTeam(any(), eq(uuid))).thenReturn(true);
137138

139+
// Players
140+
when(pm.getUUID(anyString())).thenReturn(uuid);
138141
when(plugin.getPlayers()).thenReturn(pm);
139142

140143
// Server & Scheduler
@@ -457,7 +460,7 @@ public void testTabComplete_ArgsSizeGreaterThan3_ReturnsIslandNames() {
457460
Map<String, Island> nameIslandMap = new HashMap<>();
458461
nameIslandMap.put("IslandOne", mock(Island.class));
459462
nameIslandMap.put("IslandTwo", mock(Island.class));
460-
doReturn(nameIslandMap).when(instance).getNameIslandMap(user);
463+
doReturn(nameIslandMap).when(instance).getNameIslandMap(any());
461464

462465
// Create the list of island names
463466
List<String> islandNames = new ArrayList<>(nameIslandMap.keySet());

0 commit comments

Comments
 (0)