From 9935a7b2d1d5cf779455270db3d61507ff8ca217 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Fri, 5 Feb 2021 11:38:12 +0000 Subject: [PATCH] Added /randomenchant --- .../commands/CommandRandomenchant.java | 82 +++++++++++++++++++ .../ecoenchants/enchantments/EcoEnchant.java | 7 +- .../core-plugin/src/main/resources/lang.yml | 3 + .../core-plugin/src/main/resources/plugin.yml | 7 ++ 4 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/command/commands/CommandRandomenchant.java 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 new file mode 100644 index 00000000..fa718fde --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/command/commands/CommandRandomenchant.java @@ -0,0 +1,82 @@ +package com.willfp.ecoenchants.command.commands; + +import com.willfp.eco.util.command.AbstractCommand; +import com.willfp.eco.util.plugin.AbstractEcoPlugin; +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.Material; +import org.bukkit.command.CommandSender; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.EnchantmentStorageMeta; +import org.bukkit.inventory.meta.ItemMeta; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class CommandRandomenchant extends AbstractCommand { + /** + * Instantiate a new /ecoreload command handler. + * + * @param plugin The plugin for the commands to listen for. + */ + public CommandRandomenchant(@NotNull final AbstractEcoPlugin plugin) { + super(plugin, "randomenchant", "ecoenchants.randomenchant", true); + } + + @Override + public void onExecute(@NotNull final CommandSender sender, + @NotNull final List args) { + Player player = (Player) sender; + 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")); + return; + } + + List ecoEnchants = new ArrayList<>(EcoEnchants.values()); + Collections.shuffle(ecoEnchants); + EcoEnchant enchant = null; + + List onItem = new ArrayList<>(); + + if (meta instanceof EnchantmentStorageMeta) { + onItem.addAll(((EnchantmentStorageMeta) meta).getStoredEnchants().keySet()); + } else { + onItem.addAll(meta.getEnchants().keySet()); + } + + for (EcoEnchant ecoEnchant : ecoEnchants) { + if (ecoEnchant.canEnchantItem(itemStack)) { + if (!ecoEnchant.conflictsWithAny(onItem)) { + if (!onItem.stream().anyMatch(enchantment -> enchantment.conflictsWith(ecoEnchant))) { + enchant = ecoEnchant; + } + } + } + } + + if (enchant == null) { + player.sendMessage(this.getPlugin().getLangYml().getMessage("no-enchants-available")); + return; + } + + if (meta instanceof EnchantmentStorageMeta) { + ((EnchantmentStorageMeta) meta).addStoredEnchant(enchant, enchant.getMaxLevel(), true); + } else { + meta.addEnchant(enchant, enchant.getMaxLevel(), true); + } + + itemStack.setItemMeta(meta); + String message = this.getPlugin().getLangYml().getMessage("applied-random-enchant"); + message = message.replace("%enchantment%", EnchantmentCache.getEntry(enchant).getName() + "§r"); + player.sendMessage(message); + } +} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchant.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchant.java index 8df4824c..f3fa21af 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchant.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchant.java @@ -30,6 +30,7 @@ import org.jetbrains.annotations.NotNull; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -266,12 +267,12 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist } /** - * If enchantment conflicts with any enchantment in set. + * If enchantment conflicts with any enchantment in collection. * - * @param enchantments The set to test against. + * @param enchantments The collection to test against. * @return If there are any conflicts. */ - public boolean conflictsWithAny(@NotNull final Set enchantments) { + public boolean conflictsWithAny(@NotNull final Collection enchantments) { return conflicts.stream().anyMatch(enchantments::contains); } diff --git a/eco-core/core-plugin/src/main/resources/lang.yml b/eco-core/core-plugin/src/main/resources/lang.yml index 85e874bf..d862e2f1 100644 --- a/eco-core/core-plugin/src/main/resources/lang.yml +++ b/eco-core/core-plugin/src/main/resources/lang.yml @@ -18,6 +18,9 @@ messages: outdated: "You are running an outdated version of &aEcoEnchants! &fCurrently running &e%ver%&f, newest version &e%newver%&f. Download at &a&lhttps://www.spigotmc.org/resources/ecoenchants.79573/" on-cooldown: "&fThis spell is on cooldown! Wait &a%seconds% Seconds&f to use &a%name%&f again." used-spell: "&fUsed &a%name%&f spell!" + 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!" 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 82844827..0594275c 100644 --- a/eco-core/core-plugin/src/main/resources/plugin.yml +++ b/eco-core/core-plugin/src/main/resources/plugin.yml @@ -33,6 +33,9 @@ commands: enchantinfo: description: Show information about an enchantment permission: ecoenchants.enchantinfo + randomenchant: + description: Adds a random enchantment to an item + permission: ecoenchants.randomenchant permissions: ecoenchants.*: @@ -44,6 +47,7 @@ permissions: ecoenchants.updateannounce: true ecoenchants.enchantinfo: true ecoenchants.ecodebug: true + ecoenchants.randomenchant: true ecoenchants.anvil.*: true ecoenchants.anvil.*: description: Allows all anvil permissions @@ -67,6 +71,9 @@ permissions: ecoenchants.enchantinfo: description: Allows the use of /enchantinfo to show enchant info default: true + ecoenchants.randomenchant: + description: Allows the use of /randomenchant to apply a random enchantment to an item + default: op ecoenchants.anvil.bypasshardcap: description: Allows bypassing the anvil hard cap default: op