From 7a8158508aa3d337ede175fbbf3cb2b7f5b31f91 Mon Sep 17 00:00:00 2001 From: Acrobot Date: Fri, 24 Aug 2012 10:24:20 +0200 Subject: [PATCH] Added a discount module --- com/Acrobot/ChestShop/ChestShop.java | 1 + .../PostTransaction/EmptyShopDeleter.java | 8 +++ .../PreTransaction/DiscountModule.java | 65 +++++++++++++++++++ com/Acrobot/ChestShop/Permission.java | 4 +- 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 com/Acrobot/ChestShop/Listeners/PreTransaction/DiscountModule.java diff --git a/com/Acrobot/ChestShop/ChestShop.java b/com/Acrobot/ChestShop/ChestShop.java index c50eb13..9098611 100644 --- a/com/Acrobot/ChestShop/ChestShop.java +++ b/com/Acrobot/ChestShop/ChestShop.java @@ -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()); diff --git a/com/Acrobot/ChestShop/Listeners/PostTransaction/EmptyShopDeleter.java b/com/Acrobot/ChestShop/Listeners/PostTransaction/EmptyShopDeleter.java index 6381ee6..953b612 100644 --- a/com/Acrobot/ChestShop/Listeners/PostTransaction/EmptyShopDeleter.java +++ b/com/Acrobot/ChestShop/Listeners/PostTransaction/EmptyShopDeleter.java @@ -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)); } diff --git a/com/Acrobot/ChestShop/Listeners/PreTransaction/DiscountModule.java b/com/Acrobot/ChestShop/Listeners/PreTransaction/DiscountModule.java new file mode 100644 index 0000000..5b72186 --- /dev/null +++ b/com/Acrobot/ChestShop/Listeners/PreTransaction/DiscountModule.java @@ -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 groupList = new HashSet(); + + 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; + } + } + } +} diff --git a/com/Acrobot/ChestShop/Permission.java b/com/Acrobot/ChestShop/Permission.java index 01434f8..ec1aedd 100644 --- a/com/Acrobot/ChestShop/Permission.java +++ b/com/Acrobot/ChestShop/Permission.java @@ -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;