From 9f63103ff35b1b0be01f86676b7681aa8f4c9d0d Mon Sep 17 00:00:00 2001 From: Acrobot Date: Thu, 19 Apr 2012 15:46:05 +0200 Subject: [PATCH] Changed MaxPrice to also work globally --- com/Acrobot/ChestShop/Config/Config.java | 8 +++- com/Acrobot/ChestShop/Config/MaxPrice.java | 53 ++++++++++++++++------ 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/com/Acrobot/ChestShop/Config/Config.java b/com/Acrobot/ChestShop/Config/Config.java index ce6120cf..bab30de1 100644 --- a/com/Acrobot/ChestShop/Config/Config.java +++ b/com/Acrobot/ChestShop/Config/Config.java @@ -18,7 +18,7 @@ public class Config { } public static float getFloat(Property value) { - return new Float(getValue(value.name()).toString()); + return getFloat(value.name()); } public static float getFloat(String value) { @@ -34,7 +34,11 @@ public class Config { } public static double getDouble(Property value) { - return Double.parseDouble(getValue(value.name()).toString()); + return getDouble(getValue(value.name()).toString()); + } + + public static double getDouble(String value) { + return Double.parseDouble(value); } private static String getColored(String msg) { diff --git a/com/Acrobot/ChestShop/Config/MaxPrice.java b/com/Acrobot/ChestShop/Config/MaxPrice.java index d07fb92e..df6cdf6a 100644 --- a/com/Acrobot/ChestShop/Config/MaxPrice.java +++ b/com/Acrobot/ChestShop/Config/MaxPrice.java @@ -6,24 +6,51 @@ import org.bukkit.Material; * @author Acrobot */ public class MaxPrice { - public static boolean canCreate(float buyPrice, float sellPrice, Material mat) { - float bPrice = maxBuyPrice(mat.getId()); - float sPrice = maxSellPrice(mat.getId()); - - return (bPrice == -1 || buyPrice <= maxBuyPrice(mat.getId())) - && (sPrice == -1 || sellPrice <= maxSellPrice(mat.getId())); + public static boolean canCreate(double buyPrice, double sellPrice, Material mat) { + return buyPriceWithinRange(buyPrice, mat) && sellPriceWithinRange(sellPrice, mat); } - public static float maxBuyPrice(int itemID) { - return getPrice("buy", itemID); + private static boolean buyPriceWithinRange(double buyPrice, Material material) { + double bPrice = maxBuyPrice(material); + double maxPrice = maxBuyPrice(); + + return buyPrice <= bPrice && buyPrice <= maxPrice; } - public static float maxSellPrice(int itemID) { - return getPrice("sell", itemID); + private static boolean sellPriceWithinRange(double sellPrice, Material material) { + double sPrice = maxSellPrice(material); + double maxPrice = maxSellPrice(); + + return sellPrice <= sPrice && sellPrice <= maxPrice; } - public static float getPrice(String value, int itemID) { - String node = "max-" + value + "-price-" + itemID; - return Config.exists(node) ? Config.getFloat(node) : -1; + public static double maxBuyPrice() { + return getPrice(Price.buy); + } + + public static double maxSellPrice() { + return getPrice(Price.sell); + } + + public static double maxBuyPrice(Material material) { + return getPrice(Price.buy, material.getId()); + } + + public static double maxSellPrice(Material material) { + return getPrice(Price.sell, material.getId()); + } + + public static double getPrice(Price price) { + return getPrice(price, -1); + } + + public static double getPrice(Price price, int itemID) { + String node = "max-" + price + "-price" + (itemID > 0 ? '-' + itemID : ""); + return Config.exists(node) ? Config.getDouble(node) : Double.MAX_VALUE; + } + + private static enum Price { + buy, + sell } }