mirror of
https://github.com/ChestShop-authors/ChestShop-3.git
synced 2024-11-25 19:45:36 +01:00
Implement priority getSignName price check, fallback Material price check (#594)
Co-authored-by: casptyche <172059830+casptyche@users.noreply.github.com>
This commit is contained in:
parent
6a2a3a4b36
commit
69df51849c
@ -8,6 +8,7 @@ import com.Acrobot.ChestShop.Events.ItemParseEvent;
|
|||||||
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
|
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
|
||||||
import com.Acrobot.ChestShop.Permission;
|
import com.Acrobot.ChestShop.Permission;
|
||||||
import com.Acrobot.ChestShop.Signs.ChestShopSign;
|
import com.Acrobot.ChestShop.Signs.ChestShopSign;
|
||||||
|
import com.Acrobot.ChestShop.Utils.ItemUtil;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
@ -42,7 +43,7 @@ public class PriceRestrictionModule implements Listener {
|
|||||||
|
|
||||||
configuration = YamlConfiguration.loadConfiguration(file);
|
configuration = YamlConfiguration.loadConfiguration(file);
|
||||||
|
|
||||||
configuration.options().header("In this file you can configure maximum and minimum prices for items (when creating a shop).");
|
configuration.options().header("In this file you can configure maximum and minimum prices for items (when creating a shop).\nBy default, entries are read as Materials. If you wish to use other items, simply enter the name as written on the /iteminfo command.\nExact matches to the /iteminfo name are prioritised, and materials are a fallback.");
|
||||||
|
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
configuration.addDefault("uses_materials", true);
|
configuration.addDefault("uses_materials", true);
|
||||||
@ -54,6 +55,11 @@ public class PriceRestrictionModule implements Listener {
|
|||||||
configuration.addDefault("min.buy_price.piston_head", 1.03);
|
configuration.addDefault("min.buy_price.piston_head", 1.03);
|
||||||
configuration.addDefault("min.sell_price.placed_banner", 0.51);
|
configuration.addDefault("min.sell_price.placed_banner", 0.51);
|
||||||
|
|
||||||
|
// Add example of custom item to the config
|
||||||
|
configuration.addDefault("max.buy_price.Powered Rail#1", 3.51);
|
||||||
|
configuration.addDefault("max.sell_price.Powered Rail#1", 3.52);
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
configuration.options().copyDefaults(true);
|
configuration.options().copyDefaults(true);
|
||||||
configuration.save(ChestShop.loadFile("priceLimits.yml"));
|
configuration.save(ChestShop.loadFile("priceLimits.yml"));
|
||||||
@ -101,18 +107,60 @@ public class PriceRestrictionModule implements Listener {
|
|||||||
load();
|
load();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Evaluate whether the configPath leads to a item or not
|
||||||
|
*
|
||||||
|
* @param configPathToItem the config path
|
||||||
|
* @return true if contained in config, false otherwise
|
||||||
|
*/
|
||||||
|
private boolean isValid(String configPathToItem) {
|
||||||
|
return configuration.getDouble(configPathToItem, INVALID_PATH) != INVALID_PATH;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the item reference for an item: First try get path via itemStack's getSignName, if valid
|
||||||
|
* return the getSignName, otherwise return path using itemStack's Material
|
||||||
|
*
|
||||||
|
* @param maxMinPath The min/max path
|
||||||
|
* @param itemStack The itemstack to get the config path for
|
||||||
|
* @return the getSignName for the itemStack, or the item's material
|
||||||
|
*/
|
||||||
|
private String getItemReference(String maxMinPath, ItemStack itemStack) {
|
||||||
|
String signName = ItemUtil.getSignName(itemStack);
|
||||||
|
// If there is a valid path to the itemstack using getSignName, return signName
|
||||||
|
// otherwise return the item material
|
||||||
|
return isValid((maxMinPath + signName)) ? signName
|
||||||
|
: itemStack.getType().toString().toLowerCase(Locale.ROOT);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the config path for the item 1: First try get path via itemStack's getSignName, if valid
|
||||||
|
* return the getSignName, otherwise return path using itemStack's Material
|
||||||
|
*
|
||||||
|
* @param maxMinPath The min/max path
|
||||||
|
* @param itemStack The itemstack to get the config path for
|
||||||
|
* @return the config path to the itemstack
|
||||||
|
*/
|
||||||
|
private String getConfigPath(String maxMinPath, ItemStack itemStack) {
|
||||||
|
return maxMinPath + getItemReference(maxMinPath, itemStack);
|
||||||
|
}
|
||||||
|
|
||||||
|
private BigDecimal getLimit(String itemConfigPath, int amount) {
|
||||||
|
return BigDecimal.valueOf(configuration.getDouble(itemConfigPath) * amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onPreShopCreation(PreShopCreationEvent event) {
|
public void onPreShopCreation(PreShopCreationEvent event) {
|
||||||
ItemParseEvent parseEvent = new ItemParseEvent(ChestShopSign.getItem(event.getSignLines()));
|
ItemParseEvent parseEvent = new ItemParseEvent(ChestShopSign.getItem(event.getSignLines()));
|
||||||
Bukkit.getPluginManager().callEvent(parseEvent);
|
Bukkit.getPluginManager().callEvent(parseEvent);
|
||||||
ItemStack material = parseEvent.getItem();
|
ItemStack itemStack = parseEvent.getItem();
|
||||||
Player player = event.getPlayer();
|
Player player = event.getPlayer();
|
||||||
|
|
||||||
if (material == null) {
|
if (itemStack == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String itemType = material.getType().toString().toLowerCase(Locale.ROOT);
|
|
||||||
int amount;
|
int amount;
|
||||||
try {
|
try {
|
||||||
amount = ChestShopSign.getQuantity(event.getSignLines());
|
amount = ChestShopSign.getQuantity(event.getSignLines());
|
||||||
@ -124,16 +172,18 @@ public class PriceRestrictionModule implements Listener {
|
|||||||
if (PriceUtil.hasBuyPrice(priceLine)) {
|
if (PriceUtil.hasBuyPrice(priceLine)) {
|
||||||
BigDecimal buyPrice = PriceUtil.getExactBuyPrice(priceLine);
|
BigDecimal buyPrice = PriceUtil.getExactBuyPrice(priceLine);
|
||||||
|
|
||||||
BigDecimal minBuyPrice = BigDecimal.valueOf(configuration.getDouble("min.buy_price." + itemType) * amount);
|
String minBuyItemPath = getConfigPath("min.buy_price.", itemStack);
|
||||||
if (isValid("min.buy_price." + itemType) && buyPrice.compareTo(minBuyPrice) < 0
|
BigDecimal minBuyPrice = getLimit(minBuyItemPath, amount);
|
||||||
&& !Permission.has(player, NOLIMIT_MIN_BUY) && !Permission.has(player, NOLIMIT_MIN_BUY_ID + itemType)) {
|
if (isValid(minBuyItemPath) && buyPrice.compareTo(minBuyPrice) < 0
|
||||||
|
&& !Permission.has(player, NOLIMIT_MIN_BUY) && !Permission.has(player, NOLIMIT_MIN_BUY_ID + getItemReference("min.buy_price.", itemStack))) {
|
||||||
event.setOutcome(BUY_PRICE_BELOW_MIN);
|
event.setOutcome(BUY_PRICE_BELOW_MIN);
|
||||||
Messages.BUY_PRICE_BELOW_MIN.sendWithPrefix(player, "price", buyPrice.toPlainString(), "minprice", minBuyPrice.toPlainString());
|
Messages.BUY_PRICE_BELOW_MIN.sendWithPrefix(player, "price", buyPrice.toPlainString(), "minprice", minBuyPrice.toPlainString());
|
||||||
}
|
}
|
||||||
|
|
||||||
BigDecimal maxBuyPrice = BigDecimal.valueOf(configuration.getDouble("max.buy_price." + itemType) * amount);
|
String maxBuyItemPath = getConfigPath("max.buy_price.", itemStack);
|
||||||
if (isValid("max.buy_price." + itemType) && buyPrice.compareTo(maxBuyPrice) > 0
|
BigDecimal maxBuyPrice = getLimit(maxBuyItemPath, amount);
|
||||||
&& !Permission.has(player, NOLIMIT_MAX_BUY) && !Permission.has(player, NOLIMIT_MAX_BUY_ID + itemType)) {
|
if (isValid(maxBuyItemPath) && buyPrice.compareTo(maxBuyPrice) > 0
|
||||||
|
&& !Permission.has(player, NOLIMIT_MAX_BUY) && !Permission.has(player, NOLIMIT_MAX_BUY_ID + getItemReference("max.buy_price.", itemStack))) {
|
||||||
event.setOutcome(BUY_PRICE_ABOVE_MAX);
|
event.setOutcome(BUY_PRICE_ABOVE_MAX);
|
||||||
Messages.BUY_PRICE_ABOVE_MAX.sendWithPrefix(player, "price", buyPrice.toPlainString(), "maxprice", maxBuyPrice.toPlainString());
|
Messages.BUY_PRICE_ABOVE_MAX.sendWithPrefix(player, "price", buyPrice.toPlainString(), "maxprice", maxBuyPrice.toPlainString());
|
||||||
}
|
}
|
||||||
@ -142,23 +192,21 @@ public class PriceRestrictionModule implements Listener {
|
|||||||
if (PriceUtil.hasSellPrice(priceLine)) {
|
if (PriceUtil.hasSellPrice(priceLine)) {
|
||||||
BigDecimal sellPrice = PriceUtil.getExactSellPrice(priceLine);
|
BigDecimal sellPrice = PriceUtil.getExactSellPrice(priceLine);
|
||||||
|
|
||||||
BigDecimal minSellPrice = BigDecimal.valueOf(configuration.getDouble("min.sell_price." + itemType) * amount);
|
String minSellItemPath = getConfigPath("min.sell_price.", itemStack);
|
||||||
if (isValid("min.sell_price." + itemType) && sellPrice.compareTo(minSellPrice) < 0
|
BigDecimal minSellPrice = getLimit(minSellItemPath, amount);
|
||||||
&& !Permission.has(player, NOLIMIT_MIN_SELL) && !Permission.has(player, NOLIMIT_MIN_SELL_ID + itemType)) {
|
if (isValid(minSellItemPath) && sellPrice.compareTo(minSellPrice) < 0
|
||||||
|
&& !Permission.has(player, NOLIMIT_MIN_SELL) && !Permission.has(player, NOLIMIT_MIN_SELL_ID + getItemReference("min.sell_price.", itemStack))) {
|
||||||
event.setOutcome(SELL_PRICE_BELOW_MIN);
|
event.setOutcome(SELL_PRICE_BELOW_MIN);
|
||||||
Messages.SELL_PRICE_BELOW_MIN.sendWithPrefix(player, "price", sellPrice.toPlainString(), "minprice", minSellPrice.toPlainString());
|
Messages.SELL_PRICE_BELOW_MIN.sendWithPrefix(player, "price", sellPrice.toPlainString(), "minprice", minSellPrice.toPlainString());
|
||||||
}
|
}
|
||||||
|
|
||||||
BigDecimal maxSellPrice = BigDecimal.valueOf(configuration.getDouble("max.sell_price." + itemType) * amount);
|
String maxSellItemPath = getConfigPath("max.sell_price.", itemStack);
|
||||||
if (isValid("max.sell_price." + itemType) && sellPrice.compareTo(maxSellPrice) > 0
|
BigDecimal maxSellPrice = getLimit(maxSellItemPath, amount);
|
||||||
&& !Permission.has(player, NOLIMIT_MAX_SELL) && !Permission.has(player, NOLIMIT_MAX_SELL_ID + itemType)) {
|
if (isValid(maxSellItemPath) && sellPrice.compareTo(maxSellPrice) > 0
|
||||||
|
&& !Permission.has(player, NOLIMIT_MAX_SELL) && !Permission.has(player, NOLIMIT_MAX_SELL_ID + getItemReference("max.sell_price.", itemStack))) {
|
||||||
event.setOutcome(SELL_PRICE_ABOVE_MAX);
|
event.setOutcome(SELL_PRICE_ABOVE_MAX);
|
||||||
Messages.SELL_PRICE_ABOVE_MAX.sendWithPrefix(player, "price", sellPrice.toPlainString(), "maxprice", maxSellPrice.toPlainString());
|
Messages.SELL_PRICE_ABOVE_MAX.sendWithPrefix(player, "price", sellPrice.toPlainString(), "maxprice", maxSellPrice.toPlainString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isValid(String path) {
|
|
||||||
return configuration.getDouble(path, INVALID_PATH) != INVALID_PATH;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user