mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +02:00
Renamed WrappedEnchantment to CAEnchantment (CustomAnvilEnchantment).
created an interface out of CAEnchantment and moved the implementation to CAEnchantmentBase.
This commit is contained in:
parent
fa4752ea67
commit
dafe595c5b
20 changed files with 575 additions and 463 deletions
228
src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantment.java
Normal file
228
src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantment.java
Normal file
|
|
@ -0,0 +1,228 @@
|
||||||
|
package xyz.alexcrea.cuanvil.enchant;
|
||||||
|
|
||||||
|
import io.delilaheve.util.ItemUtil;
|
||||||
|
import org.bukkit.NamespacedKey;
|
||||||
|
import org.bukkit.entity.HumanEntity;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
|
||||||
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import xyz.alexcrea.cuanvil.dependency.DependencyManager;
|
||||||
|
import xyz.alexcrea.cuanvil.dependency.EnchantmentSquaredDependency;
|
||||||
|
import xyz.alexcrea.cuanvil.group.ConflictType;
|
||||||
|
import xyz.alexcrea.cuanvil.group.EnchantConflictGroup;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represent an enchantment compatible with Custom Anvil.
|
||||||
|
* One issue with custom anvil is: it does not handle well duplicate key name (ignoring namespace) as the plugin was coded with vanilla enchantment in head
|
||||||
|
*/
|
||||||
|
public interface CAEnchantment {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the default rarity of this enchant.
|
||||||
|
* @return The default rarity of this enchant.
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
EnchantmentRarity defaultRarity();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the enchantment key.
|
||||||
|
* @return The enchantment key.
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
NamespacedKey getKey();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the enchantment name.
|
||||||
|
* @return The enchantment name.
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
String getName();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the default maximum level of this enchantment.
|
||||||
|
* @return The default maximum level of this enchantment.
|
||||||
|
*/
|
||||||
|
int defaultMaxLevel();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the enchantment have specialised group operation.
|
||||||
|
* @return If the enchantment is optimised for group operation.
|
||||||
|
*/
|
||||||
|
boolean isOptimised();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the player is allowed to use this enchantment.
|
||||||
|
* @param player The player to test.
|
||||||
|
* @return If the player is allowed to use this enchantment.
|
||||||
|
*/
|
||||||
|
boolean isAllowed(@NotNull HumanEntity player);
|
||||||
|
|
||||||
|
void addConflict(@NotNull EnchantConflictGroup conflict);
|
||||||
|
void removeConflict(@NotNull EnchantConflictGroup conflict);
|
||||||
|
|
||||||
|
void clearConflict();
|
||||||
|
|
||||||
|
@NotNull Set<EnchantConflictGroup> getConflicts();
|
||||||
|
|
||||||
|
@NotNull ConflictType testConflict();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get current level of the enchantment.
|
||||||
|
* @param item Item to search the level for.
|
||||||
|
*/
|
||||||
|
int getLevel(@NotNull ItemStack item);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get current level of the enchantment.
|
||||||
|
* @param item Item to search the level for.
|
||||||
|
* @param meta Meta of the provided item. It will not be changed and not be set on the item.
|
||||||
|
* @return Current leve of this enchantment on item. or 0 if absent.
|
||||||
|
*/
|
||||||
|
int getLevel(@NotNull ItemStack item, @NotNull ItemMeta meta);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if this enchantment is present on the provided level.
|
||||||
|
* @param item The item to set the enchantment level.
|
||||||
|
* @return If the enchantment have been found.
|
||||||
|
*/
|
||||||
|
boolean isEnchantmentPresent(@NotNull ItemStack item);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if this enchantment is present on the provided level.
|
||||||
|
* @param item The item to set the enchantment level.
|
||||||
|
* @param meta Meta of the provided item. It will not be changed and not be set on the item.
|
||||||
|
* @return If the enchantment have been found.
|
||||||
|
*/
|
||||||
|
boolean isEnchantmentPresent(@NotNull ItemStack item, @NotNull ItemMeta meta);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Force add an enchantment at the provided level.
|
||||||
|
* @param item The item to set the enchantment level.
|
||||||
|
* @param level The level to set the enchantment to.
|
||||||
|
*/
|
||||||
|
void addEnchantmentUnsafe(@NotNull ItemStack item, int level);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove this enchantment from the provided ItemStack.
|
||||||
|
* @param item The item to remove the enchantment.
|
||||||
|
*/
|
||||||
|
void removeFrom(@NotNull ItemStack item);
|
||||||
|
|
||||||
|
// Static functions
|
||||||
|
/**
|
||||||
|
* Clear every enchantment from this item.
|
||||||
|
* @param item Item to be cleared from enchantments.
|
||||||
|
*/
|
||||||
|
static void clearEnchants(@NotNull ItemStack item){
|
||||||
|
ItemMeta meta = item.getItemMeta();
|
||||||
|
if(meta == null) return;
|
||||||
|
|
||||||
|
// Clean Vanilla enchants
|
||||||
|
if (ItemUtil.INSTANCE.isEnchantedBook(item)) {
|
||||||
|
EnchantmentStorageMeta bookMeta = (EnchantmentStorageMeta) meta;
|
||||||
|
bookMeta.getStoredEnchants().forEach(
|
||||||
|
(enchantment, leve) -> bookMeta.removeStoredEnchant(enchantment)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
item.getEnchantments().forEach(
|
||||||
|
(enchantment, leve) -> item.removeEnchantment(enchantment)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean Enchant Squared enchants
|
||||||
|
EnchantmentSquaredDependency enchantmentSquared = DependencyManager.INSTANCE.getEnchantmentSquaredCompatibility();
|
||||||
|
if(enchantmentSquared != null){
|
||||||
|
enchantmentSquared.clearEnchantments(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean unoptimised enchants
|
||||||
|
for (CAEnchantment enchant : CAEnchantmentRegistry.getInstance().unoptimisedValues()) {
|
||||||
|
if(enchant.isEnchantmentPresent(item)){
|
||||||
|
enchant.removeFrom(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get enchantments of an item.
|
||||||
|
* @param item Item to get enchantment from.
|
||||||
|
* @return A map of the set enchantments and there's respective levels.
|
||||||
|
*/
|
||||||
|
static Map<CAEnchantment, Integer> getEnchants(@NotNull ItemStack item){
|
||||||
|
Map<CAEnchantment, Integer> enchantments = new HashMap<>();
|
||||||
|
CAEnchantmentRegistry registry = CAEnchantmentRegistry.getInstance();
|
||||||
|
|
||||||
|
ItemMeta meta = item.getItemMeta();
|
||||||
|
if(meta == null) return enchantments;
|
||||||
|
|
||||||
|
// Vanilla optimised get
|
||||||
|
if (ItemUtil.INSTANCE.isEnchantedBook(item)) {
|
||||||
|
((EnchantmentStorageMeta)meta).getStoredEnchants().forEach(
|
||||||
|
(enchantment, level) -> enchantments.put(registry.getByKey(enchantment.getKey()), level)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
item.getEnchantments().forEach(
|
||||||
|
(enchantment, level) -> enchantments.put(registry.getByKey(enchantment.getKey()), level)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enchants Squared get
|
||||||
|
EnchantmentSquaredDependency enchantmentSquared = DependencyManager.INSTANCE.getEnchantmentSquaredCompatibility();
|
||||||
|
if(enchantmentSquared != null){
|
||||||
|
enchantmentSquared.getEnchantmentsSquared(item, enchantments);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unoptimised enchantment get
|
||||||
|
findEnchantsFromSelectedList(item, meta, enchantments, registry.unoptimisedValues());
|
||||||
|
|
||||||
|
return enchantments;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find enchantments of an item. only test the enchantment from the list.
|
||||||
|
* @param item Item to get enchantment from.
|
||||||
|
* @param meta Meta of the provided item.
|
||||||
|
* @param enchantments Map of enchantment to complete.
|
||||||
|
* @param enchantmentToTest Enchantment to test
|
||||||
|
*/
|
||||||
|
static void findEnchantsFromSelectedList(
|
||||||
|
@NotNull ItemStack item,
|
||||||
|
@NotNull ItemMeta meta,
|
||||||
|
@NotNull Map<CAEnchantment, Integer> enchantments,
|
||||||
|
@NotNull Collection<CAEnchantment> enchantmentToTest){
|
||||||
|
|
||||||
|
for (CAEnchantment enchantment : enchantmentToTest) {
|
||||||
|
if(enchantment.isEnchantmentPresent(item, meta)){
|
||||||
|
enchantments.put(enchantment, enchantment.getLevel(item, meta));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets an array of all the registered enchantments.
|
||||||
|
* @return Array of enchantment.
|
||||||
|
*/
|
||||||
|
static @Nullable CAEnchantment getByKey(@NotNull NamespacedKey key){
|
||||||
|
return CAEnchantmentRegistry.getInstance().getByKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a list of all the unoptimised enchantments.
|
||||||
|
* @return List of enchantment.
|
||||||
|
*/
|
||||||
|
static @Nullable CAEnchantment getByName(@NotNull String name){
|
||||||
|
return CAEnchantmentRegistry.getInstance().getByName(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,117 @@
|
||||||
|
package xyz.alexcrea.cuanvil.enchant;
|
||||||
|
|
||||||
|
import org.bukkit.NamespacedKey;
|
||||||
|
import org.bukkit.entity.HumanEntity;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import xyz.alexcrea.cuanvil.group.ConflictType;
|
||||||
|
import xyz.alexcrea.cuanvil.group.EnchantConflictGroup;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public abstract class CAEnchantmentBase implements CAEnchantment {
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private final NamespacedKey key;
|
||||||
|
@NotNull
|
||||||
|
private final String name;
|
||||||
|
@NotNull
|
||||||
|
private final EnchantmentRarity defaultRarity;
|
||||||
|
private final int defaultMaxLevel;
|
||||||
|
|
||||||
|
private final Set<EnchantConflictGroup> conflicts;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor of Wrapped Enchantment.
|
||||||
|
* @param key The enchantment's key.
|
||||||
|
* @param defaultRarity Default rarity the enchantment should be.
|
||||||
|
* @param defaultMaxLevel Default max level the enchantment can be applied with.
|
||||||
|
*/
|
||||||
|
protected CAEnchantmentBase(
|
||||||
|
@NotNull NamespacedKey key,
|
||||||
|
@Nullable EnchantmentRarity defaultRarity,
|
||||||
|
int defaultMaxLevel){
|
||||||
|
this.key = key;
|
||||||
|
this.name = key.getKey();
|
||||||
|
this.defaultMaxLevel = defaultMaxLevel;
|
||||||
|
|
||||||
|
if(defaultRarity == null) this.defaultRarity = EnchantmentRarity.COMMON;
|
||||||
|
else this.defaultRarity = defaultRarity;
|
||||||
|
|
||||||
|
this.conflicts = new HashSet<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public final EnchantmentRarity defaultRarity(){
|
||||||
|
return defaultRarity;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public final NamespacedKey getKey(){
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public final String getName(){
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final int defaultMaxLevel(){
|
||||||
|
return defaultMaxLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isOptimised(){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isAllowed(@NotNull HumanEntity player){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getLevel(@NotNull ItemStack item){
|
||||||
|
ItemMeta meta = item.getItemMeta();
|
||||||
|
if(meta == null) return 0;
|
||||||
|
return getLevel(item, meta);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEnchantmentPresent(@NotNull ItemStack item){
|
||||||
|
ItemMeta meta = item.getItemMeta();
|
||||||
|
if(meta == null) return false;
|
||||||
|
return isEnchantmentPresent(item, meta);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addConflict(@NotNull EnchantConflictGroup conflict){
|
||||||
|
this.conflicts.add(conflict);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeConflict(@NotNull EnchantConflictGroup conflict){
|
||||||
|
this.conflicts.remove(conflict);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clearConflict(){
|
||||||
|
this.conflicts.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull Set<EnchantConflictGroup> getConflicts() {
|
||||||
|
return conflicts;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull ConflictType testConflict() {
|
||||||
|
return ConflictType.NO_CONFLICT;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,131 @@
|
||||||
|
package xyz.alexcrea.cuanvil.enchant;
|
||||||
|
|
||||||
|
import io.delilaheve.CustomAnvil;
|
||||||
|
import org.bukkit.NamespacedKey;
|
||||||
|
import org.bukkit.enchantments.Enchantment;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import xyz.alexcrea.cuanvil.dependency.DependencyManager;
|
||||||
|
import xyz.alexcrea.cuanvil.enchant.wrapped.CAVanillaEnchantment;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
|
public class CAEnchantmentRegistry {
|
||||||
|
|
||||||
|
private static final CAEnchantmentRegistry instance = new CAEnchantmentRegistry();
|
||||||
|
public static CAEnchantmentRegistry getInstance() {
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register enchantment functions
|
||||||
|
private final HashMap<NamespacedKey, CAEnchantment> byKeyMap;
|
||||||
|
private final HashMap<String, CAEnchantment> byNameMap;
|
||||||
|
private final List<CAEnchantment> unoptimisedValues;
|
||||||
|
|
||||||
|
private CAEnchantmentRegistry() {
|
||||||
|
byKeyMap = new HashMap<>();
|
||||||
|
byNameMap = new HashMap<>();
|
||||||
|
unoptimisedValues = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This should only be called on main of custom anvil.
|
||||||
|
* If called more than one time, chance of thing being broken will be high.
|
||||||
|
*/
|
||||||
|
public void registerStartupEnchantments(){
|
||||||
|
for (Enchantment enchantment : Enchantment.values()) {
|
||||||
|
register(new CAVanillaEnchantment(enchantment));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(DependencyManager.INSTANCE.getEnchantmentSquaredCompatibility() != null){
|
||||||
|
DependencyManager.INSTANCE.getEnchantmentSquaredCompatibility().registerEnchantments();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Can be used to register new enchantment.
|
||||||
|
* <p>
|
||||||
|
* No guarantee that the enchantment will be present on the config gui if registered late.
|
||||||
|
* (By late I mean after custom anvil startup.)
|
||||||
|
* @param enchantment The enchantment to be registered.
|
||||||
|
*/
|
||||||
|
public void register(@NotNull CAEnchantment enchantment){
|
||||||
|
if(byKeyMap.containsKey(enchantment.getKey())){
|
||||||
|
CustomAnvil.instance.getLogger().log(Level.WARNING,
|
||||||
|
"Duplicate registered enchantment. This should NOT happen.",
|
||||||
|
new IllegalStateException(enchantment.getKey()+" enchantment was already registered"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(byNameMap.containsKey(enchantment.getName())){
|
||||||
|
CustomAnvil.instance.getLogger().log(Level.WARNING,
|
||||||
|
"Duplicate registered enchantment name. There will have issue. " +
|
||||||
|
"\nI hope this do not happen to you on a production server. If it do, there is probably a plugin trying to register an enchantment with the same name than another one",
|
||||||
|
new IllegalStateException(enchantment.getKey()+" enchantment name was already registered"));
|
||||||
|
}
|
||||||
|
|
||||||
|
byKeyMap.put(enchantment.getKey(), enchantment);
|
||||||
|
byNameMap.put(enchantment.getName(), enchantment);
|
||||||
|
|
||||||
|
if(!enchantment.isOptimised()){
|
||||||
|
unoptimisedValues.add(enchantment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Can be used to unregister new enchantment.
|
||||||
|
* Please be cautious with this function.
|
||||||
|
* It should probably rarely be used.
|
||||||
|
* <p>
|
||||||
|
* No guarantee that the enchantment will absent if the config guis if unregistered late.
|
||||||
|
* (By late I mean after custom anvil startup.)
|
||||||
|
* @param enchantment The enchantment to be unregistered.
|
||||||
|
*/
|
||||||
|
public void unregister(@NotNull CAEnchantment enchantment){
|
||||||
|
byKeyMap.remove(enchantment.getKey());
|
||||||
|
byNameMap.remove(enchantment.getName());
|
||||||
|
|
||||||
|
unoptimisedValues.remove(enchantment);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the enchantment by the provided key.
|
||||||
|
* @param key Key to fetch.
|
||||||
|
* @return Registered enchantment. null if absent.
|
||||||
|
*/
|
||||||
|
public @Nullable CAEnchantment getByKey(@NotNull NamespacedKey key){
|
||||||
|
return byKeyMap.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the enchantment by the provided name.
|
||||||
|
* @param name Name to fetch.
|
||||||
|
* @return Registered enchantment. null if absent.
|
||||||
|
*/
|
||||||
|
public @Nullable CAEnchantment getByName(@NotNull String name){
|
||||||
|
return byNameMap.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets an array of all the registered enchantments.
|
||||||
|
* @return Array of enchantment.
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
public Collection<CAEnchantment> values() {
|
||||||
|
return byKeyMap.values();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a list of all the unoptimised enchantments.
|
||||||
|
* @return List of enchantment.
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
public List<CAEnchantment> unoptimisedValues() {
|
||||||
|
return unoptimisedValues;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,370 +0,0 @@
|
||||||
package xyz.alexcrea.cuanvil.enchant;
|
|
||||||
|
|
||||||
import io.delilaheve.CustomAnvil;
|
|
||||||
import io.delilaheve.util.ItemUtil;
|
|
||||||
import org.bukkit.NamespacedKey;
|
|
||||||
import org.bukkit.enchantments.Enchantment;
|
|
||||||
import org.bukkit.entity.HumanEntity;
|
|
||||||
import org.bukkit.inventory.ItemStack;
|
|
||||||
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
|
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
import xyz.alexcrea.cuanvil.dependency.DependencyManager;
|
|
||||||
import xyz.alexcrea.cuanvil.dependency.EnchantmentSquaredDependency;
|
|
||||||
import xyz.alexcrea.cuanvil.enchant.wrapped.VanillaEnchantment;
|
|
||||||
import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup;
|
|
||||||
import xyz.alexcrea.cuanvil.group.ConflictType;
|
|
||||||
import xyz.alexcrea.cuanvil.group.EnchantConflictGroup;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.logging.Level;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represent any enchantment.
|
|
||||||
* One issue with the plugin is: it does not handle well duplicate key name (ignoring namespace) as the plugin was coded with vanilla enchantment in head
|
|
||||||
*/
|
|
||||||
public abstract class WrappedEnchantment {
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private final NamespacedKey key;
|
|
||||||
@NotNull
|
|
||||||
private final String name;
|
|
||||||
@NotNull
|
|
||||||
private final EnchantmentRarity defaultRarity;
|
|
||||||
private final int defaultMaxLevel;
|
|
||||||
|
|
||||||
private final Set<EnchantConflictGroup> conflicts;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor of Wrapped Enchantment.
|
|
||||||
* @param key The enchantment's key.
|
|
||||||
* @param defaultRarity Default rarity the enchantment should be.
|
|
||||||
* @param defaultMaxLevel Default max level the enchantment can be applied with.
|
|
||||||
*/
|
|
||||||
protected WrappedEnchantment(
|
|
||||||
@NotNull NamespacedKey key,
|
|
||||||
@Nullable EnchantmentRarity defaultRarity,
|
|
||||||
int defaultMaxLevel){
|
|
||||||
this.key = key;
|
|
||||||
this.name = key.getKey();
|
|
||||||
this.defaultMaxLevel = defaultMaxLevel;
|
|
||||||
|
|
||||||
if(defaultRarity == null) this.defaultRarity = EnchantmentRarity.COMMON;
|
|
||||||
else this.defaultRarity = defaultRarity;
|
|
||||||
|
|
||||||
this.conflicts = new HashSet<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the default rarity of this enchant.
|
|
||||||
* @return The default rarity of this enchant.
|
|
||||||
*/
|
|
||||||
@NotNull
|
|
||||||
public final EnchantmentRarity defaultRarity(){
|
|
||||||
return defaultRarity;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the enchantment key.
|
|
||||||
* @return The enchantment key.
|
|
||||||
*/
|
|
||||||
@NotNull
|
|
||||||
public final NamespacedKey getKey(){
|
|
||||||
return key;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the enchantment name.
|
|
||||||
* @return The enchantment name.
|
|
||||||
*/
|
|
||||||
@NotNull
|
|
||||||
public final String getName(){
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the default maximum level of this enchantment.
|
|
||||||
* @return The default maximum level of this enchantment.
|
|
||||||
*/
|
|
||||||
public final int defaultMaxLevel(){return defaultMaxLevel;}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if the enchantment have specialised group operation.
|
|
||||||
* @return If the enchantment is optimised for group operation.
|
|
||||||
*/
|
|
||||||
protected boolean isOptimised(){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if the player is allowed to use this enchantment.
|
|
||||||
* @param player The player to test.
|
|
||||||
* @return If the player is allowed to use this enchantment.
|
|
||||||
*/
|
|
||||||
public boolean isAllowed(@NotNull HumanEntity player){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get current level of the enchantment.
|
|
||||||
* @param item Item to search the level for.
|
|
||||||
*/
|
|
||||||
public int getLevel(@NotNull ItemStack item){
|
|
||||||
ItemMeta meta = item.getItemMeta();
|
|
||||||
if(meta == null) return 0;
|
|
||||||
return getLevel(item, meta);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addConflict(@NotNull EnchantConflictGroup conflict){
|
|
||||||
this.conflicts.add(conflict);
|
|
||||||
}
|
|
||||||
public void removeConflict(@NotNull EnchantConflictGroup conflict){
|
|
||||||
this.conflicts.remove(conflict);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clearConflict(){
|
|
||||||
this.conflicts.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull Set<EnchantConflictGroup> getConflicts() {
|
|
||||||
return conflicts;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull ConflictType testConflict() {
|
|
||||||
return ConflictType.NO_CONFLICT;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get current level of the enchantment.
|
|
||||||
* @param item Item to search the level for.
|
|
||||||
* @param meta Meta of the provided item. It will not be changed and not be set on the item.
|
|
||||||
* @return Current leve of this enchantment on item. or 0 if absent.
|
|
||||||
*/
|
|
||||||
public abstract int getLevel(@NotNull ItemStack item, @NotNull ItemMeta meta);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if this enchantment is present on the provided level.
|
|
||||||
* @param item The item to set the enchantment level.
|
|
||||||
* @return If the enchantment have been found.
|
|
||||||
*/
|
|
||||||
public boolean isEnchantmentPresent(@NotNull ItemStack item){
|
|
||||||
ItemMeta meta = item.getItemMeta();
|
|
||||||
if(meta == null) return false;
|
|
||||||
return isEnchantmentPresent(item, meta);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if this enchantment is present on the provided level.
|
|
||||||
* @param item The item to set the enchantment level.
|
|
||||||
* @param meta Meta of the provided item. It will not be changed and not be set on the item.
|
|
||||||
* @return If the enchantment have been found.
|
|
||||||
*/
|
|
||||||
public abstract boolean isEnchantmentPresent(@NotNull ItemStack item, @NotNull ItemMeta meta);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Force add an enchantment at the provided level.
|
|
||||||
* @param item The item to set the enchantment level.
|
|
||||||
* @param level The level to set the enchantment to.
|
|
||||||
*/
|
|
||||||
public abstract void addEnchantmentUnsafe(@NotNull ItemStack item, int level);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove this enchantment from the provided ItemStack.
|
|
||||||
* @param item The item to remove the enchantment.
|
|
||||||
*/
|
|
||||||
public abstract void removeFrom(@NotNull ItemStack item);
|
|
||||||
|
|
||||||
// Static functions
|
|
||||||
/**
|
|
||||||
* Clear every enchantment from this item.
|
|
||||||
* @param item Item to be cleared from enchantments.
|
|
||||||
*/
|
|
||||||
public static void clearEnchants(@NotNull ItemStack item){
|
|
||||||
ItemMeta meta = item.getItemMeta();
|
|
||||||
if(meta == null) return;
|
|
||||||
|
|
||||||
// Clean Vanilla enchants
|
|
||||||
if (ItemUtil.INSTANCE.isEnchantedBook(item)) {
|
|
||||||
EnchantmentStorageMeta bookMeta = (EnchantmentStorageMeta) meta;
|
|
||||||
bookMeta.getStoredEnchants().forEach(
|
|
||||||
(enchantment, leve) -> bookMeta.removeStoredEnchant(enchantment)
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
item.getEnchantments().forEach(
|
|
||||||
(enchantment, leve) -> item.removeEnchantment(enchantment)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clean Enchant Squared enchants
|
|
||||||
EnchantmentSquaredDependency enchantmentSquared = DependencyManager.INSTANCE.getEnchantmentSquaredCompatibility();
|
|
||||||
if(enchantmentSquared != null){
|
|
||||||
enchantmentSquared.clearEnchantments(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clean unoptimised enchants
|
|
||||||
for (WrappedEnchantment enchant : unoptimisedValues()) {
|
|
||||||
if(enchant.isEnchantmentPresent(item)){
|
|
||||||
enchant.removeFrom(item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get enchantments of an item.
|
|
||||||
* @param item Item to get enchantment from.
|
|
||||||
* @return A map of the set enchantments and there's respective levels.
|
|
||||||
*/
|
|
||||||
public static Map<WrappedEnchantment, Integer> getEnchants(@NotNull ItemStack item){
|
|
||||||
Map<WrappedEnchantment, Integer> enchantments = new HashMap<>();
|
|
||||||
|
|
||||||
ItemMeta meta = item.getItemMeta();
|
|
||||||
if(meta == null) return enchantments;
|
|
||||||
|
|
||||||
// Vanilla optimised get
|
|
||||||
if (ItemUtil.INSTANCE.isEnchantedBook(item)) {
|
|
||||||
((EnchantmentStorageMeta)meta).getStoredEnchants().forEach(
|
|
||||||
(enchantment, level) -> enchantments.put(getByKey(enchantment.getKey()), level)
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
item.getEnchantments().forEach(
|
|
||||||
(enchantment, level) -> enchantments.put(getByKey(enchantment.getKey()), level)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Enchants Squared get
|
|
||||||
EnchantmentSquaredDependency enchantmentSquared = DependencyManager.INSTANCE.getEnchantmentSquaredCompatibility();
|
|
||||||
if(enchantmentSquared != null){
|
|
||||||
enchantmentSquared.getEnchantmentsSquared(item, enchantments);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unoptimised enchantment get
|
|
||||||
findEnchantsFromSelectedList(item, meta, enchantments, unoptimisedValues());
|
|
||||||
|
|
||||||
return enchantments;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Find enchantments of an item. only test the enchantment from the list.
|
|
||||||
* @param item Item to get enchantment from.
|
|
||||||
* @param meta Meta of the provided item.
|
|
||||||
* @param enchantments Map of enchantment to complete.
|
|
||||||
* @param enchantmentToTest Enchantment to test
|
|
||||||
*/
|
|
||||||
private static void findEnchantsFromSelectedList(
|
|
||||||
@NotNull ItemStack item,
|
|
||||||
@NotNull ItemMeta meta,
|
|
||||||
@NotNull Map<WrappedEnchantment, Integer> enchantments,
|
|
||||||
@NotNull Collection<WrappedEnchantment> enchantmentToTest){
|
|
||||||
|
|
||||||
for (WrappedEnchantment enchantment : enchantmentToTest) {
|
|
||||||
if(enchantment.isEnchantmentPresent(item, meta)){
|
|
||||||
enchantments.put(enchantment, enchantment.getLevel(item, meta));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Register enchantment functions
|
|
||||||
private static final HashMap<NamespacedKey, WrappedEnchantment> BY_KEY = new HashMap<>();
|
|
||||||
private static final HashMap<String, WrappedEnchantment> BY_NAME = new HashMap<>();
|
|
||||||
private static final List<WrappedEnchantment> UNOPTIMISED_ENCHANTMENT = new ArrayList<>();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This should only be called on main of custom anvil.
|
|
||||||
* If called more than one time, chance of thing being broken will be high.
|
|
||||||
*/
|
|
||||||
public static void registerEnchantments(){
|
|
||||||
for (Enchantment enchantment : Enchantment.values()) {
|
|
||||||
register(new VanillaEnchantment(enchantment));
|
|
||||||
}
|
|
||||||
|
|
||||||
if(DependencyManager.INSTANCE.getEnchantmentSquaredCompatibility() != null){
|
|
||||||
DependencyManager.INSTANCE.getEnchantmentSquaredCompatibility().registerEnchantments();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Can be used to register new enchantment.
|
|
||||||
* <p>
|
|
||||||
* No guarantee that the enchantment will be present on the config gui if registered late.
|
|
||||||
* (By late I mean after custom anvil startup.)
|
|
||||||
* @param enchantment The enchantment to be registered.
|
|
||||||
*/
|
|
||||||
public static void register(@NotNull WrappedEnchantment enchantment){
|
|
||||||
if(BY_KEY.containsKey(enchantment.getKey())){
|
|
||||||
CustomAnvil.instance.getLogger().log(Level.WARNING,
|
|
||||||
"Duplicate registered enchantment. This should NOT happen.",
|
|
||||||
new IllegalStateException(enchantment.getKey()+" enchantment was already registered"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if(BY_NAME.containsKey(enchantment.getName())){
|
|
||||||
CustomAnvil.instance.getLogger().log(Level.WARNING,
|
|
||||||
"Duplicate registered enchantment name. There will have issue. " +
|
|
||||||
"\nI hope this do not happen to you on a production server. If it do, there is probably a plugin trying to register an enchantment with the same name than another one",
|
|
||||||
new IllegalStateException(enchantment.getKey()+" enchantment name was already registered"));
|
|
||||||
}
|
|
||||||
|
|
||||||
BY_KEY.put(enchantment.getKey(), enchantment);
|
|
||||||
BY_NAME.put(enchantment.getName(), enchantment);
|
|
||||||
|
|
||||||
if(!enchantment.isOptimised()){
|
|
||||||
UNOPTIMISED_ENCHANTMENT.add(enchantment);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Can be used to unregister new enchantment.
|
|
||||||
* Please be cautious with this function.
|
|
||||||
* It should probably rarely be used.
|
|
||||||
* <p>
|
|
||||||
* No guarantee that the enchantment will absent if the config guis if unregistered late.
|
|
||||||
* (By late I mean after custom anvil startup.)
|
|
||||||
* @param enchantment The enchantment to be unregistered.
|
|
||||||
*/
|
|
||||||
public static void unregister(@NotNull WrappedEnchantment enchantment){
|
|
||||||
BY_KEY.remove(enchantment.getKey());
|
|
||||||
BY_NAME.remove(enchantment.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the enchantment by the provided key.
|
|
||||||
* @param key Key to fetch.
|
|
||||||
* @return Registered enchantment. null if absent.
|
|
||||||
*/
|
|
||||||
public static @Nullable WrappedEnchantment getByKey(@NotNull NamespacedKey key){
|
|
||||||
return BY_KEY.get(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the enchantment by the provided name.
|
|
||||||
* @param name Name to fetch.
|
|
||||||
* @return Registered enchantment. null if absent.
|
|
||||||
*/
|
|
||||||
public static @Nullable WrappedEnchantment getByName(@NotNull String name){
|
|
||||||
return BY_NAME.get(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets an array of all the registered enchantments.
|
|
||||||
* @return Array of enchantment.
|
|
||||||
*/
|
|
||||||
@NotNull
|
|
||||||
public static WrappedEnchantment[] values() {
|
|
||||||
return BY_KEY.values().toArray(new WrappedEnchantment[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets a list of all the unoptimised enchantments.
|
|
||||||
* @return List of enchantment.
|
|
||||||
*/
|
|
||||||
@NotNull
|
|
||||||
private static List<WrappedEnchantment> unoptimisedValues() {
|
|
||||||
return UNOPTIMISED_ENCHANTMENT;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -8,16 +8,16 @@ import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import xyz.alexcrea.cuanvil.dependency.DependencyManager;
|
import xyz.alexcrea.cuanvil.dependency.DependencyManager;
|
||||||
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentBase;
|
||||||
import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;
|
import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;
|
||||||
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment;
|
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public class EnchantSquaredEnchantment extends WrappedEnchantment {
|
public class CAEnchantSquaredEnchantment extends CAEnchantmentBase {
|
||||||
|
|
||||||
public final @NotNull CustomEnchant enchant;
|
public final @NotNull CustomEnchant enchant;
|
||||||
public EnchantSquaredEnchantment(@NotNull CustomEnchant enchant) {
|
public CAEnchantSquaredEnchantment(@NotNull CustomEnchant enchant) {
|
||||||
super(Objects.requireNonNull(
|
super(Objects.requireNonNull(
|
||||||
Objects.requireNonNull(DependencyManager.INSTANCE.getEnchantmentSquaredCompatibility()).getKeyFromEnchant(enchant)),
|
Objects.requireNonNull(DependencyManager.INSTANCE.getEnchantmentSquaredCompatibility()).getKeyFromEnchant(enchant)),
|
||||||
EnchantmentRarity.COMMON,
|
EnchantmentRarity.COMMON,
|
||||||
|
|
@ -27,7 +27,7 @@ public class EnchantSquaredEnchantment extends WrappedEnchantment {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean isOptimised() {
|
public boolean isOptimised() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -6,17 +6,17 @@ import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
|
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentBase;
|
||||||
import xyz.alexcrea.cuanvil.enchant.EnchantmentProperties;
|
import xyz.alexcrea.cuanvil.enchant.EnchantmentProperties;
|
||||||
import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;
|
import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;
|
||||||
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment;
|
|
||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
public class VanillaEnchantment extends WrappedEnchantment {
|
public class CAVanillaEnchantment extends CAEnchantmentBase {
|
||||||
|
|
||||||
private final @NotNull Enchantment enchantment;
|
private final @NotNull Enchantment enchantment;
|
||||||
|
|
||||||
public VanillaEnchantment(@NotNull Enchantment enchantment){
|
public CAVanillaEnchantment(@NotNull Enchantment enchantment){
|
||||||
super(enchantment.getKey(),
|
super(enchantment.getKey(),
|
||||||
getRarity(enchantment),
|
getRarity(enchantment),
|
||||||
enchantment.getMaxLevel());
|
enchantment.getMaxLevel());
|
||||||
|
|
@ -25,7 +25,7 @@ public class VanillaEnchantment extends WrappedEnchantment {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean isOptimised() {
|
public boolean isOptimised() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1,15 +1,15 @@
|
||||||
package xyz.alexcrea.cuanvil.gui.config;
|
package xyz.alexcrea.cuanvil.gui.config;
|
||||||
|
|
||||||
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment;
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public interface SelectEnchantmentContainer {
|
public interface SelectEnchantmentContainer {
|
||||||
|
|
||||||
Set<WrappedEnchantment> getSelectedEnchantments();
|
Set<CAEnchantment> getSelectedEnchantments();
|
||||||
|
|
||||||
boolean setSelectedEnchantments(Set<WrappedEnchantment> enchantments);
|
boolean setSelectedEnchantments(Set<CAEnchantment> enchantments);
|
||||||
|
|
||||||
Set<WrappedEnchantment> illegalEnchantments();
|
Set<CAEnchantment> illegalEnchantments();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ package xyz.alexcrea.cuanvil.gui.config.global;
|
||||||
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
|
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
|
||||||
import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
|
import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
|
||||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||||
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment;
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
|
||||||
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
|
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
|
||||||
import xyz.alexcrea.cuanvil.gui.config.list.SettingGuiListConfigGui;
|
import xyz.alexcrea.cuanvil.gui.config.list.SettingGuiListConfigGui;
|
||||||
import xyz.alexcrea.cuanvil.gui.config.settings.SettingGui;
|
import xyz.alexcrea.cuanvil.gui.config.settings.SettingGui;
|
||||||
|
|
@ -19,7 +19,7 @@ import java.util.function.Consumer;
|
||||||
*
|
*
|
||||||
* @param <T> Type of the factory of the type of setting the gui should edit.
|
* @param <T> Type of the factory of the type of setting the gui should edit.
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractEnchantConfigGui<T extends SettingGui.SettingGuiFactory> extends SettingGuiListConfigGui<WrappedEnchantment, T> implements ValueUpdatableGui {
|
public abstract class AbstractEnchantConfigGui<T extends SettingGui.SettingGuiFactory> extends SettingGuiListConfigGui<CAEnchantment, T> implements ValueUpdatableGui {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for a gui displaying available enchantment to edit a enchantment setting.
|
* Constructor for a gui displaying available enchantment to edit a enchantment setting.
|
||||||
|
|
@ -36,7 +36,7 @@ public abstract class AbstractEnchantConfigGui<T extends SettingGui.SettingGuiFa
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Collection<WrappedEnchantment> getEveryDisplayableInstanceOfGeneric() {
|
protected Collection<CAEnchantment> getEveryDisplayableInstanceOfGeneric() {
|
||||||
return GuiSharedConstant.SORTED_ENCHANTMENT_LIST;
|
return GuiSharedConstant.SORTED_ENCHANTMENT_LIST;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,14 +5,17 @@ import org.bukkit.Material;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
import xyz.alexcrea.cuanvil.config.ConfigHolder;
|
import xyz.alexcrea.cuanvil.config.ConfigHolder;
|
||||||
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
|
||||||
import xyz.alexcrea.cuanvil.enchant.EnchantmentProperties;
|
import xyz.alexcrea.cuanvil.enchant.EnchantmentProperties;
|
||||||
import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;
|
import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;
|
||||||
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment;
|
|
||||||
import xyz.alexcrea.cuanvil.gui.config.settings.EnchantCostSettingsGui;
|
import xyz.alexcrea.cuanvil.gui.config.settings.EnchantCostSettingsGui;
|
||||||
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
|
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
|
||||||
import xyz.alexcrea.cuanvil.util.CasedStringUtil;
|
import xyz.alexcrea.cuanvil.util.CasedStringUtil;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Global Config gui for enchantment cost settings.
|
* Global Config gui for enchantment cost settings.
|
||||||
|
|
@ -36,7 +39,7 @@ public class EnchantCostConfigGui extends AbstractEnchantConfigGui<EnchantCostSe
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EnchantCostSettingsGui.EnchantCostSettingFactory createFactory(WrappedEnchantment enchant) {
|
public EnchantCostSettingsGui.EnchantCostSettingFactory createFactory(CAEnchantment enchant) {
|
||||||
String key = enchant.getKey().getKey().toLowerCase(Locale.ENGLISH);
|
String key = enchant.getKey().getKey().toLowerCase(Locale.ENGLISH);
|
||||||
String prettyKey = CasedStringUtil.snakeToUpperSpacedCase(key);
|
String prettyKey = CasedStringUtil.snakeToUpperSpacedCase(key);
|
||||||
|
|
||||||
|
|
@ -59,7 +62,7 @@ public class EnchantCostConfigGui extends AbstractEnchantConfigGui<EnchantCostSe
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public GuiItem itemFromFactory(WrappedEnchantment enchantment, EnchantCostSettingsGui.EnchantCostSettingFactory factory) {
|
public GuiItem itemFromFactory(CAEnchantment enchantment, EnchantCostSettingsGui.EnchantCostSettingFactory factory) {
|
||||||
// Get item properties
|
// Get item properties
|
||||||
int itemCost = factory.getConfiguredValue();
|
int itemCost = factory.getConfiguredValue();
|
||||||
int bookCost = factory.getConfiguredBookValue();
|
int bookCost = factory.getConfiguredBookValue();
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ package xyz.alexcrea.cuanvil.gui.config.global;
|
||||||
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
|
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import xyz.alexcrea.cuanvil.config.ConfigHolder;
|
import xyz.alexcrea.cuanvil.config.ConfigHolder;
|
||||||
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment;
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
|
||||||
import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui;
|
import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui;
|
||||||
import xyz.alexcrea.cuanvil.util.CasedStringUtil;
|
import xyz.alexcrea.cuanvil.util.CasedStringUtil;
|
||||||
|
|
||||||
|
|
@ -32,7 +32,7 @@ public class EnchantLimitConfigGui extends AbstractEnchantConfigGui<IntSettingsG
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IntSettingsGui.IntSettingFactory createFactory(WrappedEnchantment enchant) {
|
public IntSettingsGui.IntSettingFactory createFactory(CAEnchantment enchant) {
|
||||||
String key = enchant.getKey().getKey().toLowerCase(Locale.ROOT);
|
String key = enchant.getKey().getKey().toLowerCase(Locale.ROOT);
|
||||||
String prettyKey = CasedStringUtil.snakeToUpperSpacedCase(key);
|
String prettyKey = CasedStringUtil.snakeToUpperSpacedCase(key);
|
||||||
|
|
||||||
|
|
@ -47,7 +47,7 @@ public class EnchantLimitConfigGui extends AbstractEnchantConfigGui<IntSettingsG
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public GuiItem itemFromFactory(WrappedEnchantment enchantment, IntSettingsGui.IntSettingFactory inventoryFactory) {
|
public GuiItem itemFromFactory(CAEnchantment enchantment, IntSettingsGui.IntSettingFactory inventoryFactory) {
|
||||||
return inventoryFactory.getItem(
|
return inventoryFactory.getItem(
|
||||||
Material.ENCHANTED_BOOK,
|
Material.ENCHANTED_BOOK,
|
||||||
inventoryFactory.getTitle());
|
inventoryFactory.getTitle());
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import xyz.alexcrea.cuanvil.config.ConfigHolder;
|
import xyz.alexcrea.cuanvil.config.ConfigHolder;
|
||||||
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment;
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
|
||||||
import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup;
|
import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup;
|
||||||
import xyz.alexcrea.cuanvil.group.EnchantConflictGroup;
|
import xyz.alexcrea.cuanvil.group.EnchantConflictGroup;
|
||||||
import xyz.alexcrea.cuanvil.group.EnchantConflictManager;
|
import xyz.alexcrea.cuanvil.group.EnchantConflictManager;
|
||||||
|
|
@ -118,7 +118,7 @@ public class EnchantConflictSubSettingGui extends MappedToListSubSettingGui impl
|
||||||
EnchantConflictManager manager = ConfigHolder.CONFLICT_HOLDER.getConflictManager();
|
EnchantConflictManager manager = ConfigHolder.CONFLICT_HOLDER.getConflictManager();
|
||||||
|
|
||||||
// Remove from enchantment
|
// Remove from enchantment
|
||||||
for (WrappedEnchantment enchantment : this.enchantConflict.getEnchants()) {
|
for (CAEnchantment enchantment : this.enchantConflict.getEnchants()) {
|
||||||
enchantment.removeConflict(this.enchantConflict);
|
enchantment.removeConflict(this.enchantConflict);
|
||||||
}
|
}
|
||||||
manager.conflictList.remove(this.enchantConflict);
|
manager.conflictList.remove(this.enchantConflict);
|
||||||
|
|
@ -164,12 +164,12 @@ public class EnchantConflictSubSettingGui extends MappedToListSubSettingGui impl
|
||||||
// Prepare enchantment lore
|
// Prepare enchantment lore
|
||||||
ArrayList<String> enchantLore = new ArrayList<>();
|
ArrayList<String> enchantLore = new ArrayList<>();
|
||||||
enchantLore.add("\u00A77Allow you to select a list of \u00A75Enchantments \u00A77that this conflict should include");
|
enchantLore.add("\u00A77Allow you to select a list of \u00A75Enchantments \u00A77that this conflict should include");
|
||||||
Set<WrappedEnchantment> enchants = getSelectedEnchantments();
|
Set<CAEnchantment> enchants = getSelectedEnchantments();
|
||||||
if (enchants.isEmpty()) {
|
if (enchants.isEmpty()) {
|
||||||
enchantLore.add("\u00A77There is no included enchantment for this conflict.");
|
enchantLore.add("\u00A77There is no included enchantment for this conflict.");
|
||||||
} else {
|
} else {
|
||||||
enchantLore.add("\u00A77List of included enchantment for this conflict:");
|
enchantLore.add("\u00A77List of included enchantment for this conflict:");
|
||||||
Iterator<WrappedEnchantment> enchantIterator = enchants.iterator();
|
Iterator<CAEnchantment> enchantIterator = enchants.iterator();
|
||||||
|
|
||||||
boolean greaterThanMax = enchants.size() > 5;
|
boolean greaterThanMax = enchants.size() > 5;
|
||||||
int maxindex = (greaterThanMax ? 4 : enchants.size());
|
int maxindex = (greaterThanMax ? 4 : enchants.size());
|
||||||
|
|
@ -243,12 +243,12 @@ public class EnchantConflictSubSettingGui extends MappedToListSubSettingGui impl
|
||||||
// Select enchantment container methods
|
// Select enchantment container methods
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<WrappedEnchantment> getSelectedEnchantments() {
|
public Set<CAEnchantment> getSelectedEnchantments() {
|
||||||
return this.enchantConflict.getEnchants();
|
return this.enchantConflict.getEnchants();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean setSelectedEnchantments(Set<WrappedEnchantment> enchantments) {
|
public boolean setSelectedEnchantments(Set<CAEnchantment> enchantments) {
|
||||||
if (!this.shouldWork) {
|
if (!this.shouldWork) {
|
||||||
CustomAnvil.instance.getLogger().info("Trying to save " + enchantConflict + " enchants but sub config is destroyed");
|
CustomAnvil.instance.getLogger().info("Trying to save " + enchantConflict + " enchants but sub config is destroyed");
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -260,7 +260,7 @@ public class EnchantConflictSubSettingGui extends MappedToListSubSettingGui impl
|
||||||
// Save on file configuration
|
// Save on file configuration
|
||||||
String[] enchantKeys = new String[enchantments.size()];
|
String[] enchantKeys = new String[enchantments.size()];
|
||||||
int index = 0;
|
int index = 0;
|
||||||
for (WrappedEnchantment enchantment : enchantments) {
|
for (CAEnchantment enchantment : enchantments) {
|
||||||
enchantKeys[index++] = enchantment.getKey().getKey();
|
enchantKeys[index++] = enchantment.getKey().getKey();
|
||||||
}
|
}
|
||||||
ConfigHolder.CONFLICT_HOLDER.getConfig().set(enchantConflict + ".enchantments", enchantKeys);
|
ConfigHolder.CONFLICT_HOLDER.getConfig().set(enchantConflict + ".enchantments", enchantKeys);
|
||||||
|
|
@ -280,7 +280,7 @@ public class EnchantConflictSubSettingGui extends MappedToListSubSettingGui impl
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<WrappedEnchantment> illegalEnchantments() {
|
public Set<CAEnchantment> illegalEnchantments() {
|
||||||
return Collections.emptySet();
|
return Collections.emptySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,8 @@ import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import xyz.alexcrea.cuanvil.config.ConfigHolder;
|
import xyz.alexcrea.cuanvil.config.ConfigHolder;
|
||||||
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment;
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
|
||||||
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry;
|
||||||
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
|
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
|
||||||
import xyz.alexcrea.cuanvil.gui.config.SelectEnchantmentContainer;
|
import xyz.alexcrea.cuanvil.gui.config.SelectEnchantmentContainer;
|
||||||
import xyz.alexcrea.cuanvil.gui.config.list.SettingGuiListConfigGui;
|
import xyz.alexcrea.cuanvil.gui.config.list.SettingGuiListConfigGui;
|
||||||
|
|
@ -22,14 +23,13 @@ import xyz.alexcrea.cuanvil.util.CasedStringUtil;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public class EnchantSelectSettingGui extends SettingGuiListConfigGui<WrappedEnchantment, EnchantSelectSettingGui.DummyFactory> implements SettingGui {
|
public class EnchantSelectSettingGui extends SettingGuiListConfigGui<CAEnchantment, EnchantSelectSettingGui.DummyFactory> implements SettingGui {
|
||||||
|
|
||||||
private final SelectEnchantmentContainer enchantContainer;
|
private final SelectEnchantmentContainer enchantContainer;
|
||||||
|
|
||||||
private final Set<WrappedEnchantment> selectedEnchant;
|
private final Set<CAEnchantment> selectedEnchant;
|
||||||
private final GuiItem saveItem;
|
private final GuiItem saveItem;
|
||||||
|
|
||||||
private boolean displayUnselected;
|
private boolean displayUnselected;
|
||||||
|
|
@ -62,19 +62,19 @@ public class EnchantSelectSettingGui extends SettingGuiListConfigGui<WrappedEnch
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Collection<WrappedEnchantment> getEveryDisplayableInstanceOfGeneric() {
|
protected Collection<CAEnchantment> getEveryDisplayableInstanceOfGeneric() {
|
||||||
Stream<WrappedEnchantment> toDisplayStream;
|
Stream<CAEnchantment> toDisplayStream;
|
||||||
if(this.displayUnselected){
|
if(this.displayUnselected){
|
||||||
toDisplayStream = Arrays.stream(WrappedEnchantment.values());
|
toDisplayStream = CAEnchantmentRegistry.getInstance().values().stream();
|
||||||
}else{
|
}else{
|
||||||
toDisplayStream = this.selectedEnchant.stream();
|
toDisplayStream = this.selectedEnchant.stream();
|
||||||
}
|
}
|
||||||
Set<WrappedEnchantment> illegalEnchantments = this.enchantContainer.illegalEnchantments();
|
Set<CAEnchantment> illegalEnchantments = this.enchantContainer.illegalEnchantments();
|
||||||
|
|
||||||
|
|
||||||
return toDisplayStream
|
return toDisplayStream
|
||||||
.filter(enchantment -> !illegalEnchantments.contains(enchantment))
|
.filter(enchantment -> !illegalEnchantments.contains(enchantment))
|
||||||
.collect(Collectors.toList());
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -84,7 +84,7 @@ public class EnchantSelectSettingGui extends SettingGuiListConfigGui<WrappedEnch
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected GuiItem itemFromFactory(WrappedEnchantment enchantment, DummyFactory factory) {
|
protected GuiItem itemFromFactory(CAEnchantment enchantment, DummyFactory factory) {
|
||||||
boolean isIn = this.selectedEnchant.contains(enchantment);
|
boolean isIn = this.selectedEnchant.contains(enchantment);
|
||||||
|
|
||||||
Material usedMaterial;
|
Material usedMaterial;
|
||||||
|
|
@ -151,7 +151,7 @@ public class EnchantSelectSettingGui extends SettingGuiListConfigGui<WrappedEnch
|
||||||
item.setItemMeta(meta);
|
item.setItemMeta(meta);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Consumer<InventoryClickEvent> getEnchantItemConsumer(WrappedEnchantment enchant, GuiItem guiItem) {
|
private Consumer<InventoryClickEvent> getEnchantItemConsumer(CAEnchantment enchant, GuiItem guiItem) {
|
||||||
return event -> {
|
return event -> {
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
|
|
||||||
|
|
@ -180,7 +180,7 @@ public class EnchantSelectSettingGui extends SettingGuiListConfigGui<WrappedEnch
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hadChange() {
|
public boolean hadChange() {
|
||||||
Set<WrappedEnchantment> baseGroup = this.enchantContainer.getSelectedEnchantments();
|
Set<CAEnchantment> baseGroup = this.enchantContainer.getSelectedEnchantments();
|
||||||
return baseGroup.size() != this.selectedEnchant.size() ||
|
return baseGroup.size() != this.selectedEnchant.size() ||
|
||||||
!baseGroup.containsAll(this.selectedEnchant);
|
!baseGroup.containsAll(this.selectedEnchant);
|
||||||
}
|
}
|
||||||
|
|
@ -209,7 +209,7 @@ public class EnchantSelectSettingGui extends SettingGuiListConfigGui<WrappedEnch
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
protected DummyFactory createFactory(WrappedEnchantment generic) {
|
protected DummyFactory createFactory(CAEnchantment generic) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,20 +7,18 @@ import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment;
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
|
||||||
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry;
|
||||||
import xyz.alexcrea.cuanvil.gui.config.MainConfigGui;
|
import xyz.alexcrea.cuanvil.gui.config.MainConfigGui;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.*;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class GuiSharedConstant {
|
public class GuiSharedConstant {
|
||||||
|
|
||||||
public static final List<WrappedEnchantment> SORTED_ENCHANTMENT_LIST;
|
public static final List<CAEnchantment> SORTED_ENCHANTMENT_LIST;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
SORTED_ENCHANTMENT_LIST = Arrays.asList(WrappedEnchantment.values());
|
SORTED_ENCHANTMENT_LIST = new ArrayList<>(CAEnchantmentRegistry.getInstance().values());
|
||||||
SORTED_ENCHANTMENT_LIST.sort(Comparator.comparing(ench -> ench.getKey().getKey()));
|
SORTED_ENCHANTMENT_LIST.sort(Comparator.comparing(ench -> ench.getKey().getKey()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ import xyz.alexcrea.cuanvil.command.EditConfigExecutor
|
||||||
import xyz.alexcrea.cuanvil.command.ReloadExecutor
|
import xyz.alexcrea.cuanvil.command.ReloadExecutor
|
||||||
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
||||||
import xyz.alexcrea.cuanvil.dependency.DependencyManager
|
import xyz.alexcrea.cuanvil.dependency.DependencyManager
|
||||||
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry
|
||||||
import xyz.alexcrea.cuanvil.gui.config.MainConfigGui
|
import xyz.alexcrea.cuanvil.gui.config.MainConfigGui
|
||||||
import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant
|
import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant
|
||||||
import xyz.alexcrea.cuanvil.listener.ChatEventListener
|
import xyz.alexcrea.cuanvil.listener.ChatEventListener
|
||||||
|
|
@ -94,7 +94,7 @@ class CustomAnvil : JavaPlugin() {
|
||||||
DependencyManager.loadDependency()
|
DependencyManager.loadDependency()
|
||||||
|
|
||||||
// Register enchantments
|
// Register enchantments
|
||||||
WrappedEnchantment.registerEnchantments()
|
CAEnchantmentRegistry.getInstance().registerStartupEnchantments()
|
||||||
|
|
||||||
// Load chat listener
|
// Load chat listener
|
||||||
chatListener = ChatEventListener()
|
chatListener = ChatEventListener()
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ package io.delilaheve.util
|
||||||
import io.delilaheve.CustomAnvil
|
import io.delilaheve.CustomAnvil
|
||||||
import io.delilaheve.util.EnchantmentUtil.enchantmentName
|
import io.delilaheve.util.EnchantmentUtil.enchantmentName
|
||||||
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
||||||
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Config option accessors
|
* Config option accessors
|
||||||
|
|
@ -239,7 +239,7 @@ object ConfigOptions {
|
||||||
/**
|
/**
|
||||||
* Get the given [enchantment]'s limit
|
* Get the given [enchantment]'s limit
|
||||||
*/
|
*/
|
||||||
fun enchantLimit(enchantment: WrappedEnchantment): Int {
|
fun enchantLimit(enchantment: CAEnchantment): Int {
|
||||||
return enchantLimit(enchantment.enchantmentName)
|
return enchantLimit(enchantment.enchantmentName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -273,7 +273,7 @@ object ConfigOptions {
|
||||||
* it's source [isFromBook]
|
* it's source [isFromBook]
|
||||||
*/
|
*/
|
||||||
fun enchantmentValue(
|
fun enchantmentValue(
|
||||||
enchantment: WrappedEnchantment,
|
enchantment: CAEnchantment,
|
||||||
isFromBook: Boolean
|
isFromBook: Boolean
|
||||||
): Int {
|
): Int {
|
||||||
return enchantmentValue(enchantment.enchantmentName, isFromBook)
|
return enchantmentValue(enchantment.enchantmentName, isFromBook)
|
||||||
|
|
@ -307,7 +307,7 @@ object ConfigOptions {
|
||||||
return enchantmentValue("sweeping", isFromBook)
|
return enchantmentValue("sweeping", isFromBook)
|
||||||
}
|
}
|
||||||
|
|
||||||
val enchantment = WrappedEnchantment.getByName(enchantmentName)
|
val enchantment = CAEnchantment.getByName(enchantmentName)
|
||||||
if(enchantment != null){
|
if(enchantment != null){
|
||||||
val rarity = enchantment.defaultRarity()
|
val rarity = enchantment.defaultRarity()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ import io.delilaheve.CustomAnvil
|
||||||
import org.bukkit.Material
|
import org.bukkit.Material
|
||||||
import org.bukkit.entity.HumanEntity
|
import org.bukkit.entity.HumanEntity
|
||||||
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
||||||
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
|
||||||
import xyz.alexcrea.cuanvil.group.ConflictType
|
import xyz.alexcrea.cuanvil.group.ConflictType
|
||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
import kotlin.math.min
|
import kotlin.math.min
|
||||||
|
|
@ -17,17 +17,17 @@ object EnchantmentUtil {
|
||||||
/**
|
/**
|
||||||
* Enchantment name without namespace
|
* Enchantment name without namespace
|
||||||
*/
|
*/
|
||||||
val WrappedEnchantment.enchantmentName: String
|
val CAEnchantment.enchantmentName: String
|
||||||
get() = key.key
|
get() = key.key
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Combine 2 sets of enchantments according to our configuration
|
* Combine 2 sets of enchantments according to our configuration
|
||||||
*/
|
*/
|
||||||
fun Map<WrappedEnchantment, Int>.combineWith(
|
fun Map<CAEnchantment, Int>.combineWith(
|
||||||
other: Map<WrappedEnchantment, Int>,
|
other: Map<CAEnchantment, Int>,
|
||||||
mat: Material,
|
mat: Material,
|
||||||
player: HumanEntity
|
player: HumanEntity
|
||||||
) = mutableMapOf<WrappedEnchantment, Int>().apply {
|
) = mutableMapOf<CAEnchantment, Int>().apply {
|
||||||
putAll(this@combineWith)
|
putAll(this@combineWith)
|
||||||
other.forEach { (enchantment, level) ->
|
other.forEach { (enchantment, level) ->
|
||||||
if(!enchantment.isAllowed(player)) return@forEach
|
if(!enchantment.isAllowed(player)) return@forEach
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ package io.delilaheve.util
|
||||||
import org.bukkit.Material.ENCHANTED_BOOK
|
import org.bukkit.Material.ENCHANTED_BOOK
|
||||||
import org.bukkit.inventory.ItemStack
|
import org.bukkit.inventory.ItemStack
|
||||||
import org.bukkit.inventory.meta.Damageable
|
import org.bukkit.inventory.meta.Damageable
|
||||||
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
|
||||||
import kotlin.math.ceil
|
import kotlin.math.ceil
|
||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
import kotlin.math.min
|
import kotlin.math.min
|
||||||
|
|
@ -21,13 +21,13 @@ object ItemUtil {
|
||||||
/**
|
/**
|
||||||
* Find the enchantment map for this [ItemStack] and return it as a [MutableMap]
|
* Find the enchantment map for this [ItemStack] and return it as a [MutableMap]
|
||||||
*/
|
*/
|
||||||
fun ItemStack.findEnchantments(): MutableMap<WrappedEnchantment, Int> = WrappedEnchantment.getEnchants(this)
|
fun ItemStack.findEnchantments(): MutableMap<CAEnchantment, Int> = CAEnchantment.getEnchants(this)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Apply an [enchantments] map to this [ItemStack]
|
* Apply an [enchantments] map to this [ItemStack]
|
||||||
*/
|
*/
|
||||||
fun ItemStack.setEnchantmentsUnsafe(enchantments: Map<WrappedEnchantment, Int>) {
|
fun ItemStack.setEnchantmentsUnsafe(enchantments: Map<CAEnchantment, Int>) {
|
||||||
WrappedEnchantment.clearEnchants(this)
|
CAEnchantment.clearEnchants(this)
|
||||||
|
|
||||||
//TODO maybe faster methode to add vanilla enchantment. maybe move this function to wrapped enchantment
|
//TODO maybe faster methode to add vanilla enchantment. maybe move this function to wrapped enchantment
|
||||||
enchantments.forEach { (enchantment, level) ->
|
enchantments.forEach { (enchantment, level) ->
|
||||||
|
|
|
||||||
|
|
@ -8,10 +8,10 @@ import org.bukkit.event.inventory.PrepareAnvilEvent
|
||||||
import org.bukkit.inventory.ItemStack
|
import org.bukkit.inventory.ItemStack
|
||||||
import org.bukkit.plugin.Plugin
|
import org.bukkit.plugin.Plugin
|
||||||
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
||||||
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
|
||||||
import xyz.alexcrea.cuanvil.enchant.wrapped.EnchantSquaredEnchantment
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry
|
||||||
|
import xyz.alexcrea.cuanvil.enchant.wrapped.CAEnchantSquaredEnchantment
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.collections.ArrayList
|
|
||||||
|
|
||||||
class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) {
|
class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) {
|
||||||
|
|
||||||
|
|
@ -30,12 +30,16 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
|
||||||
|
|
||||||
fun registerEnchantments(){
|
fun registerEnchantments(){
|
||||||
for (enchant in CustomEnchantManager.getInstance().allEnchants.values) {
|
for (enchant in CustomEnchantManager.getInstance().allEnchants.values) {
|
||||||
WrappedEnchantment.register(EnchantSquaredEnchantment(enchant))
|
CAEnchantmentRegistry.getInstance().register(
|
||||||
|
CAEnchantSquaredEnchantment(
|
||||||
|
enchant
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getEnchantmentsSquared(item: ItemStack, enchantments: MutableMap<WrappedEnchantment, Int>) {
|
fun getEnchantmentsSquared(item: ItemStack, enchantments: MutableMap<CAEnchantment, Int>) {
|
||||||
val customEnchants = CustomEnchantManager.getInstance().getItemsEnchantsFromPDC(item)
|
val customEnchants = CustomEnchantManager.getInstance().getItemsEnchantsFromPDC(item)
|
||||||
|
|
||||||
customEnchants.forEach{
|
customEnchants.forEach{
|
||||||
|
|
@ -51,8 +55,8 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
|
||||||
fun getKeyFromEnchant(enchant: CustomEnchant): NamespacedKey{
|
fun getKeyFromEnchant(enchant: CustomEnchant): NamespacedKey{
|
||||||
return NamespacedKey.fromString(enchant.type.lowercase(Locale.getDefault()), this.enchantmentSquaredPlugin)!!
|
return NamespacedKey.fromString(enchant.type.lowercase(Locale.getDefault()), this.enchantmentSquaredPlugin)!!
|
||||||
}
|
}
|
||||||
private fun getWrappedEnchant(enchant: CustomEnchant): WrappedEnchantment{
|
private fun getWrappedEnchant(enchant: CustomEnchant): CAEnchantment {
|
||||||
return WrappedEnchantment.getByKey(getKeyFromEnchant(enchant))!!
|
return CAEnchantment.getByKey(getKeyFromEnchant(enchant))!!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -65,9 +69,9 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
|
||||||
CustomAnvil.instance.logger.info("Preparing configuration for Enchantment Squared...")
|
CustomAnvil.instance.logger.info("Preparing configuration for Enchantment Squared...")
|
||||||
|
|
||||||
// Prepare enchantments
|
// Prepare enchantments
|
||||||
val esEnchantments = ArrayList<EnchantSquaredEnchantment>()
|
val esEnchantments = ArrayList<CAEnchantSquaredEnchantment>()
|
||||||
CustomEnchantManager.getInstance().allEnchants.forEach { (_, enchant) ->
|
CustomEnchantManager.getInstance().allEnchants.forEach { (_, enchant) ->
|
||||||
esEnchantments.add(getWrappedEnchant(enchant) as EnchantSquaredEnchantment)
|
esEnchantments.add(getWrappedEnchant(enchant) as CAEnchantSquaredEnchantment)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write default level limit and xp cost
|
// Write default level limit and xp cost
|
||||||
|
|
@ -134,7 +138,7 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun writeMaterialRestriction(esEnchantments: List<EnchantSquaredEnchantment>){
|
private fun writeMaterialRestriction(esEnchantments: List<CAEnchantSquaredEnchantment>){
|
||||||
val conflictConfig = ConfigHolder.CONFLICT_HOLDER.config
|
val conflictConfig = ConfigHolder.CONFLICT_HOLDER.config
|
||||||
for (enchantment in esEnchantments) {
|
for (enchantment in esEnchantments) {
|
||||||
val restrictionName = "restriction_${enchantment.key.key}"
|
val restrictionName = "restriction_${enchantment.key.key}"
|
||||||
|
|
@ -158,9 +162,9 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun writeEnchantmentConflicts(esEnchantments: List<EnchantSquaredEnchantment>){
|
private fun writeEnchantmentConflicts(esEnchantments: List<CAEnchantSquaredEnchantment>){
|
||||||
val otherEnchants = ArrayList<WrappedEnchantment>()
|
val otherEnchants = ArrayList<CAEnchantment>()
|
||||||
otherEnchants.addAll(WrappedEnchantment.values())
|
otherEnchants.addAll(CAEnchantmentRegistry.getInstance().values())
|
||||||
|
|
||||||
for (enchantment in esEnchantments) {
|
for (enchantment in esEnchantments) {
|
||||||
otherEnchants.remove(enchantment)
|
otherEnchants.remove(enchantment)
|
||||||
|
|
@ -174,7 +178,7 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun writeConflict(enchantment1: WrappedEnchantment, enchantment2: WrappedEnchantment){
|
private fun writeConflict(enchantment1: CAEnchantment, enchantment2: CAEnchantment){
|
||||||
val conflictConfig = ConfigHolder.CONFLICT_HOLDER.config
|
val conflictConfig = ConfigHolder.CONFLICT_HOLDER.config
|
||||||
val conflictPath = "${enchantment1.name}_with_${enchantment2.name}_conflict"
|
val conflictPath = "${enchantment1.name}_with_${enchantment2.name}_conflict"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ package xyz.alexcrea.cuanvil.group
|
||||||
|
|
||||||
import io.delilaheve.CustomAnvil
|
import io.delilaheve.CustomAnvil
|
||||||
import org.bukkit.Material
|
import org.bukkit.Material
|
||||||
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
|
||||||
|
|
||||||
class EnchantConflictGroup(
|
class EnchantConflictGroup(
|
||||||
private val name: String,
|
private val name: String,
|
||||||
|
|
@ -10,13 +10,13 @@ class EnchantConflictGroup(
|
||||||
var minBeforeBlock: Int
|
var minBeforeBlock: Int
|
||||||
) {
|
) {
|
||||||
|
|
||||||
private val enchantments = HashSet<WrappedEnchantment>()
|
private val enchantments = HashSet<CAEnchantment>()
|
||||||
|
|
||||||
fun addEnchantment(enchant: WrappedEnchantment) {
|
fun addEnchantment(enchant: CAEnchantment) {
|
||||||
enchantments.add(enchant)
|
enchantments.add(enchant)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun allowed(enchants: Set<WrappedEnchantment>, mat: Material): Boolean {
|
fun allowed(enchants: Set<CAEnchantment>, mat: Material): Boolean {
|
||||||
if (enchantments.size < minBeforeBlock) {
|
if (enchantments.size < minBeforeBlock) {
|
||||||
CustomAnvil.verboseLog("Conflicting bc of to many enchantments")
|
CustomAnvil.verboseLog("Conflicting bc of to many enchantments")
|
||||||
return true
|
return true
|
||||||
|
|
@ -44,11 +44,11 @@ class EnchantConflictGroup(
|
||||||
return this.cantConflict
|
return this.cantConflict
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getEnchants(): HashSet<WrappedEnchantment> {
|
fun getEnchants(): HashSet<CAEnchantment> {
|
||||||
return enchantments
|
return enchantments
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setEnchants(enchants: Set<WrappedEnchantment>) {
|
fun setEnchants(enchants: Set<CAEnchantment>) {
|
||||||
enchantments.clear()
|
enchantments.clear()
|
||||||
enchantments.addAll(enchants)
|
enchantments.addAll(enchants)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,8 @@ import org.bukkit.Material
|
||||||
import org.bukkit.NamespacedKey
|
import org.bukkit.NamespacedKey
|
||||||
import org.bukkit.configuration.ConfigurationSection
|
import org.bukkit.configuration.ConfigurationSection
|
||||||
import org.bukkit.enchantments.Enchantment
|
import org.bukkit.enchantments.Enchantment
|
||||||
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
|
||||||
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry
|
||||||
|
|
||||||
class EnchantConflictManager {
|
class EnchantConflictManager {
|
||||||
|
|
||||||
|
|
@ -28,8 +29,8 @@ class EnchantConflictManager {
|
||||||
|
|
||||||
// 1.20.5 compatibility TODO better update system
|
// 1.20.5 compatibility TODO better update system
|
||||||
private val SWEEPING_EDGE_ENCHANT =
|
private val SWEEPING_EDGE_ENCHANT =
|
||||||
WrappedEnchantment.getByKey(NamespacedKey.minecraft("sweeping_edge")) ?:
|
CAEnchantment.getByKey(NamespacedKey.minecraft("sweeping_edge")) ?:
|
||||||
WrappedEnchantment.getByKey(Enchantment.SWEEPING_EDGE.key)
|
CAEnchantment.getByKey(Enchantment.SWEEPING_EDGE.key)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -40,7 +41,7 @@ class EnchantConflictManager {
|
||||||
conflictList = ArrayList()
|
conflictList = ArrayList()
|
||||||
|
|
||||||
// Clear conflict if exist
|
// Clear conflict if exist
|
||||||
for (enchant in WrappedEnchantment.values()) {
|
for (enchant in CAEnchantmentRegistry.getInstance().values()) {
|
||||||
enchant.clearConflict()
|
enchant.clearConflict()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -84,7 +85,7 @@ class EnchantConflictManager {
|
||||||
}
|
}
|
||||||
conflict.addEnchantment(enchant)
|
conflict.addEnchantment(enchant)
|
||||||
}
|
}
|
||||||
if (conflict.getEnchants().size == 0) {
|
if (conflict.getEnchants().isEmpty()) {
|
||||||
if (!futureUse) { //TODO future use will be deprecated once the new update system is finished
|
if (!futureUse) { //TODO future use will be deprecated once the new update system is finished
|
||||||
CustomAnvil.instance.logger.warning("Conflict $conflictName do not have valid enchantment, it will not do anything")
|
CustomAnvil.instance.logger.warning("Conflict $conflictName do not have valid enchantment, it will not do anything")
|
||||||
}
|
}
|
||||||
|
|
@ -93,7 +94,7 @@ class EnchantConflictManager {
|
||||||
return conflict
|
return conflict
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getEnchantByName(enchantName: String): WrappedEnchantment? {
|
private fun getEnchantByName(enchantName: String): CAEnchantment? {
|
||||||
|
|
||||||
// Temporary solution for 1.20.5
|
// Temporary solution for 1.20.5
|
||||||
when(enchantName){
|
when(enchantName){
|
||||||
|
|
@ -102,7 +103,7 @@ class EnchantConflictManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return WrappedEnchantment.getByName(enchantName)
|
return CAEnchantment.getByName(enchantName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -143,7 +144,7 @@ class EnchantConflictManager {
|
||||||
return group
|
return group
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isConflicting(base: Set<WrappedEnchantment>, mat: Material, newEnchant: WrappedEnchantment): ConflictType {
|
fun isConflicting(base: Set<CAEnchantment>, mat: Material, newEnchant: CAEnchantment): ConflictType {
|
||||||
CustomAnvil.verboseLog("Testing conflict for ${newEnchant.key} on ${mat.key}")
|
CustomAnvil.verboseLog("Testing conflict for ${newEnchant.key} on ${mat.key}")
|
||||||
val conflictList = newEnchant.conflicts;
|
val conflictList = newEnchant.conflicts;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue