Merge branch 'master' into master
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.willfp.eco.core;
|
||||
|
||||
import com.willfp.eco.core.command.CommandBase;
|
||||
import com.willfp.eco.core.command.impl.PluginCommand;
|
||||
import com.willfp.eco.core.config.ConfigType;
|
||||
import com.willfp.eco.core.config.interfaces.Config;
|
||||
import com.willfp.eco.core.config.interfaces.LoadableConfig;
|
||||
@@ -35,6 +36,7 @@ import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.command.CommandMap;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Mob;
|
||||
@@ -71,7 +73,7 @@ public interface Eco {
|
||||
/**
|
||||
* Create an event manager.
|
||||
*
|
||||
* @param plugin The plugin.
|
||||
* @param plugin The plugin.F
|
||||
* @return The event manager.
|
||||
*/
|
||||
@NotNull
|
||||
@@ -524,6 +526,20 @@ public interface Eco {
|
||||
*/
|
||||
void syncCommands();
|
||||
|
||||
/**
|
||||
* Get the command map.
|
||||
*
|
||||
* @return The command map.
|
||||
*/
|
||||
@NotNull CommandMap getCommandMap();
|
||||
|
||||
/**
|
||||
* Unregister a command.
|
||||
*
|
||||
* @param command The command.
|
||||
*/
|
||||
void unregisterCommand(@NotNull final PluginCommand command);
|
||||
|
||||
/**
|
||||
* Get the instance of eco; the bridge between the api frontend and the implementation backend.
|
||||
*
|
||||
|
||||
@@ -699,7 +699,15 @@ public abstract class EcoPlugin extends JavaPlugin implements PluginLike {
|
||||
* @return lang.yml.
|
||||
*/
|
||||
protected LangYml createLangYml() {
|
||||
return new LangYml(this);
|
||||
try {
|
||||
return new LangYml(this);
|
||||
} catch (NullPointerException e) {
|
||||
this.getLogger().severe("Failed to load lang.yml!");
|
||||
this.getLogger().severe("For the developer of this plugin: make sure you have a lang.yml");
|
||||
e.printStackTrace();
|
||||
Bukkit.getPluginManager().disablePlugin(this);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -710,7 +718,15 @@ public abstract class EcoPlugin extends JavaPlugin implements PluginLike {
|
||||
* @return config.yml.
|
||||
*/
|
||||
protected ConfigYml createConfigYml() {
|
||||
return new ConfigYml(this);
|
||||
try {
|
||||
return new ConfigYml(this);
|
||||
} catch (NullPointerException e) {
|
||||
this.getLogger().severe("Failed to load config.yml!");
|
||||
this.getLogger().severe("For the developer of this plugin: make sure you have a config.yml");
|
||||
e.printStackTrace();
|
||||
Bukkit.getPluginManager().disablePlugin(this);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -139,7 +139,7 @@ public interface MenuBuilder extends PageBuilder {
|
||||
* @return The builder.
|
||||
*/
|
||||
default MenuBuilder maxPages(@NotNull final Function<Player, Integer> pages) {
|
||||
return onRender((player, menu) -> menu.setState(player, Page.MAX_PAGE_KEY, pages.apply(player)));
|
||||
return this.onRender((player, menu) -> menu.setState(player, Page.MAX_PAGE_KEY, pages.apply(player)));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
package com.willfp.eco.core.gui.slot;
|
||||
|
||||
import com.willfp.eco.core.config.interfaces.Config;
|
||||
import com.willfp.eco.core.fast.FastItemStack;
|
||||
import com.willfp.eco.core.gui.slot.functional.SlotHandler;
|
||||
import com.willfp.eco.core.items.Items;
|
||||
import com.willfp.eco.util.StringUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.inventory.ClickType;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -37,7 +39,25 @@ public class ConfigSlot extends CustomSlot {
|
||||
public ConfigSlot(@NotNull final Config config) {
|
||||
this.config = config;
|
||||
|
||||
SlotBuilder builder = Slot.builder(Items.lookup(config.getString("item")));
|
||||
ItemStack item = Items.lookup(config.getString("item")).getItem();
|
||||
|
||||
SlotBuilder builder = Slot.builder((player, menu) -> {
|
||||
if (!config.has("lore")) {
|
||||
return item;
|
||||
} else {
|
||||
FastItemStack fast = FastItemStack.wrap(item.clone());
|
||||
List<String> newLore = new ArrayList<>(fast.getLore());
|
||||
newLore.addAll(
|
||||
StringUtils.formatList(
|
||||
config.getStrings("lore"),
|
||||
player,
|
||||
StringUtils.FormatOption.WITH_PLACEHOLDERS
|
||||
)
|
||||
);
|
||||
fast.setLore(newLore);
|
||||
return fast.unwrap();
|
||||
}
|
||||
});
|
||||
|
||||
for (ClickType clickType : ClickType.values()) {
|
||||
builder.onClick(
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.willfp.eco.core.gui.menu.Menu;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* Base class for custom slot implementations.
|
||||
@@ -31,7 +32,7 @@ public abstract class CustomSlot implements Slot {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ItemStack getItemStack(@NotNull final Player player) {
|
||||
public final @NotNull ItemStack getItemStack(@NotNull final Player player) {
|
||||
if (delegate == null) {
|
||||
throw new IllegalStateException("Custom Slot was not initialized!");
|
||||
}
|
||||
@@ -40,8 +41,8 @@ public abstract class CustomSlot implements Slot {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCaptive(@NotNull final Player player,
|
||||
@NotNull final Menu menu) {
|
||||
public final boolean isCaptive(@NotNull final Player player,
|
||||
@NotNull final Menu menu) {
|
||||
if (delegate == null) {
|
||||
throw new IllegalStateException("Custom Slot was not initialized!");
|
||||
}
|
||||
@@ -50,7 +51,18 @@ public abstract class CustomSlot implements Slot {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCaptiveFromEmpty() {
|
||||
public final boolean isAllowedCaptive(@NotNull final Player player,
|
||||
@NotNull final Menu menu,
|
||||
@Nullable final ItemStack itemStack) {
|
||||
if (delegate == null) {
|
||||
throw new IllegalStateException("Custom Slot was not initialized!");
|
||||
}
|
||||
|
||||
return delegate.isAllowedCaptive(player, menu, itemStack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean isCaptiveFromEmpty() {
|
||||
if (delegate == null) {
|
||||
throw new IllegalStateException("Custom Slot was not initialized!");
|
||||
}
|
||||
@@ -59,8 +71,8 @@ public abstract class CustomSlot implements Slot {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Slot getActionableSlot(@NotNull final Player player,
|
||||
@NotNull final Menu menu) {
|
||||
public final @NotNull Slot getActionableSlot(@NotNull final Player player,
|
||||
@NotNull final Menu menu) {
|
||||
return delegate;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* Base class for custom slot implementations.
|
||||
@@ -34,11 +35,18 @@ public abstract class ReactiveSlot implements Slot {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCaptive(@NotNull final Player player,
|
||||
@NotNull final Menu menu) {
|
||||
public final boolean isCaptive(@NotNull final Player player,
|
||||
@NotNull final Menu menu) {
|
||||
return getSlot(player, menu).isCaptive(player, menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean isAllowedCaptive(@NotNull final Player player,
|
||||
@NotNull final Menu menu,
|
||||
@Nullable final ItemStack itemStack) {
|
||||
return getSlot(player, menu).isAllowedCaptive(player, menu, itemStack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final @NotNull Slot getActionableSlot(@NotNull final Player player,
|
||||
@NotNull final Menu menu) {
|
||||
|
||||
@@ -9,6 +9,7 @@ import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
@@ -45,6 +46,20 @@ public interface Slot extends GUIComponent {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the slot allows a certain item to be placed in it.
|
||||
*
|
||||
* @param player The player.
|
||||
* @param menu The menu.
|
||||
* @param itemStack The item; use null if the item is unknown.
|
||||
* @return If captive.
|
||||
*/
|
||||
default boolean isAllowedCaptive(@NotNull final Player player,
|
||||
@NotNull final Menu menu,
|
||||
@Nullable final ItemStack itemStack) {
|
||||
return this.isCaptive(player, menu);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the actionable slot to be shown.
|
||||
* <p>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.willfp.eco.core.gui.slot;
|
||||
|
||||
import com.willfp.eco.core.gui.slot.functional.CaptiveFilter;
|
||||
import com.willfp.eco.core.gui.slot.functional.SlotHandler;
|
||||
import com.willfp.eco.core.gui.slot.functional.SlotModifier;
|
||||
import com.willfp.eco.core.gui.slot.functional.SlotUpdater;
|
||||
@@ -143,7 +144,17 @@ public interface SlotBuilder {
|
||||
* @param predicate The predicate. Returns true when the slot should not be captive.
|
||||
* @return The builder.
|
||||
*/
|
||||
SlotBuilder notCaptiveFor(@NotNull Predicate<Player> predicate);
|
||||
SlotBuilder notCaptiveFor(@NotNull final Predicate<Player> predicate);
|
||||
|
||||
/**
|
||||
* Set a whitelist for allowed captive items.
|
||||
*
|
||||
* @param filter The filter.
|
||||
* @return The builder.
|
||||
*/
|
||||
default SlotBuilder setCaptiveFilter(@NotNull final CaptiveFilter filter) {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the ItemStack updater.
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.willfp.eco.core.gui.slot.functional;
|
||||
|
||||
import com.willfp.eco.core.gui.menu.Menu;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* Interface to test if a captive slot is allowed to contain an item given a player and a menu.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface CaptiveFilter {
|
||||
/**
|
||||
* Get if allowed.
|
||||
*
|
||||
* @param player The player.
|
||||
* @param menu The menu.
|
||||
* @param itemStack The item.
|
||||
* @return If captive.
|
||||
*/
|
||||
boolean isAllowed(@NotNull Player player,
|
||||
@NotNull Menu menu,
|
||||
@Nullable ItemStack itemStack);
|
||||
}
|
||||
@@ -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,41 @@ public interface ShopIntegration extends Integration {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get if an item is sellable for a player.
|
||||
*
|
||||
* @param itemStack The item.
|
||||
* @param player The player.
|
||||
* @return If sellable.
|
||||
*/
|
||||
default boolean isSellable(@NotNull final ItemStack itemStack,
|
||||
@NotNull final Player player) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of one of an item for a player.
|
||||
* <p>
|
||||
* For example, if you pass in a stack, it will only return the value of <b>one</b> item, not the full stack.
|
||||
*
|
||||
* @param itemStack The item.
|
||||
* @param player The player.
|
||||
* @return The price.
|
||||
*/
|
||||
@NotNull
|
||||
default Price getUnitValue(@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 +77,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 getUnitValue(itemStack, player).getValue(player);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
@@ -36,12 +38,57 @@ public final class ShopManager {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get if an item is sellable for a player.
|
||||
*
|
||||
* @param itemStack The item.
|
||||
* @param player The player.
|
||||
* @return If sellable.
|
||||
*/
|
||||
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 one of an item for a player.
|
||||
* <p>
|
||||
* For example, if you pass in a stack, it will only return the value of <b>one</b> item, not the full stack.
|
||||
*
|
||||
* @param itemStack The item.
|
||||
* @param player The player.
|
||||
* @return The price.
|
||||
*/
|
||||
@NotNull
|
||||
public static Price getUnitValue(@Nullable final ItemStack itemStack,
|
||||
@NotNull final Player player) {
|
||||
if (itemStack == null) {
|
||||
return new PriceFree();
|
||||
}
|
||||
|
||||
for (ShopIntegration integration : REGISTERED) {
|
||||
return integration.getUnitValue(itemStack, player);
|
||||
}
|
||||
|
||||
return new PriceFree();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the price of an item.
|
||||
*
|
||||
* @param itemStack The item.
|
||||
* @return The price.
|
||||
* @deprecated Use getValue instead. This will always return 0 as prices depend on players.
|
||||
*/
|
||||
@Deprecated(since = "6.47.0", forRemoval = true)
|
||||
public static double getItemPrice(@Nullable final ItemStack itemStack) {
|
||||
return getItemPrice(itemStack, null);
|
||||
}
|
||||
@@ -52,19 +99,17 @@ public final class ShopManager {
|
||||
* @param itemStack The item.
|
||||
* @param player The player.
|
||||
* @return The price.
|
||||
* @deprecated Use getValue instead. Null players / null items will always return 0.
|
||||
*/
|
||||
@Deprecated(since = "6.47.0", forRemoval = true)
|
||||
public static double getItemPrice(@Nullable final ItemStack itemStack,
|
||||
@Nullable final Player player) {
|
||||
if (itemStack == null) {
|
||||
if (itemStack == null || player == null) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
for (ShopIntegration shopIntegration : REGISTERED) {
|
||||
if (player == null) {
|
||||
return shopIntegration.getPrice(itemStack);
|
||||
} else {
|
||||
return shopIntegration.getPrice(itemStack, player);
|
||||
}
|
||||
return shopIntegration.getUnitValue(itemStack, player).getValue(player, itemStack.getAmount());
|
||||
}
|
||||
|
||||
return 0.0;
|
||||
|
||||
@@ -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.PriceEconomy;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.event.player.PlayerEvent;
|
||||
@@ -19,7 +21,12 @@ public class ShopSellEvent extends PlayerEvent {
|
||||
/**
|
||||
* The sell price.
|
||||
*/
|
||||
private double price;
|
||||
private Price price;
|
||||
|
||||
/**
|
||||
* The price multiplier.
|
||||
*/
|
||||
private double multiplier;
|
||||
|
||||
/**
|
||||
* The item to be sold.
|
||||
@@ -33,31 +40,64 @@ public class ShopSellEvent extends PlayerEvent {
|
||||
* @param who The player.
|
||||
* @param price The price.
|
||||
* @param item The item.
|
||||
* @deprecated Use the price system instead.
|
||||
*/
|
||||
@Deprecated(since = "6.47.0", forRemoval = true)
|
||||
public ShopSellEvent(@NotNull final Player who,
|
||||
final double price,
|
||||
@Nullable final ItemStack item) {
|
||||
this(who, new PriceEconomy(price), item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new shop sell event.
|
||||
*
|
||||
* @param who The player.
|
||||
* @param price The price.
|
||||
* @param item The item.
|
||||
*/
|
||||
public ShopSellEvent(@NotNull final Player who,
|
||||
@NotNull final Price price,
|
||||
@Nullable final ItemStack item) {
|
||||
this(who, price, item, 1.0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new shop sell event.
|
||||
*
|
||||
* @param who The player.
|
||||
* @param price The price.
|
||||
* @param item The item.
|
||||
* @param multiplier The multiplier.
|
||||
*/
|
||||
public ShopSellEvent(@NotNull final Player who,
|
||||
@NotNull final Price price,
|
||||
@Nullable final ItemStack item,
|
||||
final double multiplier) {
|
||||
super(who);
|
||||
|
||||
this.price = price;
|
||||
this.item = item;
|
||||
|
||||
this.multiplier = multiplier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the price.
|
||||
* Get the value.
|
||||
*
|
||||
* @return The price.
|
||||
* @return The value.
|
||||
*/
|
||||
public double getPrice() {
|
||||
@NotNull
|
||||
public Price getValue() {
|
||||
return this.price;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the price.
|
||||
* Set the value.
|
||||
*
|
||||
* @param price The price.
|
||||
* @param price The value.
|
||||
*/
|
||||
public void setPrice(final double price) {
|
||||
public void setValue(@NotNull final Price price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
@@ -81,6 +121,46 @@ public class ShopSellEvent extends PlayerEvent {
|
||||
return item != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the price multiplier.
|
||||
*
|
||||
* @return The multiplier.
|
||||
*/
|
||||
public double getMultiplier() {
|
||||
return multiplier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the price multiplier.
|
||||
*
|
||||
* @param multiplier The multiplier.
|
||||
*/
|
||||
public void setMultiplier(final double multiplier) {
|
||||
this.multiplier = multiplier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the price.
|
||||
*
|
||||
* @return The price.
|
||||
* @deprecated Use the price system instead.
|
||||
*/
|
||||
@Deprecated(since = "6.47.0", forRemoval = true)
|
||||
public double getPrice() {
|
||||
return this.getValue().getValue(player);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the price.
|
||||
*
|
||||
* @param price The price.
|
||||
* @deprecated Use the price system instead.
|
||||
*/
|
||||
@Deprecated(since = "6.47.0", forRemoval = true)
|
||||
public void setPrice(final double price) {
|
||||
this.setValue(new PriceEconomy(price));
|
||||
}
|
||||
|
||||
/**
|
||||
* Bukkit parity.
|
||||
*
|
||||
|
||||
@@ -70,7 +70,7 @@ public abstract class AbstractItemStackBuilder<T extends ItemMeta, U extends Abs
|
||||
|
||||
@Override
|
||||
public U setAmount(final int amount) {
|
||||
Validate.isTrue(amount >= 1 && amount <= base.getMaxStackSize());
|
||||
Validate.isTrue(amount >= 1);
|
||||
base.setAmount(amount);
|
||||
return (U) this;
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ public final class ConfiguredPrice implements Price {
|
||||
/**
|
||||
* Free.
|
||||
*/
|
||||
private static final ConfiguredPrice FREE = new ConfiguredPrice(
|
||||
public static final ConfiguredPrice FREE = new ConfiguredPrice(
|
||||
new PriceFree(),
|
||||
"Free"
|
||||
);
|
||||
@@ -52,23 +52,27 @@ public final class ConfiguredPrice implements Price {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canAfford(@NotNull final Player player) {
|
||||
return this.price.canAfford(player);
|
||||
public boolean canAfford(@NotNull final Player player,
|
||||
final double multiplier) {
|
||||
return this.price.canAfford(player, multiplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pay(@NotNull final Player player) {
|
||||
this.price.pay(player);
|
||||
public void pay(@NotNull final Player player,
|
||||
final double multiplier) {
|
||||
this.price.pay(player, multiplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void giveTo(@NotNull final Player player) {
|
||||
this.price.giveTo(player);
|
||||
public void giveTo(@NotNull final Player player,
|
||||
final double multiplier) {
|
||||
this.price.giveTo(player, multiplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getValue(@NotNull final Player player) {
|
||||
return this.price.getValue(player);
|
||||
public double getValue(@NotNull final Player player,
|
||||
final double multiplier) {
|
||||
return this.price.getValue(player, multiplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -98,8 +102,20 @@ public final class ConfiguredPrice implements Price {
|
||||
* @return The display string.
|
||||
*/
|
||||
public String getDisplay(@NotNull final Player player) {
|
||||
return this.getDisplay(player, 1.0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the display string for a player.
|
||||
*
|
||||
* @param player The player.
|
||||
* @param multiplier The multiplier.
|
||||
* @return The display string.
|
||||
*/
|
||||
public String getDisplay(@NotNull final Player player,
|
||||
final double multiplier) {
|
||||
return StringUtils.format(
|
||||
formatString.replace("%value%", NumberUtils.format(this.getPrice().getValue(player))),
|
||||
formatString.replace("%value%", NumberUtils.format(this.getPrice().getValue(player, multiplier))),
|
||||
player,
|
||||
StringUtils.FormatOption.WITH_PLACEHOLDERS
|
||||
);
|
||||
|
||||
@@ -1,49 +1,135 @@
|
||||
package com.willfp.eco.core.price;
|
||||
|
||||
import org.apache.commons.lang.NotImplementedException;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* A price that a player should pay.
|
||||
* <p>
|
||||
* There are important implementation details:
|
||||
* <p>
|
||||
* For backwards compatibility, all methods are default, however you must override the following:
|
||||
* <ul>
|
||||
* <li><code>canAfford(Player, double)</code></li>
|
||||
* <li><code>pay(Player, double)</code></li>
|
||||
* <li><code>giveTo(Player, double)</code></li>
|
||||
* <li><code>getValue(Player, double)</code></li>
|
||||
* <li><code>getMultiplier(Player)</code></li>
|
||||
* <li><code>setMultiplier(Player, double)</code></li>
|
||||
* </ul>
|
||||
* Otherwise, your implementation will throw {@link NotImplementedException}.
|
||||
* <p>
|
||||
* Also, getValue() should always return the value with player multipliers applied.
|
||||
*/
|
||||
public interface Price {
|
||||
/**
|
||||
* Get if the player can afford the price.
|
||||
* Get if a player can afford to pay the price.
|
||||
*
|
||||
* @param player The player.
|
||||
* @return If the player can afford.
|
||||
*/
|
||||
boolean canAfford(@NotNull Player player);
|
||||
default boolean canAfford(@NotNull final Player player) {
|
||||
return this.canAfford(player, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get if a player can afford to pay x times the price.
|
||||
*
|
||||
* @param player The player.
|
||||
* @param multiplier The multiplier.
|
||||
* @return If the player can afford.
|
||||
*/
|
||||
default boolean canAfford(@NotNull final Player player,
|
||||
final double multiplier) {
|
||||
throw new NotImplementedException("Override canAfford(Player, double) in your Price implementation!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Make the player pay the price.
|
||||
* <p>
|
||||
* Only run this if the player can afford the price.
|
||||
* Check canAfford first.
|
||||
*
|
||||
* @param player The player.
|
||||
*/
|
||||
void pay(@NotNull Player player);
|
||||
|
||||
/**
|
||||
* Give the value of the price to the player.
|
||||
* <p>
|
||||
* You should override this method, it's only marked as default for
|
||||
* backwards compatibility purposes.
|
||||
*
|
||||
* @param player The player.
|
||||
*/
|
||||
default void giveTo(@NotNull Player player) {
|
||||
// Override when needed.
|
||||
default void pay(@NotNull final Player player) {
|
||||
this.pay(player, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* If the price is backed by a value, get it here.
|
||||
* Make the player pay the price x times.
|
||||
* <p>
|
||||
* Check canAfford first.
|
||||
*
|
||||
* @param player The player.
|
||||
* @param multiplier The multiplier.
|
||||
*/
|
||||
default void pay(@NotNull final Player player,
|
||||
final double multiplier) {
|
||||
throw new NotImplementedException("Override pay(Player, double) in your Price implementation!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Give the price to the player.
|
||||
*
|
||||
* @param player The player.
|
||||
*/
|
||||
default void giveTo(@NotNull final Player player) {
|
||||
this.giveTo(player, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Give the price to the player x times.
|
||||
*
|
||||
* @param player The player.
|
||||
* @param multiplier The multiplier.
|
||||
*/
|
||||
default void giveTo(@NotNull final Player player,
|
||||
final double multiplier) {
|
||||
throw new NotImplementedException("Override giveTo(Player, double) in your Price implementation!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the numerical value that backs this price.
|
||||
*
|
||||
* @param player The player.
|
||||
* @return The value.
|
||||
*/
|
||||
default double getValue(@NotNull final Player player) {
|
||||
return 0;
|
||||
return getValue(player, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the numeral value that backs this price multiplied x times.
|
||||
*
|
||||
* @param player The player.
|
||||
* @param multiplier The multiplier.
|
||||
* @return The value.
|
||||
*/
|
||||
default double getValue(@NotNull final Player player,
|
||||
final double multiplier) {
|
||||
throw new NotImplementedException("Override getValue(Player, double) in your Price implementation!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value multiplier for the player.
|
||||
*
|
||||
* @param player The player.
|
||||
* @return The multiplier.
|
||||
*/
|
||||
default double getMultiplier(@NotNull final Player player) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value multiplier for the player.
|
||||
*
|
||||
* @param player The player.
|
||||
* @param multiplier The multiplier.
|
||||
*/
|
||||
default void setMultiplier(@NotNull final Player player,
|
||||
final double multiplier) {
|
||||
throw new NotImplementedException("Override setMultiplier(Player, double) in your Price implementation!");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,31 +147,10 @@ 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 Values shouldn't be fixed. This method should never work.
|
||||
*/
|
||||
@Deprecated(since = "6.45.0", forRemoval = true)
|
||||
default void setValue(final double value) {
|
||||
// Override when needed.
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the price multiplier for a player.
|
||||
*
|
||||
* @param player The player.
|
||||
* @return The value.
|
||||
*/
|
||||
default double getMultiplier(@NotNull final Player player) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the price multiplier for a player.
|
||||
*
|
||||
* @param player The player.
|
||||
* @param multiplier The multiplier.
|
||||
*/
|
||||
default void setMultiplier(@NotNull final Player player,
|
||||
final double multiplier) {
|
||||
// Override when needed.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,23 +52,27 @@ public final class PriceEconomy implements Price {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canAfford(@NotNull final Player player) {
|
||||
return EconomyManager.getBalance(player) >= getValue(player);
|
||||
public boolean canAfford(@NotNull final Player player,
|
||||
final double multiplier) {
|
||||
return EconomyManager.getBalance(player) >= getValue(player, multiplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pay(@NotNull final Player player) {
|
||||
EconomyManager.removeMoney(player, getValue(player));
|
||||
public void pay(@NotNull final Player player,
|
||||
final double multiplier) {
|
||||
EconomyManager.removeMoney(player, getValue(player, multiplier));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void giveTo(@NotNull final Player player) {
|
||||
EconomyManager.giveMoney(player, getValue(player));
|
||||
public void giveTo(@NotNull final Player player,
|
||||
final double multiplier) {
|
||||
EconomyManager.giveMoney(player, getValue(player, multiplier));
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getValue(@NotNull final Player player) {
|
||||
return this.function.apply(MathContext.copyWithPlayer(baseContext, player)) * getMultiplier(player);
|
||||
public double getValue(@NotNull final Player player,
|
||||
final double multiplier) {
|
||||
return this.function.apply(MathContext.copyWithPlayer(baseContext, player)) * getMultiplier(player) * multiplier;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -16,12 +16,37 @@ public final class PriceFree implements Price {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canAfford(@NotNull final Player player) {
|
||||
public boolean canAfford(@NotNull final Player player,
|
||||
final double multiplier) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pay(@NotNull final Player player) {
|
||||
// Do nothing.
|
||||
public void pay(@NotNull final Player player,
|
||||
final double multiplier) {
|
||||
// Nothing.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void giveTo(@NotNull final Player player,
|
||||
final double multiplier) {
|
||||
// Nothing.
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getMultiplier(@NotNull final Player player) {
|
||||
return 1.0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMultiplier(@NotNull final Player player,
|
||||
final double multiplier) {
|
||||
// Nothing.
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getValue(@NotNull final Player player,
|
||||
final double multiplier) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,8 +74,9 @@ public final class PriceItem implements Price {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canAfford(@NotNull final Player player) {
|
||||
int toRemove = (int) getValue(player);
|
||||
public boolean canAfford(@NotNull final Player player,
|
||||
final double multiplier) {
|
||||
int toRemove = (int) getValue(player, multiplier);
|
||||
if (toRemove <= 0) {
|
||||
return true;
|
||||
}
|
||||
@@ -92,8 +93,9 @@ public final class PriceItem implements Price {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pay(@NotNull final Player player) {
|
||||
int toRemove = (int) getValue(player);
|
||||
public void pay(@NotNull final Player player,
|
||||
final double multiplier) {
|
||||
int toRemove = (int) getValue(player, multiplier);
|
||||
int count = 0;
|
||||
|
||||
for (ItemStack itemStack : player.getInventory().getContents()) {
|
||||
@@ -119,9 +121,10 @@ public final class PriceItem implements Price {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void giveTo(@NotNull final Player player) {
|
||||
public void giveTo(@NotNull final Player player,
|
||||
final double multiplier) {
|
||||
ItemStack itemStack = item.getItem().clone();
|
||||
itemStack.setAmount((int) getValue(player));
|
||||
itemStack.setAmount((int) getValue(player, multiplier));
|
||||
|
||||
new DropQueue(player)
|
||||
.addItem(itemStack)
|
||||
@@ -130,9 +133,11 @@ public final class PriceItem implements Price {
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getValue(@NotNull final Player player) {
|
||||
public double getValue(@NotNull final Player player,
|
||||
final double multiplier) {
|
||||
return Math.toIntExact(Math.round(
|
||||
this.function.apply(MathContext.copyWithPlayer(baseContext, player)) * getMultiplier(player)
|
||||
this.function.apply(MathContext.copyWithPlayer(baseContext, player))
|
||||
* getMultiplier(player) * multiplier
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
@@ -74,6 +74,10 @@ fun SlotBuilder.onClick(clickType: ClickType, action: (Player, InventoryClickEve
|
||||
fun SlotBuilder.notCaptiveFor(test: (Player) -> Boolean): SlotBuilder =
|
||||
this.notCaptiveFor { test(it) }
|
||||
|
||||
/** @see SlotBuilder.setCaptiveFilter */
|
||||
fun SlotBuilder.setCaptiveFilter(test: (Player, Menu, ItemStack?) -> Boolean): SlotBuilder =
|
||||
this.setCaptiveFilter { a, b, c -> test(a, b, c) }
|
||||
|
||||
/**
|
||||
* @see SlotBuilder.setModifier
|
||||
* @deprecated Use SlotUpdater instead.
|
||||
|
||||
@@ -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.getUnitValue(player).getValue(player, this.amount.toDouble())
|
||||
|
||||
/** @see ShopManager.getUnitValue */
|
||||
fun ItemStack.getUnitValue(player: Player): Price =
|
||||
ShopManager.getUnitValue(this, player)
|
||||
|
||||
/** @see ShopManager.isSellable */
|
||||
fun ItemStack?.isSellable(player: Player): Boolean =
|
||||
ShopManager.isSellable(this, player)
|
||||
|
||||
Reference in New Issue
Block a user