mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +02:00
Moved error handling responsibility
also handle safely tryTreatAnvilResult
This commit is contained in:
parent
0df33911ce
commit
48cc28442d
3 changed files with 64 additions and 40 deletions
|
|
@ -2,6 +2,7 @@ package xyz.alexcrea.cuanvil.dependency
|
|||
|
||||
import io.delilaheve.CustomAnvil
|
||||
import org.bukkit.Bukkit
|
||||
import org.bukkit.ChatColor
|
||||
import org.bukkit.entity.HumanEntity
|
||||
import org.bukkit.event.inventory.InventoryClickEvent
|
||||
import org.bukkit.event.inventory.PrepareAnvilEvent
|
||||
|
|
@ -15,6 +16,8 @@ import xyz.alexcrea.cuanvil.dependency.packet.PacketManagerSelector
|
|||
import xyz.alexcrea.cuanvil.dependency.scheduler.BukkitScheduler
|
||||
import xyz.alexcrea.cuanvil.dependency.scheduler.FoliaScheduler
|
||||
import xyz.alexcrea.cuanvil.dependency.scheduler.TaskScheduler
|
||||
import xyz.alexcrea.cuanvil.listener.PrepareAnvilListener.Companion.ANVIL_OUTPUT_SLOT
|
||||
import java.util.logging.Level
|
||||
|
||||
object DependencyManager {
|
||||
|
||||
|
|
@ -96,10 +99,25 @@ object DependencyManager {
|
|||
|
||||
// Then handle plugin reload
|
||||
ecoEnchantCompatibility?.handleConfigReload()
|
||||
|
||||
}
|
||||
|
||||
// Return true if should bypass (either by a dependency or error)
|
||||
fun tryEventPreAnvilBypass(event: PrepareAnvilEvent, player: HumanEntity): Boolean {
|
||||
try {
|
||||
return unsafeTryEventPreAnvilBypass(event, player)
|
||||
} catch (e: Exception){
|
||||
CustomAnvil.instance.logger.log(Level.SEVERE, "Error while trying to handle custom anvil supported plugin: ", 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.RED.toString() + "Error while handling the anvil.")
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
private fun unsafeTryEventPreAnvilBypass(event: PrepareAnvilEvent, player: HumanEntity): Boolean {
|
||||
var bypass = false
|
||||
|
||||
// Test if disenchantment used prepare anvil
|
||||
|
|
@ -118,11 +136,44 @@ object DependencyManager {
|
|||
return bypass
|
||||
}
|
||||
|
||||
fun treatAnvilResult(event: PrepareAnvilEvent, result: ItemStack) {
|
||||
// Return true only if error occurred (and so should bypass rest)
|
||||
fun tryTreatAnvilResult(event: PrepareAnvilEvent, result: ItemStack): Boolean {
|
||||
try {
|
||||
unsafeTryTreatAnvilResult(event, result)
|
||||
return false
|
||||
} catch (e: Exception){
|
||||
CustomAnvil.instance.logger.log(Level.SEVERE, "Error while trying to handle custom anvil supported plugin: ", 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.RED.toString() + "Error while handling the anvil.")
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
private fun unsafeTryTreatAnvilResult(event: PrepareAnvilEvent, result: ItemStack) {
|
||||
excellentEnchantsCompatibility?.treatAnvilResult(event, result)
|
||||
}
|
||||
|
||||
// Return true if should bypass (either by a dependency or error)
|
||||
fun tryClickAnvilResultBypass(event: InventoryClickEvent, inventory: AnvilInventory): Boolean {
|
||||
try {
|
||||
return unsafeTryClickAnvilResultBypass(event, inventory)
|
||||
} catch (e: Exception){
|
||||
CustomAnvil.instance.logger.log(Level.SEVERE, "Error while trying to handle custom anvil supported plugin: ", 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.RED.toString() + "Error while handling the anvil.")
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
private fun unsafeTryClickAnvilResultBypass(event: InventoryClickEvent, inventory: AnvilInventory): Boolean {
|
||||
var bypass = false
|
||||
|
||||
// Test if disenchantment used event click
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ import io.delilaheve.CustomAnvil
|
|||
import io.delilaheve.util.ConfigOptions
|
||||
import io.delilaheve.util.ItemUtil.canMergeWith
|
||||
import io.delilaheve.util.ItemUtil.unitRepair
|
||||
import org.bukkit.ChatColor
|
||||
import org.bukkit.GameMode
|
||||
import org.bukkit.Material
|
||||
import org.bukkit.entity.Player
|
||||
|
|
@ -24,7 +23,6 @@ import xyz.alexcrea.cuanvil.recipe.AnvilCustomRecipe
|
|||
import xyz.alexcrea.cuanvil.util.AnvilXpUtil
|
||||
import xyz.alexcrea.cuanvil.util.CustomRecipeUtil
|
||||
import xyz.alexcrea.cuanvil.util.UnitRepairUtil.getRepair
|
||||
import java.util.logging.Level
|
||||
import kotlin.math.min
|
||||
|
||||
class AnvilResultListener: Listener {
|
||||
|
|
@ -49,26 +47,13 @@ class AnvilResultListener: Listener {
|
|||
}
|
||||
|
||||
// Test if the event should bypass custom anvil.
|
||||
var shouldBypass: Boolean
|
||||
try {
|
||||
shouldBypass = DependencyManager.tryClickAnvilResultBypass(event, inventory)
|
||||
} catch (e: Exception){
|
||||
shouldBypass = true
|
||||
CustomAnvil.instance.logger.log(Level.SEVERE, "Error while trying to handle custom anvil supported plugin: ", 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
|
||||
player.sendMessage(ChatColor.RED.toString() + "Error while handling the anvil.")
|
||||
}
|
||||
if(shouldBypass) return
|
||||
if(DependencyManager.tryClickAnvilResultBypass(event, inventory)) return
|
||||
|
||||
val output = inventory.getItem(ANVIL_OUTPUT_SLOT) ?: return
|
||||
val leftItem = inventory.getItem(ANVIL_INPUT_LEFT) ?: return
|
||||
val rightItem = inventory.getItem(ANVIL_INPUT_RIGHT)
|
||||
|
||||
if(!GameMode.CREATIVE.equals(player.gameMode) && inventory.repairCost >= inventory.maximumRepairCost) {
|
||||
if(GameMode.CREATIVE != player.gameMode && inventory.repairCost >= inventory.maximumRepairCost) {
|
||||
event.result = Event.Result.DENY
|
||||
return
|
||||
}
|
||||
|
|
@ -112,7 +97,6 @@ class AnvilResultListener: Listener {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
private fun onCustomCraft(event: InventoryClickEvent,
|
||||
recipe: AnvilCustomRecipe,
|
||||
player: Player,
|
||||
|
|
@ -138,7 +122,7 @@ class AnvilResultListener: Listener {
|
|||
|
||||
// Handle not creative middle click...
|
||||
if (event.click != ClickType.MIDDLE &&
|
||||
!handleCustomCraftClick(event, recipe, inventory, player, leftItem, rightItem, amount, xpCost)) return;
|
||||
!handleCustomCraftClick(event, recipe, inventory, player, leftItem, rightItem, amount, xpCost)) return
|
||||
|
||||
// Finally, we add the item to the player
|
||||
if (slotDestination.type == SlotType.CURSOR) {
|
||||
|
|
@ -238,7 +222,7 @@ class AnvilResultListener: Listener {
|
|||
resultCopy: ItemStack, resultAmount: Int): Int {
|
||||
if (player.gameMode == GameMode.CREATIVE) return 0
|
||||
|
||||
var repairCost = 0;
|
||||
var repairCost = 0
|
||||
// Get repairCost
|
||||
leftItem.itemMeta?.let { leftMeta ->
|
||||
val leftName = leftMeta.displayName
|
||||
|
|
|
|||
|
|
@ -46,20 +46,7 @@ class PrepareAnvilListener : Listener {
|
|||
val player: HumanEntity = event.viewers.first()
|
||||
|
||||
// Test if the event should bypass custom anvil.
|
||||
var shouldBypass: Boolean
|
||||
try {
|
||||
shouldBypass = DependencyManager.tryEventPreAnvilBypass(event, player)
|
||||
} catch (e: Exception){
|
||||
shouldBypass = true
|
||||
CustomAnvil.instance.logger.log(Level.SEVERE, "Error while trying to handle custom anvil supported plugin: ", 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
|
||||
player.sendMessage(ChatColor.RED.toString() + "Error while handling the anvil.")
|
||||
}
|
||||
if(shouldBypass) return
|
||||
if (DependencyManager.tryEventPreAnvilBypass(event, player)) return
|
||||
|
||||
val inventory = event.inventory
|
||||
val first = inventory.getItem(ANVIL_INPUT_LEFT) ?: return
|
||||
|
|
@ -90,6 +77,7 @@ class PrepareAnvilListener : Listener {
|
|||
|
||||
}
|
||||
|
||||
// return true if a custom recipe exist with these ingredient
|
||||
private fun testCustomRecipe(event: PrepareAnvilEvent, inventory: AnvilInventory,
|
||||
player: HumanEntity,
|
||||
first: ItemStack, second: ItemStack?): Boolean {
|
||||
|
|
@ -103,7 +91,7 @@ class PrepareAnvilListener : Listener {
|
|||
resultItem.amount *= amount
|
||||
|
||||
event.result = resultItem
|
||||
DependencyManager.treatAnvilResult(event, resultItem)
|
||||
if(DependencyManager.tryTreatAnvilResult(event, resultItem)) return true
|
||||
AnvilXpUtil.setAnvilInvXp(inventory, event.view, player, recipe.xpCostPerCraft * amount, true)
|
||||
|
||||
return true
|
||||
|
|
@ -122,7 +110,7 @@ class PrepareAnvilListener : Listener {
|
|||
}
|
||||
|
||||
event.result = resultItem
|
||||
DependencyManager.treatAnvilResult(event, resultItem)
|
||||
if(DependencyManager.tryTreatAnvilResult(event, resultItem)) return
|
||||
|
||||
anvilCost += AnvilXpUtil.calculatePenalty(first, null, resultItem)
|
||||
|
||||
|
|
@ -195,11 +183,12 @@ class PrepareAnvilListener : Listener {
|
|||
|
||||
// Finally, we set result
|
||||
event.result = resultItem
|
||||
DependencyManager.treatAnvilResult(event, resultItem)
|
||||
if(DependencyManager.tryTreatAnvilResult(event, resultItem)) return
|
||||
|
||||
AnvilXpUtil.setAnvilInvXp(inventory, event.view, player, anvilCost)
|
||||
}
|
||||
|
||||
// return true if there is a valid unit repair with these ingredients
|
||||
private fun testUnitRepair(event: PrepareAnvilEvent, inventory: AnvilInventory, player: HumanEntity,
|
||||
first: ItemStack, second: ItemStack): Boolean {
|
||||
val unitRepairAmount = first.getRepair(second) ?: return false
|
||||
|
|
@ -221,7 +210,7 @@ class PrepareAnvilListener : Listener {
|
|||
return true
|
||||
}
|
||||
event.result = resultItem
|
||||
DependencyManager.treatAnvilResult(event, resultItem)
|
||||
if(DependencyManager.tryTreatAnvilResult(event, resultItem)) return true
|
||||
|
||||
AnvilXpUtil.setAnvilInvXp(inventory, event.view, player, anvilCost)
|
||||
return true
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue