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 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.
* <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;
}
@ -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);
}

View file

@ -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

View file

@ -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)

View file

@ -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
)