Fix NPE with unregister command

This commit is contained in:
tastybento 2024-02-23 21:51:17 -08:00
parent 887f09c41b
commit 59124cfa8a
2 changed files with 20 additions and 6 deletions

View File

@ -73,6 +73,10 @@ public class AdminUnregisterCommand extends ConfirmableCommand {
user.sendMessage("commands.admin.unregister.errors.specify-island-location"); user.sendMessage("commands.admin.unregister.errors.specify-island-location");
return false; return false;
} else if (!islands.containsKey(args.get(1))) { } else if (!islands.containsKey(args.get(1))) {
if (args.get(1).equalsIgnoreCase("help")) {
this.showHelp(this, user);
return false;
}
user.sendMessage("commands.admin.unregister.errors.unknown-island-location"); user.sendMessage("commands.admin.unregister.errors.unknown-island-location");
return false; return false;
} }
@ -126,7 +130,7 @@ public class AdminUnregisterCommand extends ConfirmableCommand {
} else if (args.size() == 2) { } else if (args.size() == 2) {
List<String> options = new ArrayList<>(Util.getOnlinePlayerList(user)); List<String> options = new ArrayList<>(Util.getOnlinePlayerList(user));
return Optional.of(Util.tabLimit(options, lastArg)); return Optional.of(Util.tabLimit(options, lastArg));
} else { } else if (args.size() > 2) {
// Find out which user // Find out which user
UUID uuid = getPlayers().getUUID(args.get(1)); UUID uuid = getPlayers().getUUID(args.get(1));
if (uuid != null) { if (uuid != null) {

View File

@ -240,16 +240,16 @@ public class AdminUnregisterCommandTest {
* {@link AdminUnregisterCommand#canExecute(User, String, java.util.List)}. * {@link AdminUnregisterCommand#canExecute(User, String, java.util.List)}.
*/ */
@Test @Test
public void testExecutePlayerSuccessMultiIsland() { public void testCanExecutePlayerSuccessMultiIsland() {
assertTrue(itl.canExecute(user, itl.getLabel(), List.of("tastybento", "1,2,3"))); assertTrue(itl.canExecute(user, itl.getLabel(), List.of("tastybento", "1,2,3")));
assertTrue(itl.canExecute(user, itl.getLabel(), List.of("tastybento", "4,5,6"))); assertTrue(itl.canExecute(user, itl.getLabel(), List.of("tastybento", "4,5,6")));
} }
/** /**
* Test method for {@link AdminUnregisterCommand#execute(User, String, java.util.List)}. * Test method for {@link AdminUnregisterCommand#canExecute(User, String, java.util.List)}.
*/ */
@Test @Test
public void testExecuteSuccessOneIsland() { public void testCanExecuteSuccessOneIsland() {
when(im.getOwnedIslands(world, uuid)).thenReturn(Set.of(island)); when(im.getOwnedIslands(world, uuid)).thenReturn(Set.of(island));
itl.canExecute(user, itl.getLabel(), List.of("tastybento")); itl.canExecute(user, itl.getLabel(), List.of("tastybento"));
assertTrue(itl.execute(user, itl.getLabel(), List.of("tastybento"))); assertTrue(itl.execute(user, itl.getLabel(), List.of("tastybento")));
@ -262,7 +262,7 @@ public class AdminUnregisterCommandTest {
*/ */
@Test @Test
public void testUnregisterIsland() { public void testUnregisterIsland() {
this.testExecuteSuccessOneIsland(); this.testCanExecuteSuccessOneIsland();
itl.unregisterIsland(user); itl.unregisterIsland(user);
verify(user).sendMessage("commands.admin.unregister.unregistered-island", TextVariables.XYZ, "1,2,3", verify(user).sendMessage("commands.admin.unregister.unregistered-island", TextVariables.XYZ, "1,2,3",
TextVariables.NAME, "name"); TextVariables.NAME, "name");
@ -274,9 +274,19 @@ public class AdminUnregisterCommandTest {
*/ */
@Test @Test
public void testUnregisterIslandMulti() { public void testUnregisterIslandMulti() {
this.testExecutePlayerSuccessMultiIsland(); this.testCanExecutePlayerSuccessMultiIsland();
itl.unregisterIsland(user); itl.unregisterIsland(user);
verify(user).sendMessage("commands.admin.unregister.unregistered-island", TextVariables.XYZ, "4,5,6", verify(user).sendMessage("commands.admin.unregister.unregistered-island", TextVariables.XYZ, "4,5,6",
TextVariables.NAME, "name"); TextVariables.NAME, "name");
} }
/**
* Test method for
* {@link AdminUnregisterCommand#canExecute(User, String, java.util.List)}.
*/
@Test
public void testCanExecuteHelp() {
assertFalse(itl.canExecute(user, itl.getLabel(), List.of("tastybento", "help")));
verify(user).sendMessage("commands.help.header", TextVariables.LABEL, null);
}
} }