Address potential NPE's

This commit is contained in:
tastybento 2021-11-12 13:19:19 -08:00
parent 6b6ab711e5
commit 7616f6aa2a
5 changed files with 91 additions and 48 deletions

View File

@ -57,7 +57,7 @@ public class CommandRankClickListener implements ClickHandler {
// Get the user's island
Island island = plugin.getIslands().getIsland(panel.getWorld().orElse(user.getWorld()), user.getUniqueId());
if (island == null || !island.getOwner().equals(user.getUniqueId())) {
if (island == null || island.getOwner() == null || !island.getOwner().equals(user.getUniqueId())) {
user.sendMessage("general.errors.not-owner");
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_METAL_HIT, 1F, 1F);
return true;

View File

@ -512,7 +512,7 @@ public class IslandsManager {
int islandMax = island.getMaxMembers(rank) == null ? worldDefault : island.getMaxMembers(rank);
// Update based on owner permissions if online
if (Bukkit.getPlayer(island.getOwner()) != null) {
if (island.getOwner() != null && Bukkit.getPlayer(island.getOwner()) != null) {
User owner = User.getInstance(island.getOwner());
islandMax = owner.getPermissionValue(plugin.getIWM().getPermissionPrefix(island.getWorld())
+ perm, islandMax);
@ -544,7 +544,7 @@ public class IslandsManager {
public int getMaxHomes(@NonNull Island island) {
int islandMax = island.getMaxHomes() == null ? plugin.getIWM().getMaxHomes(island.getWorld()) : island.getMaxHomes();
// Update based on owner permissions if online
if (Bukkit.getPlayer(island.getOwner()) != null) {
if (island.getOwner() != null && Bukkit.getPlayer(island.getOwner()) != null) {
User owner = User.getInstance(island.getOwner());
islandMax = owner.getPermissionValue(plugin.getIWM().getPermissionPrefix(island.getWorld())
+ "island.maxhomes", islandMax);
@ -705,10 +705,13 @@ public class IslandsManager {
return l;
} else {
// try owner's home
Location tlh = getHomeLocation(world, getOwner(world, user.getUniqueId()));
if (tlh != null && isSafeLocation(tlh)) {
setHomeLocation(user, tlh, name);
return tlh;
UUID owner = getOwner(world, user.getUniqueId());
if (owner != null) {
Location tlh = getHomeLocation(world, owner);
if (tlh != null && isSafeLocation(tlh)) {
setHomeLocation(user, tlh, name);
return tlh;
}
}
}
} else {
@ -874,7 +877,7 @@ public class IslandsManager {
// No migration required
return;
}
if (island.getOwner().equals(uuid)) {
if (island.getOwner() != null && island.getOwner().equals(uuid)) {
// Owner
island.setHomes(homes.entrySet().stream().collect(Collectors.toMap(this::getHomeName, Map.Entry::getKey)));
plugin.getPlayers().clearHomeLocations(world, uuid);

View File

@ -555,7 +555,10 @@ public class PlayersManager {
if (target.isOnline()) {
target.getPlayer().getEnderChest().clear();
} else {
getPlayer(target.getUniqueId()).addToPendingKick(world);
Players p = getPlayer(target.getUniqueId());
if (p != null) {
p.addToPendingKick(world);
}
}
}
if ((kicked && plugin.getIWM().isOnLeaveResetInventory(world) && !plugin.getIWM().isKickedKeepInventory(world))
@ -563,7 +566,10 @@ public class PlayersManager {
if (target.isOnline()) {
target.getPlayer().getInventory().clear();
} else {
getPlayer(target.getUniqueId()).addToPendingKick(world);
Players p = getPlayer(target.getUniqueId());
if (p != null) {
p.addToPendingKick(world);
}
}
}

View File

@ -5,6 +5,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
@ -141,7 +142,11 @@ public class IslandCache {
*/
@Nullable
public Island get(@NonNull World world, @NonNull UUID uuid) {
return islandsByUUID.containsKey(Util.getWorld(world)) ? islandsByUUID.get(Util.getWorld(world)).get(uuid) : null;
World w = Util.getWorld(world);
if (w == null) {
return null;
}
return islandsByUUID.containsKey(w) ? islandsByUUID.get(w).get(uuid) : null;
}
/**
@ -153,10 +158,11 @@ public class IslandCache {
*/
@Nullable
public Island getIslandAt(@NonNull Location location) {
if (!grids.containsKey(Util.getWorld(location.getWorld()))) {
World w = Util.getWorld(location.getWorld());
if (w == null || !grids.containsKey(w)) {
return null;
}
return grids.get(Util.getWorld(location.getWorld())).getIslandAt(location.getBlockX(), location.getBlockZ());
return grids.get(w).getIslandAt(location.getBlockX(), location.getBlockZ());
}
/**
@ -177,7 +183,9 @@ public class IslandCache {
@NonNull
public Collection<Island> getIslands(@NonNull World world) {
World overworld = Util.getWorld(world);
if (overworld == null) {
return Collections.emptyList();
}
return islandsByLocation.entrySet().stream()
.filter(entry -> overworld.equals(Util.getWorld(entry.getKey().getWorld()))) // shouldn't make NPEs
.map(Map.Entry::getValue).toList();
@ -191,7 +199,11 @@ public class IslandCache {
*/
@NonNull
public Set<UUID> getMembers(@NonNull World world, @NonNull UUID uuid, int minimumRank) {
Island island = islandsByUUID.computeIfAbsent(Util.getWorld(world), k -> new HashMap<>()).get(uuid);
World w = Util.getWorld(world);
if (w == null) {
return new HashSet<>();
}
Island island = islandsByUUID.computeIfAbsent(w, k -> new HashMap<>()).get(uuid);
return island != null ? island.getMemberSet(minimumRank) : new HashSet<>();
}
@ -202,7 +214,11 @@ public class IslandCache {
*/
@Nullable
public UUID getOwner(@NonNull World world, @NonNull UUID uuid) {
Island island = islandsByUUID.computeIfAbsent(Util.getWorld(world), k -> new HashMap<>()).get(uuid);
World w = Util.getWorld(world);
if (w == null) {
return null;
}
Island island = islandsByUUID.computeIfAbsent(w, k -> new HashMap<>()).get(uuid);
return island != null ? island.getOwner() : null;
}
@ -213,7 +229,11 @@ public class IslandCache {
* @return true if player has island and owns it
*/
public boolean hasIsland(@NonNull World world, @NonNull UUID uuid) {
Island island = islandsByUUID.computeIfAbsent(Util.getWorld(world), k -> new HashMap<>()).get(uuid);
World w = Util.getWorld(world);
if (w == null) {
return false;
}
Island island = islandsByUUID.computeIfAbsent(w, k -> new HashMap<>()).get(uuid);
return island != null && uuid.equals(island.getOwner());
}
@ -226,7 +246,11 @@ public class IslandCache {
*/
@Nullable
public Island removePlayer(@NonNull World world, @NonNull UUID uuid) {
Island island = islandsByUUID.computeIfAbsent(Util.getWorld(world), k -> new HashMap<>()).get(uuid);
World w = Util.getWorld(world);
if (w == null) {
return null;
}
Island island = islandsByUUID.computeIfAbsent(w, k -> new HashMap<>()).get(uuid);
if (island != null) {
if (uuid.equals(island.getOwner())) {
// Clear ownership and members
@ -237,7 +261,7 @@ public class IslandCache {
island.removeMember(uuid);
}
}
islandsByUUID.get(world).remove(uuid);
islandsByUUID.get(w).remove(uuid);
return island;
}
@ -267,7 +291,7 @@ public class IslandCache {
public void setOwner(@NonNull Island island, @Nullable UUID newOwnerUUID) {
island.setOwner(newOwnerUUID);
if (newOwnerUUID != null) {
islandsByUUID.computeIfAbsent(Util.getWorld(island.getWorld()), k -> new HashMap<>()).put(newOwnerUUID, island);
islandsByUUID.computeIfAbsent(Objects.requireNonNull(Util.getWorld(island.getWorld())), k -> new HashMap<>()).put(newOwnerUUID, island);
}
islandsByLocation.put(island.getCenter(), island);
islandsById.put(island.getUniqueId(), island);
@ -292,11 +316,15 @@ public class IslandCache {
public void removeIsland(@NonNull Island island) {
islandsByLocation.values().removeIf(island::equals);
islandsById.values().removeIf(island::equals);
if (islandsByUUID.containsKey(Util.getWorld(island.getWorld()))) {
islandsByUUID.get(Util.getWorld(island.getWorld())).values().removeIf(island::equals);
World w = Util.getWorld(island.getWorld());
if (w == null) {
return;
}
if (grids.containsKey(Util.getWorld(island.getWorld()))) {
grids.get(Util.getWorld(island.getWorld())).removeFromGrid(island);
if (islandsByUUID.containsKey(w)) {
islandsByUUID.get(w).values().removeIf(island::equals);
}
if (grids.containsKey(w)) {
grids.get(w).removeFromGrid(island);
}
}
@ -307,6 +335,9 @@ public class IslandCache {
*/
public void resetAllFlags(World world) {
World w = Util.getWorld(world);
if (w == null) {
return;
}
islandsById.values().stream().filter(i -> i.getWorld().equals(w)).forEach(Island::setFlagsDefaults);
}
@ -318,6 +349,9 @@ public class IslandCache {
*/
public void resetFlag(World world, Flag flag) {
World w = Util.getWorld(world);
if (w == null) {
return;
}
int setting = BentoBox.getInstance().getIWM().getDefaultIslandFlags(w).getOrDefault(flag, flag.getDefaultRank());
islandsById.values().stream().filter(i -> i.getWorld().equals(w)).forEach(i -> i.setFlag(flag, setting));
}

View File

@ -238,7 +238,7 @@ public class Util {
/**
* Convert world to an overworld
* @param world - world
* @return over world
* @return over world or null if world is null or a world cannot be found
*/
@Nullable
public static World getWorld(@Nullable World world) {
@ -284,21 +284,21 @@ public class Util {
*/
public static float blockFaceToFloat(BlockFace face) {
return switch (face) {
case EAST -> 90F;
case EAST_NORTH_EAST -> 67.5F;
case NORTH_EAST -> 45F;
case NORTH_NORTH_EAST -> 22.5F;
case NORTH_NORTH_WEST -> 337.5F;
case NORTH_WEST -> 315F;
case SOUTH -> 180F;
case SOUTH_EAST -> 135F;
case SOUTH_SOUTH_EAST -> 157.5F;
case SOUTH_SOUTH_WEST -> 202.5F;
case SOUTH_WEST -> 225F;
case WEST -> 270F;
case WEST_NORTH_WEST -> 292.5F;
case WEST_SOUTH_WEST -> 247.5F;
default -> 0F;
case EAST -> 90F;
case EAST_NORTH_EAST -> 67.5F;
case NORTH_EAST -> 45F;
case NORTH_NORTH_EAST -> 22.5F;
case NORTH_NORTH_WEST -> 337.5F;
case NORTH_WEST -> 315F;
case SOUTH -> 180F;
case SOUTH_EAST -> 135F;
case SOUTH_SOUTH_EAST -> 157.5F;
case SOUTH_SOUTH_WEST -> 202.5F;
case SOUTH_WEST -> 225F;
case WEST -> 270F;
case WEST_NORTH_WEST -> 292.5F;
case WEST_SOUTH_WEST -> 247.5F;
default -> 0F;
};
}
@ -548,21 +548,21 @@ public class Util {
if (group.length() == 6) {
// Parses #ffffff to a color text.
matcher.appendReplacement(buffer, ChatColor.COLOR_CHAR + "x"
+ ChatColor.COLOR_CHAR + group.charAt(0) + ChatColor.COLOR_CHAR + group.charAt(1)
+ ChatColor.COLOR_CHAR + group.charAt(2) + ChatColor.COLOR_CHAR + group.charAt(3)
+ ChatColor.COLOR_CHAR + group.charAt(4) + ChatColor.COLOR_CHAR + group.charAt(5));
+ ChatColor.COLOR_CHAR + group.charAt(0) + ChatColor.COLOR_CHAR + group.charAt(1)
+ ChatColor.COLOR_CHAR + group.charAt(2) + ChatColor.COLOR_CHAR + group.charAt(3)
+ ChatColor.COLOR_CHAR + group.charAt(4) + ChatColor.COLOR_CHAR + group.charAt(5));
} else {
// Parses #fff to a color text.
matcher.appendReplacement(buffer, ChatColor.COLOR_CHAR + "x"
+ ChatColor.COLOR_CHAR + group.charAt(0) + ChatColor.COLOR_CHAR + group.charAt(0)
+ ChatColor.COLOR_CHAR + group.charAt(1) + ChatColor.COLOR_CHAR + group.charAt(1)
+ ChatColor.COLOR_CHAR + group.charAt(2) + ChatColor.COLOR_CHAR + group.charAt(2));
+ ChatColor.COLOR_CHAR + group.charAt(0) + ChatColor.COLOR_CHAR + group.charAt(0)
+ ChatColor.COLOR_CHAR + group.charAt(1) + ChatColor.COLOR_CHAR + group.charAt(1)
+ ChatColor.COLOR_CHAR + group.charAt(2) + ChatColor.COLOR_CHAR + group.charAt(2));
}
}
// transform normal codes and strip spaces after color code.
return Util.stripSpaceAfterColorCodes(
ChatColor.translateAlternateColorCodes('&', matcher.appendTail(buffer).toString()));
ChatColor.translateAlternateColorCodes('&', matcher.appendTail(buffer).toString()));
}
@ -717,7 +717,7 @@ public class Util {
throw new IllegalStateException("Class " + clazz.getName() + " does not implement NMSAbstraction");
}
}
/**
* Broadcast a localized message to all players with the permission {@link Server#BROADCAST_CHANNEL_USERS}
*