diff --git a/src/main/java/xyz/alexcrea/cuanvil/api/AnvilRecipeBuilder.java b/src/main/java/xyz/alexcrea/cuanvil/api/AnvilRecipeBuilder.java index 9d33bfb..76b4a68 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/api/AnvilRecipeBuilder.java +++ b/src/main/java/xyz/alexcrea/cuanvil/api/AnvilRecipeBuilder.java @@ -14,7 +14,10 @@ public class AnvilRecipeBuilder { private @NotNull String name; private boolean exactCount; - private int xpCostPerCraft; + private int levelCostPerCraft; + private int linearXpCostPerCraft; + + private boolean removeExactLinearXp; private @Nullable ItemStack leftItem; private @Nullable ItemStack rightItem; @@ -31,7 +34,7 @@ public class AnvilRecipeBuilder { this.name = name; this.exactCount = true; - this.xpCostPerCraft = 1; + this.levelCostPerCraft = 1; this.leftItem = null; this.rightItem = null; @@ -89,9 +92,11 @@ public class AnvilRecipeBuilder { * Get the xp level cost per craft. * * @return The xp level cost per craft + * @deprecated use {@link #getLevelCostPerCraft() getLevelCostPerCraft} instead */ + @Deprecated(since = "1.13.0") public int getXpCostPerCraft() { - return xpCostPerCraft; + return getLevelCostPerCraft(); } /** @@ -99,9 +104,78 @@ public class AnvilRecipeBuilder { * * @param xpCostPerCraft The xp level cost per craft * @return This recipe builder instance. + * @deprecated use {@link #setLevelCostPerCraft(int) setLevelCostPerCraft} instead */ + @Deprecated(since = "1.13.0") public AnvilRecipeBuilder setXpCostPerCraft(int xpCostPerCraft) { - this.xpCostPerCraft = xpCostPerCraft; + return setLevelCostPerCraft(xpCostPerCraft); + } + + /** + * Get the xp level cost per craft. + * + * @return The xp level cost per craft + */ + public int getLevelCostPerCraft() { + return levelCostPerCraft; + } + + /** + * Sets the xp level cost per craft. + * + * @param levelCostPerCraft The xp level cost per craft + * @return This recipe builder instance. + */ + public AnvilRecipeBuilder setLevelCostPerCraft(int levelCostPerCraft) { + this.levelCostPerCraft = levelCostPerCraft; + return this; + } + + /** + * Get the linear xp cost (not xp level cost) per craft. + * + * @return The xp level cost per craft + */ + public int getLinearXpCostPerCraft() { + return linearXpCostPerCraft; + } + + /** + * Sets the linear xp cost (not xp level cost) per craft. + * + * @param linearXpCostPerCraft The linear xp cost per craft + * @return This recipe builder instance. + */ + public AnvilRecipeBuilder setLinearXpCostPerCraft(int linearXpCostPerCraft) { + this.linearXpCostPerCraft = linearXpCostPerCraft; + return this; + } + + /** + * Get if the linear xp should get removed by an exact amount. + *
+ * If false (default) level cost will be the level that would be reached by a player with this amount of xp. + * If true will require the level that has at least the specified level of xp then on click remove only the necessary xp + *
+ * linear xp cost are applied after level cost + * @return if we should remove the exact amount of linear xp + */ + public boolean isRemoveExactLinearXp() { + return removeExactLinearXp; + } + + /** + * Set if the linear xp should get removed by an exact amount. + *
+ * If false (default) level cost will be the level that would be reached by a player with this amount of xp. + * If true will require the level that has at least the specified level of xp then on click remove only the necessary xp + *
+ * linear xp cost are applied after level cost + * @param removeExactLinearXp if we should remove the exact amount of linear xp + * @return This recipe builder instance. + */ + public AnvilRecipeBuilder setRemoveExactLinearXp(boolean removeExactLinearXp) { + this.removeExactLinearXp = removeExactLinearXp; return this; } @@ -182,12 +256,14 @@ public class AnvilRecipeBuilder { */ @Nullable // null if missing argument public AnvilCustomRecipe build() { - if(leftItem == null || resultItem == null) return null; + if (leftItem == null || resultItem == null) return null; return new AnvilCustomRecipe( this.name, this.exactCount, - this.xpCostPerCraft, + this.levelCostPerCraft, + this.linearXpCostPerCraft, + this.removeExactLinearXp, this.leftItem, this.rightItem, this.resultItem ); } @@ -198,7 +274,7 @@ public class AnvilRecipeBuilder { * * @return True if successful. */ - public boolean registerIfAbsent(){ + public boolean registerIfAbsent() { return CustomAnvilRecipeApi.addRecipe(this); } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/AnvilResultListener.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/AnvilResultListener.kt index 8e37169..74c62c0 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/AnvilResultListener.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/AnvilResultListener.kt @@ -132,12 +132,12 @@ class AnvilResultListener : Listener { val amount = CustomRecipeUtil.getCustomRecipeAmount(recipe, leftItem, rightItem) val xpCost = recipe.determineCost(amount, leftItem, output) val finalCost = - if (recipe.removeExactXp) xpCost + if (recipe.removeExactLinearXp) xpCost else AnvilXpUtil.calculateLevelForXp(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){ - if(recipe.removeExactXp){ + if(recipe.removeExactLinearXp){ if(player.totalExperience < finalCost) return }else if(player.level < finalCost) return } @@ -149,7 +149,7 @@ class AnvilResultListener : Listener { // Handle not creative middle click... if (event.click != ClickType.MIDDLE && - !handleCustomCraftClick(event, recipe, inventory, player, leftItem, rightItem, amount, finalCost, recipe.removeExactXp) + !handleCustomCraftClick(event, recipe, inventory, player, leftItem, rightItem, amount, finalCost, recipe.removeExactLinearXp) ) return // Finally, we add the item to the player diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt index ad9a103..604c20f 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt @@ -139,7 +139,7 @@ class PrepareAnvilListener : Listener { val xpCost = recipe.determineCost(amount, first, resultItem) val levelCost = - if (recipe.removeExactXp) AnvilXpUtil.calculateMinimumLevelForXp(xpCost) + if (recipe.removeExactLinearXp) AnvilXpUtil.calculateMinimumLevelForXp(xpCost) else AnvilXpUtil.calculateLevelForXp(xpCost) AnvilXpUtil.setAnvilInvXp(inventory, event.view, player, levelCost, true) diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/recipe/AnvilCustomRecipe.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/recipe/AnvilCustomRecipe.kt index 6633a3d..fa1a977 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/recipe/AnvilCustomRecipe.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/recipe/AnvilCustomRecipe.kt @@ -17,7 +17,7 @@ class AnvilCustomRecipe( var levelCostPerCraft: Int, var XpCostPerCraft: Int, - var removeExactXp: Boolean, + var removeExactLinearXp: Boolean, var leftItem: ItemStack?, var rightItem: ItemStack?, @@ -96,7 +96,7 @@ class AnvilCustomRecipe( fileConfig["$name.$XP_LEVEL_COST_CONFIG"] = levelCostPerCraft fileConfig["$name.$LINEAR_XP_COST_CONFIG"] = XpCostPerCraft - fileConfig["$name.$REMOVE_EXACT_XP_CONFIG"] = removeExactXp + fileConfig["$name.$REMOVE_EXACT_XP_CONFIG"] = removeExactLinearXp fileConfig["$name.$LEFT_ITEM_CONFIG"] = leftItem fileConfig["$name.$RIGHT_ITEM_CONFIG"] = rightItem @@ -132,7 +132,7 @@ class AnvilCustomRecipe( DEFAULT_LINEAR_XP_COST_CONFIG ) - this.removeExactXp = ConfigHolder.CUSTOM_RECIPE_HOLDER.config.getBoolean( + this.removeExactLinearXp = ConfigHolder.CUSTOM_RECIPE_HOLDER.config.getBoolean( "$name.$REMOVE_EXACT_XP_CONFIG", DEFAULT_REMOVE_EXACT_XP_CONFIG ) diff --git a/src/test/java/xyz/alexcrea/cuanvil/api/AnvilRecipeBuilderTest.java b/src/test/java/xyz/alexcrea/cuanvil/api/AnvilRecipeBuilderTest.java index 2b57bda..9a65e12 100644 --- a/src/test/java/xyz/alexcrea/cuanvil/api/AnvilRecipeBuilderTest.java +++ b/src/test/java/xyz/alexcrea/cuanvil/api/AnvilRecipeBuilderTest.java @@ -63,9 +63,9 @@ public class AnvilRecipeBuilderTest extends SharedOnlyMockBukkit { @Test void setXpCostPerCraft(){ - assertEquals(1, builder.getXpCostPerCraft()); - builder.setXpCostPerCraft(2); - assertEquals(2, builder.getXpCostPerCraft()); + assertEquals(1, builder.getLevelCostPerCraft()); + builder.setLevelCostPerCraft(2); + assertEquals(2, builder.getLevelCostPerCraft()); } @Test diff --git a/src/test/java/xyz/alexcrea/cuanvil/api/CustomAnvilRecipeApiTests.java b/src/test/java/xyz/alexcrea/cuanvil/api/CustomAnvilRecipeApiTests.java index 3ebdd7c..c93b7e7 100644 --- a/src/test/java/xyz/alexcrea/cuanvil/api/CustomAnvilRecipeApiTests.java +++ b/src/test/java/xyz/alexcrea/cuanvil/api/CustomAnvilRecipeApiTests.java @@ -61,7 +61,7 @@ public class CustomAnvilRecipeApiTests extends ConfigResetCustomAnvilTest { // Add and test recipe AnvilRecipeBuilder builder = new AnvilRecipeBuilder(recipeName); - builder.setExactCount(true).setLeftItem(stick).setResultItem(stick).setXpCostPerCraft(2); + builder.setExactCount(true).setLeftItem(stick).setResultItem(stick).setLevelCostPerCraft(2); assertTrue(builder.registerIfAbsent()); AnvilFuseTestUtil.executeAnvilFuseTest(anvil, player, legalResultData); @@ -125,7 +125,7 @@ public class CustomAnvilRecipeApiTests extends ConfigResetCustomAnvilTest { builder.setExactCount(false) .setLeftItem(stick) .setResultItem(stick2) - .setXpCostPerCraft(2); + .setLevelCostPerCraft(2); assertTrue(builder.registerIfAbsent());