Version NPE protections.

This commit is contained in:
tastybento 2021-09-25 16:42:54 -07:00
parent 3236c68e5c
commit f66eeed4a1
5 changed files with 60 additions and 56 deletions

View File

@ -72,7 +72,7 @@ public class IslandBanCommand extends CompositeCommand {
user.sendMessage("commands.island.ban.cannot-ban-member");
return false;
}
if (getIslands().getIsland(getWorld(), playerUUID).isBanned(targetUUID)) {
if (island.isBanned(targetUUID)) {
user.sendMessage("commands.island.ban.player-already-banned");
return false;
}

View File

@ -113,31 +113,19 @@ public class IslandResetCommand extends ConfirmableCommand {
}
}
/**
* Reset island
* @param user user
* @param name name of Blueprint Bundle
* @return true if successful
*/
private boolean resetIsland(User user, String name) {
// Get the player's old island
Island oldIsland = getIslands().getIsland(getWorld(), user);
// Fire island preclear event
IslandEvent.builder()
.involvedPlayer(user.getUniqueId())
.reason(Reason.PRECLEAR)
.island(oldIsland)
.oldIsland(oldIsland)
.location(oldIsland.getCenter())
.build();
// Reset the island
if (oldIsland != null) {
deleteOldIsland(user, oldIsland);
}
user.sendMessage("commands.island.create.creating-island");
// Kick all island members (including the owner)
kickMembers(oldIsland);
// Add a reset
getPlayers().addReset(getWorld(), user.getUniqueId());
// Reset the homes of the player
getPlayers().clearHomeLocations(getWorld(), user.getUniqueId());
// Create new island and then delete the old one
try {
Builder builder = NewIsland.builder()
@ -157,6 +145,28 @@ public class IslandResetCommand extends ConfirmableCommand {
return true;
}
private void deleteOldIsland(User user, Island oldIsland) {
// Fire island preclear event
IslandEvent.builder()
.involvedPlayer(user.getUniqueId())
.reason(Reason.PRECLEAR)
.island(oldIsland)
.oldIsland(oldIsland)
.location(oldIsland.getCenter())
.build();
// Reset the island
// Kick all island members (including the owner)
kickMembers(oldIsland);
// Add a reset
getPlayers().addReset(getWorld(), user.getUniqueId());
// Reset the homes of the player
getPlayers().clearHomeLocations(getWorld(), user.getUniqueId());
}
/**
* Kicks the members (incl. owner) of the island.
* @since 1.7.0

View File

@ -6,6 +6,8 @@ import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.events.IslandBaseEvent;
import world.bentobox.bentobox.api.events.island.IslandEvent;
@ -21,6 +23,8 @@ import world.bentobox.bentobox.util.Util;
*/
public class IslandUnbanCommand extends CompositeCommand {
private @Nullable UUID targetUUID;
public IslandUnbanCommand(CompositeCommand islandCommand) {
super(islandCommand, "unban", "pardon");
}
@ -55,7 +59,7 @@ public class IslandUnbanCommand extends CompositeCommand {
return false;
}
// Get target player
UUID targetUUID = getPlayers().getUUID(args.get(0));
targetUUID = getPlayers().getUUID(args.get(0));
if (targetUUID == null) {
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0));
return false;
@ -65,7 +69,7 @@ public class IslandUnbanCommand extends CompositeCommand {
user.sendMessage("commands.island.unban.cannot-unban-yourself");
return false;
}
if (!getIslands().getIsland(getWorld(), playerUUID).isBanned(targetUUID)) {
if (!island.isBanned(targetUUID)) {
user.sendMessage("commands.island.unban.player-not-banned");
return false;
}
@ -75,7 +79,7 @@ public class IslandUnbanCommand extends CompositeCommand {
@Override
public boolean execute(User user, String label, List<String> args) {
User target = User.getInstance(getPlayers().getUUID(args.get(0)));
User target = User.getInstance(targetUUID);
Island island = getIslands().getIsland(getWorld(), user.getUniqueId());
// Run the event

View File

@ -176,16 +176,30 @@ public class User implements MetaDataAble {
return sender.getEffectivePermissions();
}
/**
* Get the user's inventory
* @return player's inventory
* @throws {@link NullPointerException} - if user is not a player
*/
@NonNull
public PlayerInventory getInventory() {
return Objects.requireNonNull(player, "getInventory can only be called for online players!").getInventory();
}
/**
* Get the user's location
* @return location
* @throws {@link NullPointerException} - if user is not a player
*/
@NonNull
public Location getLocation() {
return Objects.requireNonNull(player, "getLocation can only be called for online players!").getLocation();
}
/**
* Get the user's name
* @return player's name
*/
@NonNull
public String getName() {
return player != null ? player.getName() : plugin.getPlayers().getName(playerUUID);
@ -211,6 +225,7 @@ public class User implements MetaDataAble {
* Use {@link #isOfflinePlayer()} before calling this method
* @return the offline player
* @since 1.3.0
* @throws {@link NullPointerException} - if user is not an offline player
*/
@NonNull
public OfflinePlayer getOfflinePlayer() {
@ -511,10 +526,13 @@ public class User implements MetaDataAble {
/**
* Gets the current world this entity resides in
* @return World - world or null
* @return World - world
* @throws {@link NullPointerException} - if user is not a player
*/
@NonNull
public World getWorld() {
return player == null ? null : player.getWorld();
Objects.requireNonNull(player, "Cannot be called on a non-player User!");
return Objects.requireNonNull(player.getWorld(), "Player's world cannot be null!");
}
/**

View File

@ -6,7 +6,6 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ -248,39 +247,12 @@ public class IslandUnbanCommandTest {
// Allow removing from ban list
when(island.unban(any(), any())).thenReturn(true);
assertTrue(iubc.canExecute(user, iubc.getLabel(), Collections.singletonList("bill")));
assertTrue(iubc.execute(user, iubc.getLabel(), Collections.singletonList("bill")));
verify(user).sendMessage("commands.island.unban.player-unbanned", TextVariables.NAME, targetUser.getName());
verify(targetUser).sendMessage("commands.island.unban.you-are-unbanned", TextVariables.NAME, user.getName());
}
/**
* Test method for {@link IslandUnbanCommand#execute(User, String, List)}
*/
@Test
public void testCancelledUnban() {
IslandUnbanCommand iubc = new IslandUnbanCommand(ic);
when(im.hasIsland(any(), eq(uuid))).thenReturn(true);
when(im.isOwner(any(), eq(uuid))).thenReturn(true);
UUID targetUUID = UUID.randomUUID();
when(pm.getUUID(Mockito.anyString())).thenReturn(targetUUID);
PowerMockito.mockStatic(User.class);
User targetUser = mock(User.class);
when(targetUser.isOp()).thenReturn(false);
when(targetUser.isPlayer()).thenReturn(true);
when(targetUser.isOnline()).thenReturn(false);
when(User.getInstance(any(UUID.class))).thenReturn(targetUser);
// Mark as banned
when(island.isBanned(eq(targetUUID))).thenReturn(true);
// Allow removing from ban list
when(island.unban(any(), any())).thenReturn(false);
assertFalse(iubc.execute(user, iubc.getLabel(), Collections.singletonList("bill")));
verify(user, never()).sendMessage("commands.island.unban.player-unbanned", TextVariables.NAME, targetUser.getName());
verify(targetUser, never()).sendMessage("commands.island.unban.you-are-unbanned", "[owner]", user.getName());
}
/**
* Test method for {@link IslandUnbanCommand#tabComplete(User, String, List)}
*/