add api for recipe changes

This commit is contained in:
alexcrea 2025-07-04 22:52:18 +02:00
parent 8914369d38
commit b3cc234ef3
Signed by: alexcrea
GPG key ID: E346CD16413450E3
6 changed files with 95 additions and 19 deletions

View file

@ -14,7 +14,10 @@ public class AnvilRecipeBuilder {
private @NotNull String name; private @NotNull String name;
private boolean exactCount; private boolean exactCount;
private int xpCostPerCraft; private int levelCostPerCraft;
private int linearXpCostPerCraft;
private boolean removeExactLinearXp;
private @Nullable ItemStack leftItem; private @Nullable ItemStack leftItem;
private @Nullable ItemStack rightItem; private @Nullable ItemStack rightItem;
@ -31,7 +34,7 @@ public class AnvilRecipeBuilder {
this.name = name; this.name = name;
this.exactCount = true; this.exactCount = true;
this.xpCostPerCraft = 1; this.levelCostPerCraft = 1;
this.leftItem = null; this.leftItem = null;
this.rightItem = null; this.rightItem = null;
@ -89,9 +92,11 @@ public class AnvilRecipeBuilder {
* Get the xp level cost per craft. * Get the xp level cost per craft.
* *
* @return 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() { public int getXpCostPerCraft() {
return xpCostPerCraft; return getLevelCostPerCraft();
} }
/** /**
@ -99,9 +104,78 @@ public class AnvilRecipeBuilder {
* *
* @param xpCostPerCraft The xp level cost per craft * @param xpCostPerCraft The xp level cost per craft
* @return This recipe builder instance. * @return This recipe builder instance.
* @deprecated use {@link #setLevelCostPerCraft(int) setLevelCostPerCraft} instead
*/ */
@Deprecated(since = "1.13.0")
public AnvilRecipeBuilder setXpCostPerCraft(int xpCostPerCraft) { 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.
* <p>
* 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
* <p>
* 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.
* <p>
* 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
* <p>
* 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; return this;
} }
@ -187,7 +261,9 @@ public class AnvilRecipeBuilder {
return new AnvilCustomRecipe( return new AnvilCustomRecipe(
this.name, this.name,
this.exactCount, this.exactCount,
this.xpCostPerCraft, this.levelCostPerCraft,
this.linearXpCostPerCraft,
this.removeExactLinearXp,
this.leftItem, this.rightItem, this.resultItem this.leftItem, this.rightItem, this.resultItem
); );
} }

View file

@ -132,12 +132,12 @@ class AnvilResultListener : Listener {
val amount = CustomRecipeUtil.getCustomRecipeAmount(recipe, leftItem, rightItem) val amount = CustomRecipeUtil.getCustomRecipeAmount(recipe, leftItem, rightItem)
val xpCost = recipe.determineCost(amount, leftItem, output) val xpCost = recipe.determineCost(amount, leftItem, output)
val finalCost = val finalCost =
if (recipe.removeExactXp) xpCost if (recipe.removeExactLinearXp) xpCost
else AnvilXpUtil.calculateLevelForXp(xpCost) else AnvilXpUtil.calculateLevelForXp(xpCost)
CustomAnvil.log("gamemode: ${player.gameMode != GameMode.CREATIVE}, cost: $finalCost, level: ${player.level}, result: ${player.totalExperience < finalCost} ${player.level < finalCost}") 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 (player.gameMode != GameMode.CREATIVE){
if(recipe.removeExactXp){ if(recipe.removeExactLinearXp){
if(player.totalExperience < finalCost) return if(player.totalExperience < finalCost) return
}else if(player.level < finalCost) return }else if(player.level < finalCost) return
} }
@ -149,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, finalCost, recipe.removeExactXp) !handleCustomCraftClick(event, recipe, inventory, player, leftItem, rightItem, amount, finalCost, recipe.removeExactLinearXp)
) return ) return
// Finally, we add the item to the player // Finally, we add the item to the player

View file

@ -139,7 +139,7 @@ class PrepareAnvilListener : Listener {
val xpCost = recipe.determineCost(amount, first, resultItem) val xpCost = recipe.determineCost(amount, first, resultItem)
val levelCost = val levelCost =
if (recipe.removeExactXp) AnvilXpUtil.calculateMinimumLevelForXp(xpCost) if (recipe.removeExactLinearXp) AnvilXpUtil.calculateMinimumLevelForXp(xpCost)
else AnvilXpUtil.calculateLevelForXp(xpCost) else AnvilXpUtil.calculateLevelForXp(xpCost)
AnvilXpUtil.setAnvilInvXp(inventory, event.view, player, levelCost, true) AnvilXpUtil.setAnvilInvXp(inventory, event.view, player, levelCost, true)

View file

@ -17,7 +17,7 @@ class AnvilCustomRecipe(
var levelCostPerCraft: Int, var levelCostPerCraft: Int,
var XpCostPerCraft: Int, var XpCostPerCraft: Int,
var removeExactXp: Boolean, var removeExactLinearXp: Boolean,
var leftItem: ItemStack?, var leftItem: ItemStack?,
var rightItem: ItemStack?, var rightItem: ItemStack?,
@ -96,7 +96,7 @@ class AnvilCustomRecipe(
fileConfig["$name.$XP_LEVEL_COST_CONFIG"] = levelCostPerCraft fileConfig["$name.$XP_LEVEL_COST_CONFIG"] = levelCostPerCraft
fileConfig["$name.$LINEAR_XP_COST_CONFIG"] = XpCostPerCraft 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.$LEFT_ITEM_CONFIG"] = leftItem
fileConfig["$name.$RIGHT_ITEM_CONFIG"] = rightItem fileConfig["$name.$RIGHT_ITEM_CONFIG"] = rightItem
@ -132,7 +132,7 @@ class AnvilCustomRecipe(
DEFAULT_LINEAR_XP_COST_CONFIG 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", "$name.$REMOVE_EXACT_XP_CONFIG",
DEFAULT_REMOVE_EXACT_XP_CONFIG DEFAULT_REMOVE_EXACT_XP_CONFIG
) )

View file

@ -63,9 +63,9 @@ public class AnvilRecipeBuilderTest extends SharedOnlyMockBukkit {
@Test @Test
void setXpCostPerCraft(){ void setXpCostPerCraft(){
assertEquals(1, builder.getXpCostPerCraft()); assertEquals(1, builder.getLevelCostPerCraft());
builder.setXpCostPerCraft(2); builder.setLevelCostPerCraft(2);
assertEquals(2, builder.getXpCostPerCraft()); assertEquals(2, builder.getLevelCostPerCraft());
} }
@Test @Test

View file

@ -61,7 +61,7 @@ public class CustomAnvilRecipeApiTests extends ConfigResetCustomAnvilTest {
// Add and test recipe // Add and test recipe
AnvilRecipeBuilder builder = new AnvilRecipeBuilder(recipeName); 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()); assertTrue(builder.registerIfAbsent());
AnvilFuseTestUtil.executeAnvilFuseTest(anvil, player, legalResultData); AnvilFuseTestUtil.executeAnvilFuseTest(anvil, player, legalResultData);
@ -125,7 +125,7 @@ public class CustomAnvilRecipeApiTests extends ConfigResetCustomAnvilTest {
builder.setExactCount(false) builder.setExactCount(false)
.setLeftItem(stick) .setLeftItem(stick)
.setResultItem(stick2) .setResultItem(stick2)
.setXpCostPerCraft(2); .setLevelCostPerCraft(2);
assertTrue(builder.registerIfAbsent()); assertTrue(builder.registerIfAbsent());