diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/AnvilResultListener.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/AnvilResultListener.kt index 27c99a2..8e37169 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/AnvilResultListener.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/AnvilResultListener.kt @@ -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,7 +178,11 @@ class AnvilResultListener : Listener { inventory.setItem(ANVIL_INPUT_LEFT, leftItem) 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 diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt index c6b3570..ad9a103 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt @@ -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 } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/recipe/AnvilCustomRecipe.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/recipe/AnvilCustomRecipe.kt index 07db447..6633a3d 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/recipe/AnvilCustomRecipe.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/recipe/AnvilCustomRecipe.kt @@ -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 + } + }