Modify modules + fix maxPrice

This commit is contained in:
Acrobot 2013-04-23 21:04:59 +02:00
parent 688d146732
commit 12a1dd87f3
4 changed files with 86 additions and 63 deletions

View File

@ -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);
}

View File

@ -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
}
}

View File

@ -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;

View File

@ -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);
}
}
}
}