linear cost logic

This commit is contained in:
alexcrea 2025-07-04 15:48:59 +02:00
parent ec4351e70d
commit 8f3c721820
Signed by: alexcrea
GPG key ID: E346CD16413450E3
3 changed files with 37 additions and 10 deletions

View file

@ -130,10 +130,17 @@ class AnvilResultListener : Listener {
if (recipe.leftItem == null) return // in case it changed
val amount = CustomRecipeUtil.getCustomRecipeAmount(recipe, leftItem, rightItem)
val xpCost = amount * recipe.levelCostPerCraft
val xpCost = recipe.determineCost(amount, leftItem, output)
val finalCost =
if (recipe.removeExactXp) xpCost
else AnvilXpUtil.calculateLevelForXp(xpCost)
CustomAnvil.log("gamemode: ${player.gameMode != GameMode.CREATIVE}, cost: $xpCost, level: ${player.level}, result: ${player.level < xpCost}")
if ((player.gameMode != GameMode.CREATIVE) && (player.level < xpCost)) return
CustomAnvil.log("gamemode: ${player.gameMode != GameMode.CREATIVE}, cost: $finalCost, level: ${player.level}, result: ${player.totalExperience < finalCost} ${player.level < finalCost}")
if (player.gameMode != GameMode.CREATIVE){
if(recipe.removeExactXp){
if(player.totalExperience < finalCost) return
}else if(player.level < finalCost) return
}
// We give the item manually
// But first we check if we should give the item
@ -142,7 +149,7 @@ class AnvilResultListener : Listener {
// Handle not creative middle click...
if (event.click != ClickType.MIDDLE &&
!handleCustomCraftClick(event, recipe, inventory, player, leftItem, rightItem, amount, xpCost)
!handleCustomCraftClick(event, recipe, inventory, player, leftItem, rightItem, amount, finalCost, recipe.removeExactXp)
) return
// Finally, we add the item to the player
@ -157,7 +164,7 @@ class AnvilResultListener : Listener {
event: InventoryClickEvent, recipe: AnvilCustomRecipe,
inventory: AnvilInventory, player: Player,
leftItem: ItemStack, rightItem: ItemStack?,
amount: Int, xpCost: Int
amount: Int, xpCost: Int, linearCost: Boolean = false
): Boolean {
// We remove what should be removed
if (rightItem != null) {
@ -171,8 +178,12 @@ class AnvilResultListener : Listener {
inventory.setItem(ANVIL_INPUT_LEFT, leftItem)
if (player.gameMode != GameMode.CREATIVE) {
if(linearCost){
player.totalExperience -= xpCost
} else{
player.level -= xpCost
}
}
// Then we try to find the new values for the anvil
val newAmount = CustomRecipeUtil.getCustomRecipeAmount(recipe, leftItem, rightItem)

View file

@ -136,12 +136,13 @@ class PrepareAnvilListener : Listener {
event.result = resultItem
if (DependencyManager.tryTreatAnvilResult(event, resultItem)) return true
// Maybe add an option on custom craft to ignore/not ignore penalty ??
var xpCost = recipe.levelCostPerCraft * amount
xpCost += AnvilXpUtil.calculatePenalty(first, null, resultItem, AnvilUseType.CUSTOM_CRAFT)
val xpCost = recipe.determineCost(amount, first, resultItem)
AnvilXpUtil.setAnvilInvXp(inventory, event.view, player, xpCost, true)
val levelCost =
if (recipe.removeExactXp) AnvilXpUtil.calculateMinimumLevelForXp(xpCost)
else AnvilXpUtil.calculateLevelForXp(xpCost)
AnvilXpUtil.setAnvilInvXp(inventory, event.view, player, levelCost, true)
return true
}

View file

@ -5,6 +5,8 @@ import org.bukkit.configuration.ConfigurationSection
import org.bukkit.inventory.ItemStack
import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant
import xyz.alexcrea.cuanvil.util.AnvilUseType
import xyz.alexcrea.cuanvil.util.AnvilXpUtil
class AnvilCustomRecipe(
val name: String,
@ -196,5 +198,18 @@ class AnvilCustomRecipe(
return name
}
fun determineCost(amount: Int, first: ItemStack, resultItem: ItemStack): Int {
// First we determine the non linear level cost
var levelCost = levelCostPerCraft * amount
// TODO Maybe add an option per custom craft to ignore/not ignore penalty ??
levelCost += AnvilXpUtil.calculatePenalty(first, null, resultItem, AnvilUseType.CUSTOM_CRAFT)
var xpCost = AnvilXpUtil.calculateXpForLevel(levelCost)
// Then we add the linear cost
xpCost += XpCostPerCraft * amount
return xpCost
}
}