diff --git a/src/main/java/world/bentobox/bentobox/api/addons/AddonClassLoader.java b/src/main/java/world/bentobox/bentobox/api/addons/AddonClassLoader.java index 6f5b6a512..85ba85c88 100644 --- a/src/main/java/world/bentobox/bentobox/api/addons/AddonClassLoader.java +++ b/src/main/java/world/bentobox/bentobox/api/addons/AddonClassLoader.java @@ -79,8 +79,8 @@ public class AddonClassLoader extends URLClassLoader { */ @NonNull public static AddonDescription asDescription(YamlConfiguration data) throws InvalidAddonDescriptionException { - AddonDescription.Builder builder = new AddonDescription.Builder(data.getString("main"), data.getString("name"), data.getString("version")) - .authors(data.getString("authors")) + AddonDescription.Builder builder = new AddonDescription.Builder(Objects.requireNonNull(data.getString("main")), Objects.requireNonNull(data.getString("name")), Objects.requireNonNull(data.getString("version"))) + .authors(Objects.requireNonNull(data.getString("authors"))) .metrics(data.getBoolean("metrics", true)) .repository(data.getString("repository", "")); @@ -92,7 +92,7 @@ public class AddonClassLoader extends URLClassLoader { if (softDepend != null) { builder.softDependencies(Arrays.asList(softDepend.split("\\s*,\\s*"))); } - builder.icon(Material.getMaterial(data.getString("icon", "PAPER"))); + builder.icon(Objects.requireNonNull(Material.getMaterial(data.getString("icon", "PAPER")))); String apiVersion = data.getString("api-version"); if (apiVersion != null) { diff --git a/src/main/java/world/bentobox/bentobox/api/addons/AddonDescription.java b/src/main/java/world/bentobox/bentobox/api/addons/AddonDescription.java index b52cd4e61..c8a70e965 100644 --- a/src/main/java/world/bentobox/bentobox/api/addons/AddonDescription.java +++ b/src/main/java/world/bentobox/bentobox/api/addons/AddonDescription.java @@ -279,6 +279,6 @@ public final class AddonDescription { @Override public String toString() { return "AddonDescription [" + (name != null ? "name=" + name + ", " : "") - + (version != null ? "version=" + version : "") + "]"; + + "version=" + version + "]"; } } diff --git a/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java index 1ee54502f..d87034440 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java @@ -476,9 +476,11 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi * Convenience method to check if a user is a player * @param user - the User * @return true if sender is a player + * @deprecated use {@link User#isPlayer()} */ + @Deprecated protected boolean isPlayer(User user) { - return user.getPlayer() != null; + return user.isPlayer(); } /** diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminDeleteCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminDeleteCommand.java index eee47f960..083594515 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminDeleteCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminDeleteCommand.java @@ -86,7 +86,7 @@ public class AdminDeleteCommand extends ConfirmableCommand { User target = User.getInstance(targetUUID); // Remove them from this island (it still exists and will be deleted later) getIslands().removePlayer(getWorld(), targetUUID); - if (target.isOnline()) { + if (target.getPlayer() != null && target.isOnline()) { // Execute commands when leaving Util.runCommands(user, getIWM().getOnLeaveCommands(getWorld()), "leave"); // Remove money inventory etc. diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminSetrankCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminSetrankCommand.java index 43b9297a7..7a835c70a 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminSetrankCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminSetrankCommand.java @@ -101,6 +101,10 @@ public class AdminSetrankCommand extends CompositeCommand { } else { island = getIslands().getIsland(getWorld(), targetUUID); } + if (island == null) { + user.sendMessage("general.errors.player-has-no-island"); + return false; + } int currentRank = island.getRank(target); island.setRank(target, rankValue); IslandEvent.builder() diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminTeleportCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminTeleportCommand.java index 279531f66..abdeffe81 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminTeleportCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminTeleportCommand.java @@ -19,6 +19,7 @@ import world.bentobox.bentobox.util.teleport.SafeSpotTeleport; public class AdminTeleportCommand extends CompositeCommand { + private static final String NOT_SAFE = "general.errors.no-safe-location-found"; private @Nullable UUID targetUUID; private @Nullable User userToTeleport; @@ -86,9 +87,13 @@ public class AdminTeleportCommand extends CompositeCommand { } else if (getLabel().equals("tpend")) { world = getPlugin().getIWM().getEndWorld(getWorld()); } + if (world == null) { + user.sendMessage(NOT_SAFE); + return false; + } Location warpSpot = getSpot(world); - if (world == null || warpSpot == null) { - user.sendMessage("general.errors.no-safe-location-found"); + if (warpSpot == null) { + user.sendMessage(NOT_SAFE); return false; } @@ -99,7 +104,7 @@ public class AdminTeleportCommand extends CompositeCommand { Player player = user.getPlayer(); if (args.size() == 2) { player = userToTeleport.getPlayer(); - failureMessage = userToTeleport.getTranslation("general.errors.no-safe-location-found"); + failureMessage = userToTeleport.getTranslation(NOT_SAFE); } // Teleport @@ -118,7 +123,7 @@ public class AdminTeleportCommand extends CompositeCommand { return island.getSpawnPoint(world.getEnvironment()); } // Return the default island protection center - return getIslands().getIslandLocation(getWorld(), targetUUID).toVector().toLocation(world); + return island.getProtectionCenter().toVector().toLocation(world); } @Override diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintCommand.java index 63312b29d..a869ed72a 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintCommand.java @@ -64,7 +64,7 @@ public class AdminBlueprintCommand extends ConfirmableCommand { protected void showClipboard(User user) { displayClipboards.putIfAbsent(user, Bukkit.getScheduler().scheduleSyncRepeatingTask(getPlugin(), () -> { - if (!user.getPlayer().isOnline()) { + if (!user.isPlayer() || !user.getPlayer().isOnline()) { hideClipboard(user); } diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintOriginCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintOriginCommand.java index 0a4def22e..df254b3bb 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintOriginCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintOriginCommand.java @@ -1,6 +1,7 @@ package world.bentobox.bentobox.api.commands.admin.blueprints; import java.util.List; +import java.util.Objects; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -34,7 +35,7 @@ public class AdminBlueprintOriginCommand extends CompositeCommand { } // Get the block player is looking at - Block b = user.getPlayer().getLineOfSight(null, 20).stream().filter(x -> !x.getType().equals(Material.AIR)).findFirst().orElse(null); + Block b = Objects.requireNonNull(user.getPlayer()).getLineOfSight(null, 20).stream().filter(x -> !x.getType().equals(Material.AIR)).findFirst().orElse(null); if (b != null) { clipboard.setOrigin(b.getLocation().toVector()); user.getPlayer().sendBlockChange(b.getLocation(), Material.REDSTONE_BLOCK.createBlockData()); diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintPos1Command.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintPos1Command.java index 1fa6d9e44..534124ce6 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintPos1Command.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintPos1Command.java @@ -30,7 +30,7 @@ public class AdminBlueprintPos1Command extends CompositeCommand { return false; } clipboard.setPos1(user.getLocation()); - user.sendMessage("commands.admin.blueprint.set-pos1", "[vector]", Util.xyz(clipboard.getPos1().toVector())); + user.sendMessage("commands.admin.blueprint.set-pos1", "[vector]", Util.xyz(user.getLocation().toVector())); parent.getClipboards().put(user.getUniqueId(), clipboard); parent.showClipboard(user); return true; diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintPos2Command.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintPos2Command.java index 481ece58c..9c1bcae44 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintPos2Command.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintPos2Command.java @@ -30,7 +30,7 @@ public class AdminBlueprintPos2Command extends CompositeCommand { return false; } clipboard.setPos2(user.getLocation()); - user.sendMessage("commands.admin.blueprint.set-pos2", "[vector]", Util.xyz((clipboard.getPos2()).toVector())); + user.sendMessage("commands.admin.blueprint.set-pos2", "[vector]", Util.xyz(user.getLocation().toVector())); parent.getClipboards().put(user.getUniqueId(), clipboard); parent.showClipboard(user); return true; diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintSaveCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintSaveCommand.java index 7c7cece13..7ec6af2ba 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintSaveCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintSaveCommand.java @@ -55,7 +55,7 @@ public class AdminBlueprintSaveCommand extends ConfirmableCommand { private boolean hideAndSave(User user, AdminBlueprintCommand parent, BlueprintClipboard clipboard, String name) { parent.hideClipboard(user); boolean result = new BlueprintClipboardManager(getPlugin(), parent.getBlueprintsFolder(), clipboard).save(user, name); - if (result) { + if (result && clipboard.getBlueprint() != null) { getPlugin().getBlueprintsManager().addBlueprint(getAddon(), clipboard.getBlueprint()); } return result; diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeAddCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeAddCommand.java index 67a9981c4..ff64a6796 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeAddCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeAddCommand.java @@ -1,6 +1,7 @@ package world.bentobox.bentobox.api.commands.admin.range; import java.util.List; +import java.util.Objects; import java.util.UUID; import org.eclipse.jdt.annotation.NonNull; @@ -52,7 +53,7 @@ public class AdminRangeAddCommand extends CompositeCommand { return false; } - Island island = getIslands().getIsland(getWorld(), targetUUID); + Island island = Objects.requireNonNull(getIslands().getIsland(getWorld(), targetUUID)); int newRange = island.getProtectionRange() + Integer.parseInt(args.get(1)); if (newRange > island.getRange() * 2) { diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeRemoveCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeRemoveCommand.java index ee7692a11..5615ec5e7 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeRemoveCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeRemoveCommand.java @@ -1,6 +1,7 @@ package world.bentobox.bentobox.api.commands.admin.range; import java.util.List; +import java.util.Objects; import java.util.UUID; import org.eclipse.jdt.annotation.NonNull; @@ -52,7 +53,7 @@ public class AdminRangeRemoveCommand extends CompositeCommand { return false; } - Island island = getIslands().getIsland(getWorld(), targetUUID); + Island island = Objects.requireNonNull(getIslands().getIsland(getWorld(), targetUUID)); int newRange = island.getProtectionRange() - Integer.parseInt(args.get(1)); if (newRange <= 1) { diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeResetCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeResetCommand.java index dce72db0d..4a76593d9 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeResetCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeResetCommand.java @@ -2,6 +2,7 @@ package world.bentobox.bentobox.api.commands.admin.range; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.Optional; import java.util.UUID; @@ -45,7 +46,7 @@ public class AdminRangeResetCommand extends CompositeCommand { } // Get island - Island island = getIslands().getIsland(getWorld(), targetUUID); + Island island = Objects.requireNonNull(getIslands().getIsland(getWorld(), targetUUID)); // Get old range for event int oldRange = island.getProtectionRange(); diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeSetCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeSetCommand.java index aff757749..c63912004 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeSetCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeSetCommand.java @@ -2,6 +2,7 @@ package world.bentobox.bentobox.api.commands.admin.range; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.Optional; import java.util.UUID; @@ -52,7 +53,7 @@ public class AdminRangeSetCommand extends CompositeCommand { int range = Integer.parseInt(args.get(1)); // Get island - Island island = getIslands().getIsland(getWorld(), targetUUID); + Island island = Objects.requireNonNull(getIslands().getIsland(getWorld(), targetUUID)); // Do some sanity checks to make sure the new protection range won't cause problems if (range < 1) { diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamSetownerCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamSetownerCommand.java index 77945a9ba..2001367cc 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamSetownerCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamSetownerCommand.java @@ -1,6 +1,7 @@ package world.bentobox.bentobox.api.commands.admin.team; import java.util.List; +import java.util.Objects; import java.util.UUID; import world.bentobox.bentobox.api.commands.CompositeCommand; @@ -54,11 +55,10 @@ public class AdminTeamSetownerCommand extends CompositeCommand { } // Get the User corresponding to the current owner - User previousOwner = User.getInstance(previousOwnerUUID); User target = User.getInstance(targetUUID); // Fire event so add-ons know - Island island = getIslands().getIsland(getWorld(), targetUUID); + Island island = Objects.requireNonNull(getIslands().getIsland(getWorld(), targetUUID)); // Call the setowner event TeamEvent.builder() .island(island) @@ -82,15 +82,16 @@ public class AdminTeamSetownerCommand extends CompositeCommand { user.sendMessage("commands.admin.team.setowner.success", TextVariables.NAME, args.get(0)); // Call the rank change event for the old island owner - // We need to call it AFTER the actual change. - IslandEvent.builder() - .island(island) - .involvedPlayer(previousOwnerUUID) - .admin(true) - .reason(IslandEvent.Reason.RANK_CHANGE) - .rankChange(RanksManager.OWNER_RANK, island.getRank(previousOwner)) - .build(); - + if (previousOwnerUUID != null) { + // We need to call it AFTER the actual change. + IslandEvent.builder() + .island(island) + .involvedPlayer(previousOwnerUUID) + .admin(true) + .reason(IslandEvent.Reason.RANK_CHANGE) + .rankChange(RanksManager.OWNER_RANK, island.getRank(previousOwnerUUID)) + .build(); + } return true; } } diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandBanCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandBanCommand.java index ce9788d3a..9848cb8c7 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandBanCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandBanCommand.java @@ -51,8 +51,8 @@ public class IslandBanCommand extends CompositeCommand { return false; } // Check rank to use command - Island island = getIslands().getIsland(getWorld(), user); - int rank = Objects.requireNonNull(island).getRank(user); + Island island = Objects.requireNonNull(getIslands().getIsland(getWorld(), user)); + int rank = island.getRank(user); if (rank < island.getRankCommand(getUsage())) { user.sendMessage("general.errors.insufficient-rank", TextVariables.RANK, user.getTranslation(getPlugin().getRanksManager().getRank(rank))); return false; @@ -95,7 +95,7 @@ public class IslandBanCommand extends CompositeCommand { } private boolean ban(@NonNull User issuer, User target) { - Island island = getIslands().getIsland(getWorld(), issuer.getUniqueId()); + Island island = Objects.requireNonNull(getIslands().getIsland(getWorld(), issuer.getUniqueId())); // Check if player can ban any more players int banLimit = issuer.getPermissionValue(getPermissionPrefix() + "ban.maxlimit", getIWM().getBanLimit(getWorld())); diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamPromoteCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamPromoteCommand.java index 6d3ee539c..49ff21566 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamPromoteCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamPromoteCommand.java @@ -66,7 +66,7 @@ public class IslandTeamPromoteCommand extends CompositeCommand { user.sendMessage("commands.island.team.demote.errors.cant-demote-yourself"); return true; } - if (!inTeam(getWorld(), target) || !getOwner(getWorld(), user).equals(getOwner(getWorld(), target))) { + if (!inTeam(getWorld(), target) || !Objects.requireNonNull(getOwner(getWorld(), user), "Island has no owner!").equals(getOwner(getWorld(), target))) { user.sendMessage("general.errors.not-in-team"); return true; } diff --git a/src/main/java/world/bentobox/bentobox/api/localization/TextVariables.java b/src/main/java/world/bentobox/bentobox/api/localization/TextVariables.java index 85c778d7c..61bce6fb5 100644 --- a/src/main/java/world/bentobox/bentobox/api/localization/TextVariables.java +++ b/src/main/java/world/bentobox/bentobox/api/localization/TextVariables.java @@ -34,4 +34,8 @@ public class TextVariables { * @since 1.16.0 */ public static final String XYZ = "[xyz]"; + /** + * @since 1.17.2 + */ + public static final String UUID = "[uuid]"; } diff --git a/src/main/java/world/bentobox/bentobox/api/user/User.java b/src/main/java/world/bentobox/bentobox/api/user/User.java index 411318084..9bc2030ad 100644 --- a/src/main/java/world/bentobox/bentobox/api/user/User.java +++ b/src/main/java/world/bentobox/bentobox/api/user/User.java @@ -192,10 +192,12 @@ public class User implements MetaDataAble { } /** + * Check if the User is a player before calling this method. {@link #isPlayer()} * @return the player */ + @NonNull public Player getPlayer() { - return player; + return Objects.requireNonNull(player, "User is not a player!"); } /** @@ -206,11 +208,13 @@ public class User implements MetaDataAble { } /** + * Use {@link #isOfflinePlayer()} before calling this method * @return the offline player * @since 1.3.0 */ + @NonNull public OfflinePlayer getOfflinePlayer() { - return offlinePlayer; + return Objects.requireNonNull(offlinePlayer, "User is not an OfflinePlayer!"); } /** diff --git a/src/main/java/world/bentobox/bentobox/blueprints/BlueprintClipboard.java b/src/main/java/world/bentobox/bentobox/blueprints/BlueprintClipboard.java index 7e7fcbc1e..66ed549ad 100644 --- a/src/main/java/world/bentobox/bentobox/blueprints/BlueprintClipboard.java +++ b/src/main/java/world/bentobox/bentobox/blueprints/BlueprintClipboard.java @@ -151,6 +151,7 @@ public class BlueprintClipboard { } if (index > vectorsToCopy.size()) { copyTask.cancel(); + assert blueprint != null; blueprint.setAttached(bpAttachable); blueprint.setBlocks(bpBlocks); blueprint.setEntities(bpEntities); diff --git a/src/main/java/world/bentobox/bentobox/database/objects/Island.java b/src/main/java/world/bentobox/bentobox/database/objects/Island.java index be3f978d6..29f2181c9 100644 --- a/src/main/java/world/bentobox/bentobox/database/objects/Island.java +++ b/src/main/java/world/bentobox/bentobox/database/objects/Island.java @@ -193,7 +193,7 @@ public class Island implements DataObject, MetaDataAble { private Boolean reserved = null; /** - * A place to store meta data for this island. + * A place to store metadata for this island. * @since 1.15.4 */ @Expose @@ -354,7 +354,7 @@ public class Island implements DataObject, MetaDataAble { } /** - * Gets the Island Guard flag's setting. If this is a protection flag, the this will be the + * Gets the Island Guard flag's setting. If this is a protection flag, then this will be the * rank needed to bypass this flag. If it is a Settings flag, any non-zero value means the * setting is allowed. * @param flag - flag @@ -427,41 +427,41 @@ public class Island implements DataObject, MetaDataAble { } /** - * Get the minimum protected X block coord based on the island location. + * Get the minimum protected X block coordinate based on the island location. * It will never be less than {@link #getMinX()} * @return the minProtectedX */ public int getMinProtectedX() { - return getProtectionCenter() == null ? 0 : Math.max(getMinX(), getProtectionCenter().getBlockX() - protectionRange); + return Math.max(getMinX(), getProtectionCenter().getBlockX() - protectionRange); } /** - * Get the maximum protected X block coord based on the island location. + * Get the maximum protected X block coordinate based on the island location. * It will never be more than {@link #getMaxX()} * @return the maxProtectedX * @since 1.5.2 */ public int getMaxProtectedX() { - return getProtectionCenter() == null ? 0 : Math.min(getMaxX(), getProtectionCenter().getBlockX() + protectionRange); + return Math.min(getMaxX(), getProtectionCenter().getBlockX() + protectionRange); } /** - * Get the minimum protected Z block coord based on the island location. + * Get the minimum protected Z block coordinate based on the island location. * It will never be less than {@link #getMinZ()} * @return the minProtectedZ */ public int getMinProtectedZ() { - return getProtectionCenter() == null ? 0 : Math.max(getMinZ(), getProtectionCenter().getBlockZ() - protectionRange); + return Math.max(getMinZ(), getProtectionCenter().getBlockZ() - protectionRange); } /** - * Get the maximum protected Z block coord based on the island location. + * Get the maximum protected Z block coordinate based on the island location. * It will never be more than {@link #getMinZ()} * @return the maxProtectedZ * @since 1.5.2 */ public int getMaxProtectedZ() { - return getProtectionCenter() == null ? 0 : Math.min(getMaxZ(), getProtectionCenter().getBlockZ() + protectionRange); + return Math.min(getMaxZ(), getProtectionCenter().getBlockZ() + protectionRange); } /** @@ -998,7 +998,7 @@ public class Island implements DataObject, MetaDataAble { * Sets whether this island is a spawn or not. *
* If {@code true}, the members and the owner will be removed from this island. - * The flags will also be resetted to default values. + * The flags will also be reset to default values. * @param isSpawn {@code true} if the island is a spawn, {@code false} otherwise. */ public void setSpawn(boolean isSpawn){ @@ -1380,7 +1380,7 @@ public class Island implements DataObject, MetaDataAble { * @return a clone of the protection center location * @since 1.16.0 */ - @Nullable + @NonNull public Location getProtectionCenter() { return location == null ? getCenter() : location.clone(); } diff --git a/src/main/java/world/bentobox/bentobox/listeners/BlockEndDragon.java b/src/main/java/world/bentobox/bentobox/listeners/BlockEndDragon.java index 8c219a353..47819492c 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/BlockEndDragon.java +++ b/src/main/java/world/bentobox/bentobox/listeners/BlockEndDragon.java @@ -2,6 +2,7 @@ package world.bentobox.bentobox.listeners; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.World; import org.bukkit.World.Environment; import org.bukkit.block.Block; import org.bukkit.event.EventHandler; @@ -35,14 +36,15 @@ public class BlockEndDragon implements Listener { } private void testLocation(Location location) { - if (!plugin.getIWM().isIslandEnd(location.getWorld()) - || !Flags.REMOVE_END_EXIT_ISLAND.isSetForWorld(location.getWorld()) - || location.getWorld().getBlockAt(0, 255, 0).getType().equals(Material.END_PORTAL)) { + World w = location.getWorld(); + if (w == null || !plugin.getIWM().isIslandEnd(w) + || !Flags.REMOVE_END_EXIT_ISLAND.isSetForWorld(w) + || w.getBlockAt(0, 255, 0).getType().equals(Material.END_PORTAL)) { return; } // Setting a End Portal at the top will trick dragon legacy check. - location.getWorld().getBlockAt(0, 255, 0).setType(Material.END_PORTAL, false); + w.getBlockAt(0, 255, 0).setType(Material.END_PORTAL, false); } /** diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/protection/BlockInteractionListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/BlockInteractionListener.java index 98a382cec..55deac57e 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/protection/BlockInteractionListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/BlockInteractionListener.java @@ -44,7 +44,7 @@ public class BlockInteractionListener extends FlagListener { @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) public void onPlayerInteract(final PlayerInteractEvent e) { // We only care about the RIGHT_CLICK_BLOCK action. - if (!e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) { + if (!e.getAction().equals(Action.RIGHT_CLICK_BLOCK) || e.getClickedBlock() == null) { return; } diff --git a/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java b/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java index b7853161d..707a9051e 100644 --- a/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java @@ -1,9 +1,6 @@ package world.bentobox.bentobox.managers; -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; +import java.io.*; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; @@ -97,8 +94,13 @@ public class AddonsManager { plugin.log("Registering " + parent.getDescription().getName()); // Get description in the addon.yml file + InputStream resource = parent.getResource("addon.yml"); + if (resource == null) { + plugin.logError("Failed to register addon: no addon.yml found"); + return; + } // Open a reader to the jar - try (BufferedReader reader = new BufferedReader(new InputStreamReader(parent.getResource("addon.yml")))) { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource))) { setAddonFile(parent, addon); // Grab the description in the addon.yml file YamlConfiguration data = new YamlConfiguration(); @@ -266,7 +268,11 @@ public class AddonsManager { } void registerPermission(ConfigurationSection perms, String perm) throws InvalidAddonDescriptionException { - PermissionDefault pd = PermissionDefault.getByName(perms.getString(perm + DEFAULT)); + String name = perms.getString(perm + DEFAULT); + if (name == null) { + throw new InvalidAddonDescriptionException("Permission default is invalid in addon.yml: " + perm + DEFAULT); + } + PermissionDefault pd = PermissionDefault.getByName(name); if (pd == null) { throw new InvalidAddonDescriptionException("Permission default is invalid in addon.yml: " + perm + DEFAULT); } @@ -503,7 +509,7 @@ public class AddonsManager { @Nullable public Class getClassByName(@NonNull final String name) { try { - return classes.getOrDefault(name, loaders.values().stream().map(l -> l.findClass(name, false)).filter(Objects::nonNull).findFirst().orElse(null)); + return classes.getOrDefault(name, loaders.values().stream().filter(Objects::nonNull).map(l -> l.findClass(name, false)).filter(Objects::nonNull).findFirst().orElse(null)); } catch (Exception ignored) { // Ignored. } diff --git a/src/main/java/world/bentobox/bentobox/managers/BlueprintClipboardManager.java b/src/main/java/world/bentobox/bentobox/managers/BlueprintClipboardManager.java index 5921c27db..cc7ea3c16 100644 --- a/src/main/java/world/bentobox/bentobox/managers/BlueprintClipboardManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/BlueprintClipboardManager.java @@ -144,10 +144,12 @@ public class BlueprintClipboardManager { * @return - true if successful, false if error */ public boolean save(User user, String newName) { - clipboard.getBlueprint().setName(newName); - if (saveBlueprint(clipboard.getBlueprint())) { - user.sendMessage("general.success"); - return true; + if (clipboard.getBlueprint() != null) { + clipboard.getBlueprint().setName(newName); + if (saveBlueprint(clipboard.getBlueprint())) { + user.sendMessage("general.success"); + return true; + } } user.sendMessage("commands.admin.blueprint.could-not-save", "[message]", "Could not save temp blueprint file."); return false; diff --git a/src/main/java/world/bentobox/bentobox/util/IslandInfo.java b/src/main/java/world/bentobox/bentobox/util/IslandInfo.java index dbe55854a..5c4d9d957 100644 --- a/src/main/java/world/bentobox/bentobox/util/IslandInfo.java +++ b/src/main/java/world/bentobox/bentobox/util/IslandInfo.java @@ -44,11 +44,11 @@ public class IslandInfo { */ public void showAdminInfo(User user) { user.sendMessage("commands.admin.info.title"); - user.sendMessage("commands.admin.info.island-uuid", "[uuid]", island.getUniqueId()); + user.sendMessage("commands.admin.info.island-uuid", TextVariables.UUID, island.getUniqueId()); if (owner == null) { user.sendMessage("commands.admin.info.unowned"); } else { - user.sendMessage("commands.admin.info.owner", "[owner]", plugin.getPlayers().getName(owner), "[uuid]", owner.toString()); + user.sendMessage("commands.admin.info.owner", "[owner]", plugin.getPlayers().getName(owner), TextVariables.UUID, owner.toString()); // Fixes #getLastPlayed() returning 0 when it is the owner's first connection. long lastPlayed = (Bukkit.getOfflinePlayer(owner).getLastPlayed() != 0) ? @@ -99,7 +99,7 @@ public class IslandInfo { if (owner == null) { user.sendMessage("commands.admin.info.unowned"); } else { - user.sendMessage("commands.admin.info.owner", "[owner]", plugin.getPlayers().getName(owner)); + user.sendMessage("commands.admin.info.owner", "[owner]", plugin.getPlayers().getName(owner), TextVariables.UUID, owner.toString()); user.sendMessage("commands.admin.info.deaths", "[number]", String.valueOf(plugin.getPlayers().getDeaths(world, owner))); String resets = String.valueOf(plugin.getPlayers().getResets(world, owner)); String total = plugin.getIWM().getResetLimit(world) < 0 ? "Unlimited" : String.valueOf(plugin.getIWM().getResetLimit(world)); diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminTeleportCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminTeleportCommandTest.java index 1457140d2..71960c644 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminTeleportCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminTeleportCommandTest.java @@ -164,6 +164,11 @@ public class AdminTeleportCommandTest { // Return an island for spawn checking when(im.getIsland(any(), any(UUID.class))).thenReturn(island); + + when(island.getCenter()).thenReturn(location); + when(location.clone()).thenReturn(location); + when(location.toVector()).thenReturn(new Vector(0,0,0)); + when(island.getProtectionCenter()).thenReturn(location); // Util PowerMockito.mockStatic(Util.class, Mockito.RETURNS_MOCKS); when(Util.getUUID(anyString())).thenCallRealMethod(); diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandInfoCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandInfoCommandTest.java index db650ebb5..d7f29cb13 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandInfoCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandInfoCommandTest.java @@ -196,7 +196,7 @@ public class IslandInfoCommandTest { public void testExecuteUserStringListOfStringNoArgsSuccess() { assertTrue(iic.execute(user, "", Collections.emptyList())); verify(user).sendMessage("commands.admin.info.title"); - verify(user).sendMessage(eq("commands.admin.info.owner"), eq("[owner]"), eq(null)); + verify(user).sendMessage(eq("commands.admin.info.owner"), eq("[owner]"), eq(null), eq("[uuid]"), anyString()); verify(user).sendMessage("commands.admin.info.deaths", "[number]", "0"); verify(user).sendMessage("commands.admin.info.resets-left", "[number]", "0", "[total]", "0"); verify(user).sendMessage("commands.admin.info.team-members-title"); @@ -213,7 +213,7 @@ public class IslandInfoCommandTest { public void testExecuteUserStringListOfStringArgsSuccess() { assertTrue(iic.execute(user, "", Collections.singletonList("tastybento"))); verify(user).sendMessage("commands.admin.info.title"); - verify(user).sendMessage(eq("commands.admin.info.owner"), eq("[owner]"), eq(null)); + verify(user).sendMessage(eq("commands.admin.info.owner"), eq("[owner]"), eq(null), eq("[uuid]"), anyString()); verify(user).sendMessage("commands.admin.info.deaths", "[number]", "0"); verify(user).sendMessage("commands.admin.info.resets-left", "[number]", "0", "[total]", "0"); verify(user).sendMessage("commands.admin.info.team-members-title"); diff --git a/src/test/java/world/bentobox/bentobox/api/user/UserTest.java b/src/test/java/world/bentobox/bentobox/api/user/UserTest.java index c1508d15a..82b386574 100644 --- a/src/test/java/world/bentobox/bentobox/api/user/UserTest.java +++ b/src/test/java/world/bentobox/bentobox/api/user/UserTest.java @@ -4,7 +4,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; @@ -164,7 +163,6 @@ public class UserTest { // If the player has been removed from the cache, then code will ask server for player // Return null and check if instance is null will show that the player is not in the cache when(Bukkit.getPlayer(any(UUID.class))).thenReturn(null); - assertNull(User.getInstance(uuid).getPlayer()); verify(pm).removePlayer(player); }