diff --git a/src/main/java/com/Acrobot/ChestShop/ChestShop.java b/src/main/java/com/Acrobot/ChestShop/ChestShop.java index 0854a74..9aa5bef 100644 --- a/src/main/java/com/Acrobot/ChestShop/ChestShop.java +++ b/src/main/java/com/Acrobot/ChestShop/ChestShop.java @@ -14,6 +14,8 @@ import com.Acrobot.ChestShop.Listeners.Block.Break.SignBreak; import com.Acrobot.ChestShop.Listeners.Block.SignCreate; import com.Acrobot.ChestShop.Listeners.Item.ItemMoveListener; import com.Acrobot.ChestShop.Listeners.ItemInfoListener; +import com.Acrobot.ChestShop.Listeners.Modules.DiscountModule; +import com.Acrobot.ChestShop.Listeners.Modules.PriceRestrictionModule; import com.Acrobot.ChestShop.Listeners.Player.PlayerConnect; import com.Acrobot.ChestShop.Listeners.Player.PlayerInteract; import com.Acrobot.ChestShop.Listeners.Player.PlayerInventory; @@ -169,6 +171,8 @@ public class ChestShop extends JavaPlugin { registerPostShopCreationEvents(); registerPostTransactionEvents(); + registerModules(); + registerEvent(new SignBreak()); registerEvent(new SignCreate()); registerEvent(new ChestBreak()); @@ -217,7 +221,6 @@ public class ChestShop extends JavaPlugin { } registerEvent(new CreativeModeIgnorer()); - registerEvent(new DiscountModule()); registerEvent(new ErrorMessageSender()); registerEvent(new PermissionChecker()); registerEvent(new PriceValidator()); @@ -234,6 +237,11 @@ public class ChestShop extends JavaPlugin { registerEvent(new TransactionMessageSender()); } + private void registerModules() { + registerEvent(new DiscountModule()); + registerEvent(new PriceRestrictionModule()); + } + public void registerEvent(Listener listener) { getServer().getPluginManager().registerEvents(listener, this); } diff --git a/src/main/java/com/Acrobot/ChestShop/Configuration/MaxPrice.java b/src/main/java/com/Acrobot/ChestShop/Configuration/MaxPrice.java deleted file mode 100644 index 6a57cd5..0000000 --- a/src/main/java/com/Acrobot/ChestShop/Configuration/MaxPrice.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.Acrobot.ChestShop.Configuration; - -import com.Acrobot.ChestShop.ChestShop; -import org.bukkit.Material; -import org.bukkit.configuration.Configuration; -import org.bukkit.configuration.file.YamlConfiguration; - -/** - * @author Acrobot - */ -public class MaxPrice { - private static Configuration config = YamlConfiguration.loadConfiguration(ChestShop.loadFile("config.yml")); - - public static boolean canCreate(double buyPrice, double sellPrice, Material mat) { - return buyPriceWithinRange(buyPrice, mat) && sellPriceWithinRange(sellPrice, mat); - } - - private static boolean buyPriceWithinRange(double buyPrice, Material material) { - double bPrice = maxBuyPrice(material); - double maxPrice = maxBuyPrice(); - - return buyPrice <= bPrice && buyPrice <= maxPrice; - } - - private static boolean sellPriceWithinRange(double sellPrice, Material material) { - double sPrice = maxSellPrice(material); - double maxPrice = maxSellPrice(); - - return sellPrice <= sPrice && sellPrice <= maxPrice; - } - - 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.isSet(node) ? config.getDouble(node) : Double.MAX_VALUE; - } - - private static enum Price { - buy, - sell - } -} diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/PreTransaction/DiscountModule.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/DiscountModule.java similarity index 97% rename from src/main/java/com/Acrobot/ChestShop/Listeners/PreTransaction/DiscountModule.java rename to src/main/java/com/Acrobot/ChestShop/Listeners/Modules/DiscountModule.java index 75d573b..332f4ad 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/PreTransaction/DiscountModule.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/DiscountModule.java @@ -1,4 +1,4 @@ -package com.Acrobot.ChestShop.Listeners.PreTransaction; +package com.Acrobot.ChestShop.Listeners.Modules; import com.Acrobot.Breeze.Utils.PriceUtil; import com.Acrobot.ChestShop.ChestShop; diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/PriceRestrictionModule.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/PriceRestrictionModule.java new file mode 100644 index 0000000..4b6f10f --- /dev/null +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/PriceRestrictionModule.java @@ -0,0 +1,76 @@ +package com.Acrobot.ChestShop.Listeners.Modules; + +import com.Acrobot.Breeze.Utils.MaterialUtil; +import com.Acrobot.Breeze.Utils.PriceUtil; +import com.Acrobot.ChestShop.ChestShop; +import com.Acrobot.ChestShop.Events.PreShopCreationEvent; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; + +import java.io.File; +import java.io.IOException; + +import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.INVALID_PRICE; +import static com.Acrobot.ChestShop.Signs.ChestShopSign.ITEM_LINE; +import static com.Acrobot.ChestShop.Signs.ChestShopSign.PRICE_LINE; + +/** + * @author Acrobot + */ +public class PriceRestrictionModule implements Listener { + private YamlConfiguration configuration; + + public PriceRestrictionModule() { + File file = new File(ChestShop.getFolder(), "priceLimits.yml"); + + configuration = YamlConfiguration.loadConfiguration(file); + + configuration.options().header("In this file you can configure maximum and minimum prices for items (when creating a shop)."); + + if (!file.exists()) { + configuration.addDefault("max.buy_price.itemID", 5.53); + configuration.addDefault("max.buy_price.988", 3.51); + configuration.addDefault("max.sell_price.978", 3.52); + + configuration.addDefault("min.buy_price.979", 1.03); + configuration.addDefault("min.sell_price.989", 0.51); + + try { + configuration.options().copyDefaults(true); + configuration.save(ChestShop.loadFile("priceLimits.yml")); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + @EventHandler + public void onPreShopCreation(PreShopCreationEvent event) { + int itemID = MaterialUtil.getItem(event.getSignLine(ITEM_LINE)).getTypeId(); + + if (PriceUtil.hasBuyPrice(event.getSignLine(PRICE_LINE))) { + double buyPrice = PriceUtil.getBuyPrice(event.getSignLine(PRICE_LINE)); + + if (configuration.isDouble("min.buy_price." + itemID) && buyPrice < configuration.getDouble("min.buy_price." + itemID)) { + event.setOutcome(INVALID_PRICE); + } + + if (configuration.isDouble("max.buy_price." + itemID) && buyPrice > configuration.getDouble("max.buy_price." + itemID)) { + event.setOutcome(INVALID_PRICE); + } + } + + if (PriceUtil.hasSellPrice(event.getSignLine(PRICE_LINE))) { + double sellPrice = PriceUtil.getSellPrice(event.getSignLine(PRICE_LINE)); + + if (configuration.isDouble("min.sell_price." + itemID) && sellPrice < configuration.getDouble("min.sell_price." + itemID)) { + event.setOutcome(INVALID_PRICE); + } + + if (configuration.isDouble("max.sell_price." + itemID) && sellPrice > configuration.getDouble("max.sell_price." + itemID)) { + event.setOutcome(INVALID_PRICE); + } + } + } +}