Added a discount module

This commit is contained in:
Acrobot 2012-08-24 10:24:20 +02:00
parent af692eb4f8
commit 7a8158508a
4 changed files with 77 additions and 1 deletions

View File

@ -164,6 +164,7 @@ public class ChestShop extends JavaPlugin {
registerEvent(new SpamClickProtector(Config.getInteger(SHOP_INTERACTION_INTERVAL)));
registerEvent(new RestrictedSign());
registerEvent(new DiscountModule());
if (Config.getBoolean(ALLOW_PARTIAL_TRANSACTIONS)) {
registerEvent(new PartialTransactionModule());

View File

@ -3,7 +3,10 @@ package com.Acrobot.ChestShop.Listeners.PostTransaction;
import com.Acrobot.ChestShop.Config.Config;
import com.Acrobot.ChestShop.Config.Property;
import com.Acrobot.ChestShop.Events.TransactionEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.Utils.uBlock;
import org.bukkit.Material;
import org.bukkit.block.Chest;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.inventory.Inventory;
@ -20,6 +23,11 @@ public class EmptyShopDeleter implements Listener {
}
if (shopShouldBeRemoved(event.getOwnerInventory())) {
if (!ChestShopSign.isAdminShop(event.getSign())) {
Chest connectedChest = uBlock.findConnectedChest(event.getSign());
connectedChest.getBlock().setType(Material.AIR);
}
event.getSign().getBlock().setType(Material.AIR);
event.getOwnerInventory().addItem(new ItemStack(Material.SIGN, 1));
}

View File

@ -0,0 +1,65 @@
package com.Acrobot.ChestShop.Listeners.PreTransaction;
import com.Acrobot.Breeze.Utils.PriceUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Config.BreezeConfiguration;
import com.Acrobot.ChestShop.Containers.AdminInventory;
import com.Acrobot.ChestShop.Events.PreTransactionEvent;
import com.Acrobot.ChestShop.Permission;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
import static com.Acrobot.Breeze.Utils.PriceUtil.NO_PRICE;
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 BreezeConfiguration config;
private Set<String> groupList = new HashSet<String>();
public DiscountModule() {
config = BreezeConfiguration.loadConfiguration(new File(ChestShop.getFolder(), "discounts.yml"));
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!)");
config.reload();
groupList = config.getKeys(false);
}
@EventHandler
public void onPreTransaction(PreTransactionEvent event) {
if (event.isCancelled() || event.getTransactionType() != BUY || !(event.getOwnerInventory() instanceof AdminInventory)) {
return;
}
Player client = event.getClient();
if (Permission.has(client, Permission.ADMIN)) {
return;
}
if (PriceUtil.getSellPrice(event.getSign().getLine(PRICE_LINE)) != NO_PRICE) {
return;
}
for (String group : groupList) {
if (Permission.has(client, Permission.DISCOUNT + group)) {
event.setPrice(event.getPrice() * (config.getDouble(group) / 100));
return;
}
}
}
}

View File

@ -21,7 +21,9 @@ public enum Permission {
MOD("ChestShop.mod"),
OTHER_NAME("ChestShop.name."),
GROUP("ChestShop.group."),
NOFEE("ChestShop.nofee");
NOFEE("ChestShop.nofee"),
DISCOUNT("ChestShop.discount.");
private final String permission;