prepare to use item adder dependency for clone

This commit is contained in:
alexcrea 2026-04-10 12:09:58 +02:00
parent 8141232c46
commit 190f334656
Signed by: alexcrea
GPG key ID: E346CD16413450E3
3 changed files with 69 additions and 64 deletions

View file

@ -5,10 +5,12 @@ import io.delilaheve.CustomAnvil
import net.kyori.adventure.text.Component
import org.bukkit.Bukkit
import org.bukkit.ChatColor
import org.bukkit.command.CommandSender
import org.bukkit.entity.HumanEntity
import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.event.inventory.PrepareAnvilEvent
import org.bukkit.inventory.AnvilInventory
import org.bukkit.inventory.Inventory
import org.bukkit.inventory.ItemStack
import xyz.alexcrea.cuanvil.api.event.listener.CAClickResultBypassEvent
import xyz.alexcrea.cuanvil.api.event.listener.CAEarlyPreAnvilBypassEvent
@ -46,6 +48,8 @@ object DependencyManager {
var axPlayerWarpsCompatibility: AxPlayerWarpsDependency? = null
var itemsAdderCompatibility: ItemsAdderDependency? = null
val genericDependencies = ArrayList<GenericPluginDependency>()
fun loadDependency() {
@ -98,6 +102,12 @@ object DependencyManager {
axPlayerWarpsCompatibility = AxPlayerWarpsDependency()
}
if (pluginManager.isPluginEnabled("ItemsAdder")){
val dependency = ItemsAdderDependency(pluginManager.getPlugin("ItemsAdder")!!)
itemsAdderCompatibility = dependency
genericDependencies.add(dependency)
}
// "Generic" dependencies
if (pluginManager.isPluginEnabled("ToolStats"))
genericDependencies.add(ToolStatsDependency(pluginManager.getPlugin("ToolStats")!!))
@ -138,12 +148,7 @@ object DependencyManager {
ecoEnchantCompatibility?.handleConfigReload()
}
// Return true if should bypass (either by a dependency or error)
// called before immutability test
fun earlyTryEventPreAnvilBypass(event: PrepareAnvilEvent, player: HumanEntity): Boolean {
try {
return earlyUnsafeTryEventPreAnvilBypass(event, player)
} catch (e: Exception) {
private fun logException(target: CommandSender, e: Exception) {
CustomAnvil.instance.logger.log(
Level.SEVERE,
"Error while trying to handle custom anvil supported plugin: ",
@ -151,14 +156,27 @@ object DependencyManager {
)
trackError(e)
// Just in case to avoid illegal items
event.inventory.setItem(ANVIL_OUTPUT_SLOT, null)
// Finally, warn the player, maybe a lot of time but better warn than do nothing
event.view.player.sendMessage(
// Finally, warn the player
target.sendMessage(
"[" + ChatColor.YELLOW.toString() + "CustomAnvil" + ChatColor.WHITE.toString() + "] " +
ChatColor.RED.toString() + "Error while handling the anvil."
)
}
private fun logExceptionAndClear(target: CommandSender, inventory: Inventory, e: Exception) {
// Just in case to avoid illegal items
inventory.setItem(ANVIL_OUTPUT_SLOT, null)
logException(target, e)
}
// Return true if should bypass (either by a dependency or error)
// called before immutability test
fun earlyTryEventPreAnvilBypass(event: PrepareAnvilEvent, player: HumanEntity): Boolean {
try {
return earlyUnsafeTryEventPreAnvilBypass(event, player)
} catch (e: Exception) {
logExceptionAndClear(event.view.player, event.inventory, e)
return true
}
}
@ -184,21 +202,7 @@ object DependencyManager {
try {
return unsafeTryEventPreAnvilBypass(event, player)
} catch (e: Exception) {
CustomAnvil.instance.logger.log(
Level.SEVERE,
"Error while trying to handle custom anvil supported plugin: ",
e
)
trackError(e)
// Just in case to avoid illegal items
event.inventory.setItem(ANVIL_OUTPUT_SLOT, null)
// Finally, warn the player, maybe a lot of time but better warn than do nothing
event.view.player.sendMessage(
"[" + ChatColor.YELLOW.toString() + "CustomAnvil" + ChatColor.WHITE.toString() + "] " +
ChatColor.RED.toString() + "Error while handling the anvil."
)
logExceptionAndClear(event.view.player, event.inventory, e)
return true
}
}
@ -238,21 +242,7 @@ object DependencyManager {
unsafeTryTreatAnvilResult(treatEvent)
return treatEvent;
} catch (e: Exception) {
CustomAnvil.instance.logger.log(
Level.SEVERE,
"Error while trying to handle custom anvil supported plugin: ",
e
)
trackError(e)
// Just in case to avoid illegal items
event.inventory.setItem(ANVIL_OUTPUT_SLOT, null)
// Finally, warn the player, maybe a lot of time but better warn than do nothing
event.view.player.sendMessage(
"[" + ChatColor.YELLOW.toString() + "CustomAnvil" + ChatColor.WHITE.toString() + "] " +
ChatColor.RED.toString() + "Error while handling the anvil."
)
logExceptionAndClear(event.view.player, event.inventory, e)
return null
}
}
@ -268,21 +258,7 @@ object DependencyManager {
try {
return unsafeTryClickAnvilResultBypass(event, inventory)
} catch (e: Exception) {
CustomAnvil.instance.logger.log(
Level.SEVERE,
"Error while trying to handle custom anvil supported plugin: ",
e
)
trackError(e)
// Just in case to avoid illegal items
event.inventory.setItem(ANVIL_OUTPUT_SLOT, null)
// Finally, warn the player, maybe a lot of time but better warn than do nothing
event.whoClicked.sendMessage(
"[" + ChatColor.YELLOW.toString() + "CustomAnvil" + ChatColor.WHITE.toString() + "] " +
ChatColor.RED.toString() + "Error while handling the anvil."
)
logExceptionAndClear(event.view.player, event.inventory, e)
return true
}
}
@ -316,6 +292,23 @@ object DependencyManager {
return bypass
}
// Clone item and use plugin specific clone if needed
fun cloneItem(event: PrepareAnvilEvent, item: ItemStack): ItemStack {
try {
return unsafeCloneItem(item)
} catch (e: Exception) {
logException(event.view.player, e)
return item.clone()
}
}
private fun unsafeCloneItem(item: ItemStack): ItemStack {
var cloned: ItemStack? = null
if(cloned == null) cloned = item.clone()
return cloned
}
fun stripLore(item: ItemStack): MutableList<Component?> {
val dummy = item.clone()

View file

@ -0,0 +1,12 @@
package xyz.alexcrea.cuanvil.dependency.plugins;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
public class ItemsAdderDependency extends GenericPluginDependency{
public ItemsAdderDependency(@NotNull Plugin plugin) {
super(plugin);
}
}

View file

@ -155,7 +155,7 @@ class PrepareAnvilListener : Listener {
val amount = CustomRecipeUtil.getCustomRecipeAmount(recipe, first, second)
val resultItem: ItemStack = recipe.resultItem!!.clone()
val resultItem: ItemStack = DependencyManager.cloneItem(event, recipe.resultItem!!)
resultItem.amount *= amount
// Maybe add an option on custom craft to ignore/not ignore penalty ??
@ -179,7 +179,7 @@ class PrepareAnvilListener : Listener {
event: PrepareAnvilEvent, inventory: AnvilInventory,
player: HumanEntity, first: ItemStack
) {
val resultItem = first.clone()
val resultItem = DependencyManager.cloneItem(event, first)
var anvilCost = handleRename(resultItem, inventory, player)
// Test/stop if nothing changed.
@ -251,7 +251,7 @@ class PrepareAnvilListener : Listener {
.combineWith(second.findEnchantments(), first, player)
var hasChanged = !isIdentical(first.findEnchantments(), newEnchants)
val resultItem = first.clone()
val resultItem = DependencyManager.cloneItem(event, first)
var anvilCost = 0
if(hasChanged){
resultItem.setEnchantmentsUnsafe(newEnchants)
@ -307,7 +307,7 @@ class PrepareAnvilListener : Listener {
): Boolean {
val unitRepairAmount = first.getRepair(second) ?: return false
val resultItem = first.clone()
val resultItem = DependencyManager.cloneItem(event, first)
var anvilCost = handleRename(resultItem, inventory, player)
val repairAmount = resultItem.unitRepair(second.amount, unitRepairAmount)