Added /randomenchant for other players

This commit is contained in:
Auxilor 2021-03-31 19:12:14 +01:00
parent 59ec5b010b
commit b1ccd9ab31
4 changed files with 85 additions and 3 deletions

View File

@ -1,11 +1,14 @@
package com.willfp.ecoenchants.command.commands; package com.willfp.ecoenchants.command.commands;
import com.willfp.eco.util.command.AbstractCommand; import com.willfp.eco.util.command.AbstractCommand;
import com.willfp.eco.util.command.AbstractTabCompleter;
import com.willfp.eco.util.plugin.AbstractEcoPlugin; import com.willfp.eco.util.plugin.AbstractEcoPlugin;
import com.willfp.ecoenchants.command.tabcompleters.TabCompleterRandomEnchant;
import com.willfp.ecoenchants.display.EnchantmentCache; import com.willfp.ecoenchants.display.EnchantmentCache;
import com.willfp.ecoenchants.enchantments.EcoEnchant; import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants; import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentTarget; import com.willfp.ecoenchants.enchantments.meta.EnchantmentTarget;
import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
@ -26,18 +29,39 @@ public class CommandRandomenchant extends AbstractCommand {
* @param plugin The plugin for the commands to listen for. * @param plugin The plugin for the commands to listen for.
*/ */
public CommandRandomenchant(@NotNull final AbstractEcoPlugin plugin) { 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 @Override
public void onExecute(@NotNull final CommandSender sender, public void onExecute(@NotNull final CommandSender sender,
@NotNull final List<String> args) { @NotNull final List<String> 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(); ItemStack itemStack = player.getInventory().getItemInMainHand();
ItemMeta meta = itemStack.getItemMeta(); ItemMeta meta = itemStack.getItemMeta();
if (itemStack.getType() == Material.AIR || meta == null || !EnchantmentTarget.ALL.getMaterials().contains(itemStack.getType())) { if (itemStack.getType() == Material.AIR || meta == null || !EnchantmentTarget.ALL.getMaterials().contains(itemStack.getType())) {
if (player.equals(sender)) {
player.sendMessage(this.getPlugin().getLangYml().getMessage("must-hold-item")); player.sendMessage(this.getPlugin().getLangYml().getMessage("must-hold-item"));
} else {
sender.sendMessage(this.getPlugin().getLangYml().getMessage("must-hold-item-other"));
}
return; return;
} }

View File

@ -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<String> onTab(@NotNull final CommandSender sender,
@NotNull final List<String> args) {
List<String> completions = new ArrayList<>();
List<String> 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<>();
}
}

View File

@ -20,6 +20,8 @@ messages:
must-hold-item: "&cYou must be holding an enchantable item!" must-hold-item: "&cYou must be holding an enchantable item!"
no-enchants-available: "&fNo available enchantments found for this item!" no-enchants-available: "&fNo available enchantments found for this item!"
applied-random-enchant: "&fAdded %enchantment% to your 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-targets: "&cCannot be applied"
no-conflicts: "&cNo conflicts" no-conflicts: "&cNo conflicts"

View File

@ -75,6 +75,9 @@ permissions:
ecoenchants.randomenchant: ecoenchants.randomenchant:
description: Allows the use of /randomenchant to apply a random enchantment to an item description: Allows the use of /randomenchant to apply a random enchantment to an item
default: op 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: ecoenchants.randomenchant.bypasshardcap:
description: Allows /randomenchant bypassing the anvil hard cap description: Allows /randomenchant bypassing the anvil hard cap
default: op default: op