diff --git a/src/main/java/xyz/alexcrea/cuanvil/enchant/wrapped/CAEnchantSquaredEnchantment.java b/src/main/java/xyz/alexcrea/cuanvil/enchant/wrapped/CAEnchantSquaredEnchantment.java index 56b4bc7..ee5b9c0 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/enchant/wrapped/CAEnchantSquaredEnchantment.java +++ b/src/main/java/xyz/alexcrea/cuanvil/enchant/wrapped/CAEnchantSquaredEnchantment.java @@ -3,7 +3,6 @@ package xyz.alexcrea.cuanvil.enchant.wrapped; import me.athlaeos.enchantssquared.enchantments.CustomEnchant; import me.athlaeos.enchantssquared.managers.CustomEnchantManager; import org.bukkit.entity.HumanEntity; -import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.jetbrains.annotations.NotNull; diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiSharedConstant.java b/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiSharedConstant.java index f5b8a2e..66dd1bd 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiSharedConstant.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiSharedConstant.java @@ -9,7 +9,8 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import xyz.alexcrea.cuanvil.gui.config.MainConfigGui; -import java.util.*; +import java.util.Arrays; +import java.util.Collections; public class GuiSharedConstant { diff --git a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt index f5f3888..e2f92e9 100644 --- a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt +++ b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt @@ -2,7 +2,6 @@ package io.delilaheve import io.delilaheve.util.ConfigOptions import io.delilaheve.util.EnchantmentUtil.combineWith -import io.delilaheve.util.EnchantmentUtil.enchantmentName import io.delilaheve.util.ItemUtil.canMergeWith import io.delilaheve.util.ItemUtil.findEnchantments import io.delilaheve.util.ItemUtil.isEnchantedBook @@ -25,14 +24,14 @@ import org.bukkit.event.inventory.PrepareAnvilEvent import org.bukkit.inventory.AnvilInventory 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.dependency.DependencyManager import xyz.alexcrea.cuanvil.dependency.packet.PacketManager -import xyz.alexcrea.cuanvil.group.ConflictType import xyz.alexcrea.cuanvil.recipe.AnvilCustomRecipe +import xyz.alexcrea.cuanvil.util.AnvilXpUtil.calculatePenalty +import xyz.alexcrea.cuanvil.util.AnvilXpUtil.getRightValues +import xyz.alexcrea.cuanvil.util.AnvilXpUtil.setAnvilInvXp import xyz.alexcrea.cuanvil.util.UnitRepairUtil.getRepair -import xyz.alexcrea.cuanvil.util.XpSetterUtil.setAnvilInvXp import java.util.regex.Matcher import java.util.regex.Pattern import kotlin.math.min @@ -513,96 +512,6 @@ class AnvilEventListener(private val packetManager: PacketManager) : Listener { } } - /** - * Function to calculate work penalty of anvil work - * Also change result work penalty if right item is not null - */ - private fun calculatePenalty(left: ItemStack, right: ItemStack?, result: ItemStack): Int { - return calculatePenalty(left, right, result, false) - } - - /** - * Function to calculate work penalty of anvil work - * Also change result work penalty if right item is not null - */ - private fun calculatePenalty(left: ItemStack, right: ItemStack?, result: ItemStack, unitRepair: Boolean): Int { - // Extracted From https://minecraft.fandom.com/wiki/Anvil_mechanics#Enchantment_equation - // Calculate work penalty - val leftPenalty = (left.itemMeta as? Repairable)?.repairCost ?: 0 - val rightPenalty = - if (right == null) { - 0 - } else { - (right.itemMeta as? Repairable)?.repairCost ?: 0 - } - - // Increase penalty on fusing or unit repair - if(right != null || unitRepair){ - result.itemMeta?.let { - (it as? Repairable)?.repairCost = leftPenalty * 2 + 1 - result.itemMeta = it - - } - } - - CustomAnvil.log( - "Calculated penalty: " + - "leftPenalty: $leftPenalty, " + - "rightPenalty: $rightPenalty, " + - "result penalty: ${(result.itemMeta as? Repairable)?.repairCost ?: "none"}" - ) - - return leftPenalty + rightPenalty - } - - /** - * Function to calculate right enchantment values - * it include enchantment placed on final item and conflicting enchantment - */ - private fun getRightValues(right: ItemStack, result: ItemStack): Int { - // Calculate right value and illegal enchant penalty - var illegalPenalty = 0 - var rightValue = 0 - - val rightIsFormBook = right.isEnchantedBook() - val resultEnchs = result.findEnchantments() - val resultEnchsKeys = HashMap(resultEnchs) - - for (enchantment in right.findEnchantments()) { - // count enchant as illegal enchant if it conflicts with another enchant or not in result - if ((enchantment.key !in resultEnchsKeys)) { - resultEnchsKeys[enchantment.key] = enchantment.value - val conflictType = ConfigHolder.CONFLICT_HOLDER.conflictManager.isConflicting( - resultEnchsKeys, - result, - enchantment.key - ) - resultEnchsKeys.remove(enchantment.key) - - if (ConflictType.ENCHANTMENT_CONFLICT == conflictType) { - illegalPenalty += ConfigOptions.sacrificeIllegalCost - CustomAnvil.verboseLog("Big conflict. Adding illegal price penalty") - } - continue - } - // We know "enchantment.key in resultEnchs" true - val resultLevel = resultEnchs[enchantment.key]!! - - val enchantmentMultiplier = ConfigOptions.enchantmentValue(enchantment.key, rightIsFormBook) - val value = resultLevel * enchantmentMultiplier - CustomAnvil.log("Value for ${enchantment.key.enchantmentName} level ${enchantment.value} is $value ($resultLevel * $enchantmentMultiplier)") - rightValue += value - - } - CustomAnvil.log( - "Calculated right values: " + - "rightValue: $rightValue, " + - "illegalPenalty: $illegalPenalty" - ) - - return rightValue + illegalPenalty - } - private fun getCustomRecipe ( leftItem: ItemStack, rightItem: ItemStack?) : AnvilCustomRecipe? { diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/DisenchantmentDependency.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/DisenchantmentDependency.kt index e050a68..278e3ef 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/DisenchantmentDependency.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/DisenchantmentDependency.kt @@ -11,7 +11,7 @@ import org.bukkit.event.inventory.PrepareAnvilEvent import org.bukkit.inventory.AnvilInventory import org.bukkit.inventory.ItemStack import org.bukkit.plugin.RegisteredListener -import xyz.alexcrea.cuanvil.util.XpSetterUtil +import xyz.alexcrea.cuanvil.util.AnvilXpUtil class DisenchantmentDependency { @@ -80,14 +80,14 @@ class DisenchantmentDependency { if(event.result != null) { CustomAnvil.log("Detected pre anvil item extract bypass.") - XpSetterUtil.setAnvilInvXp(event.inventory, event.view, event.inventory.repairCost) + AnvilXpUtil.setAnvilInvXp(event.inventory, event.view, event.inventory.repairCost) return true } splitEvent.onDisenchantmentEvent(event) if(event.result != null) { CustomAnvil.log("Detected pre anvil split enchant bypass.") - XpSetterUtil.setAnvilInvXp(event.inventory, event.view, event.inventory.repairCost) + AnvilXpUtil.setAnvilInvXp(event.inventory, event.view, event.inventory.repairCost) return true } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/util/AnvilXpUtil.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/util/AnvilXpUtil.kt new file mode 100644 index 0000000..8b949ab --- /dev/null +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/util/AnvilXpUtil.kt @@ -0,0 +1,163 @@ +package xyz.alexcrea.cuanvil.util + +import io.delilaheve.CustomAnvil +import io.delilaheve.util.ConfigOptions +import io.delilaheve.util.EnchantmentUtil.enchantmentName +import io.delilaheve.util.ItemUtil.findEnchantments +import io.delilaheve.util.ItemUtil.isEnchantedBook +import org.bukkit.GameMode +import org.bukkit.entity.Player +import org.bukkit.inventory.AnvilInventory +import org.bukkit.inventory.InventoryView +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.dependency.DependencyManager +import xyz.alexcrea.cuanvil.group.ConflictType +import kotlin.math.min + +object AnvilXpUtil { + + /** + * Display xp needed for the work on the anvil inventory + */ + fun setAnvilInvXp( + inventory: AnvilInventory, + view: InventoryView, + anvilCost: Int, + ignoreRules: Boolean = false + ) { + // Test repair cost limit + val finalAnvilCost = if ( + !ignoreRules && + !ConfigOptions.doRemoveCostLimit && + ConfigOptions.doCapCost) { + min(anvilCost, ConfigOptions.maxAnvilCost) + } else { + anvilCost + } + + /* Because Minecraft likes to have the final say in the repair cost displayed + * we need to wait for the event to end before overriding it, this ensures that + * we have the final say in the process. */ + CustomAnvil.instance + .server + .scheduler + .runTask(CustomAnvil.instance, Runnable { + inventory.maximumRepairCost = + if (ConfigOptions.doRemoveCostLimit || ignoreRules) + { Int.MAX_VALUE } + else + { ConfigOptions.maxAnvilCost + 1 } + + val player = view.player + + inventory.repairCost = finalAnvilCost + view.setProperty(REPAIR_COST, finalAnvilCost) + player.openInventory.setProperty(REPAIR_COST, finalAnvilCost) + + if(player is Player){ + if(player.gameMode != GameMode.CREATIVE ){ + val bypassToExpensive = (ConfigOptions.doReplaceTooExpensive) && + (finalAnvilCost >= 40) && + finalAnvilCost < inventory.maximumRepairCost + + DependencyManager.packetManager.setInstantBuild(player, bypassToExpensive) + } + + player.updateInventory() + } + }) + } + /** + * Function to calculate work penalty of anvil work + * Also change result work penalty if right item is not null + */ + fun calculatePenalty(left: ItemStack, right: ItemStack?, result: ItemStack): Int { + return calculatePenalty(left, right, result, false) + } + + /** + * Function to calculate work penalty of anvil work + * Also change result work penalty if right item is not null + */ + fun calculatePenalty(left: ItemStack, right: ItemStack?, result: ItemStack, unitRepair: Boolean): Int { + // Extracted From https://minecraft.fandom.com/wiki/Anvil_mechanics#Enchantment_equation + // Calculate work penalty + val leftPenalty = (left.itemMeta as? Repairable)?.repairCost ?: 0 + val rightPenalty = + if (right == null) { + 0 + } else { + (right.itemMeta as? Repairable)?.repairCost ?: 0 + } + + // Increase penalty on fusing or unit repair + if(right != null || unitRepair){ + result.itemMeta?.let { + (it as? Repairable)?.repairCost = leftPenalty * 2 + 1 + result.itemMeta = it + + } + } + + CustomAnvil.log( + "Calculated penalty: " + + "leftPenalty: $leftPenalty, " + + "rightPenalty: $rightPenalty, " + + "result penalty: ${(result.itemMeta as? Repairable)?.repairCost ?: "none"}" + ) + + return leftPenalty + rightPenalty + } + + /** + * Function to calculate right enchantment values + * it include enchantment placed on final item and conflicting enchantment + */ + fun getRightValues(right: ItemStack, result: ItemStack): Int { + // Calculate right value and illegal enchant penalty + var illegalPenalty = 0 + var rightValue = 0 + + val rightIsFormBook = right.isEnchantedBook() + val resultEnchs = result.findEnchantments() + val resultEnchsKeys = HashMap(resultEnchs) + + for (enchantment in right.findEnchantments()) { + // count enchant as illegal enchant if it conflicts with another enchant or not in result + if ((enchantment.key !in resultEnchsKeys)) { + resultEnchsKeys[enchantment.key] = enchantment.value + val conflictType = ConfigHolder.CONFLICT_HOLDER.conflictManager.isConflicting( + resultEnchsKeys, + result, + enchantment.key + ) + resultEnchsKeys.remove(enchantment.key) + + if (ConflictType.ENCHANTMENT_CONFLICT == conflictType) { + illegalPenalty += ConfigOptions.sacrificeIllegalCost + CustomAnvil.verboseLog("Big conflict. Adding illegal price penalty") + } + continue + } + // We know "enchantment.key in resultEnchs" true + val resultLevel = resultEnchs[enchantment.key]!! + + val enchantmentMultiplier = ConfigOptions.enchantmentValue(enchantment.key, rightIsFormBook) + val value = resultLevel * enchantmentMultiplier + CustomAnvil.log("Value for ${enchantment.key.enchantmentName} level ${enchantment.value} is $value ($resultLevel * $enchantmentMultiplier)") + rightValue += value + + } + CustomAnvil.log( + "Calculated right values: " + + "rightValue: $rightValue, " + + "illegalPenalty: $illegalPenalty" + ) + + return rightValue + illegalPenalty + } + +} \ No newline at end of file diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/util/XpSetterUtil.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/util/XpSetterUtil.kt deleted file mode 100644 index d334099..0000000 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/util/XpSetterUtil.kt +++ /dev/null @@ -1,67 +0,0 @@ -package xyz.alexcrea.cuanvil.util - -import io.delilaheve.CustomAnvil -import io.delilaheve.util.ConfigOptions -import org.bukkit.GameMode -import org.bukkit.entity.Player -import org.bukkit.inventory.AnvilInventory -import org.bukkit.inventory.InventoryView -import org.bukkit.inventory.InventoryView.Property.REPAIR_COST -import xyz.alexcrea.cuanvil.dependency.DependencyManager -import kotlin.math.min - -object XpSetterUtil { - - /** - * Display xp needed for the work on the anvil inventory - */ - fun setAnvilInvXp( - inventory: AnvilInventory, - view: InventoryView, - anvilCost: Int, - ignoreRules: Boolean = false - ) { - // Test repair cost limit - val finalAnvilCost = if ( - !ignoreRules && - !ConfigOptions.doRemoveCostLimit && - ConfigOptions.doCapCost) { - min(anvilCost, ConfigOptions.maxAnvilCost) - } else { - anvilCost - } - - /* Because Minecraft likes to have the final say in the repair cost displayed - * we need to wait for the event to end before overriding it, this ensures that - * we have the final say in the process. */ - CustomAnvil.instance - .server - .scheduler - .runTask(CustomAnvil.instance, Runnable { - inventory.maximumRepairCost = - if (ConfigOptions.doRemoveCostLimit || ignoreRules) - { Int.MAX_VALUE } - else - { ConfigOptions.maxAnvilCost + 1 } - - val player = view.player - - inventory.repairCost = finalAnvilCost - view.setProperty(REPAIR_COST, finalAnvilCost) - player.openInventory.setProperty(REPAIR_COST, finalAnvilCost) - - if(player is Player){ - if(player.gameMode != GameMode.CREATIVE ){ - val bypassToExpensive = (ConfigOptions.doReplaceTooExpensive) && - (finalAnvilCost >= 40) && - finalAnvilCost < inventory.maximumRepairCost - - DependencyManager.packetManager.setInstantBuild(player, bypassToExpensive) - } - - player.updateInventory() - } - }) - } - -} \ No newline at end of file