From 40240c72255392bfefdf7fbcbb71e86f5b39c4d2 Mon Sep 17 00:00:00 2001 From: BONNe Date: Fri, 24 Apr 2020 15:33:44 +0300 Subject: [PATCH] Add AdminSetSpawnPointCommand to change an island's spawn point (#1295) Implements #937. --- .../admin/AdminSetSpawnPointCommand.java | 94 +++++++++++++++++++ src/main/resources/locales/en-US.yml | 6 ++ 2 files changed, 100 insertions(+) create mode 100644 src/main/java/world/bentobox/bentobox/api/commands/admin/AdminSetSpawnPointCommand.java diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminSetSpawnPointCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminSetSpawnPointCommand.java new file mode 100644 index 000000000..37a850723 --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminSetSpawnPointCommand.java @@ -0,0 +1,94 @@ +package world.bentobox.bentobox.api.commands.admin; + + +import org.bukkit.World; +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +import world.bentobox.bentobox.api.commands.CompositeCommand; +import world.bentobox.bentobox.api.commands.ConfirmableCommand; +import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.database.objects.Island; + + +/** + * This command sets spawn point for island at admin location for island on which admin is located. + * This command is only for player entity. + * @author BONNe + * @since 1.13.0 + */ +public class AdminSetSpawnPointCommand extends ConfirmableCommand +{ + /** + * Sub-command constructor + * + * @param parent - the parent composite command + */ + public AdminSetSpawnPointCommand(CompositeCommand parent) + { + super(parent, "setspawnpoint"); + } + + + /** + * {@inheritDoc} + */ + @Override + public void setup() + { + this.setPermission("admin.setspawnpoint"); + this.setOnlyPlayer(true); + this.setDescription("commands.admin.setspawnpoint.description"); + } + + + /** + * This method finds an island in user location and asks confirmation if spawn point + * must be changed to that location. + * {@inheritDoc} + */ + @Override + public boolean execute(User user, String label, List args) + { + Optional optionalIsland = this.getIslands().getIslandAt(user.getLocation()); + + if (optionalIsland.isPresent() && + (optionalIsland.get().hasNetherIsland() || + !World.Environment.NETHER.equals(user.getLocation().getWorld().getEnvironment())) && + (optionalIsland.get().hasEndIsland() || + !World.Environment.THE_END.equals(user.getLocation().getWorld().getEnvironment()))) + { + // Everything's fine, we can set the location as spawn point for island :) + this.askConfirmation(user, user.getTranslation("commands.admin.setspawnpoint.confirmation"), + () -> this.setSpawnPoint(user, optionalIsland.get())); + + return true; + } + else + { + user.sendMessage("commands.admin.setspawnpoint.no-island-here"); + return false; + } + } + + + /** + * This method changes spawn point for island at given user location. + * @param user User who initiate spawn point change. + * @param island Island which is targeted by user. + */ + private void setSpawnPoint(User user, Island island) + { + island.setSpawnPoint(Objects.requireNonNull(user.getLocation().getWorld()).getEnvironment(), + user.getLocation()); + user.sendMessage("commands.admin.setspawnpoint.success"); + + if (!island.isSpawn()) + { + island.getPlayersOnIsland().forEach(player -> + User.getInstance(player).sendMessage("commands.admin.setspawnpoint.island-spawnpoint-changed", + "[user]", user.getName())); + } + } +} diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index a627a0f7d..9e5623b07 100644 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -246,6 +246,12 @@ commands: no-island-here: "&c There is no island here." confirmation: "&c Are you sure you want to set this island as the spawn for this world?" success: "&a Successfully set this island as the spawn for this world." + setspawnpoint: + description: "set current location as spawn point for this island" + no-island-here: "&c There is no island here." + confirmation: "&c Are you sure you want to set this location as the spawn point for this island?" + success: "&a Successfully set this location as the spawn point for this island." + island-spawnpoint-changed: "&a [user] changed this island spawn point." settings: parameters: "[player]" description: "open system settings or island settings for player"