Updated price API
This commit is contained in:
@@ -24,6 +24,16 @@ public interface Price {
|
||||
*/
|
||||
void pay(@NotNull Player player);
|
||||
|
||||
/**
|
||||
* Give the value of the price to the player.
|
||||
*
|
||||
* @param player The player.
|
||||
* @apiNote You should override this method, it's only marked as default for backwards compatibility purposes.
|
||||
*/
|
||||
default void giveTo(@NotNull Player player) {
|
||||
// Override when needed.
|
||||
}
|
||||
|
||||
/**
|
||||
* If the price is backed by a value, get it here.
|
||||
*
|
||||
@@ -37,7 +47,9 @@ public interface Price {
|
||||
* If the price is backed by a value, set it here.
|
||||
*
|
||||
* @param value The value.
|
||||
* @deprecated Values shouldn't be fixed.
|
||||
*/
|
||||
@Deprecated(since = "6.45.0", forRemoval = true)
|
||||
default void setValue(final double value) {
|
||||
// Override when needed.
|
||||
}
|
||||
|
||||
@@ -3,9 +3,12 @@ package com.willfp.eco.core.price;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Create prices.
|
||||
*
|
||||
* @apiNote You must override one of the create methods to prevent infinite loops.
|
||||
*/
|
||||
public interface PriceFactory {
|
||||
/**
|
||||
@@ -23,5 +26,17 @@ public interface PriceFactory {
|
||||
* @param value The value.
|
||||
* @return The price.
|
||||
*/
|
||||
@NotNull Price create(double value);
|
||||
default @NotNull Price create(double value) {
|
||||
return create(() -> value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the price.
|
||||
*
|
||||
* @param function The value function.
|
||||
* @return The price.
|
||||
*/
|
||||
default @NotNull Price create(@NotNull Supplier<@NotNull Double> function) {
|
||||
return create(function.get());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Class to manage prices.
|
||||
@@ -65,12 +66,12 @@ public final class Prices {
|
||||
public static Price create(@NotNull final String expression,
|
||||
@Nullable final String priceName,
|
||||
@NotNull final MathContext context) {
|
||||
double value = NumberUtils.evaluateExpression(
|
||||
Supplier<Double> value = () -> NumberUtils.evaluateExpression(
|
||||
expression,
|
||||
context
|
||||
);
|
||||
|
||||
if (value <= 0) {
|
||||
if (value.get() <= 0) {
|
||||
return new PriceFree();
|
||||
}
|
||||
|
||||
@@ -90,7 +91,7 @@ public final class Prices {
|
||||
return new PriceFree();
|
||||
}
|
||||
|
||||
return new PriceItem((int) Math.round(value), item);
|
||||
return new PriceItem(() -> Math.toIntExact(Math.round(value.get())), item);
|
||||
} else {
|
||||
return factory.create(value);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import com.willfp.eco.core.price.Price;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Economy-based price (for Vault, Treasury, etc.)
|
||||
*/
|
||||
@@ -12,7 +14,7 @@ public final class PriceEconomy implements Price {
|
||||
/**
|
||||
* The value of the price.
|
||||
*/
|
||||
private double value;
|
||||
private final Supplier<Double> function;
|
||||
|
||||
/**
|
||||
* Create a new economy-based price.
|
||||
@@ -20,26 +22,35 @@ public final class PriceEconomy implements Price {
|
||||
* @param value The value.
|
||||
*/
|
||||
public PriceEconomy(final double value) {
|
||||
this.value = value;
|
||||
this.function = () -> value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new economy-based price.
|
||||
*
|
||||
* @param function The function.
|
||||
*/
|
||||
public PriceEconomy(@NotNull final Supplier<@NotNull Double> function) {
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canAfford(@NotNull Player player) {
|
||||
return EconomyManager.getBalance(player) >= value;
|
||||
public boolean canAfford(@NotNull final Player player) {
|
||||
return EconomyManager.getBalance(player) >= getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pay(@NotNull Player player) {
|
||||
EconomyManager.removeMoney(player, value);
|
||||
public void pay(@NotNull final Player player) {
|
||||
EconomyManager.removeMoney(player, getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void giveTo(@NotNull final Player player) {
|
||||
EconomyManager.giveMoney(player, getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(final double value) {
|
||||
this.value = value;
|
||||
return function.get();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,12 +16,12 @@ public final class PriceFree implements Price {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canAfford(@NotNull Player player) {
|
||||
public boolean canAfford(@NotNull final Player player) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pay(@NotNull Player player) {
|
||||
public void pay(@NotNull final Player player) {
|
||||
// Do nothing.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.willfp.eco.core.price.impl;
|
||||
|
||||
import com.willfp.eco.core.drops.DropQueue;
|
||||
import com.willfp.eco.core.items.TestableItem;
|
||||
import com.willfp.eco.core.price.Price;
|
||||
import org.bukkit.Material;
|
||||
@@ -7,6 +8,8 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Item-based price.
|
||||
*/
|
||||
@@ -14,7 +17,7 @@ public final class PriceItem implements Price {
|
||||
/**
|
||||
* The amount of items.
|
||||
*/
|
||||
private final int amountToRemove;
|
||||
private final Supplier<Integer> amountToRemove;
|
||||
|
||||
/**
|
||||
* The item.
|
||||
@@ -29,7 +32,18 @@ public final class PriceItem implements Price {
|
||||
*/
|
||||
public PriceItem(final int amount,
|
||||
@NotNull final TestableItem item) {
|
||||
this.amountToRemove = Math.max(0, amount);
|
||||
this(() -> amount, item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new economy-based price.
|
||||
*
|
||||
* @param amount The amount.
|
||||
* @param item The item.
|
||||
*/
|
||||
public PriceItem(@NotNull final Supplier<@NotNull Integer> amount,
|
||||
@NotNull final TestableItem item) {
|
||||
this.amountToRemove = amount;
|
||||
this.item = item;
|
||||
}
|
||||
|
||||
@@ -43,8 +57,9 @@ public final class PriceItem implements Price {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canAfford(@NotNull Player player) {
|
||||
if (amountToRemove == 0) {
|
||||
public boolean canAfford(@NotNull final Player player) {
|
||||
int toRemove = amountToRemove.get();
|
||||
if (toRemove <= 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -56,26 +71,27 @@ public final class PriceItem implements Price {
|
||||
}
|
||||
}
|
||||
|
||||
return count >= amountToRemove;
|
||||
return count >= toRemove;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pay(@NotNull Player player) {
|
||||
public void pay(@NotNull final Player player) {
|
||||
int toRemove = amountToRemove.get();
|
||||
int count = 0;
|
||||
|
||||
for (ItemStack itemStack : player.getInventory().getContents()) {
|
||||
if (count >= amountToRemove) {
|
||||
if (count >= toRemove) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (item.matches(itemStack)) {
|
||||
int itemAmount = itemStack.getAmount();
|
||||
|
||||
if (itemAmount > amountToRemove) {
|
||||
itemStack.setAmount(itemAmount - amountToRemove);
|
||||
if (itemAmount > toRemove) {
|
||||
itemStack.setAmount(itemAmount - toRemove);
|
||||
}
|
||||
|
||||
if (itemAmount <= amountToRemove) {
|
||||
if (itemAmount <= toRemove) {
|
||||
itemStack.setAmount(0);
|
||||
itemStack.setType(Material.AIR);
|
||||
}
|
||||
@@ -84,4 +100,12 @@ public final class PriceItem implements Price {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void giveTo(@NotNull final Player player) {
|
||||
new DropQueue(player)
|
||||
.addItem(item.getItem())
|
||||
.forceTelekinesis()
|
||||
.push();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.willfp.eco.internal.price
|
||||
import com.willfp.eco.core.price.Price
|
||||
import com.willfp.eco.core.price.PriceFactory
|
||||
import com.willfp.eco.core.price.impl.PriceEconomy
|
||||
import java.util.function.Supplier
|
||||
|
||||
object PriceFactoryEconomy : PriceFactory {
|
||||
override fun getNames() = listOf(
|
||||
@@ -10,5 +11,7 @@ object PriceFactoryEconomy : PriceFactory {
|
||||
"$"
|
||||
)
|
||||
|
||||
override fun create(value: Double): Price = PriceEconomy(value)
|
||||
override fun create(function: Supplier<Double>): Price {
|
||||
return PriceEconomy(function)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.willfp.eco.internal.price
|
||||
import com.willfp.eco.core.price.Price
|
||||
import com.willfp.eco.core.price.PriceFactory
|
||||
import org.bukkit.entity.Player
|
||||
import java.util.function.Supplier
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
object PriceFactoryXP : PriceFactory {
|
||||
@@ -12,15 +13,21 @@ object PriceFactoryXP : PriceFactory {
|
||||
"experience"
|
||||
)
|
||||
|
||||
override fun create(value: Double): Price = PriceXP(value.roundToInt())
|
||||
override fun create(function: Supplier<Double>): Price {
|
||||
return PriceXP { function.get().roundToInt() }
|
||||
}
|
||||
|
||||
private class PriceXP(
|
||||
private val xp: Int
|
||||
private val xp: () -> Int
|
||||
) : Price {
|
||||
override fun canAfford(player: Player) = player.totalExperience >= xp
|
||||
override fun canAfford(player: Player) = player.totalExperience >= xp()
|
||||
|
||||
override fun pay(player: Player) {
|
||||
player.totalExperience -= xp
|
||||
player.totalExperience -= xp()
|
||||
}
|
||||
|
||||
override fun giveTo(player: Player) {
|
||||
player.totalExperience += xp()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.willfp.eco.internal.price
|
||||
import com.willfp.eco.core.price.Price
|
||||
import com.willfp.eco.core.price.PriceFactory
|
||||
import org.bukkit.entity.Player
|
||||
import java.util.function.Supplier
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
object PriceFactoryXPLevels : PriceFactory {
|
||||
@@ -13,15 +14,21 @@ object PriceFactoryXPLevels : PriceFactory {
|
||||
"explevels",
|
||||
)
|
||||
|
||||
override fun create(value: Double): Price = PriceXPLevel(value.roundToInt())
|
||||
override fun create(function: Supplier<Double>): Price {
|
||||
return PriceXPLevel { function.get().roundToInt() }
|
||||
}
|
||||
|
||||
private class PriceXPLevel(
|
||||
private val levels: Int
|
||||
private val levels: () -> Int
|
||||
) : Price {
|
||||
override fun canAfford(player: Player) = player.level >= levels
|
||||
override fun canAfford(player: Player) = player.level >= levels()
|
||||
|
||||
override fun pay(player: Player) {
|
||||
player.level -= levels
|
||||
player.level -= levels()
|
||||
}
|
||||
|
||||
override fun giveTo(player: Player) {
|
||||
player.level += levels()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user