Reworked Shop API

This commit is contained in:
Auxilor
2022-11-24 23:12:25 +00:00
parent 52841f7f04
commit 18d882dac6
6 changed files with 117 additions and 14 deletions

View File

@@ -1,6 +1,8 @@
package com.willfp.eco.core.integrations.shop;
import com.willfp.eco.core.integrations.Integration;
import com.willfp.eco.core.price.Price;
import com.willfp.eco.core.price.impl.PriceFree;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
@@ -29,12 +31,38 @@ public interface ShopIntegration extends Integration {
return null;
}
/**
* Get if an item is sellable for a player.
*
* @param itemStack The item.
* @param player The player.
*/
default boolean isSellable(@NotNull final ItemStack itemStack,
@NotNull final Player player) {
return false;
}
/**
* Get the value of an item for a player.
*
* @param itemStack The item.
* @param player The player.
* @return The price.
*/
@NotNull
default Price getValue(@NotNull final ItemStack itemStack,
@NotNull final Player player) {
return new PriceFree();
}
/**
* Get the price of an item.
*
* @param itemStack The item.
* @return The price.
* @deprecated Use getValue instead.
*/
@Deprecated(since = "6.47.0", forRemoval = true)
default double getPrice(@NotNull final ItemStack itemStack) {
// Do nothing unless overridden.
return 0.0;
@@ -46,9 +74,11 @@ public interface ShopIntegration extends Integration {
* @param itemStack The item.
* @param player The player.
* @return The price.
* @deprecated Use getValue instead.
*/
@Deprecated(since = "6.47.0", forRemoval = true)
default double getPrice(@NotNull final ItemStack itemStack,
@NotNull final Player player) {
return getPrice(itemStack);
return getValue(itemStack, player).getValue(player);
}
}

View File

@@ -1,5 +1,7 @@
package com.willfp.eco.core.integrations.shop;
import com.willfp.eco.core.price.Price;
import com.willfp.eco.core.price.impl.PriceFree;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
@@ -11,6 +13,7 @@ import java.util.Set;
/**
* Class to handle shop integrations.
*/
@SuppressWarnings("removal")
public final class ShopManager {
/**
* A set of all registered integrations.
@@ -36,12 +39,54 @@ public final class ShopManager {
}
}
/**
* Get if an item is sellable for a player.
*
* @param itemStack The item.
* @param player The player.
*/
public static boolean isSellable(@Nullable final ItemStack itemStack,
@NotNull final Player player) {
if (itemStack == null) {
return false;
}
for (ShopIntegration integration : REGISTERED) {
return integration.isSellable(itemStack, player);
}
return false;
}
/**
* Get the value of an item for a player.
*
* @param itemStack The item.
* @param player The player.
* @return The price.
*/
@NotNull
public static Price getValue(@Nullable final ItemStack itemStack,
@NotNull final Player player) {
if (itemStack == null) {
return new PriceFree();
}
for (ShopIntegration integration : REGISTERED) {
return integration.getValue(itemStack, player);
}
return new PriceFree();
}
/**
* Get the price of an item.
*
* @param itemStack The item.
* @return The price.
* @deprecated Use getValue instead.
*/
@Deprecated(since = "6.47.0", forRemoval = true)
public static double getItemPrice(@Nullable final ItemStack itemStack) {
return getItemPrice(itemStack, null);
}
@@ -52,7 +97,9 @@ public final class ShopManager {
* @param itemStack The item.
* @param player The player.
* @return The price.
* @deprecated Use getValue instead.
*/
@Deprecated(since = "6.47.0", forRemoval = true)
public static double getItemPrice(@Nullable final ItemStack itemStack,
@Nullable final Player player) {
if (itemStack == null) {

View File

@@ -2,13 +2,31 @@
package com.willfp.eco.core.integrations.shop
import com.willfp.eco.core.price.Price
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
/** @see ShopManager.getItemPrice **/
/** @see ShopManager.getItemPrice * */
@Deprecated(
"Prices depend on players, this will always return 0.",
level = DeprecationLevel.ERROR,
replaceWith = ReplaceWith("this.getValue(player)")
)
val ItemStack.price: Double
get() = ShopManager.getItemPrice(this)
get() = 0.0
/** @see ShopManager.getItemPrice **/
/** @see ShopManager.getItemPrice * */
@Deprecated(
"Use the price system instead, prices may not be currencies.",
ReplaceWith("this.getValue(player)"),
)
fun ItemStack.getPrice(player: Player): Double =
ShopManager.getItemPrice(this, player)
this.getValue(player).getValue(player)
/** @see ShopManager.getValue */
fun ItemStack.getValue(player: Player): Price =
ShopManager.getValue(this, player)
/** @see ShopManager.isSellable */
fun ItemStack?.isSellable(player: Player): Boolean =
ShopManager.isSellable(this, player)