Fiiiixeees

This commit is contained in:
Acrobot 2012-09-02 22:41:24 +02:00
parent 0a5707c4d8
commit 7ef93e7bc1
6 changed files with 52 additions and 23 deletions

View File

@ -16,8 +16,8 @@ public enum Property {
ALLOW_SIGN_CHEST_OPEN(true, "Can shop's chest be opened by owner with right-clicking a shop's sign?"),
ALLOW_LEFT_CLICK_DESTROYING(true, "If true, if you left-click your own shop sign you won't open chest's inventory, but instead you will start destroying the sign.\n"),
REMOVE_EMPTY_SHOPS(false, "If true, if the shop is empty, the sign is destroyed and put into the chest, so the shop isn't usable anymore.\n"),
REMOVE_EMPTY_CHESTS(false, "If true, if the REMOVE_EMPTY_SHOPS option is turned on, the chest is also destroyed."),
REMOVE_EMPTY_SHOPS(false, "If true, if the shop is empty, the sign is destroyed and put into the chest, so the shop isn't usable anymore."),
REMOVE_EMPTY_CHESTS(false, "If true, if the REMOVE_EMPTY_SHOPS option is turned on, the chest is also destroyed.\n"),
ADMIN_SHOP_NAME("Admin Shop", "First line of your Admin Shop's sign should look like this"),
SERVER_ECONOMY_ACCOUNT("", "The economy account which Admin Shops should use and to which all taxes will go"),

View File

@ -1,7 +1,9 @@
package com.Acrobot.ChestShop.Economy;
import com.Acrobot.Breeze.Utils.NumberUtil;
import com.Acrobot.ChestShop.Config.Config;
import com.Acrobot.ChestShop.Config.Property;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.Utils.uName;
import static com.Acrobot.Breeze.Utils.NumberUtil.roundUp;
@ -20,9 +22,9 @@ public class Economy {
public static String getServerAccountName() {
return Config.getString(Property.SERVER_ECONOMY_ACCOUNT);
}
public static boolean isServerAccount(String acc) {
return getServerAccountName().equals(acc);
return ChestShopSign.isAdminShop(acc);
}
public static void add(String name, double amount) {
@ -30,29 +32,24 @@ public class Economy {
double tax = getTax(taxAmount, amount);
if (tax != 0) {
if (!serverAccount().isEmpty()) {
if (!getServerAccountName().isEmpty()) {
economy.add(getServerAccountName(), tax);
}
amount -= tax;
}
if (name.isEmpty()) return;
economy.add(uName.getName(name), amount);
}
public static double getTax(Property tax, double price) {
return roundDown((Config.getFloat(tax) / 100F) * price);
return NumberUtil.roundDown((Config.getFloat(tax) / 100F) * price);
}
public static void subtract(String name, double amount) {
if (name.isEmpty()) return;
economy.subtract(uName.getName(name), roundUp(amount));
}
public static boolean hasEnough(String name, double amount) {
if (isServerAccount(name)) {
return true;
}
return economy.hasEnough(uName.getName(name), roundUp(amount));
}

View File

@ -1,9 +1,13 @@
package com.Acrobot.ChestShop.Listeners.PostTransaction;
import com.Acrobot.ChestShop.Config.Config;
import com.Acrobot.ChestShop.Config.Property;
import com.Acrobot.ChestShop.Containers.AdminInventory;
import com.Acrobot.ChestShop.Economy.Economy;
import com.Acrobot.ChestShop.Events.TransactionEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.inventory.Inventory;
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.BUY;
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.SELL;
@ -18,8 +22,11 @@ public class EconomicModule implements Listener {
return;
}
if (isOwnerEconomicalyActive(event)) {
Economy.add(event.getOwner().getName(), event.getPrice());
}
Economy.subtract(event.getClient().getName(), event.getPrice());
Economy.add(event.getOwner().getName(), event.getPrice());
}
@EventHandler
@ -28,7 +35,22 @@ public class EconomicModule implements Listener {
return;
}
Economy.subtract(event.getOwner().getName(), event.getPrice());
if (isOwnerEconomicalyActive(event)) {
Economy.subtract(event.getOwner().getName(), event.getPrice());
}
Economy.add(event.getClient().getName(), event.getPrice());
}
public static String getServerAccountName() {
return Config.getString(Property.SERVER_ECONOMY_ACCOUNT);
}
public static boolean isServerShop(Inventory inventory) {
return inventory instanceof AdminInventory;
}
public static boolean isOwnerEconomicalyActive(TransactionEvent event) {
return !isServerShop(event.getOwnerInventory()) || !getServerAccountName().isEmpty();
}
}

View File

@ -24,8 +24,8 @@ public class EmptyShopDeleter implements Listener {
return;
}
if (shopShouldBeRemoved(event.getOwnerInventory())) {
if (Config.getBoolean(REMOVE_EMPTY_CHESTS) && !ChestShopSign.isAdminShop(event.getSign())) {
if (shopShouldBeRemoved(event.getOwnerInventory(), event.getStock())) {
if (Config.getBoolean(REMOVE_EMPTY_CHESTS) && !ChestShopSign.isAdminShop(event.getSign()) && chestIsEmpty(event.getOwnerInventory())) {
Chest connectedChest = uBlock.findConnectedChest(event.getSign());
connectedChest.getBlock().setType(Material.AIR);
}
@ -35,11 +35,7 @@ public class EmptyShopDeleter implements Listener {
}
}
private static boolean shopShouldBeRemoved(Inventory inventory) {
return Config.getBoolean(REMOVE_EMPTY_SHOPS) && isEmpty(inventory);
}
public static boolean isEmpty(Inventory inventory) {
private static boolean chestIsEmpty(Inventory inventory) {
for (ItemStack item : inventory.getContents()) {
if (item != null) {
return false;
@ -48,4 +44,18 @@ public class EmptyShopDeleter implements Listener {
return true;
}
private static boolean shopShouldBeRemoved(Inventory inventory, ItemStack[] stock) {
return Config.getBoolean(REMOVE_EMPTY_SHOPS) && !hasMoreStock(inventory, stock);
}
private static boolean hasMoreStock(Inventory inventory, ItemStack[] stock) {
for (ItemStack stack : stock) {
if (!inventory.contains(stack, 1)) {
return false;
}
}
return true;
}
}

View File

@ -23,7 +23,7 @@ public class ChestShopSign {
public static final Pattern[] SHOP_SIGN_PATTERN = {
Pattern.compile("^[\\w ]*$"),
Pattern.compile("[0-9]+"),
Pattern.compile("^[\\dbs(free) :]+$"),
Pattern.compile("(?i)^[\\dbs(free) :]+$"),
Pattern.compile("[\\w : -]+")
};

View File

@ -2,7 +2,7 @@ name: ChestShop
main: com.Acrobot.ChestShop.ChestShop
version: 3.50t0004
version: 3.50t0005
#for CButD
dev-url: http://dev.bukkit.org/server-mods/chestshop/