From 2c7f54fe10c0a9625df956c93c933f56d4ce163f Mon Sep 17 00:00:00 2001 From: tastybento Date: Fri, 8 Jan 2021 20:05:23 -0800 Subject: [PATCH] Use orElseGet instead of orElse orElse is a trap because it evaluates even if the Optional is empty. orElseGet is the correct way. --- .../api/commands/CompositeCommand.java | 2 +- .../commands/admin/AdminRegisterCommand.java | 4 +++- .../commands/island/IslandSettingsCommand.java | 2 +- .../bentobox/api/flags/FlagListener.java | 2 +- .../bentobox/database/DatabaseSetup.java | 2 +- .../hooks/placeholders/PlaceholderAPIHook.java | 18 +++++++++--------- .../listeners/PortalTeleportationListener.java | 8 ++++---- .../flags/protection/FireListener.java | 2 +- .../flags/protection/TNTListener.java | 2 +- .../flags/settings/DecayListener.java | 2 +- .../flags/settings/MobSpawnListener.java | 6 +++--- .../bentobox/managers/IslandWorldManager.java | 4 ++-- .../bentobox/managers/PlayersManager.java | 4 ++-- .../util/teleport/SafeSpotTeleport.java | 2 +- .../PortalTeleportationListenerTest.java | 8 ++++---- 15 files changed, 35 insertions(+), 33 deletions(-) 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 51ae122af..e69407e29 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java @@ -602,7 +602,7 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi return options; } // Add any tab completion from the subcommand - options.addAll(command.tabComplete(User.getInstance(sender), alias, new LinkedList<>(Arrays.asList(args))).orElse(new ArrayList<>())); + options.addAll(command.tabComplete(User.getInstance(sender), alias, new LinkedList<>(Arrays.asList(args))).orElseGet(() -> new ArrayList<>())); if (command.hasSubCommands()) { options.addAll(getSubCommandLabels(sender, command)); } diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminRegisterCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminRegisterCommand.java index 613b38409..ef4e6db8d 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminRegisterCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminRegisterCommand.java @@ -61,7 +61,9 @@ public class AdminRegisterCommand extends ConfirmableCommand { } // Check if island is owned Optional island = getIslands().getIslandAt(user.getLocation()); - if (island.map(Island::isOwned).orElse(false)) { + if (island.filter(Island::isOwned) + .filter(i -> !i.getOwner().equals(targetUUID)) + .isPresent()) { user.sendMessage("commands.admin.register.already-owned"); return false; } diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandSettingsCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandSettingsCommand.java index 31aca161d..83648998f 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandSettingsCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandSettingsCommand.java @@ -39,7 +39,7 @@ public class IslandSettingsCommand extends CompositeCommand { @Override public boolean execute(User user, String label, List args) { - Island island = getIslands().getIslandAt(user.getLocation()).orElse(getIslands().getIsland(user.getWorld(), user.getUniqueId())); + Island island = getIslands().getIslandAt(user.getLocation()).orElseGet(() -> getIslands().getIsland(user.getWorld(), user.getUniqueId())); new TabbedPanelBuilder() .user(user) .world(getWorld()) diff --git a/src/main/java/world/bentobox/bentobox/api/flags/FlagListener.java b/src/main/java/world/bentobox/bentobox/api/flags/FlagListener.java index 2c7ad98b8..74343e06d 100644 --- a/src/main/java/world/bentobox/bentobox/api/flags/FlagListener.java +++ b/src/main/java/world/bentobox/bentobox/api/flags/FlagListener.java @@ -146,7 +146,7 @@ public abstract class FlagListener implements Listener { } else { report(user, e, loc, flag, flag.isSetForWorld(loc.getWorld()) ? Why.SETTING_ALLOWED_IN_WORLD : Why.SETTING_NOT_ALLOWED_IN_WORLD); } - return island.map(x -> x.isAllowed(flag)).orElse(flag.isSetForWorld(loc.getWorld())); + return island.map(x -> x.isAllowed(flag)).orElseGet(() -> flag.isSetForWorld(loc.getWorld())); } // Protection flag diff --git a/src/main/java/world/bentobox/bentobox/database/DatabaseSetup.java b/src/main/java/world/bentobox/bentobox/database/DatabaseSetup.java index e24e0b817..1456b51df 100644 --- a/src/main/java/world/bentobox/bentobox/database/DatabaseSetup.java +++ b/src/main/java/world/bentobox/bentobox/database/DatabaseSetup.java @@ -43,7 +43,7 @@ public interface DatabaseSetup { .filter(plugin.getSettings().getDatabaseType()::equals) .findFirst() .map(t -> t.database) - .orElse(DatabaseType.JSON.database); + .orElseGet(() -> DatabaseType.JSON.database); } /** diff --git a/src/main/java/world/bentobox/bentobox/hooks/placeholders/PlaceholderAPIHook.java b/src/main/java/world/bentobox/bentobox/hooks/placeholders/PlaceholderAPIHook.java index ed58a4d90..91177291e 100644 --- a/src/main/java/world/bentobox/bentobox/hooks/placeholders/PlaceholderAPIHook.java +++ b/src/main/java/world/bentobox/bentobox/hooks/placeholders/PlaceholderAPIHook.java @@ -29,7 +29,7 @@ public class PlaceholderAPIHook extends PlaceholderHook { private Map addonsExpansions; private final Set bentoBoxPlaceholders; private final Map> addonPlaceholders; - + public PlaceholderAPIHook() { super("PlaceholderAPI"); @@ -105,15 +105,15 @@ public class PlaceholderAPIHook extends PlaceholderHook { } /** - * + * */ @Override @NonNull public String replacePlaceholders(@NonNull Player player, @NonNull String string) { // Transform [gamemode] in string to the game mode description name, or remove it for the default replacement - String newString = BentoBox.getInstance().getIWM().getAddon(player.getWorld()).map(gm -> - string.replace(TextVariables.GAMEMODE, gm.getDescription().getName().toLowerCase()) - ).orElse(removeGMPlaceholder(string)); + String newString = BentoBox.getInstance().getIWM().getAddon(player.getWorld()).map(gm -> + string.replace(TextVariables.GAMEMODE, gm.getDescription().getName().toLowerCase()) + ).orElseGet(() -> removeGMPlaceholder(string)); return PlaceholderAPI.setPlaceholders(player, newString); } @@ -121,10 +121,10 @@ public class PlaceholderAPIHook extends PlaceholderHook { String newString = string; // Get placeholders - TODO: my regex moh=jo isn't good enough to grab only placeholders with [gamemode] in yet! Matcher m = Pattern.compile("(%)(.*?)(%)").matcher(string); - while (m.find()) { - String ph = m.group(); - if (ph.contains(TextVariables.GAMEMODE)) newString = newString.replace(ph,""); - } + while (m.find()) { + String ph = m.group(); + if (ph.contains(TextVariables.GAMEMODE)) newString = newString.replace(ph,""); + } return newString; } diff --git a/src/main/java/world/bentobox/bentobox/listeners/PortalTeleportationListener.java b/src/main/java/world/bentobox/bentobox/listeners/PortalTeleportationListener.java index 6f9ed5441..adce11c2d 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/PortalTeleportationListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/PortalTeleportationListener.java @@ -150,7 +150,7 @@ public class PortalTeleportationListener implements Listener { // If entering an ender portal in the End. if (fromWorld.getEnvironment() == Environment.THE_END) { // If this is from the island nether, then go to the same vector, otherwise try island home location - Location to = plugin.getIslands().getIslandAt(e.getFrom()).map(i -> i.getSpawnPoint(Environment.NORMAL)).orElse(e.getFrom().toVector().toLocation(overWorld)); + Location to = plugin.getIslands().getIslandAt(e.getFrom()).map(i -> i.getSpawnPoint(Environment.NORMAL)).orElseGet(() -> e.getFrom().toVector().toLocation(overWorld)); e.setCancelled(true); // Else other worlds teleport to the overworld new SafeSpotTeleport.Builder(plugin) @@ -165,7 +165,7 @@ public class PortalTeleportationListener implements Listener { World endWorld = plugin.getIWM().getEndWorld(overWorld); // If this is to island End, then go to the same vector, otherwise try spawn Optional optionalIsland = plugin.getIslands().getIslandAt(e.getFrom()); - Location to = optionalIsland.map(i -> i.getSpawnPoint(Environment.THE_END)).orElse(e.getFrom().toVector().toLocation(endWorld)); + Location to = optionalIsland.map(i -> i.getSpawnPoint(Environment.THE_END)).orElseGet(() -> e.getFrom().toVector().toLocation(endWorld)); e.setCancelled(true); // Check if there is a missing end island if (plugin.getIWM().isPasteMissingIslands(overWorld) @@ -247,7 +247,7 @@ public class PortalTeleportationListener implements Listener { // If entering a nether portal in the nether, teleport to portal in overworld if there is one if (fromWorld.getEnvironment() == Environment.NETHER) { // If this is from the island nether, then go to the same vector, otherwise try island home location - Location to = plugin.getIslands().getIslandAt(e.getFrom()).map(i -> i.getSpawnPoint(Environment.NORMAL)).orElse(e.getFrom().toVector().toLocation(overWorld)); + Location to = plugin.getIslands().getIslandAt(e.getFrom()).map(i -> i.getSpawnPoint(Environment.NORMAL)).orElseGet(() -> e.getFrom().toVector().toLocation(overWorld)); e.setCancelled(true); // Else other worlds teleport to the nether new SafeSpotTeleport.Builder(plugin) @@ -262,7 +262,7 @@ public class PortalTeleportationListener implements Listener { World nether = plugin.getIWM().getNetherWorld(overWorld); // If this is to island nether, then go to the same vector, otherwise try spawn Optional optionalIsland = plugin.getIslands().getIslandAt(e.getFrom()); - Location to = optionalIsland.map(i -> i.getSpawnPoint(Environment.NETHER)).orElse(e.getFrom().toVector().toLocation(nether)); + Location to = optionalIsland.map(i -> i.getSpawnPoint(Environment.NETHER)).orElseGet(() -> e.getFrom().toVector().toLocation(nether)); e.setCancelled(true); // Check if there is an island there or not if (plugin.getIWM().isPasteMissingIslands(overWorld) && diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/protection/FireListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/FireListener.java index c40ef6873..07322a213 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/protection/FireListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/FireListener.java @@ -38,7 +38,7 @@ public class FireListener extends FlagListener { return false; } // Check if the island exists and if fire is allowed - boolean cancel = getIslands().getIslandAt(l).map(i -> !i.isAllowed(flag)).orElse(!flag.isSetForWorld(l.getWorld())); + boolean cancel = getIslands().getIslandAt(l).map(i -> !i.isAllowed(flag)).orElseGet(() -> !flag.isSetForWorld(l.getWorld())); e.setCancelled(cancel); return cancel; } diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/protection/TNTListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/TNTListener.java index 2a55db41a..514d255bf 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/protection/TNTListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/TNTListener.java @@ -95,7 +95,7 @@ public class TNTListener extends FlagListener { protected boolean protect(Location location) { return getIslands().getProtectedIslandAt(location).map(i -> !i.isAllowed(Flags.TNT_DAMAGE)) - .orElse(!Flags.WORLD_TNT_DAMAGE.isSetForWorld(location.getWorld())); + .orElseGet(() -> !Flags.WORLD_TNT_DAMAGE.isSetForWorld(location.getWorld())); } /** diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/settings/DecayListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/settings/DecayListener.java index 0932ebd4e..0a262bd44 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/settings/DecayListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/settings/DecayListener.java @@ -29,6 +29,6 @@ public class DecayListener extends FlagListener { Optional island = getIslands().getIslandAt(e.getBlock().getLocation()); // Cancel the event if needed - this means if this is not allowed on the island or in the world. - e.setCancelled(island.map(i -> !i.isAllowed(Flags.LEAF_DECAY)).orElse(!Flags.LEAF_DECAY.isSetForWorld(e.getBlock().getWorld()))); + e.setCancelled(island.map(i -> !i.isAllowed(Flags.LEAF_DECAY)).orElseGet(() -> !Flags.LEAF_DECAY.isSetForWorld(e.getBlock().getWorld()))); } } diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/settings/MobSpawnListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/settings/MobSpawnListener.java index 62433600e..22f9142cb 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/settings/MobSpawnListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/settings/MobSpawnListener.java @@ -54,7 +54,7 @@ public class MobSpawnListener extends FlagListener { boolean cancelNatural = shouldCancel(e.getEntity(), e.getLocation(), Flags.ANIMAL_NATURAL_SPAWN, Flags.MONSTER_NATURAL_SPAWN); e.setCancelled(cancelNatural); return cancelNatural; - // Spawners + // Spawners case SPAWNER: boolean cancelSpawners = shouldCancel(e.getEntity(), e.getLocation(), Flags.ANIMAL_SPAWNERS_SPAWN, Flags.MONSTER_SPAWNERS_SPAWN); e.setCancelled(cancelSpawners); @@ -67,9 +67,9 @@ public class MobSpawnListener extends FlagListener { private boolean shouldCancel(Entity entity, Location loc, Flag animalSpawnFlag, Flag monsterSpawnFlag) { Optional island = getIslands().getIslandAt(loc); if (Util.isHostileEntity(entity) && !(entity instanceof PufferFish)) { - return island.map(i -> !i.isAllowed(monsterSpawnFlag)).orElse(!monsterSpawnFlag.isSetForWorld(entity.getWorld())); + return island.map(i -> !i.isAllowed(monsterSpawnFlag)).orElseGet(() -> !monsterSpawnFlag.isSetForWorld(entity.getWorld())); } else if (Util.isPassiveEntity(entity) || entity instanceof PufferFish) { - return island.map(i -> !i.isAllowed(animalSpawnFlag)).orElse(!animalSpawnFlag.isSetForWorld(entity.getWorld())); + return island.map(i -> !i.isAllowed(animalSpawnFlag)).orElseGet(() -> !animalSpawnFlag.isSetForWorld(entity.getWorld())); } return false; } diff --git a/src/main/java/world/bentobox/bentobox/managers/IslandWorldManager.java b/src/main/java/world/bentobox/bentobox/managers/IslandWorldManager.java index 4f5388a6e..85ec99ef5 100644 --- a/src/main/java/world/bentobox/bentobox/managers/IslandWorldManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/IslandWorldManager.java @@ -659,7 +659,7 @@ public class IslandWorldManager { public List getOnLeaveCommands(@NonNull World world) { return gameModes.containsKey(world) ? gameModes.get(world).getWorldSettings().getOnLeaveCommands() : Collections.emptyList(); } - + /** * Returns a list of commands to execute when the player respawns and {@link Flags#ISLAND_RESPAWN} is true. * @param world the World @@ -678,7 +678,7 @@ public class IslandWorldManager { * @return data folder file object or the plugin's data folder if none found */ public File getDataFolder(@NonNull World world) { - return getAddon(world).map(GameModeAddon::getDataFolder).orElse(plugin.getDataFolder()); + return getAddon(world).map(GameModeAddon::getDataFolder).orElseGet(() -> plugin.getDataFolder()); } /** diff --git a/src/main/java/world/bentobox/bentobox/managers/PlayersManager.java b/src/main/java/world/bentobox/bentobox/managers/PlayersManager.java index 0010ea959..5d5c260c9 100644 --- a/src/main/java/world/bentobox/bentobox/managers/PlayersManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/PlayersManager.java @@ -296,7 +296,7 @@ public class PlayersManager { return playerCache.values().stream() .filter(p -> p.getPlayerName().equalsIgnoreCase(name)).findFirst() .map(p -> UUID.fromString(p.getUniqueId())) - .orElse(names.objectExists(name) ? names.loadObject(name).getUuid() : null); + .orElseGet(() -> names.objectExists(name) ? names.loadObject(name).getUuid() : null); } /** @@ -548,7 +548,7 @@ public class PlayersManager { target.getPlayer().getInventory().clear(); } else { getPlayer(target.getUniqueId()).addToPendingKick(world); - } + } } if (plugin.getSettings().isUseEconomy() && plugin.getIWM().isOnLeaveResetMoney(world)) { diff --git a/src/main/java/world/bentobox/bentobox/util/teleport/SafeSpotTeleport.java b/src/main/java/world/bentobox/bentobox/util/teleport/SafeSpotTeleport.java index 5f69bf60b..27a589ce7 100644 --- a/src/main/java/world/bentobox/bentobox/util/teleport/SafeSpotTeleport.java +++ b/src/main/java/world/bentobox/bentobox/util/teleport/SafeSpotTeleport.java @@ -173,7 +173,7 @@ public class SafeSpotTeleport { */ private List> getChunksToScan() { List> chunksToScan = new ArrayList<>(); - int maxRadius = plugin.getIslands().getIslandAt(location).map(Island::getProtectionRange).orElse(plugin.getIWM().getIslandProtectionRange(location.getWorld())); + int maxRadius = plugin.getIslands().getIslandAt(location).map(Island::getProtectionRange).orElseGet(() -> plugin.getIWM().getIslandProtectionRange(location.getWorld())); maxRadius = Math.min(MAX_RADIUS, maxRadius); int x = location.getBlockX(); int z = location.getBlockZ(); diff --git a/src/test/java/world/bentobox/bentobox/listeners/PortalTeleportationListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/PortalTeleportationListenerTest.java index 0c17490c4..b6fc0afa8 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/PortalTeleportationListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/PortalTeleportationListenerTest.java @@ -439,10 +439,10 @@ public class PortalTeleportationListenerTest { assertTrue(np.onNetherPortal(e)); // Verify assertTrue(e.isCancelled()); - // If nether islands, then to = from but in nether - verify(from).toVector(); - // Do not go to spawn - verify(nether, never()).getSpawnLocation(); + // If nether islands, then to spawn location + verify(island).getSpawnPoint(eq(Environment.NETHER)); + // Do not go to from + verify(from, never()).toVector(); } /**