From cafb41567f68903cf70f8085e0bf48e5f3754f9c Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sat, 14 Aug 2021 13:19:28 +0100 Subject: [PATCH] Added /ecobosses give --- .../ecobosses/commands/CommandEcobosses.java | 3 +- .../ecobosses/commands/CommandGive.java | 151 ++++++++++++++++++ .../core-plugin/src/main/resources/lang.yml | 5 + .../core-plugin/src/main/resources/plugin.yml | 5 + 4 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 eco-core/core-plugin/src/main/java/com/willfp/ecobosses/commands/CommandGive.java diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/commands/CommandEcobosses.java b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/commands/CommandEcobosses.java index 514ab3c..92ebf10 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/commands/CommandEcobosses.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/commands/CommandEcobosses.java @@ -16,7 +16,8 @@ public class CommandEcobosses extends PluginCommand { this.addSubcommand(new CommandReload(plugin)) .addSubcommand(new CommandKillall(plugin)) .addSubcommand(new CommandSpawn(plugin)) - .addSubcommand(new CommandBase64(plugin)); + .addSubcommand(new CommandBase64(plugin)) + .addSubcommand(new CommandGive(plugin)); } @Override diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/commands/CommandGive.java b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/commands/CommandGive.java new file mode 100644 index 0000000..b650e29 --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/commands/CommandGive.java @@ -0,0 +1,151 @@ +package com.willfp.ecobosses.commands; + + +import com.willfp.eco.core.EcoPlugin; +import com.willfp.eco.core.command.CommandHandler; +import com.willfp.eco.core.command.TabCompleteHandler; +import com.willfp.eco.core.command.impl.Subcommand; +import com.willfp.eco.core.config.updating.ConfigUpdater; +import com.willfp.ecobosses.bosses.EcoBoss; +import com.willfp.ecobosses.bosses.EcoBosses; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +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 CommandGive extends Subcommand { + /** + * The cached names. + */ + private static final List BOSS_NAMES = new ArrayList<>(); + + /** + * The cached numbers. + */ + private static final List NUMBERS = Arrays.asList( + "1", + "2", + "3", + "4", + "5", + "10", + "32", + "64" + ); + + /** + * Instantiate a new command handler. + * + * @param plugin The plugin for the commands to listen for. + */ + public CommandGive(@NotNull final EcoPlugin plugin) { + super(plugin, "give", "ecobosses.command.give", false); + } + + /** + * Called on reload. + */ + @ConfigUpdater + public static void reload() { + BOSS_NAMES.clear(); + BOSS_NAMES.addAll(EcoBosses.values().stream() + .filter(boss -> boss.getSpawnEgg() != null) + .map(EcoBoss::getName) + .collect(Collectors.toList())); + } + + @Override + public CommandHandler getHandler() { + return (sender, args) -> { + if (args.isEmpty()) { + sender.sendMessage(this.getPlugin().getLangYml().getMessage("needs-player")); + return; + } + + if (args.size() == 1) { + sender.sendMessage(this.getPlugin().getLangYml().getMessage("needs-boss")); + return; + } + + int amount = 1; + + if (args.size() > 2) { + try { + amount = Integer.parseInt(args.get(2)); + } catch (NumberFormatException ignored) { + // do nothing + } + } + + String recieverName = args.get(0); + Player reciever = Bukkit.getPlayer(recieverName); + + if (reciever == null) { + sender.sendMessage(this.getPlugin().getLangYml().getMessage("invalid-player")); + return; + } + + String key = args.get(1); + + EcoBoss boss = EcoBosses.getByName(key); + + if (boss == null || boss.getSpawnEgg() == null) { + sender.sendMessage(this.getPlugin().getLangYml().getMessage("invalid-boss")); + return; + } + + String message = this.getPlugin().getLangYml().getMessage("give-success"); + message = message.replace("%boss%", boss.getName()).replace("%recipient%", reciever.getName()); + sender.sendMessage(message); + + ItemStack itemStack = boss.getSpawnEgg(); + itemStack.setAmount(amount); + reciever.getInventory().addItem(itemStack); + }; + } + + @Override + public TabCompleteHandler getTabCompleter() { + return (sender, args) -> { + List completions = new ArrayList<>(); + + if (args.isEmpty()) { + // Currently, this case is not ever reached + return BOSS_NAMES; + } + + if (args.size() == 1) { + StringUtil.copyPartialMatches(args.get(0), Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList()), completions); + return completions; + } + + if (args.size() == 2) { + StringUtil.copyPartialMatches(args.get(1), BOSS_NAMES, completions); + + Collections.sort(completions); + return completions; + } + + if (args.size() == 3) { + StringUtil.copyPartialMatches(args.get(2), NUMBERS, completions); + + completions.sort((s1, s2) -> { + int t1 = Integer.parseInt(s1); + int t2 = Integer.parseInt(s2); + return t1 - t2; + }); + + return completions; + } + + return new ArrayList<>(0); + }; + } +} \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/resources/lang.yml b/eco-core/core-plugin/src/main/resources/lang.yml index 03d0ab1..3dfbcf6 100644 --- a/eco-core/core-plugin/src/main/resources/lang.yml +++ b/eco-core/core-plugin/src/main/resources/lang.yml @@ -9,5 +9,10 @@ messages: invalid-location: "&cInvalid location!" spawned: "Spawned a boss!" killall: "Killed &a%amount%&f bosses!" + needs-player: "&cYou must specify a player" + invalid-player: "&cInvalid player!" + needs-stone: "&cYou must specify a boss" + invalid-stone: "&cInvalid boss!" + give-success: "Gave &a%boss%&r reforge stone to &a%recipient%" na: "N/A" \ 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 1506f38..a83f2a1 100644 --- a/eco-core/core-plugin/src/main/resources/plugin.yml +++ b/eco-core/core-plugin/src/main/resources/plugin.yml @@ -27,6 +27,7 @@ permissions: ecobosses.command.reload: true ecobosses.command.spawn: true ecobosses.command.base64: true + ecobosses.command.give: true ecobosses.command.ecobosses: description: Allows the use of /ecobosses @@ -36,6 +37,10 @@ permissions: description: Allows the use of /ecobosses base64 default: op + ecobosses.command.give: + description: Allows the use of /ecobosses give + default: op + ecobosses.command.spawn: description: Allows the use of /ecobosses spawn default: op