Rework the conflict system to be mostly handled by the enchantment.

This commit is contained in:
alexcrea 2024-06-20 02:57:52 +02:00
parent b7fda4dece
commit fa4752ea67
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
4 changed files with 69 additions and 28 deletions

View file

@ -13,6 +13,9 @@ import org.jetbrains.annotations.Nullable;
import xyz.alexcrea.cuanvil.dependency.DependencyManager; import xyz.alexcrea.cuanvil.dependency.DependencyManager;
import xyz.alexcrea.cuanvil.dependency.EnchantmentSquaredDependency; import xyz.alexcrea.cuanvil.dependency.EnchantmentSquaredDependency;
import xyz.alexcrea.cuanvil.enchant.wrapped.VanillaEnchantment; 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.*;
import java.util.logging.Level; import java.util.logging.Level;
@ -31,6 +34,8 @@ public abstract class WrappedEnchantment {
private final EnchantmentRarity defaultRarity; private final EnchantmentRarity defaultRarity;
private final int defaultMaxLevel; private final int defaultMaxLevel;
private final Set<EnchantConflictGroup> conflicts;
/** /**
* Constructor of Wrapped Enchantment. * Constructor of Wrapped Enchantment.
* @param key The enchantment's key. * @param key The enchantment's key.
@ -47,6 +52,8 @@ public abstract class WrappedEnchantment {
if(defaultRarity == null) this.defaultRarity = EnchantmentRarity.COMMON; if(defaultRarity == null) this.defaultRarity = EnchantmentRarity.COMMON;
else this.defaultRarity = defaultRarity; else this.defaultRarity = defaultRarity;
this.conflicts = new HashSet<>();
} }
/** /**
@ -109,6 +116,25 @@ public abstract class WrappedEnchantment {
return getLevel(item, meta); 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. * Get current level of the enchantment.
* @param item Item to search the level for. * @param item Item to search the level for.

View file

@ -117,9 +117,9 @@ public class EnchantConflictSubSettingGui extends MappedToListSubSettingGui impl
Supplier<Boolean> deleteSupplier = () -> { Supplier<Boolean> deleteSupplier = () -> {
EnchantConflictManager manager = ConfigHolder.CONFLICT_HOLDER.getConflictManager(); EnchantConflictManager manager = ConfigHolder.CONFLICT_HOLDER.getConflictManager();
// Remove from manager // Remove from enchantment
for (WrappedEnchantment enchantment : this.enchantConflict.getEnchants()) { for (WrappedEnchantment enchantment : this.enchantConflict.getEnchants()) {
manager.removeConflictFromMap(enchantment, this.enchantConflict); enchantment.removeConflict(this.enchantConflict);
} }
manager.conflictList.remove(this.enchantConflict); manager.conflictList.remove(this.enchantConflict);

View file

@ -55,7 +55,7 @@ object EnchantmentUtil {
} }
// Enchantment already in result list // Enchantment already in result list
else { else {
val oldLevel = this[enchantment]!! // <- should not be null. see the comment above val oldLevel = this[enchantment]!! // <- should not be null. (enchantment already in result list)
// ... and they are conflicting // ... and they are conflicting
val conflictType = val conflictType =

View file

@ -33,43 +33,35 @@ class EnchantConflictManager {
} }
private lateinit var conflictMap: HashMap<WrappedEnchantment, ArrayList<EnchantConflictGroup>>
lateinit var conflictList: ArrayList<EnchantConflictGroup> lateinit var conflictList: ArrayList<EnchantConflictGroup>
// Read and prepare all conflict // Read and prepare all conflict
fun prepareConflicts(config: ConfigurationSection, itemManager: ItemGroupManager) { fun prepareConflicts(config: ConfigurationSection, itemManager: ItemGroupManager) {
conflictMap = HashMap()
conflictList = ArrayList() conflictList = ArrayList()
// Clear conflict if exist
for (enchant in WrappedEnchantment.values()) {
enchant.clearConflict()
}
val keys = config.getKeys(false) val keys = config.getKeys(false)
for (key in keys) { for (key in keys) {
val section = config.getConfigurationSection(key)!! val section = config.getConfigurationSection(key)!!
val conflict = createConflict(section, itemManager, key) val conflict = createConflict(section, itemManager, key)
addToMap(conflict) addConflictToEnchantments(conflict)
conflictList.add(conflict) conflictList.add(conflict)
} }
} }
// Add the conflict to the map // Add the conflict to enchantments
private fun addToMap(conflict: EnchantConflictGroup) { private fun addConflictToEnchantments(conflict: EnchantConflictGroup) {
conflict.getEnchants().forEach { enchant -> conflict.getEnchants().forEach { enchant ->
addConflictToConflictMap(enchant, conflict) enchant.addConflict(conflict)
} }
} }
fun addConflictToConflictMap(enchant: WrappedEnchantment, conflict: EnchantConflictGroup) {
if (!conflictMap.containsKey(enchant)) {
conflictMap[enchant] = ArrayList()
}
conflictMap[enchant]!!.add(conflict)
}
fun removeConflictFromMap(enchant: WrappedEnchantment, conflict: EnchantConflictGroup): Boolean {
return conflictMap[enchant]!!.remove(conflict)
}
// create and read a conflict from a yaml section // create and read a conflict from a yaml section
private fun createConflict( private fun createConflict(
section: ConfigurationSection, section: ConfigurationSection,
@ -85,7 +77,7 @@ class EnchantConflictManager {
for (enchantName in enchantList) { for (enchantName in enchantList) {
val enchant = getEnchantByName(enchantName) val enchant = getEnchantByName(enchantName)
if (enchant == null) { if (enchant == null) {
if (!futureUse) { if (!futureUse) { //TODO future use will be deprecated once the new update system is finished
CustomAnvil.instance.logger.warning("Enchantment $enchantName do not exist but was asked for conflict $conflictName") CustomAnvil.instance.logger.warning("Enchantment $enchantName do not exist but was asked for conflict $conflictName")
} }
continue continue
@ -153,8 +145,7 @@ class EnchantConflictManager {
fun isConflicting(base: Set<WrappedEnchantment>, mat: Material, newEnchant: WrappedEnchantment): ConflictType { fun isConflicting(base: Set<WrappedEnchantment>, mat: Material, newEnchant: WrappedEnchantment): ConflictType {
CustomAnvil.verboseLog("Testing conflict for ${newEnchant.key} on ${mat.key}") CustomAnvil.verboseLog("Testing conflict for ${newEnchant.key} on ${mat.key}")
val conflictList = conflictMap[newEnchant] ?: return ConflictType.NO_CONFLICT val conflictList = newEnchant.conflicts;
CustomAnvil.verboseLog("Did not get skipped")
var result = ConflictType.NO_CONFLICT var result = ConflictType.NO_CONFLICT
for (conflict in conflictList) { for (conflict in conflictList) {
@ -171,15 +162,39 @@ class EnchantConflictManager {
} }
} }
} }
return result
// Test conflict with other conflict system.
val otherConflict = newEnchant.testConflict()
return result.getWorstConflict(otherConflict)
} }
} }
enum class ConflictType { /**
NO_CONFLICT, * Provide information about the current conflict.
SMALL_CONFLICT, */
BIG_CONFLICT enum class ConflictType(private val importance: Int) {
/**
* Allowed to proceed the anvil process.
*/
NO_CONFLICT(0),
/**
* Inform that the anvil process should not change the current applied enchantment.
*/
SMALL_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);
fun getWorstConflict(otherConflict: ConflictType): ConflictType {
return if(this.importance > otherConflict.importance) this
else otherConflict
}
} }