fix issue about xp and item display

This commit is contained in:
alexcrea 2024-02-14 01:56:37 +01:00
parent c366b45e16
commit 1c802bc1c0

View file

@ -60,16 +60,18 @@ class AnvilEventListener : Listener {
if(second == null){ if(second == null){
val resultItem = first.clone() val resultItem = first.clone()
var anvilCost = handleRename(resultItem, inventory) var anvilCost = handleRename(resultItem, inventory)
anvilCost+= calculatePenalty(first,null,resultItem)
// Test/stop if nothing changed. // Test/stop if nothing changed.
if(first == resultItem){ if(first == resultItem){
event.result = null event.result = null
return return
} }
// We do set item here as vanilla do all of our job (renaming) // We don't manually set item here as vanilla do it (renaming)
//event.result = null
handleDisplayedXp(inventory, event, anvilCost) anvilCost+= calculatePenalty(first,null,resultItem)
handleAnvilXp(inventory, event, anvilCost)
return return
} }
@ -81,8 +83,9 @@ class AnvilEventListener : Listener {
val resultItem = first.clone() val resultItem = first.clone()
resultItem.setEnchantmentsUnsafe(newEnchants) resultItem.setEnchantmentsUnsafe(newEnchants)
var anvilCost = calculatePenalty(first, second, resultItem) // Calculate enchantment cost
anvilCost+= getRightValues(second, resultItem) var anvilCost = getRightValues(second, resultItem)
// Calculate repair cost
if (!first.isEnchantedBook() && !second.isEnchantedBook()) { if (!first.isEnchantedBook() && !second.isEnchantedBook()) {
// we only need to be concerned with repair when neither item is a book // we only need to be concerned with repair when neither item is a book
val repaired = resultItem.repairFrom(first, second) val repaired = resultItem.repairFrom(first, second)
@ -94,15 +97,15 @@ class AnvilEventListener : Listener {
event.result = null event.result = null
return return
} }
// As calculatePenalty edit result, we need to calculate penalty after checking equality
anvilCost+= calculatePenalty(first, second, resultItem)
// Calculate rename cost
anvilCost+= handleRename(resultItem, inventory) anvilCost+= handleRename(resultItem, inventory)
if (ConfigOptions.limitRepairCost) { // Finally, we set result
anvilCost = min(anvilCost, ConfigOptions.limitRepairValue)
}
event.result = resultItem event.result = resultItem
handleDisplayedXp(inventory, event, anvilCost) handleAnvilXp(inventory, event, anvilCost)
return return
} }
@ -111,13 +114,13 @@ class AnvilEventListener : Listener {
if(unitRepairAmount != null){ if(unitRepairAmount != null){
val resultItem = first.clone() val resultItem = first.clone()
var anvilCost = handleRename(resultItem, inventory) var anvilCost = handleRename(resultItem, inventory)
// We do not care about right item penalty for unit repair
anvilCost+= calculatePenalty(first,null,resultItem)
val repairAmount = resultItem.unitRepair(second.amount, unitRepairAmount) val repairAmount = resultItem.unitRepair(second.amount, unitRepairAmount)
if(repairAmount > 0){ if(repairAmount > 0){
anvilCost += repairAmount*ConfigOptions.unitRepairCost anvilCost += repairAmount*ConfigOptions.unitRepairCost
} }
// We do not care about right item penalty for unit repair
anvilCost+= calculatePenalty(first,null,resultItem)
// Test/stop if nothing changed. // Test/stop if nothing changed.
if(first == resultItem){ if(first == resultItem){
@ -126,7 +129,7 @@ class AnvilEventListener : Listener {
} }
event.result = resultItem event.result = resultItem
handleDisplayedXp(inventory, event, anvilCost) handleAnvilXp(inventory, event, anvilCost)
}else{ }else{
event.result = null event.result = null
} }
@ -162,6 +165,7 @@ class AnvilEventListener : Listener {
val allowed = (rightItem == null) val allowed = (rightItem == null)
|| (canMerge) || (canMerge)
|| (unitRepairResult != null) || (unitRepairResult != null)
// True if there was no change or not allowed // True if there was no change or not allowed
if((output == inventory.getItem(ANVIL_INPUT_LEFT)) if((output == inventory.getItem(ANVIL_INPUT_LEFT))
|| !allowed){ || !allowed){
@ -343,11 +347,17 @@ class AnvilEventListener : Listener {
/** /**
* Display xp needed for the work on the anvil inventory * Display xp needed for the work on the anvil inventory
*/ */
private fun handleDisplayedXp(inventory: AnvilInventory, private fun handleAnvilXp(inventory: AnvilInventory,
event: PrepareAnvilEvent, event: PrepareAnvilEvent,
anvilCost: Int){ anvilCost: Int){
inventory.maximumRepairCost = Int.MAX_VALUE // Test repair cost limit
inventory.repairCost = anvilCost val finalAnvilCost: Int
if (ConfigOptions.limitRepairCost) {
finalAnvilCost = min(anvilCost, ConfigOptions.limitRepairValue)
}else{
finalAnvilCost = anvilCost
}
/* Because Minecraft likes to have the final say in the repair cost displayed /* 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 need to wait for the event to end before overriding it, this ensures that
* we have the final say in the process. */ * we have the final say in the process. */
@ -358,7 +368,7 @@ class AnvilEventListener : Listener {
if (ConfigOptions.removeRepairLimit) { if (ConfigOptions.removeRepairLimit) {
inventory.maximumRepairCost = Int.MAX_VALUE inventory.maximumRepairCost = Int.MAX_VALUE
} }
inventory.repairCost = anvilCost inventory.repairCost = finalAnvilCost
event.view.setProperty(REPAIR_COST, anvilCost) event.view.setProperty(REPAIR_COST, anvilCost)
}) })