diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/command/commands/CommandRandomenchant.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/command/commands/CommandRandomenchant.java index a9bd3dc7..021f6aea 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/command/commands/CommandRandomenchant.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/command/commands/CommandRandomenchant.java @@ -1,11 +1,14 @@ package com.willfp.ecoenchants.command.commands; import com.willfp.eco.util.command.AbstractCommand; +import com.willfp.eco.util.command.AbstractTabCompleter; import com.willfp.eco.util.plugin.AbstractEcoPlugin; +import com.willfp.ecoenchants.command.tabcompleters.TabCompleterRandomEnchant; import com.willfp.ecoenchants.display.EnchantmentCache; import com.willfp.ecoenchants.enchantments.EcoEnchant; import com.willfp.ecoenchants.enchantments.EcoEnchants; import com.willfp.ecoenchants.enchantments.meta.EnchantmentTarget; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.command.CommandSender; import org.bukkit.enchantments.Enchantment; @@ -26,18 +29,39 @@ public class CommandRandomenchant extends AbstractCommand { * @param plugin The plugin for the commands to listen for. */ public CommandRandomenchant(@NotNull final AbstractEcoPlugin plugin) { - super(plugin, "randomenchant", "ecoenchants.randomenchant", true); + super(plugin, "randomenchant", "ecoenchants.randomenchant", false); + } + + @Override + public AbstractTabCompleter getTab() { + return new TabCompleterRandomEnchant(this); } @Override public void onExecute(@NotNull final CommandSender sender, @NotNull final List args) { - Player player = (Player) sender; + Player player; + + if ((args.isEmpty() && sender instanceof Player) || !sender.hasPermission("ecoenchants.randomenchant.others")) { + player = (Player) sender; + } else { + player = Bukkit.getServer().getPlayer(args.get(0)); + } + + if (player == null) { + sender.sendMessage(this.getPlugin().getLangYml().getMessage("invalid-player")); + return; + } + ItemStack itemStack = player.getInventory().getItemInMainHand(); ItemMeta meta = itemStack.getItemMeta(); if (itemStack.getType() == Material.AIR || meta == null || !EnchantmentTarget.ALL.getMaterials().contains(itemStack.getType())) { - player.sendMessage(this.getPlugin().getLangYml().getMessage("must-hold-item")); + if (player.equals(sender)) { + player.sendMessage(this.getPlugin().getLangYml().getMessage("must-hold-item")); + } else { + sender.sendMessage(this.getPlugin().getLangYml().getMessage("must-hold-item-other")); + } return; } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/command/tabcompleters/TabCompleterRandomEnchant.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/command/tabcompleters/TabCompleterRandomEnchant.java new file mode 100644 index 00000000..3f7a4a3f --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/command/tabcompleters/TabCompleterRandomEnchant.java @@ -0,0 +1,53 @@ +package com.willfp.ecoenchants.command.tabcompleters; + +import com.willfp.eco.util.command.AbstractCommand; +import com.willfp.eco.util.command.AbstractTabCompleter; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.util.StringUtil; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +public class TabCompleterRandomEnchant extends AbstractTabCompleter { + /** + * Instantiate a new tab-completer for /randomenchant. + * + * @param command /randomenchant. + */ + public TabCompleterRandomEnchant(@NotNull final AbstractCommand command) { + super(command); + } + + /** + * 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<>(); + + List playerNames = Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList()); + + if (args.isEmpty() || !sender.hasPermission("ecoenchants.randomenchant.others")) { + // Currently, this case is not ever reached + return playerNames; + } + + if (args.size() == 1) { + StringUtil.copyPartialMatches(String.join(" ", args), playerNames, completions); + Collections.sort(completions); + return completions; + } + + return new ArrayList<>(); + } +} diff --git a/eco-core/core-plugin/src/main/resources/lang.yml b/eco-core/core-plugin/src/main/resources/lang.yml index fd130b75..c37f2f44 100644 --- a/eco-core/core-plugin/src/main/resources/lang.yml +++ b/eco-core/core-plugin/src/main/resources/lang.yml @@ -20,6 +20,8 @@ messages: must-hold-item: "&cYou must be holding an enchantable item!" no-enchants-available: "&fNo available enchantments found for this item!" applied-random-enchant: "&fAdded %enchantment% to your item!" + invalid-player: "&cInvalid Player!" + must-hold-item-other: "&cPlayer is not holding an enchantable item!" no-targets: "&cCannot be applied" no-conflicts: "&cNo conflicts" diff --git a/eco-core/core-plugin/src/main/resources/plugin.yml b/eco-core/core-plugin/src/main/resources/plugin.yml index eba43b72..a0007306 100644 --- a/eco-core/core-plugin/src/main/resources/plugin.yml +++ b/eco-core/core-plugin/src/main/resources/plugin.yml @@ -75,6 +75,9 @@ permissions: ecoenchants.randomenchant: description: Allows the use of /randomenchant to apply a random enchantment to an item default: op + ecoenchants.randomenchant.others: + description: Allows the use of /randomenchant to apply a random enchantment to an item for another player + default: op ecoenchants.randomenchant.bypasshardcap: description: Allows /randomenchant bypassing the anvil hard cap default: op