SPIGOT-5766: Updates related to the villager trading changes introduced in MC 1.14.

* The VillagerReplenishTradeEvent is now called whenever a villager restocks one of its trades. Previously, it was called in some (but not all) cases in which a villager updates the special price values of its merchant recipes.
* VillagerReplenishTradeEvent#get/setBonus have been deprecated and #setBonus has no effect anymore. The way villagers restock their trades has changed in MC 1.14: Instead of adjusting the max uses of their trades, they reset their uses to 0. #getBonus returns the value of MerchantRecipe#getUses now.
* Updated various outdated and inaccurate javadoc in MerchantRecipe.

By: blablubbabc <lukas@wirsindwir.de>
This commit is contained in:
Bukkit/Spigot
2021-12-25 10:14:12 +11:00
parent 489aef2efc
commit e9b09df87c
2 changed files with 30 additions and 42 deletions

View File

@@ -1,16 +1,18 @@
package org.bukkit.event.entity; package org.bukkit.event.entity;
import org.bukkit.entity.AbstractVillager; import org.bukkit.entity.AbstractVillager;
import org.bukkit.entity.Villager;
import org.bukkit.event.Cancellable; import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.bukkit.inventory.MerchantRecipe; import org.bukkit.inventory.MerchantRecipe;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* Called when a villager's trade's maximum uses is increased, due to a player's * Called when a {@link Villager} is about to restock one of its trades.
* trade. * <p>
* * If this event passes, the villager will reset the
* @see MerchantRecipe#getMaxUses() * {@link MerchantRecipe#getUses() uses} of the affected {@link #getRecipe()
* MerchantRecipe} to <code>0</code>.
*/ */
public class VillagerReplenishTradeEvent extends EntityEvent implements Cancellable { public class VillagerReplenishTradeEvent extends EntityEvent implements Cancellable {
@@ -18,12 +20,10 @@ public class VillagerReplenishTradeEvent extends EntityEvent implements Cancella
private boolean cancelled; private boolean cancelled;
// //
private MerchantRecipe recipe; private MerchantRecipe recipe;
private int bonus;
public VillagerReplenishTradeEvent(@NotNull AbstractVillager what, @NotNull MerchantRecipe recipe, int bonus) { public VillagerReplenishTradeEvent(@NotNull AbstractVillager what, @NotNull MerchantRecipe recipe) {
super(what); super(what);
this.recipe = recipe; this.recipe = recipe;
this.bonus = bonus;
} }
/** /**
@@ -46,23 +46,26 @@ public class VillagerReplenishTradeEvent extends EntityEvent implements Cancella
} }
/** /**
* Get the bonus uses added. The maximum uses of the recipe will be * Get the bonus uses added.
* increased by this number.
* *
* @return the extra uses added * @return the extra uses added
* @deprecated MC 1.14 has changed how villagers restock their trades. Use
* {@link MerchantRecipe#getUses()}.
*/ */
@Deprecated
public int getBonus() { public int getBonus() {
return bonus; return recipe.getUses();
} }
/** /**
* Set the bonus uses added. * Set the bonus uses added.
* *
* @param bonus the extra uses added * @param bonus the extra uses added
* @see VillagerReplenishTradeEvent#getBonus() * @deprecated MC 1.14 has changed how villagers restock their trades. This
* has no effect anymore.
*/ */
@Deprecated
public void setBonus(int bonus) { public void setBonus(int bonus) {
this.bonus = bonus;
} }
@Override @Override

View File

@@ -4,7 +4,7 @@ import com.google.common.base.Preconditions;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.event.entity.VillagerReplenishTradeEvent; import org.bukkit.entity.Villager;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.NumberConversions; import org.bukkit.util.NumberConversions;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -12,16 +12,16 @@ import org.jetbrains.annotations.Nullable;
/** /**
* Represents a merchant's trade. * Represents a merchant's trade.
* * <p>
* Trades can take one or two ingredients, and provide one result. The * Trades can take one or two ingredients, and provide one result. The
* ingredients' ItemStack amounts are respected in the trade. * ingredients' ItemStack amounts are respected in the trade.
* <br> * <p>
* A trade has a limited number of uses, after which the trade can no longer be * A trade has a maximum number of uses. A {@link Villager} may periodically
* used, unless the player uses a different trade, which will cause its maximum * replenish its trades by resetting the {@link #getUses uses} of its merchant
* uses to increase. * recipes to <code>0</code>, allowing them to be used again.
* <br> * <p>
* A trade may or may not reward experience for being completed. * A trade may or may not reward experience for being completed.
* <br> * <p>
* During trades, the {@link MerchantRecipe} dynamically adjusts the amount of * During trades, the {@link MerchantRecipe} dynamically adjusts the amount of
* its first ingredient based on the following criteria: * its first ingredient based on the following criteria:
* <ul> * <ul>
@@ -45,8 +45,6 @@ import org.jetbrains.annotations.Nullable;
* integer value greater than or equal to 0, and the special price, and then * integer value greater than or equal to 0, and the special price, and then
* constraining the resulting value between <code>1</code> and the item stack's * constraining the resulting value between <code>1</code> and the item stack's
* {@link ItemStack#getMaxStackSize() maximum stack size}. * {@link ItemStack#getMaxStackSize() maximum stack size}.
*
* @see org.bukkit.event.entity.VillagerReplenishTradeEvent
*/ */
public class MerchantRecipe implements Recipe { public class MerchantRecipe implements Recipe {
@@ -159,20 +157,18 @@ public class MerchantRecipe implements Recipe {
} }
/** /**
* Get the value of the demand for the item in {@link #getResult()}. * Get the demand for this trade.
* *
* @return the demand for the item * @return the demand
*/ */
public int getDemand() { public int getDemand() {
return demand; return demand;
} }
/** /**
* Set the value of the demand for the item in {@link #getResult()}. * Set the demand for this trade.
* <br>
* <b>Note: </b> This value is updated when the item is purchase
* *
* @param demand demand value * @param demand the new demand
*/ */
public void setDemand(int demand) { public void setDemand(int demand) {
this.demand = demand; this.demand = demand;
@@ -180,10 +176,6 @@ public class MerchantRecipe implements Recipe {
/** /**
* Get the special price for this trade. * Get the special price for this trade.
* <br>
* <b>Note: </b> This value can be updated by
* {@link VillagerReplenishTradeEvent#getBonus()} or by
* {@link PotionEffectType#HERO_OF_THE_VILLAGE}
* *
* @return special price value * @return special price value
*/ */
@@ -192,11 +184,7 @@ public class MerchantRecipe implements Recipe {
} }
/** /**
* Set the special value for this trade. * Set the special price for this trade.
* <br>
* <b>Note: </b> This value can be updated by
* {@link VillagerReplenishTradeEvent#getBonus()} or by
* {@link PotionEffectType#HERO_OF_THE_VILLAGE}
* *
* @param specialPrice special price value * @param specialPrice special price value
*/ */
@@ -224,9 +212,6 @@ public class MerchantRecipe implements Recipe {
/** /**
* Get the maximum number of uses this trade has. * Get the maximum number of uses this trade has.
* <br>
* The maximum uses of this trade may increase when a player trades with the
* owning merchant.
* *
* @return the maximum number of uses * @return the maximum number of uses
*/ */
@@ -282,7 +267,7 @@ public class MerchantRecipe implements Recipe {
} }
/** /**
* Gets the additive price multiplier for the cost of this trade. * Gets the price multiplier for the cost of this trade.
* *
* @return price multiplier * @return price multiplier
*/ */
@@ -291,7 +276,7 @@ public class MerchantRecipe implements Recipe {
} }
/** /**
* Sets the additive price multiplier for the cost of this trade. * Sets the price multiplier for the cost of this trade.
* *
* @param priceMultiplier new price multiplier * @param priceMultiplier new price multiplier
*/ */