Added /randomenchant

This commit is contained in:
Auxilor 2021-02-05 11:38:12 +00:00
parent 539e24d08c
commit 9935a7b2d1
4 changed files with 96 additions and 3 deletions

View File

@ -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<String> 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<EcoEnchant> ecoEnchants = new ArrayList<>(EcoEnchants.values());
Collections.shuffle(ecoEnchants);
EcoEnchant enchant = null;
List<Enchantment> 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);
}
}

View File

@ -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<? extends Enchantment> enchantments) {
public boolean conflictsWithAny(@NotNull final Collection<? extends Enchantment> enchantments) {
return conflicts.stream().anyMatch(enchantments::contains);
}

View File

@ -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"

View File

@ -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