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

View file

@ -136,12 +136,13 @@ class PrepareAnvilListener : Listener {
event.result = resultItem event.result = resultItem
if (DependencyManager.tryTreatAnvilResult(event, resultItem)) return true if (DependencyManager.tryTreatAnvilResult(event, resultItem)) return true
// Maybe add an option on custom craft to ignore/not ignore penalty ?? val xpCost = recipe.determineCost(amount, first, resultItem)
var xpCost = recipe.levelCostPerCraft * amount
xpCost += AnvilXpUtil.calculatePenalty(first, null, resultItem, AnvilUseType.CUSTOM_CRAFT)
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 return true
} }

View file

@ -5,6 +5,8 @@ import org.bukkit.configuration.ConfigurationSection
import org.bukkit.inventory.ItemStack import org.bukkit.inventory.ItemStack
import xyz.alexcrea.cuanvil.config.ConfigHolder import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant
import xyz.alexcrea.cuanvil.util.AnvilUseType
import xyz.alexcrea.cuanvil.util.AnvilXpUtil
class AnvilCustomRecipe( class AnvilCustomRecipe(
val name: String, val name: String,
@ -196,5 +198,18 @@ class AnvilCustomRecipe(
return name 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
}
} }