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,14 +90,15 @@ public interface CAEnchantment {
@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 itemMat Material of the tested item.
* @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.
*/
@NotNull
ConflictType testConflict(@NotNull Map<CAEnchantment, Integer> baseEnchantments,
ConflictType testOtherConflicts(
@NotNull Map<CAEnchantment, Integer> baseEnchantments,
@NotNull Material itemMat,
@NotNull Supplier<ItemStack> itemSupply);

View file

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

View file

@ -85,7 +85,8 @@ public class CAEnchantmentRegistry {
* (By late I mean after custom anvil startup.)
* @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());
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.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentBase;
import xyz.alexcrea.cuanvil.enchant.EnchantmentProperties;
import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;
@ -16,12 +17,15 @@ public class CAVanillaEnchantment extends CAEnchantmentBase {
private final @NotNull Enchantment enchantment;
public CAVanillaEnchantment(@NotNull Enchantment enchantment){
public CAVanillaEnchantment(@NotNull Enchantment enchantment, @Nullable EnchantmentRarity rarity){
super(enchantment.getKey(),
getRarity(enchantment),
rarity,
enchantment.getMaxLevel());
this.enchantment = enchantment;
}
public CAVanillaEnchantment(@NotNull Enchantment enchantment){
this(enchantment, getRarity(enchantment));
}
@Override
@ -77,6 +81,7 @@ public class CAVanillaEnchantment extends CAEnchantmentBase {
}
@NotNull
public static EnchantmentRarity getRarity(Enchantment enchantment){
try {
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.Collection;
import java.util.List;
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.Collection;
import java.util.List;
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.Arrays;
import java.util.Collection;
import java.util.List;
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.Arrays;
import java.util.Collection;
import java.util.List;
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.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 {

View file

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

View file

@ -11,7 +11,6 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui;
import xyz.alexcrea.cuanvil.gui.config.settings.SettingGui;
import java.util.ArrayList;

View file

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

View file

@ -9,6 +9,7 @@ object DependencyManager {
lateinit var packetManager: PacketManager
var enchantmentSquaredCompatibility: EnchantmentSquaredDependency? = null
var ecoEnchantCompatibility: EcoEnchantDependency? = null
fun loadDependency(){
val pluginManager = Bukkit.getPluginManager()
@ -24,11 +25,17 @@ object DependencyManager {
enchantmentSquaredCompatibility!!.disableAnvilListener()
}
// EcoEnchants dependency
if(pluginManager.isPluginEnabled("EcoEnchants")){
ecoEnchantCompatibility = EcoEnchantDependency(pluginManager.getPlugin("EcoEnchants")!!)
ecoEnchantCompatibility!!.disableAnvilListener()
}
}
fun handleConfigChanges() {
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,9 +15,7 @@ import java.util.*
class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) {
fun disableAnvilListener(){
PrepareAnvilEvent.getHandlerList().unregister(this.enchantmentSquaredPlugin)
init {
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(
@ -28,6 +26,10 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
"configuration values.")
}
fun disableAnvilListener(){
PrepareAnvilEvent.getHandlerList().unregister(this.enchantmentSquaredPlugin)
}
fun registerEnchantments(){
for (enchant in CustomEnchantManager.getInstance().allEnchants.values) {
CAEnchantmentRegistry.getInstance().register(
@ -118,7 +120,7 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
if(!groupConfig.isConfigurationSection("hoes")){
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")){

View file

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

View file

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