From 992ca8c8fd57ea87074596cc32942682b9645974 Mon Sep 17 00:00:00 2001 From: SamB440 Date: Tue, 26 Oct 2021 19:26:47 +0100 Subject: [PATCH] Allow console to run /rpgr add --- .../commands/RPGRegionsCommand.java | 51 ++++++++++++++----- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/rpgregions/src/main/java/net/islandearth/rpgregions/commands/RPGRegionsCommand.java b/rpgregions/src/main/java/net/islandearth/rpgregions/commands/RPGRegionsCommand.java index aae6a37..1900597 100644 --- a/rpgregions/src/main/java/net/islandearth/rpgregions/commands/RPGRegionsCommand.java +++ b/rpgregions/src/main/java/net/islandearth/rpgregions/commands/RPGRegionsCommand.java @@ -23,8 +23,11 @@ import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.OfflinePlayer; +import org.bukkit.World; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.File; import java.io.FileReader; @@ -81,26 +84,46 @@ public class RPGRegionsCommand extends BaseCommand { @Subcommand("add") @CommandPermission("rpgregions.add") @CommandCompletion("@integration-regions") - public void onAdd(Player player, String region) { - if (!plugin.getManagers().getIntegrationManager().exists(player.getLocation().getWorld(), region)) { - player.sendMessage(StringUtils.colour("&cThat region does not exist in your protection plugin.")); - return; - } - + public void onAdd(CommandSender sender, String region, @co.aikar.commands.annotation.Optional @Nullable String worldName) { if (plugin.getManagers().getRegionsCache().getConfiguredRegion(region).isPresent()) { - player.sendMessage(StringUtils.colour("&cThat region is already configured.")); + sender.sendMessage(StringUtils.colour("&cThat region is already configured.")); return; } - ConfiguredRegion configuredRegion = new ConfiguredRegion(player.getWorld(), region, region, new ArrayList<>(), - new ArrayList<>()); - configuredRegion.setLocation(player.getLocation()); - plugin.getManagers().getRegionsCache().addConfiguredRegion(configuredRegion); - player.sendMessage(StringUtils.colour("&aAdded configured region " + region + "!")); - player.sendMessage(StringUtils.colour("&e&oNow use /rpgregions edit " + Location location = null; + if (sender instanceof Player player) { + location = player.getLocation(); + } + + final World world = worldName == null ? null : Bukkit.getWorld(worldName); + if (world != null) { + if (location == null) location = new Location(world, 0, 100, 0); + else location.setWorld(world); + } + + if (location == null) { + sender.sendMessage(ChatColor.RED + "Console needs to provide a world name!"); + return; + } + + if (!plugin.getManagers().getIntegrationManager().exists(location.getWorld(), region)) { + sender.sendMessage(StringUtils.colour("&cThat region does not exist in your protection plugin.")); + return; + } + + add(location, region); + sender.sendMessage(StringUtils.colour("&aAdded configured region " + region + "!")); + sender.sendMessage(StringUtils.colour("&e&oNow use /rpgregions edit " + region + " to edit it!")); - player.sendMessage(StringUtils.colour("&e&oUse /rpgregions save to save this to file for editing.")); + sender.sendMessage(StringUtils.colour("&e&oUse /rpgregions save to save this to file for editing.")); + } + + private void add(@NotNull final Location location, final String region) { + ConfiguredRegion configuredRegion = new ConfiguredRegion(location.getWorld(), region, region, new ArrayList<>(), + new ArrayList<>()); + configuredRegion.setLocation(location); + plugin.getManagers().getRegionsCache().addConfiguredRegion(configuredRegion); } @Subcommand("remove")