Skip to content

Commit

Permalink
Implement priority getSignName price check, fallback Material price c…
Browse files Browse the repository at this point in the history
…heck (#594)

Co-authored-by: casptyche <[email protected]>
  • Loading branch information
casptyche and casptyche authored Jul 7, 2024
1 parent 6a2a3a4 commit 69df518
Showing 1 changed file with 69 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.Utils.ItemUtil;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
Expand Down Expand Up @@ -42,7 +43,7 @@ private void load() {

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()) {
configuration.addDefault("uses_materials", true);
Expand All @@ -54,6 +55,11 @@ private void load() {
configuration.addDefault("min.buy_price.piston_head", 1.03);
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 {
configuration.options().copyDefaults(true);
configuration.save(ChestShop.loadFile("priceLimits.yml"));
Expand Down Expand Up @@ -101,18 +107,60 @@ public void onReload(ChestShopReloadEvent event) {
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
public void onPreShopCreation(PreShopCreationEvent event) {
ItemParseEvent parseEvent = new ItemParseEvent(ChestShopSign.getItem(event.getSignLines()));
Bukkit.getPluginManager().callEvent(parseEvent);
ItemStack material = parseEvent.getItem();
ItemStack itemStack = parseEvent.getItem();
Player player = event.getPlayer();

if (material == null) {
if (itemStack == null) {
return;
}

String itemType = material.getType().toString().toLowerCase(Locale.ROOT);
int amount;
try {
amount = ChestShopSign.getQuantity(event.getSignLines());
Expand All @@ -124,41 +172,41 @@ public void onPreShopCreation(PreShopCreationEvent event) {
if (PriceUtil.hasBuyPrice(priceLine)) {
BigDecimal buyPrice = PriceUtil.getExactBuyPrice(priceLine);

BigDecimal minBuyPrice = BigDecimal.valueOf(configuration.getDouble("min.buy_price." + itemType) * amount);
if (isValid("min.buy_price." + itemType) && buyPrice.compareTo(minBuyPrice) < 0
&& !Permission.has(player, NOLIMIT_MIN_BUY) && !Permission.has(player, NOLIMIT_MIN_BUY_ID + itemType)) {
String minBuyItemPath = getConfigPath("min.buy_price.", itemStack);
BigDecimal minBuyPrice = getLimit(minBuyItemPath, amount);
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);
Messages.BUY_PRICE_BELOW_MIN.sendWithPrefix(player, "price", buyPrice.toPlainString(), "minprice", minBuyPrice.toPlainString());
}

BigDecimal maxBuyPrice = BigDecimal.valueOf(configuration.getDouble("max.buy_price." + itemType) * amount);
if (isValid("max.buy_price." + itemType) && buyPrice.compareTo(maxBuyPrice) > 0
&& !Permission.has(player, NOLIMIT_MAX_BUY) && !Permission.has(player, NOLIMIT_MAX_BUY_ID + itemType)) {
String maxBuyItemPath = getConfigPath("max.buy_price.", itemStack);
BigDecimal maxBuyPrice = getLimit(maxBuyItemPath, amount);
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);
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());
}
}

if (PriceUtil.hasSellPrice(priceLine)) {
BigDecimal sellPrice = PriceUtil.getExactSellPrice(priceLine);

BigDecimal minSellPrice = BigDecimal.valueOf(configuration.getDouble("min.sell_price." + itemType) * amount);
if (isValid("min.sell_price." + itemType) && sellPrice.compareTo(minSellPrice) < 0
&& !Permission.has(player, NOLIMIT_MIN_SELL) && !Permission.has(player, NOLIMIT_MIN_SELL_ID + itemType)) {
String minSellItemPath = getConfigPath("min.sell_price.", itemStack);
BigDecimal minSellPrice = getLimit(minSellItemPath, amount);
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);
Messages.SELL_PRICE_BELOW_MIN.sendWithPrefix(player, "price", sellPrice.toPlainString(), "minprice", minSellPrice.toPlainString());
}

BigDecimal maxSellPrice = BigDecimal.valueOf(configuration.getDouble("max.sell_price." + itemType) * amount);
if (isValid("max.sell_price." + itemType) && sellPrice.compareTo(maxSellPrice) > 0
&& !Permission.has(player, NOLIMIT_MAX_SELL) && !Permission.has(player, NOLIMIT_MAX_SELL_ID + itemType)) {
String maxSellItemPath = getConfigPath("max.sell_price.", itemStack);
BigDecimal maxSellPrice = getLimit(maxSellItemPath, amount);
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);
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;
}
}

0 comments on commit 69df518

Please sign in to comment.