From 2bf6bf474d34baa115702b4f45f85b621494bd6a Mon Sep 17 00:00:00 2001 From: Auxilor Date: Fri, 12 Mar 2021 21:15:07 +0000 Subject: [PATCH] Added /ebspawn --- .../com/willfp/ecobosses/EcoBossesPlugin.java | 4 +- .../ecobosses/commands/CommandEbspawn.java | 143 ++++++++++++++++++ .../commands/TabCompleterEbspawn.java | 105 +++++++++++++ .../core-plugin/src/main/resources/lang.yml | 5 +- .../core-plugin/src/main/resources/plugin.yml | 8 + 5 files changed, 263 insertions(+), 2 deletions(-) create mode 100644 eco-core/core-plugin/src/main/java/com/willfp/ecobosses/commands/CommandEbspawn.java create mode 100644 eco-core/core-plugin/src/main/java/com/willfp/ecobosses/commands/TabCompleterEbspawn.java diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/EcoBossesPlugin.java b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/EcoBossesPlugin.java index 4de5f88..5e491f6 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/EcoBossesPlugin.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/EcoBossesPlugin.java @@ -11,6 +11,7 @@ import com.willfp.ecobosses.bosses.listeners.DeathListeners; import com.willfp.ecobosses.bosses.listeners.SpawnListeners; import com.willfp.ecobosses.commands.CommandEbdrop; import com.willfp.ecobosses.commands.CommandEbreload; +import com.willfp.ecobosses.commands.CommandEbspawn; import lombok.Getter; import org.bukkit.event.Listener; import org.jetbrains.annotations.Nullable; @@ -94,7 +95,8 @@ public class EcoBossesPlugin extends AbstractEcoPlugin { public List getCommands() { return Arrays.asList( new CommandEbreload(this), - new CommandEbdrop(this) + new CommandEbdrop(this), + new CommandEbspawn(this) ); } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/commands/CommandEbspawn.java b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/commands/CommandEbspawn.java new file mode 100644 index 0000000..2972520 --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/commands/CommandEbspawn.java @@ -0,0 +1,143 @@ +package com.willfp.ecobosses.commands; + +import com.willfp.eco.util.command.AbstractCommand; +import com.willfp.eco.util.command.AbstractTabCompleter; +import com.willfp.ecobosses.EcoBossesPlugin; +import com.willfp.ecobosses.bosses.EcoBoss; +import com.willfp.ecobosses.bosses.EcoBosses; +import org.bukkit.Bukkit; +import org.bukkit.Location; +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.util.List; + +public class CommandEbspawn extends AbstractCommand { + /** + * Instantiate a new executor for /ebspawn. + * + * @param plugin The plugin to manage the execution for. + */ + public CommandEbspawn(@NotNull final EcoBossesPlugin plugin) { + super(plugin, "ebspawn", "ecobosses.ebspawn", false); + } + + @Override + @Nullable + public AbstractTabCompleter getTab() { + return new TabCompleterEbspawn(this); + } + + @Override + public void onExecute(@NotNull final CommandSender sender, + @NotNull final List args) { + if (args.isEmpty()) { + sender.sendMessage(this.getPlugin().getLangYml().getMessage("specify-boss")); + return; + } + + String bossName = args.get(0); + + EcoBoss boss = EcoBosses.getByName(bossName.toLowerCase()); + + Location location = null; + + if (sender instanceof Player) { + location = ((Player) sender).getLocation(); + } + + if (args.size() >= 4) { + String xString = args.get(1); + String yString = args.get(2); + String zString = args.get(3); + + double xPos; + double yPos; + double zPos; + + if (xString.startsWith("~") && sender instanceof Player) { + String xDiff = xString.replace("~", ""); + String yDiff = yString.replace("~", ""); + String zDiff = zString.replace("~", ""); + + if (xDiff.isEmpty()) { + xPos = ((Player) sender).getLocation().getX(); + } else { + try { + xPos = ((Player) sender).getLocation().getX() + Double.parseDouble(xDiff); + } catch (NumberFormatException e) { + xPos = ((Player) sender).getLocation().getX(); + } + } + + if (yDiff.isEmpty()) { + yPos = ((Player) sender).getLocation().getY(); + } else { + try { + yPos = ((Player) sender).getLocation().getY() + Double.parseDouble(yDiff); + } catch (NumberFormatException e) { + yPos = ((Player) sender).getLocation().getY(); + } + } + + if (zDiff.isEmpty()) { + zPos = ((Player) sender).getLocation().getZ(); + } else { + try { + zPos = ((Player) sender).getLocation().getZ() + Double.parseDouble(yDiff); + } catch (NumberFormatException e) { + zPos = ((Player) sender).getLocation().getZ(); + } + } + } else { + try { + xPos = Double.parseDouble(xString); + } catch (NumberFormatException e) { + sender.sendMessage(this.getPlugin().getLangYml().getMessage("invalid-location")); + return; + } + try { + yPos = Double.parseDouble(yString); + } catch (NumberFormatException e) { + sender.sendMessage(this.getPlugin().getLangYml().getMessage("invalid-location")); + return; + } + try { + zPos = Double.parseDouble(zString); + } catch (NumberFormatException e) { + sender.sendMessage(this.getPlugin().getLangYml().getMessage("invalid-location")); + return; + } + } + + location = new Location(null, xPos, yPos, zPos); + } + + World world = null; + if (sender instanceof Player) { + world = ((Player) sender).getWorld(); + } + + if (args.size() >= 5) { + world = Bukkit.getWorld(args.get(4)); + } + + if (location == null) { + sender.sendMessage(this.getPlugin().getLangYml().getMessage("invalid-location")); + return; + } + + location.setWorld(world); + + if (world == null) { + sender.sendMessage(this.getPlugin().getLangYml().getMessage("invalid-world")); + return; + } + + boss.spawn(location); + sender.sendMessage(this.getPlugin().getLangYml().getMessage("spawned")); + } +} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/commands/TabCompleterEbspawn.java b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/commands/TabCompleterEbspawn.java new file mode 100644 index 0000000..9af0839 --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/commands/TabCompleterEbspawn.java @@ -0,0 +1,105 @@ +package com.willfp.ecobosses.commands; + +import com.willfp.eco.util.command.AbstractTabCompleter; +import com.willfp.eco.util.config.updating.annotations.ConfigUpdater; +import com.willfp.ecobosses.bosses.EcoBoss; +import com.willfp.ecobosses.bosses.EcoBosses; +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.command.CommandSender; +import org.bukkit.util.StringUtil; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +public class TabCompleterEbspawn extends AbstractTabCompleter { + /** + * The cached names. + */ + private static final List BOSS_NAMES = new ArrayList<>(); + + /** + * The cached numbers. + */ + private static final List TILDE = Arrays.asList( + "~" + ); + + /** + * Instantiate a new tab-completer for /ebspawn. + * + * @param command Instance of /ebspawn. + */ + public TabCompleterEbspawn(@NotNull final CommandEbspawn command) { + super(command); + reload(); + } + + /** + * Called on reload. + */ + @ConfigUpdater + public static void reload() { + BOSS_NAMES.clear(); + BOSS_NAMES.addAll(EcoBosses.values().stream().map(EcoBoss::getName).collect(Collectors.toList())); + } + + /** + * The execution of the tabcompleter. + * + * @param sender The sender of the command. + * @param args The arguments of the command. + * @return A list of tab-completions. + */ + @Override + public List onTab(@NotNull final CommandSender sender, + @NotNull final List args) { + List completions = new ArrayList<>(); + + if (args.isEmpty()) { + // Currently, this case is not ever reached + return new ArrayList<>(); + } + + if (args.size() == 1) { + StringUtil.copyPartialMatches(args.get(0), BOSS_NAMES, completions); + + Collections.sort(completions); + return completions; + } + + if (args.size() == 2) { + StringUtil.copyPartialMatches(args.get(1), TILDE, completions); + + Collections.sort(completions); + return completions; + } + + if (args.size() == 3) { + StringUtil.copyPartialMatches(args.get(2), TILDE, completions); + + Collections.sort(completions); + return completions; + } + + if (args.size() == 4) { + StringUtil.copyPartialMatches(args.get(3), TILDE, completions); + + Collections.sort(completions); + return completions; + } + + if (args.size() == 5) { + StringUtil.copyPartialMatches(args.get(4), Bukkit.getWorlds().stream().map(World::getName).collect(Collectors.toList()), completions); + + Collections.sort(completions); + return completions; + } + + return new ArrayList<>(0); + } +} diff --git a/eco-core/core-plugin/src/main/resources/lang.yml b/eco-core/core-plugin/src/main/resources/lang.yml index d19884a..9efb0f9 100644 --- a/eco-core/core-plugin/src/main/resources/lang.yml +++ b/eco-core/core-plugin/src/main/resources/lang.yml @@ -3,4 +3,7 @@ messages: no-permission: "&cYou don't have permission to do this!" not-player: "&cThis command must be run by a player" reloaded: "Reloaded!" - sent-drop: "Check console for the drop!" \ No newline at end of file + sent-drop: "Check console for the drop!" + specify-boss: "&cYou must specify a valid boss!" + invalid-location: "&cInvalid location!" + spawned: "Spawned a boss!" \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/resources/plugin.yml b/eco-core/core-plugin/src/main/resources/plugin.yml index 4a1d676..519b745 100644 --- a/eco-core/core-plugin/src/main/resources/plugin.yml +++ b/eco-core/core-plugin/src/main/resources/plugin.yml @@ -13,6 +13,9 @@ commands: ebdrop: description: Sends the held item to console to be added as a drop permission: ecobosses.ebdrop + ebspawn: + description: Spawns a boss + permission: ecobosses.ebspawn permissions: ecobosses.*: @@ -21,6 +24,7 @@ permissions: children: ecobosses.reload: true ecobosses.ebdrop: true + ecobosses.ebspawn: true ecobosses.reload: description: Allows reloading the config @@ -28,4 +32,8 @@ permissions: ecobosses.ebdrop: description: Allows the use of /ebdrop + default: op + + ecobosses.ebspawn: + description: Allows the use of /ebspawn default: op \ No newline at end of file