Partially implemented eco enchant compatibility.

This commit is contained in:
alexcrea 2024-06-22 02:42:25 +02:00
parent 2e29e7f04e
commit 19806773a6
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
18 changed files with 143 additions and 35 deletions

View file

@ -90,16 +90,17 @@ public interface CAEnchantment {
@NotNull Collection<EnchantConflictGroup> getConflicts(); @NotNull Collection<EnchantConflictGroup> getConflicts();
/** /**
* Test if the provided item can be compatible with this * Test if the provided item can be compatible with this enchantment. only non-Custom Anvil conflict.
* @param baseEnchantments Validated enchantments for the item. * @param baseEnchantments Validated enchantments for the item.
* @param itemMat Material of the tested item. * @param itemMat Material of the tested item.
* @param itemSupply Provide a new instance of used item stack but with baseEnchantments as enchantments. * @param itemSupply Provide a new instance of used item stack but with baseEnchantments as enchantments.
* @return Type of conflict this enchantment has with the provided item. * @return Type of conflict this enchantment has with the provided item.
*/ */
@NotNull @NotNull
ConflictType testConflict(@NotNull Map<CAEnchantment, Integer> baseEnchantments, ConflictType testOtherConflicts(
@NotNull Material itemMat, @NotNull Map<CAEnchantment, Integer> baseEnchantments,
@NotNull Supplier<ItemStack> itemSupply); @NotNull Material itemMat,
@NotNull Supplier<ItemStack> itemSupply);
/** /**
* Get current level of the enchantment. * Get current level of the enchantment.

View file

@ -113,9 +113,10 @@ public abstract class CAEnchantmentBase implements CAEnchantment {
} }
@Override @Override
public @NotNull ConflictType testConflict(@NotNull Map<CAEnchantment, Integer> baseEnchantments, public @NotNull ConflictType testOtherConflicts(
@NotNull Material itemMat, @NotNull Map<CAEnchantment, Integer> baseEnchantments,
@NotNull Supplier<ItemStack> itemSupply) { @NotNull Material itemMat,
@NotNull Supplier<ItemStack> itemSupply) {
return ConflictType.NO_CONFLICT; return ConflictType.NO_CONFLICT;
} }

View file

@ -85,7 +85,8 @@ public class CAEnchantmentRegistry {
* (By late I mean after custom anvil startup.) * (By late I mean after custom anvil startup.)
* @param enchantment The enchantment to be unregistered. * @param enchantment The enchantment to be unregistered.
*/ */
public void unregister(@NotNull CAEnchantment enchantment){ public void unregister(CAEnchantment enchantment){
if(enchantment == null) return;
byKeyMap.remove(enchantment.getKey()); byKeyMap.remove(enchantment.getKey());
byNameMap.remove(enchantment.getName()); byNameMap.remove(enchantment.getName());

View file

@ -0,0 +1,57 @@
package xyz.alexcrea.cuanvil.enchant.wrapped;
import com.willfp.ecoenchants.enchant.EcoEnchant;
import com.willfp.ecoenchants.target.EnchantmentTarget;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;
import xyz.alexcrea.cuanvil.group.ConflictType;
import java.util.Map;
import java.util.function.Supplier;
public class CAEcoEnchant extends CAVanillaEnchantment {
private final @NotNull EcoEnchant ecoEnchant;
public CAEcoEnchant(@NotNull EcoEnchant enchant) {
super(enchant.getEnchantment(), EnchantmentRarity.COMMON);
this.ecoEnchant = enchant;
}
@Override
public @NotNull ConflictType testOtherConflicts(
@NotNull Map<CAEnchantment, Integer> baseEnchantments,
@NotNull Material itemMat,
@NotNull Supplier<ItemStack> itemSupply) {
if(!baseEnchantments.isEmpty()){
if(this.ecoEnchant.getConflictsWithEverything()){
return ConflictType.ENCHANTMENT_CONFLICT;
}
for (CAEnchantment other : baseEnchantments.keySet()) {
if(other instanceof CAVanillaEnchantment otherVanilla //TODO ISSUE FOUND: if vanilla is applied first conflcit is ignored. maybe export only enchantment conflict but keep target from ecoEnchant.
&& this.ecoEnchant.conflictsWith(otherVanilla.getEnchant())){
return ConflictType.ENCHANTMENT_CONFLICT;
}
}
}
// Allow enchanted book
if(Material.ENCHANTED_BOOK.equals(itemMat)){
return ConflictType.NO_CONFLICT;
}
ItemStack item = itemSupply.get();
for (EnchantmentTarget target : this.ecoEnchant.getTargets()) {
if(target.matches(item)){
return ConflictType.NO_CONFLICT;
}
}
return ConflictType.ITEM_CONFLICT;
}
}

View file

@ -6,6 +6,7 @@ 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 org.jetbrains.annotations.Nullable;
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentBase; 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;
@ -16,12 +17,15 @@ public class CAVanillaEnchantment extends CAEnchantmentBase {
private final @NotNull Enchantment enchantment; private final @NotNull Enchantment enchantment;
public CAVanillaEnchantment(@NotNull Enchantment enchantment){ public CAVanillaEnchantment(@NotNull Enchantment enchantment, @Nullable EnchantmentRarity rarity){
super(enchantment.getKey(), super(enchantment.getKey(),
getRarity(enchantment), rarity,
enchantment.getMaxLevel()); enchantment.getMaxLevel());
this.enchantment = enchantment; this.enchantment = enchantment;
}
public CAVanillaEnchantment(@NotNull Enchantment enchantment){
this(enchantment, getRarity(enchantment));
} }
@Override @Override
@ -77,6 +81,7 @@ public class CAVanillaEnchantment extends CAEnchantmentBase {
} }
@NotNull
public static EnchantmentRarity getRarity(Enchantment enchantment){ public static EnchantmentRarity getRarity(Enchantment enchantment){
try { try {
return EnchantmentProperties.valueOf(enchantment.getKey().getKey().toUpperCase(Locale.ENGLISH)).getRarity(); return EnchantmentProperties.valueOf(enchantment.getKey().getKey().toUpperCase(Locale.ENGLISH)).getRarity();
@ -85,4 +90,8 @@ public class CAVanillaEnchantment extends CAEnchantmentBase {
} }
} }
@NotNull
protected Enchantment getEnchant() {
return this.enchantment;
}
} }

View file

@ -13,7 +13,6 @@ import xyz.alexcrea.cuanvil.util.CasedStringUtil;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List;
public class CustomRecipeConfigGui extends MappedGuiListConfigGui<AnvilCustomRecipe, CustomRecipeSubSettingGui> { public class CustomRecipeConfigGui extends MappedGuiListConfigGui<AnvilCustomRecipe, CustomRecipeSubSettingGui> {

View file

@ -15,7 +15,6 @@ import xyz.alexcrea.cuanvil.util.CasedStringUtil;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List;
public class EnchantConflictGui extends MappedGuiListConfigGui<EnchantConflictGroup, EnchantConflictSubSettingGui> { public class EnchantConflictGui extends MappedGuiListConfigGui<EnchantConflictGroup, EnchantConflictSubSettingGui> {

View file

@ -17,7 +17,6 @@ import xyz.alexcrea.cuanvil.util.CasedStringUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List;
public class GroupConfigGui extends MappedGuiListConfigGui<IncludeGroup, GroupConfigSubSettingGui> { public class GroupConfigGui extends MappedGuiListConfigGui<IncludeGroup, GroupConfigSubSettingGui> {

View file

@ -15,7 +15,6 @@ import xyz.alexcrea.cuanvil.util.CasedStringUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List;
public class UnitRepairConfigGui extends MappedGuiListConfigGui<Material, UnitRepairElementListGui> { public class UnitRepairConfigGui extends MappedGuiListConfigGui<Material, UnitRepairElementListGui> {

View file

@ -20,7 +20,10 @@ import xyz.alexcrea.cuanvil.gui.config.MainConfigGui;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant; import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant;
import java.util.*; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.UUID;
public abstract class ElementListConfigGui< T > extends ChestGui implements ValueUpdatableGui { public abstract class ElementListConfigGui< T > extends ChestGui implements ValueUpdatableGui {

View file

@ -6,7 +6,6 @@ import org.bukkit.entity.HumanEntity;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui;
import xyz.alexcrea.cuanvil.gui.config.settings.SettingGui; import xyz.alexcrea.cuanvil.gui.config.settings.SettingGui;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;

View file

@ -11,7 +11,6 @@ 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.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui;
import xyz.alexcrea.cuanvil.gui.config.settings.SettingGui; import xyz.alexcrea.cuanvil.gui.config.settings.SettingGui;
import java.util.ArrayList; import java.util.ArrayList;

View file

@ -449,7 +449,7 @@ class AnvilEventListener(private val packetManager: PacketManager) : Listener {
) )
resultEnchsKeys.remove(enchantment.key) resultEnchsKeys.remove(enchantment.key)
if (ConflictType.BIG_CONFLICT == conflictType) { if (ConflictType.ENCHANTMENT_CONFLICT == conflictType) {
illegalPenalty += ConfigOptions.sacrificeIllegalCost illegalPenalty += ConfigOptions.sacrificeIllegalCost
CustomAnvil.verboseLog("Big conflict. Adding illegal price penalty") CustomAnvil.verboseLog("Big conflict. Adding illegal price penalty")
} }

View file

@ -9,6 +9,7 @@ object DependencyManager {
lateinit var packetManager: PacketManager lateinit var packetManager: PacketManager
var enchantmentSquaredCompatibility: EnchantmentSquaredDependency? = null var enchantmentSquaredCompatibility: EnchantmentSquaredDependency? = null
var ecoEnchantCompatibility: EcoEnchantDependency? = null
fun loadDependency(){ fun loadDependency(){
val pluginManager = Bukkit.getPluginManager() val pluginManager = Bukkit.getPluginManager()
@ -24,11 +25,17 @@ object DependencyManager {
enchantmentSquaredCompatibility!!.disableAnvilListener() enchantmentSquaredCompatibility!!.disableAnvilListener()
} }
// EcoEnchants dependency
if(pluginManager.isPluginEnabled("EcoEnchants")){
ecoEnchantCompatibility = EcoEnchantDependency(pluginManager.getPlugin("EcoEnchants")!!)
ecoEnchantCompatibility!!.disableAnvilListener()
}
} }
fun handleConfigChanges() { fun handleConfigChanges() {
enchantmentSquaredCompatibility?.registerPluginConfiguration() enchantmentSquaredCompatibility?.registerPluginConfiguration()
ecoEnchantCompatibility?.registerEnchantments()
} }

View file

@ -0,0 +1,31 @@
package xyz.alexcrea.cuanvil.dependency
import com.willfp.ecoenchants.enchant.EcoEnchants
import io.delilaheve.CustomAnvil
import org.bukkit.event.inventory.PrepareAnvilEvent
import org.bukkit.plugin.Plugin
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry
import xyz.alexcrea.cuanvil.enchant.wrapped.CAEcoEnchant
class EcoEnchantDependency(private val ecoEnchantPlugin: Plugin) {
init {
CustomAnvil.instance.logger.info("Eco Enchant Detected !")
}
fun disableAnvilListener(){
PrepareAnvilEvent.getHandlerList().unregister(this.ecoEnchantPlugin)
}
fun registerEnchantments() {
val registery = CAEnchantmentRegistry.getInstance()
for (ecoEnchant in EcoEnchants.values()) {
val enchantments: CAEnchantment = CAEcoEnchant(ecoEnchant)
registery.unregister(registery.getByKey(ecoEnchant.enchantment.key)) // As
registery.register(enchantments)
}
}
}

View file

@ -15,17 +15,19 @@ import java.util.*
class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) { class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) {
fun disableAnvilListener(){ init {
PrepareAnvilEvent.getHandlerList().unregister(this.enchantmentSquaredPlugin)
CustomAnvil.instance.logger.info("Enchantment Squared Detected !") CustomAnvil.instance.logger.info("Enchantment Squared Detected !")
CustomAnvil.instance.logger.info("Please be aware that Custom Anvil is bypassing Enchantment Squared ") CustomAnvil.instance.logger.info("Please be aware that Custom Anvil is bypassing Enchantment Squared ")
CustomAnvil.instance.logger.info( CustomAnvil.instance.logger.info(
"compatible_with, " + "compatible_with, " +
"disable_anvil, " + "disable_anvil, " +
"incompatible_vanilla_enchantments, " + "incompatible_vanilla_enchantments, " +
"incompatible_custom_enchantments and max_level " + "incompatible_custom_enchantments and max_level " +
"configuration values.") "configuration values.")
}
fun disableAnvilListener(){
PrepareAnvilEvent.getHandlerList().unregister(this.enchantmentSquaredPlugin)
} }
fun registerEnchantments(){ fun registerEnchantments(){
@ -118,7 +120,7 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
if(!groupConfig.isConfigurationSection("hoes")){ if(!groupConfig.isConfigurationSection("hoes")){
groupConfig["hoes.type"] = "include" groupConfig["hoes.type"] = "include"
groupConfig["hoes.items"] = listOf("wooden_hoe", "stone_ho", "iron_hoe", "diamond_hoe", "golden_hoe", "netherite_hoe") groupConfig["hoes.items"] = listOf("wooden_hoe", "stone_hoe", "iron_hoe", "diamond_hoe", "golden_hoe", "netherite_hoe")
} }
if(!groupConfig.isConfigurationSection("shield")){ if(!groupConfig.isConfigurationSection("shield")){

View file

@ -148,7 +148,7 @@ class EnchantConflictManager {
fun isConflicting(appliedEnchants: Map<CAEnchantment, Int>, item: ItemStack, newEnchant: CAEnchantment): ConflictType { fun isConflicting(appliedEnchants: Map<CAEnchantment, Int>, item: ItemStack, newEnchant: CAEnchantment): ConflictType {
val mat = item.type val mat = item.type
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
var result = ConflictType.NO_CONFLICT var result = ConflictType.NO_CONFLICT
for (conflict in conflictList) { for (conflict in conflictList) {
@ -157,17 +157,17 @@ class EnchantConflictManager {
CustomAnvil.verboseLog("Was against $conflict and conflicting: ${!allowed} ") CustomAnvil.verboseLog("Was against $conflict and conflicting: ${!allowed} ")
if (!allowed) { if (!allowed) {
if (conflict.getEnchants().size <= 1) { if (conflict.getEnchants().size <= 1) {
result = ConflictType.SMALL_CONFLICT result = ConflictType.ITEM_CONFLICT
CustomAnvil.verboseLog("Small conflict, continuing") CustomAnvil.verboseLog("Small conflict, continuing")
} else { } else {
CustomAnvil.verboseLog("Big conflict, probably stoping") CustomAnvil.verboseLog("Big conflict, probably stoping")
return ConflictType.BIG_CONFLICT return ConflictType.ENCHANTMENT_CONFLICT
} }
} }
} }
// Test conflict with other conflict system. // Test conflict with other conflict system.
val otherConflict = newEnchant.testConflict(appliedEnchants, mat, reEnchantSupplier(item, appliedEnchants)) val otherConflict = newEnchant.testOtherConflicts(appliedEnchants, mat, reEnchantSupplier(item, appliedEnchants))
return result.getWorstConflict(otherConflict) return result.getWorstConflict(otherConflict)
} }
@ -181,7 +181,7 @@ class EnchantConflictManager {
enchantment -> enchantment.key.addEnchantmentUnsafe(item, enchantment.value) enchantment -> enchantment.key.addEnchantmentUnsafe(item, enchantment.value)
} }
return@Supplier newItem; return@Supplier newItem
} }
} }
@ -199,13 +199,13 @@ enum class ConflictType(private val importance: Int) {
/** /**
* Inform that the anvil process should not change the current applied enchantment. * Inform that the anvil process should not change the current applied enchantment.
*/ */
SMALL_CONFLICT(1), ITEM_CONFLICT(1),
/** /**
* Inform that the anvil process should not change the current applied enchantment. * Inform that the anvil process should not change the current applied enchantment.
* Also add sacrificeIllegalCost for every enchantment marked as big conflict. * Also add sacrificeIllegalCost for every enchantment marked as big conflict.
*/ */
BIG_CONFLICT(2); ENCHANTMENT_CONFLICT(2);
fun getWorstConflict(otherConflict: ConflictType): ConflictType { fun getWorstConflict(otherConflict: ConflictType): ConflictType {
return if(this.importance > otherConflict.importance) this return if(this.importance > otherConflict.importance) this

View file

@ -46,3 +46,5 @@ softdepend:
- UnsafeEnchantsPlus - UnsafeEnchantsPlus
- ProtocolLib - ProtocolLib
- EnchantsSquared - EnchantsSquared
- EcoEnchants
- eco