mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +02:00
Use Bulk operation object for bukkit and enchantment² enchants.
This commit is contained in:
parent
d3252eecbd
commit
b89a8951b7
5 changed files with 73 additions and 40 deletions
|
|
@ -11,6 +11,8 @@ import xyz.alexcrea.cuanvil.config.ConfigHolder;
|
|||
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
|
||||
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry;
|
||||
import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;
|
||||
import xyz.alexcrea.cuanvil.enchant.bulk.BulkCleanEnchantOperation;
|
||||
import xyz.alexcrea.cuanvil.enchant.bulk.BulkGetEnchantOperation;
|
||||
import xyz.alexcrea.cuanvil.enchant.wrapped.CAVanillaEnchantment;
|
||||
import xyz.alexcrea.cuanvil.gui.config.global.EnchantCostConfigGui;
|
||||
import xyz.alexcrea.cuanvil.gui.config.global.EnchantLimitConfigGui;
|
||||
|
|
@ -186,5 +188,20 @@ public class EnchantmentApi {
|
|||
}, 0L);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a bulk get operator.
|
||||
* @param operation An optimised get enchantments operation
|
||||
*/
|
||||
public static void addBulkGet(@NotNull BulkGetEnchantOperation operation){
|
||||
CAEnchantmentRegistry.getInstance().getOptimisedGetOperators().add(operation);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a bulk clean operator.
|
||||
* @param operation An optimised clean enchantments operation
|
||||
*/
|
||||
public static void addBulkClean(@NotNull BulkCleanEnchantOperation operation){
|
||||
CAEnchantmentRegistry.getInstance().getOptimisedCleanOperators().add(operation);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,13 @@
|
|||
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.enchant.bulk.BulkCleanEnchantOperation;
|
||||
import xyz.alexcrea.cuanvil.enchant.bulk.BulkGetEnchantOperation;
|
||||
import xyz.alexcrea.cuanvil.group.EnchantConflictGroup;
|
||||
|
||||
import java.util.Collection;
|
||||
|
|
@ -148,26 +146,20 @@ public interface CAEnchantment {
|
|||
* @param item Item to be cleared from enchantments.
|
||||
*/
|
||||
static void clearEnchants(@NotNull ItemStack item){
|
||||
// Optimised enchantment clean using item stack
|
||||
for (BulkCleanEnchantOperation cleanOperator : CAEnchantmentRegistry.getInstance().getOptimisedCleanOperators()) {
|
||||
cleanOperator.bulkClear(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)
|
||||
);
|
||||
// Optimised enchantment clean using item meta
|
||||
for (BulkCleanEnchantOperation cleanOperator : CAEnchantmentRegistry.getInstance().getOptimisedCleanOperators()) {
|
||||
cleanOperator.bulkClear(item, meta);
|
||||
}
|
||||
|
||||
// Clean Enchant Squared enchants
|
||||
EnchantmentSquaredDependency enchantmentSquared = DependencyManager.INSTANCE.getEnchantmentSquaredCompatibility();
|
||||
if(enchantmentSquared != null){
|
||||
enchantmentSquared.clearEnchantments(item);
|
||||
}
|
||||
item.setItemMeta(meta);
|
||||
|
||||
// Clean unoptimised enchants
|
||||
for (CAEnchantment enchant : CAEnchantmentRegistry.getInstance().unoptimisedCleanValues()) {
|
||||
|
|
@ -190,21 +182,9 @@ public interface CAEnchantment {
|
|||
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);
|
||||
// Optimised enchantment get
|
||||
for (BulkGetEnchantOperation getOperator : CAEnchantmentRegistry.getInstance().getOptimisedGetOperators()) {
|
||||
getOperator.bulkGet(enchantments, item, meta);
|
||||
}
|
||||
|
||||
// Unoptimised enchantment get
|
||||
|
|
|
|||
|
|
@ -5,6 +5,9 @@ import org.bukkit.NamespacedKey;
|
|||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import xyz.alexcrea.cuanvil.enchant.bulk.BukkitEnchantBulkOperation;
|
||||
import xyz.alexcrea.cuanvil.enchant.bulk.BulkCleanEnchantOperation;
|
||||
import xyz.alexcrea.cuanvil.enchant.bulk.BulkGetEnchantOperation;
|
||||
import xyz.alexcrea.cuanvil.enchant.wrapped.CAVanillaEnchantment;
|
||||
|
||||
import java.util.*;
|
||||
|
|
@ -20,25 +23,39 @@ public class CAEnchantmentRegistry {
|
|||
// Register enchantment functions
|
||||
private final HashMap<NamespacedKey, CAEnchantment> byKeyMap;
|
||||
private final HashMap<String, CAEnchantment> byNameMap;
|
||||
|
||||
private final List<CAEnchantment> unoptimisedGetValues;
|
||||
private final List<CAEnchantment> unoptimisedCleanValues;
|
||||
|
||||
private final List<BulkGetEnchantOperation> optimisedGetOperators;
|
||||
private final List<BulkCleanEnchantOperation> optimisedCleanOperators;
|
||||
|
||||
private CAEnchantmentRegistry() {
|
||||
byKeyMap = new HashMap<>();
|
||||
byNameMap = new HashMap<>();
|
||||
|
||||
unoptimisedGetValues = new ArrayList<>();
|
||||
unoptimisedCleanValues = new ArrayList<>();
|
||||
|
||||
optimisedGetOperators = new ArrayList<>();
|
||||
optimisedCleanOperators = 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(){
|
||||
public void registerBukkit(){
|
||||
// Register enchantment
|
||||
for (Enchantment enchantment : Enchantment.values()) {
|
||||
register(new CAVanillaEnchantment(enchantment));
|
||||
}
|
||||
|
||||
// Add bukkit enchantment bulk operation
|
||||
BukkitEnchantBulkOperation bukkitOperation = new BukkitEnchantBulkOperation();
|
||||
optimisedGetOperators.add(bukkitOperation);
|
||||
optimisedCleanOperators.add(bukkitOperation);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -152,5 +169,20 @@ public class CAEnchantmentRegistry {
|
|||
return unoptimisedCleanValues;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get "clean optimised operation" for get enchantments.
|
||||
* @return Get mutable "clean enchantments optimised operation" list.
|
||||
*/
|
||||
public List<BulkCleanEnchantOperation> getOptimisedCleanOperators() {
|
||||
return optimisedCleanOperators;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get "get optimised operation" for get enchantments.
|
||||
* @return Get mutable "get enchantments optimised operation" list.
|
||||
*/
|
||||
public List<BulkGetEnchantOperation> getOptimisedGetOperators() {
|
||||
return optimisedGetOperators;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -116,7 +116,7 @@ class CustomAnvil : JavaPlugin() {
|
|||
if (!ConfigHolder.loadDefaultConfig()) return
|
||||
|
||||
// Register enchantments
|
||||
CAEnchantmentRegistry.getInstance().registerStartupEnchantments()
|
||||
CAEnchantmentRegistry.getInstance().registerBukkit()
|
||||
DependencyManager.registerEnchantments()
|
||||
|
||||
val enchantReadyEvent = CAEnchantRegistryReadyEvent()
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ import xyz.alexcrea.cuanvil.api.EnchantmentApi
|
|||
import xyz.alexcrea.cuanvil.api.MaterialGroupApi
|
||||
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
|
||||
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry
|
||||
import xyz.alexcrea.cuanvil.enchant.bulk.EnchantSquaredBulkOperation
|
||||
import xyz.alexcrea.cuanvil.enchant.wrapped.CAEnchantSquaredEnchantment
|
||||
import xyz.alexcrea.cuanvil.group.IncludeGroup
|
||||
import java.util.*
|
||||
|
|
@ -36,10 +37,17 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
|
|||
|
||||
fun registerEnchantments(){
|
||||
CustomAnvil.instance.logger.info("Preparing Enchantment Squared compatibility...")
|
||||
|
||||
// Register enchantments
|
||||
for (enchant in CustomEnchantManager.getInstance().allEnchants.values) {
|
||||
EnchantmentApi.registerEnchantment(CAEnchantSquaredEnchantment(enchant))
|
||||
}
|
||||
|
||||
// Register bulk operation
|
||||
val bulkOpperations = EnchantSquaredBulkOperation()
|
||||
EnchantmentApi.addBulkGet(bulkOpperations)
|
||||
EnchantmentApi.addBulkClean(bulkOpperations)
|
||||
|
||||
}
|
||||
|
||||
fun getEnchantmentsSquared(item: ItemStack, enchantments: MutableMap<CAEnchantment, Int>) {
|
||||
|
|
@ -51,10 +59,6 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
|
|||
|
||||
}
|
||||
|
||||
fun clearEnchantments(item: ItemStack) {
|
||||
CustomEnchantManager.getInstance().removeAllEnchants(item)
|
||||
}
|
||||
|
||||
fun getKeyFromEnchant(enchant: CustomEnchant): NamespacedKey{
|
||||
return NamespacedKey.fromString(enchant.type.lowercase(Locale.getDefault()), this.enchantmentSquaredPlugin)!!
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue