Optimised Enchant Squared get and clean.

This commit is contained in:
alexcrea 2024-06-16 19:11:09 +02:00
parent c589a14b0b
commit 88c4f0509b
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
9 changed files with 65 additions and 23 deletions

View file

@ -10,6 +10,7 @@ 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 java.util.*;
@ -35,7 +36,7 @@ public abstract class WrappedEnchantment {
* @param defaultRarity Default rarity the enchantment should be.
* @param defaultMaxLevel Default max level the enchantment can be applied with.
*/
public WrappedEnchantment(
protected WrappedEnchantment(
@NotNull NamespacedKey key,
@Nullable EnchantmentRarity defaultRarity,
int defaultMaxLevel){
@ -159,6 +160,12 @@ public abstract class WrappedEnchantment {
);
}
// 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)){
@ -190,6 +197,12 @@ public abstract class WrappedEnchantment {
);
}
// Enchants Squared get
EnchantmentSquaredDependency enchantmentSquared = DependencyManager.INSTANCE.getEnchantmentSquaredCompatibility();
if(enchantmentSquared != null){
enchantmentSquared.getEnchantmentsSquared(item, enchantments);
}
// Unoptimised enchantment get
findEnchantsFromSelectedList(item, meta, enchantments, unoptimisedValues());
@ -234,7 +247,7 @@ public abstract class WrappedEnchantment {
}
if(DependencyManager.INSTANCE.getEnchantmentSquaredCompatibility() != null){
DependencyManager.INSTANCE.getEnchantmentSquaredCompatibility().registerEnchantements();
DependencyManager.INSTANCE.getEnchantmentSquaredCompatibility().registerEnchantments();
}
}

View file

