From 7525bc05e23bb99adb4d9e54a359bcd41367b4fe Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Sun, 20 Jan 2019 09:22:26 +0100 Subject: [PATCH] Added IslandsManager#spawnTeleport() and made #getSpawn(World) return an optional --- .../bentobox/managers/IslandsManager.java | 55 ++++++++++++++++--- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java b/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java index 0f6f3cd7e..5c74f71f4 100644 --- a/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java @@ -465,12 +465,13 @@ public class IslandsManager { } /** - * Get the island that is defined as spawn in this world + * Gets the island that is defined as spawn in this world * @param world world - * @return island or null + * @return optional island, may be empty */ - public Island getSpawn(World world){ - return spawn.get(world); + @NonNull + public Optional getSpawn(@NonNull World world){ + return Optional.ofNullable(spawn.get(world)); } /** @@ -609,6 +610,46 @@ public class IslandsManager { } } + /** + * Teleports the player to the spawn location for this world + * @param world world + * @param player player to teleport + * @since 1.1 + */ + public void spawnTeleport(@NonNull World world, @NonNull Player player) { + User user = User.getInstance(player); + Optional spawn = getSpawn(world); + + if (!spawn.isPresent()) { + // There is no spawn here. + user.sendMessage("commands.island.spawn.no-spawn"); + } else { + // Teleport the player to the spawn + + // Stop any gliding + player.setGliding(false); + // Check if the player is a passenger in a boat + if (player.isInsideVehicle()) { + Entity boat = player.getVehicle(); + if (boat instanceof Boat) { + player.leaveVehicle(); + // Remove the boat so they don't lie around everywhere + boat.remove(); + player.getInventory().addItem(new ItemStack(Material.getMaterial(((Boat) boat).getWoodType().toString() + "_BOAT"), 1)); + player.updateInventory(); + } + } + + user.sendMessage("commands.island.spawn.teleporting"); + player.teleport(spawn.get().getSpawnPoint(World.Environment.NORMAL)); + + // If the player is in SPECTATOR gamemode, reset it to default + if (player.getGameMode().equals(GameMode.SPECTATOR)) { + player.setGameMode(plugin.getIWM().getDefaultGameMode(world)); + } + } + } + /** * Indicates whether a player is at an island spawn or not * @@ -626,11 +667,7 @@ public class IslandsManager { * @param spawn the Island to set as spawn. * Must not be null. */ - public void setSpawn(Island spawn) { - if (spawn == null) { - return; - } - + public void setSpawn(@NonNull Island spawn) { // Checking if there is already a spawn set for this world if (this.spawn.containsKey(spawn.getWorld()) && this.spawn.get(spawn.getWorld()) != null) { Island oldSpawn = this.spawn.get(spawn.getWorld());