ChestShop-3/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/DiscountModule.java

66 lines
2.2 KiB
Java
Raw Normal View History

2013-04-23 21:04:59 +02:00
package com.Acrobot.ChestShop.Listeners.Modules;
2012-08-24 10:24:20 +02:00
import com.Acrobot.Breeze.Utils.PriceUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Containers.AdminInventory;
import com.Acrobot.ChestShop.Events.PreTransactionEvent;
import com.Acrobot.ChestShop.Permission;
import org.bukkit.configuration.file.YamlConfiguration;
2012-08-24 10:24:20 +02:00
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
2012-08-24 10:24:20 +02:00
import org.bukkit.event.Listener;
import java.io.IOException;
2012-08-24 10:24:20 +02:00
import java.util.HashSet;
import java.util.Set;
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.BUY;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.PRICE_LINE;
/**
* @author Acrobot
*/
public class DiscountModule implements Listener {
private YamlConfiguration config;
2012-08-24 10:24:20 +02:00
private Set<String> groupList = new HashSet<String>();
public DiscountModule() {
config = YamlConfiguration.loadConfiguration(ChestShop.loadFile("discounts.yml"));
2012-08-24 10:24:20 +02:00
config.options().header("This file is for discount management. You are able to do that:\n" +
"group1: 75\n" +
"That means that the person with ChestShop.discount.group1 permission will pay only 75% of the price. \n" +
"For example, if the price is 100 dollars, the player pays only 75 dollars.\n" +
"(Only works in buy-only Admin Shops!)");
try {
config.save(ChestShop.loadFile("discounts.yml"));
} catch (IOException e) {
e.printStackTrace();
}
2012-08-24 10:24:20 +02:00
groupList = config.getKeys(false);
}
@EventHandler(priority = EventPriority.LOW)
2012-08-24 10:24:20 +02:00
public void onPreTransaction(PreTransactionEvent event) {
if (event.isCancelled() || event.getTransactionType() != BUY || !(event.getOwnerInventory() instanceof AdminInventory)) {
return;
}
Player client = event.getClient();
2013-02-23 22:22:08 +01:00
if (!PriceUtil.hasBuyPrice(event.getSign().getLine(PRICE_LINE))) {
2012-08-24 10:24:20 +02:00
return;
}
for (String group : groupList) {
if (Permission.has(client, Permission.DISCOUNT + group)) {
event.setPrice(event.getPrice() * (config.getDouble(group) / 100));
return;
}
}
}
}