@ -2,11 +2,11 @@ package xyz.alexcrea.cuanvil.enchant.wrapped;
import me.athlaeos.enchantssquared.enchantments.CustomEnchant;
import me.athlaeos.enchantssquared.managers.CustomEnchantManager;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.dependency.DependencyManager;
import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment;
import java.util.Map;
@ -14,14 +14,20 @@ import java.util.Objects;
public class EnchantSquaredEnchantment extends WrappedEnchantment {
private final @NotNull CustomEnchant enchant;
public EnchantSquaredEnchantment(@NotNull CustomEnchant enchant, @NotNull Plugin enchantSquared) {
super(Objects.requireNonNull(NamespacedKey.fromString(enchant.getType().toLowerCase(), enchantSquared)), null, enchant.getMaxLevel());
public final @NotNull CustomEnchant enchant;
public EnchantSquaredEnchantment(@NotNull CustomEnchant enchant) {
super(Objects.requireNonNull(
Objects.requireNonNull(DependencyManager.INSTANCE.getEnchantmentSquaredCompatibility()).getKeyFromEnchant(enchant)),
EnchantmentRarity.COMMON,
enchant.getMaxLevel());
this.enchant = enchant;
}
//TODO optimise for bulk operation
@Override
protected boolean isOptimised() {
return true;
}
@Override
public int getLevel(@NotNull ItemStack item, @NotNull ItemMeta meta) {

View file

@ -78,10 +78,11 @@ public class VanillaEnchantment extends WrappedEnchantment {
}
public static EnchantmentRarity getRarity(Enchantment enchantment){
try {return EnchantmentProperties.valueOf(enchantment.getKey().getKey().toUpperCase(Locale.ENGLISH)).getRarity();}
catch (IllegalArgumentException ignored) {}
try {
return EnchantmentProperties.valueOf(enchantment.getKey().getKey().toUpperCase(Locale.ENGLISH)).getRarity();
} catch (IllegalArgumentException ignored) {
return EnchantmentRarity.COMMON;
}
}
}

View file

@ -8,9 +8,9 @@ import io.delilaheve.CustomAnvil;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import xyz.alexcrea.cuanvil.dependency.protocolib.PacketManager;
import xyz.alexcrea.cuanvil.gui.config.global.*;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import xyz.alexcrea.cuanvil.dependency.protocolib.PacketManager;
import java.util.Collections;

View file

@ -11,6 +11,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.dependency.protocolib.PacketManager;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.config.MainConfigGui;
import xyz.alexcrea.cuanvil.gui.config.settings.BoolSettingsGui;
@ -18,7 +19,6 @@ import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant;
import xyz.alexcrea.cuanvil.dependency.protocolib.PacketManager;
import java.util.ArrayList;
import java.util.Arrays;

View file

@ -26,13 +26,12 @@ import org.bukkit.inventory.InventoryView.Property.REPAIR_COST
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.Repairable
import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.group.ConflictType
import xyz.alexcrea.cuanvil.dependency.protocolib.PacketManager
import xyz.alexcrea.cuanvil.group.ConflictType
import xyz.alexcrea.cuanvil.recipe.AnvilCustomRecipe
import xyz.alexcrea.cuanvil.util.UnitRepairUtil.getRepair
import kotlin.math.min
/**
* Listener for anvil events
*/

View file

@ -12,7 +12,6 @@ import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment
import xyz.alexcrea.cuanvil.gui.config.MainConfigGui
import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant
import xyz.alexcrea.cuanvil.listener.ChatEventListener
import xyz.alexcrea.cuanvil.dependency.protocolib.PacketManager
import xyz.alexcrea.cuanvil.update.Update_1_21
import xyz.alexcrea.cuanvil.util.Metrics
import java.io.File

View file

@ -8,15 +8,15 @@ import xyz.alexcrea.cuanvil.dependency.protocolib.ProtocoLibWrapper
object DependencyManager {
lateinit var packetManager: PacketManager
var enchantmentSquaredCompatibility: EnchantmentSquaredDependency? = null;
var enchantmentSquaredCompatibility: EnchantmentSquaredDependency? = null
fun loadDependency(){
val pluginManager = Bukkit.getPluginManager();
val pluginManager = Bukkit.getPluginManager()
// ProtocolLib dependency
packetManager =
if(pluginManager.isPluginEnabled("ProtocolLib")) ProtocoLibWrapper();
else NoProtocoLib();
if(pluginManager.isPluginEnabled("ProtocolLib")) ProtocoLibWrapper()
else NoProtocoLib()
// Enchantment Squared dependency
enchantmentSquaredCompatibility =

View file

@ -1,17 +1,41 @@
package xyz.alexcrea.cuanvil.dependency
import me.athlaeos.enchantssquared.enchantments.CustomEnchant
import me.athlaeos.enchantssquared.managers.CustomEnchantManager
import org.bukkit.NamespacedKey
import org.bukkit.inventory.ItemStack
import org.bukkit.plugin.Plugin
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment
import xyz.alexcrea.cuanvil.enchant.wrapped.EnchantSquaredEnchantment
import java.util.*
class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) {
fun registerEnchantements(){
fun registerEnchantments(){
for (enchant in CustomEnchantManager.getInstance().allEnchants.values) {
WrappedEnchantment.register(EnchantSquaredEnchantment(enchant, enchantmentSquaredPlugin))
WrappedEnchantment.register(EnchantSquaredEnchantment(enchant))
}
}
fun getEnchantmentsSquared(item: ItemStack, enchantments: MutableMap<WrappedEnchantment, Int>) {
val customEnchants = CustomEnchantManager.getInstance().getItemsEnchantsFromPDC(item)
customEnchants.forEach{
(enchantment, level ) -> enchantments[getWrappedEnchant(enchantment)] = level
}
}
fun clearEnchantments(item: ItemStack) {
CustomEnchantManager.getInstance().removeAllEnchants(item)
}
fun getKeyFromEnchant(enchant: CustomEnchant): NamespacedKey{
return NamespacedKey.fromString(enchant.type.lowercase(Locale.getDefault()), this.enchantmentSquaredPlugin)!!
}
private fun getWrappedEnchant(enchant: CustomEnchant): WrappedEnchantment{
return WrappedEnchantment.getByKey(getKeyFromEnchant(enchant))!!
}
}