diff --git a/com/Acrobot/iConomyChestShop/Basic.java b/com/Acrobot/iConomyChestShop/Basic.java index 5cf2dcc..e82342d 100644 --- a/com/Acrobot/iConomyChestShop/Basic.java +++ b/com/Acrobot/iConomyChestShop/Basic.java @@ -1,5 +1,6 @@ package com.Acrobot.iConomyChestShop; +import com.Acrobot.iConomyChestShop.Chests.MinecraftChest; import com.Acrobot.iConomyChestShop.MinecartMania.MinecartManiaChest; import info.somethingodd.bukkit.OddItem.OddItem; import java.io.File; @@ -166,14 +167,17 @@ public static String stripName(String name) { //What do I even have to say? public static int getItemAmountFromInventory(Inventory inv, ItemStack is) { + return getItemAmount(inv.getContents(), is); + } + + public static int getItemAmount(ItemStack[] items, ItemStack is){ int id = is.getTypeId(); boolean checkDurability = true; if(ConfigManager.getBoolean("allowUsedItemsToBeSold") && id >= 256 && id <= 317){ checkDurability = false; } int count = 0; - ItemStack Items[] = inv.getContents(); - for (ItemStack Item : Items) { + for (ItemStack Item : items) { if (Item == null) { continue; } @@ -224,6 +228,7 @@ public static void removeItemStackFromInventory(Inventory inv, ItemStack is, int public static void addItemToInventory(MinecartManiaChest chest, ItemStack is, int left){ addItemToInventory(chest.getInventory(), is, left); } + //Adds item to inventory public static void addItemToInventory(Inventory inv, ItemStack is, int left){ int maxStackSize = is.getType().getMaxStackSize(); @@ -253,14 +258,14 @@ public static void addItemToInventory(Inventory inv, ItemStack is, int left){ } } - public static Chest findChest(Block block){ + public static MinecraftChest findChest(Block block){ if(!ConfigManager.getString("position").toUpperCase().equals("ANY")){ Block faceBlock = block.getFace(BlockFace.valueOf(ConfigManager.getString("position").toUpperCase()), ConfigManager.getInt("distance")); - return (faceBlock != null && faceBlock.getType() == Material.CHEST ? (Chest) faceBlock.getState() : null); + return (faceBlock != null && faceBlock.getType() == Material.CHEST ? new MinecraftChest((Chest) faceBlock.getState()) : null); } for(BlockFace bf : BlockFace.values()){ if(block.getFace(bf) != null && block.getFace(bf).getType() == Material.CHEST){ - return (Chest) block.getFace(bf).getState(); + return new MinecraftChest((Chest) block.getFace(bf).getState()); } } return null; @@ -292,15 +297,16 @@ public static boolean checkFreeSpace(MinecartManiaChest chest,ItemStack is, int //Checks if there is enough free space in inventory public static boolean checkFreeSpace(Inventory inv, ItemStack is, int left){ - ItemStack[] contents = inv.getContents(); + return checkFreeSpace(inv.getContents(), is, left); + } + public static boolean checkFreeSpace(ItemStack[] contents, ItemStack is, int left){ Material type = is.getType(); short durability = is.getDurability(); int maxStack = is.getType().getMaxStackSize(); - for(int i=0; i < inv.getSize(); i++){ + for(ItemStack curitem : contents){ if(left <= 0){ return true; } - ItemStack curitem = contents[i]; if(curitem == null){ left = left - maxStack; continue; diff --git a/com/Acrobot/iConomyChestShop/Chests/ChestObject.java b/com/Acrobot/iConomyChestShop/Chests/ChestObject.java new file mode 100644 index 0000000..0a1adad --- /dev/null +++ b/com/Acrobot/iConomyChestShop/Chests/ChestObject.java @@ -0,0 +1,19 @@ +package com.Acrobot.iConomyChestShop.Chests; + +import org.bukkit.inventory.ItemStack; + +public interface ChestObject { + public ItemStack[] getContents(); + + public void setSlot(int slot, ItemStack item); + public void clearSlot(int slot); + + public void addItem(ItemStack item, int amount); + public void removeItem(ItemStack item, int amount); + + public int amount(ItemStack item); + public boolean hasEnough(ItemStack item, int amount); + public boolean fits(ItemStack item, int amount); + + public int getSize(); +} diff --git a/com/Acrobot/iConomyChestShop/Chests/GiftPost.java b/com/Acrobot/iConomyChestShop/Chests/GiftPost.java new file mode 100644 index 0000000..e9629ae --- /dev/null +++ b/com/Acrobot/iConomyChestShop/Chests/GiftPost.java @@ -0,0 +1,77 @@ +package com.Acrobot.iConomyChestShop.Chests; + +import com.Acrobot.iConomyChestShop.Basic; +import com.Acrobot.iConomyChestShop.ConfigManager; +import com.aranai.virtualchest.VirtualChest; +import org.bukkit.inventory.ItemStack; + +/** + * Compatibility with VirtualChests - future relases + * @author Acrobot + */ +public class GiftPost implements ChestObject{ + private final VirtualChest chest; + + public GiftPost(VirtualChest v){ + this.chest = v; + } + + public ItemStack[] getContents() { + return chest.getContents(); + } + + public void setSlot(int slot, ItemStack item) { + chest.setItem(slot, item); + } + + public void clearSlot(int slot) { + chest.setItem(slot, null); + } + + public void addItem(ItemStack item, int amount) { + item.setAmount(amount); + chest.addItem(item); + } + + public void removeItem(ItemStack item, int amount) { + int type = item.getTypeId(); + boolean checkDurability = true; + if(ConfigManager.getBoolean("allowUsedItemsToBeSold") && type >= 256 && type <= 317){ + checkDurability = false; + } + for (int i = 0; i < chest.usedCases(); i++) { + ItemStack is = chest.getItem(i); + if (is != null) { + short durability = item.getDurability(); + int isAmount = is.getAmount(); + if (is.getTypeId() == type && (!checkDurability || durability == -1 || (is.getDurability() == durability))) { + if (isAmount - amount > 0) { + chest.setItem(i, new ItemStack(type, isAmount - amount, durability)); + return; + } else if (isAmount - amount == 0) { + return; + } else { + amount -= isAmount; + chest.setItem(i, null); + } + } + } + } + } + + public int amount(ItemStack item) { + return Basic.getItemAmount(chest.getContents(), item); + } + + public boolean hasEnough(ItemStack item, int amount) { + return amount(item) >= amount; + } + + public boolean fits(ItemStack item, int amount) { + return true; + } + + public int getSize() { + return chest.usedCases(); + } +} diff --git a/com/Acrobot/iConomyChestShop/ChestObject.java b/com/Acrobot/iConomyChestShop/Chests/MinecraftChest.java similarity index 85% rename from com/Acrobot/iConomyChestShop/ChestObject.java rename to com/Acrobot/iConomyChestShop/Chests/MinecraftChest.java index fbe5bbe..96151b9 100644 --- a/com/Acrobot/iConomyChestShop/ChestObject.java +++ b/com/Acrobot/iConomyChestShop/Chests/MinecraftChest.java @@ -1,5 +1,6 @@ -package com.Acrobot.iConomyChestShop; +package com.Acrobot.iConomyChestShop.Chests; +import com.Acrobot.iConomyChestShop.Basic; import com.Acrobot.iConomyChestShop.MinecartMania.MinecartManiaChest; import com.Acrobot.iConomyChestShop.MinecartMania.MinecartManiaDoubleChest; import org.bukkit.block.Chest; @@ -9,18 +10,23 @@ * * @author Acrobot */ -public class ChestObject { - private final MinecartManiaChest main; - private final MinecartManiaChest extended; +public class MinecraftChest implements ChestObject{ + public final MinecartManiaChest main; + public final MinecartManiaChest extended; - ChestObject(Chest main){ + public MinecraftChest(Chest main){ this.main = new MinecartManiaChest(main); this.extended = this.main.getNeighborChest(); } + + public boolean exists(){ + return main != null; + } private MinecartManiaDoubleChest doubleChest(){ return new MinecartManiaDoubleChest(main, extended); } + public ItemStack[] getContents(){ if(extended != null){ return doubleChest().getContents(); @@ -71,7 +77,7 @@ public boolean hasEnough(ItemStack item, int amount){ return amount(item) >= amount; } - public boolean hasFreeSpace(ItemStack item, int amount){ + public boolean fits(ItemStack item, int amount){ if(extended != null){ return Basic.checkFreeSpace(main, item, amount) || Basic.checkFreeSpace(extended, item, amount); } diff --git a/com/Acrobot/iConomyChestShop/ConfigManager.java b/com/Acrobot/iConomyChestShop/ConfigManager.java index d360dd3..8a6a4f0 100644 --- a/com/Acrobot/iConomyChestShop/ConfigManager.java +++ b/com/Acrobot/iConomyChestShop/ConfigManager.java @@ -45,7 +45,7 @@ public static void buyingString(int amount, String item, String owner, Player pl String str = getLanguage("You_bought_items"); str = str.replace("", amount + ""); str = str.replace("", item); - str = str.replace("", owner); + str = str.replace("", owner.replace("[", "")); str = str.replace("", formattedBalance(cost)); player.sendMessage(str); @@ -60,7 +60,7 @@ public static void sellingString(int amount, String item, String owner, Player p String str = getLanguage("You_sold_items"); str = str.replace("", amount + ""); str = str.replace("", item); - str = str.replace("", owner); + str = str.replace("", owner.replace("[", "")); str = str.replace("", formattedBalance(cost)); player.sendMessage(str); diff --git a/com/Acrobot/iConomyChestShop/MinecartMania/MinecartManiaChest.java b/com/Acrobot/iConomyChestShop/MinecartMania/MinecartManiaChest.java index 446eff8..d1faabc 100644 --- a/com/Acrobot/iConomyChestShop/MinecartMania/MinecartManiaChest.java +++ b/com/Acrobot/iConomyChestShop/MinecartMania/MinecartManiaChest.java @@ -72,6 +72,7 @@ public static Block getNeightborChestBlock(Block block){ * @param x coordinate to search * @param y coordinate to search * @param z coordinate to search + * @return neighbor chest */ public static MinecartManiaChest getNeighborChest(World w, int x, int y, int z) { if (MinecartManiaWorld.getBlockAt(w, x - 1, y, z).getTypeId() == Material.CHEST.getId()) { diff --git a/com/Acrobot/iConomyChestShop/Shop.java b/com/Acrobot/iConomyChestShop/Shop.java index b1903a7..ddbe411 100644 --- a/com/Acrobot/iConomyChestShop/Shop.java +++ b/com/Acrobot/iConomyChestShop/Shop.java @@ -1,5 +1,7 @@ package com.Acrobot.iConomyChestShop; +import com.Acrobot.iConomyChestShop.Chests.ChestObject; +import com.Acrobot.iConomyChestShop.Chests.MinecraftChest; import org.bukkit.block.Sign; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -67,8 +69,11 @@ public boolean buy(Player player){ if(!isAdminShop()){ chest.removeItem(stock, stockAmount); EconomyManager.add(owner, getBuyPrice()); - }else if(EconomyManager.hasAccount(owner)){ - EconomyManager.add(owner, getBuyPrice()); + }else{ + String account = ConfigManager.getString("serverAccountName"); + if(!account.equals("") && EconomyManager.hasAccount(account)){ + EconomyManager.add(account, getSellPrice()); + } } stock.setAmount(stockAmount); Basic.addItemToInventory(player.getInventory(), stock, stockAmount); @@ -102,8 +107,11 @@ public boolean sell(Player player){ } chest.addItem(stock, stockAmount); EconomyManager.substract(owner, getSellPrice()); - }else if(EconomyManager.hasAccount(owner)){ - EconomyManager.substract(owner, getSellPrice()); + }else{ + String account = ConfigManager.getString("serverAccountName"); + if(!account.equals("") && EconomyManager.hasAccount(account)){ + EconomyManager.substract(account, getSellPrice()); + } } Basic.removeItemStackFromInventory(player.getInventory(), stock, stockAmount); @@ -120,6 +128,14 @@ public boolean sell(Player player){ public boolean isAdminShop(){ return owner.toLowerCase().replace(" ", "").equals("adminshop"); } + + public String shopOwnerName(){ + if(isAdminShop()){ + return ConfigManager.getString("serverAccountName"); + }else{ + return owner; + } + } //////////////////////////////////////////////////// @@ -148,7 +164,7 @@ private boolean isBuying(){ } private boolean hasFreeSpace(){ - return chest.hasFreeSpace(stock, stockAmount); + return chest.fits(stock, stockAmount); } private boolean playerHasFreeSpace(Player player){ diff --git a/com/Acrobot/iConomyChestShop/ShopManager.java b/com/Acrobot/iConomyChestShop/ShopManager.java index bd55086..8c18265 100644 --- a/com/Acrobot/iConomyChestShop/ShopManager.java +++ b/com/Acrobot/iConomyChestShop/ShopManager.java @@ -1,9 +1,7 @@ package com.Acrobot.iConomyChestShop; -import org.bukkit.Material; -import org.bukkit.block.Block; -import org.bukkit.block.BlockFace; -import org.bukkit.block.Chest; +import com.Acrobot.iConomyChestShop.Chests.ChestObject; +import com.Acrobot.iConomyChestShop.Chests.MinecraftChest; import org.bukkit.block.Sign; import org.bukkit.entity.Player; import org.bukkit.event.block.Action; @@ -19,11 +17,10 @@ public class ShopManager extends PlayerListener { public static void transaction(PlayerInteractEvent event) { boolean reverseButtons = ConfigManager.getBoolean("reverseButtons"); Sign sign = (Sign) event.getClickedBlock().getState(); - Chest chest = Basic.findChest(event.getClickedBlock()); Player player = event.getPlayer(); boolean adminShop = sign.getLine(0).toLowerCase().replace(" ", "").equals("adminshop"); - Shop shop = new Shop(sign, (!adminShop ? new ChestObject(chest) : null)); + Shop shop = new Shop(sign, (!adminShop ? Basic.findChest(event.getClickedBlock()) : null)); Action action = event.getAction(); @@ -69,7 +66,7 @@ public static void buy(Shop shop, Player player) { } public static void sell(Shop shop, Player player) { - if (EconomyManager.hasAccount(shop.owner) && !EconomyManager.hasEnough(shop.owner, shop.getSellPrice())) { + if (EconomyManager.hasAccount(shop.shopOwnerName()) && !EconomyManager.hasEnough(shop.shopOwnerName(), shop.getSellPrice())) { player.sendMessage(ConfigManager.getLanguage("Seller_has_not_enough_money")); return; } diff --git a/com/Acrobot/iConomyChestShop/SignManager.java b/com/Acrobot/iConomyChestShop/SignManager.java index 0964f38..b19bfb8 100644 --- a/com/Acrobot/iConomyChestShop/SignManager.java +++ b/com/Acrobot/iConomyChestShop/SignManager.java @@ -1,6 +1,8 @@ package com.Acrobot.iConomyChestShop; +import com.Acrobot.iConomyChestShop.Chests.MinecraftChest; import com.Acrobot.iConomyChestShop.MinecartMania.MinecartManiaChest; +import com.Balor.bukkit.GiftPost.GiftPostWorker; import net.minecraft.server.EntityPlayer; import org.bukkit.Location; import org.bukkit.Material; @@ -43,21 +45,21 @@ public void onSignChange(SignChangeEvent event) { } String text[] = event.getLines(); - Chest chest = Basic.findChest(block);//event.getBlock().getFace(BlockFace.valueOf(ConfigManager.getString("position").toUpperCase()), ConfigManager.getInt("distance")); + MinecraftChest chest = Basic.findChest(block);//event.getBlock().getFace(BlockFace.valueOf(ConfigManager.getString("position").toUpperCase()), ConfigManager.getInt("distance")); if (Basic.isInt(text[1])) { String split[] = text[2].split(":"); boolean isFormated = mySign(text); boolean isGoodSign; try { - isGoodSign = (!text[0].contains("[") && !text[0].contains("]") && (split.length == 2 ? (Basic.isFloat(split[0]) && Basic.isFloat(split[1])) : Basic.isFloat(text[2]))) || isFormated; + isGoodSign = (!text[0].contains("[") && (split.length == 2 ? (Basic.isFloat(split[0]) && Basic.isFloat(split[1])) : Basic.isFloat(text[2]))) || isFormated; } catch (Exception ex) { return; } if (isGoodSign) { if (chest != null) { if(!isAdmin){ - Block ChestB = chest.getBlock(); + Block ChestB = chest.main.getChest().getBlock(); CraftSign sign; if((sign = ProtectionManager.getSign(ChestB, false)) != null){ if(!sign.getLine(0).equals(p.getName())){ @@ -75,10 +77,8 @@ public void onSignChange(SignChangeEvent event) { } } } - MinecartManiaChest mmc = new MinecartManiaChest(chest); - MinecartManiaChest neighbor = mmc.getNeighborChest(); - if (neighbor != null) { - CraftSign sig = ProtectionManager.getSign(neighbor.getChest().getBlock(), true); + if (chest.extended != null) { + CraftSign sig = ProtectionManager.getSign(chest.extended.getChest().getBlock(), true); if (sig != null) { if (!sig.getLine(0).equals(playerName) && !isAdmin) { p.sendMessage(ConfigManager.getLanguage("You_tried_to_steal")); @@ -89,14 +89,14 @@ public void onSignChange(SignChangeEvent event) { } } } else if (!text[0].toLowerCase().replace(" ", "").equals("adminshop")) { - if(ConfigManager.getBoolean("doNotRemoveSignIfNotCorrect")){ + if (ConfigManager.getBoolean("doNotRemoveSignIfNotCorrect")) { return; } Basic.cancelEventAndDropSign(event); p.sendMessage(ConfigManager.getLanguage("Shop_cannot_be_created")); return; } - if (!(!text[0].equals("") && isAdmin)) { + if (!(!text[0].equals("") && !text[0].startsWith("[") && isAdmin)) { event.setLine(0, playerName); } if (priceIsNegative(text[2])) { @@ -147,7 +147,6 @@ public void onSignChange(SignChangeEvent event) { if (alias != null) { event.setLine(3, alias); } - } if ((text[2].length() > 11 && !isFormated) || text[1].length() > 15) { Basic.cancelEventAndDropSign(event); @@ -161,7 +160,7 @@ public void onSignChange(SignChangeEvent event) { ProtectionManager.protectBlock(block, text[0]); } if (chest != null) { - Block ChestB = chest.getBlock(); + Block ChestB = chest.main.getChest().getBlock(); if (ProtectionManager.protectBlock(ChestB, text[0])) { p.sendMessage(ConfigManager.getLanguage("Shop_was_LWC_protected")); } @@ -189,7 +188,7 @@ public static boolean mySign(Sign sign) { public static boolean mySign(String text[]) { try { String textToParse = text[2].replace(" ", ""); - return (textToParse.contains("S") || textToParse.contains("B")) && !text[3].isEmpty() && Basic.isInt(text[1]) && !text[0].contains("[") && !text[0].contains("]"); + return (textToParse.contains("S") || textToParse.contains("B")) && !text[3].isEmpty() && Basic.isInt(text[1]) && !text[0].contains("["); } catch (Exception ex) { return false; } diff --git a/com/Acrobot/iConomyChestShop/StatGenerator.java b/com/Acrobot/iConomyChestShop/StatGenerator.java index c408060..d759eb6 100644 --- a/com/Acrobot/iConomyChestShop/StatGenerator.java +++ b/com/Acrobot/iConomyChestShop/StatGenerator.java @@ -19,6 +19,7 @@ public class StatGenerator implements Runnable{ private static String header = fileToString("header"); private static String footer = fileToString("footer"); private static boolean unusedItems = ConfigManager.getBoolean("showUnusedItemsInGeneratedList"); + private static String filePath = ConfigManager.getString("generatedTransactionListPath"); private static BufferedWriter buf; public static void setPlugin(iConomyChestShop plugin){ @@ -105,12 +106,12 @@ public static void generateStatisticForItem(int itemID, int itemDurability){ } } public static void fileStart() throws IOException{ - FileWriter fw = new FileWriter("plugins/iConomyChestShop/ChestShop.stats.html"); + FileWriter fw = new FileWriter(filePath); fw.write(header); fw.close(); } public static void fileEnd() throws IOException{ - FileWriter fw = new FileWriter("plugins/iConomyChestShop/ChestShop.stats.html", true); + FileWriter fw = new FileWriter(filePath, true); fw.write(footer.replace("%time", time + "")); fw.close(); } @@ -120,7 +121,7 @@ public static void generateStatistics(){ plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Logging()); fileStart(); - FileWriter fileWriter = new FileWriter("plugins/iConomyChestShop/ChestShop.stats.html", true); + FileWriter fileWriter = new FileWriter(filePath, true); buf = new BufferedWriter(fileWriter); for (Material m : Material.values()) { diff --git a/com/Acrobot/iConomyChestShop/VirtualChestObject.java b/com/Acrobot/iConomyChestShop/VirtualChestObject.java deleted file mode 100644 index 92c932f..0000000 --- a/com/Acrobot/iConomyChestShop/VirtualChestObject.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.Acrobot.iConomyChestShop; - -import com.aranai.virtualchest.VirtualChest; -import org.bukkit.inventory.ItemStack; - -/** - * Compatibility with VirtualChests (IN FUTURE RELASES) - * @author Acrobot - */ -public class VirtualChestObject { - private final VirtualChest chest; - - VirtualChestObject(VirtualChest v){ - this.chest = v; - } - - public ItemStack[] getContents(){ - return chest.getContents(); - } - - public void setSlot(int slot, ItemStack item){ - chest.setItem(slot, item); - } - - public void clearSlot(int slot){ - chest.setItem(slot, null); - } - - public void addItem(ItemStack item, int amount){ - chest.addItem(new ItemStack(item.getType(), amount, item.getDurability())); - } - - public void removeItem(ItemStack item, int amount){ - int type = item.getTypeId(); - boolean checkDurability = true; - if(ConfigManager.getBoolean("allowUsedItemsToBeSold") && type >= 256 && type <= 317){ - checkDurability = false; - } - for (int i = 0; i < chest.usedCases(); i++) { - ItemStack is = chest.getItem(i); - if (is != null) { - short durability = is.getDurability(); - if (is.getTypeId() == type && (!checkDurability || durability == -1 || (is.getDurability() == durability))) { - if (is.getAmount() - amount > 0) { - chest.setItem(i, new ItemStack(type, is.getAmount() - amount, durability)); - return; - } else if (is.getAmount() - amount == 0) { - return; - } else { - amount -= is.getAmount(); - chest.setItem(i, null); - } - } - } - } - } - - public int amount(ItemStack item) { - int type = item.getTypeId(); - int amount = 0; - boolean checkDurability = true; - if (ConfigManager.getBoolean("allowUsedItemsToBeSold") && type >= 256 && type <= 317) { - checkDurability = false; - } - - for (int i = 0; i < chest.usedCases(); i++) { - ItemStack is = chest.getItem(i); - if (is != null) { - short durability = is.getDurability(); - if (is.getTypeId() == type && (!checkDurability || durability == -1 || (is.getDurability() == durability))) { - amount += is.getAmount(); - } - } - } - return amount; - } - - public boolean hasEnough(ItemStack item, int amount){ - return amount(item) >= amount; - } - -} diff --git a/com/Acrobot/iConomyChestShop/economyManager.java b/com/Acrobot/iConomyChestShop/economyManager.java index 64b7ea6..63a4133 100644 --- a/com/Acrobot/iConomyChestShop/economyManager.java +++ b/com/Acrobot/iConomyChestShop/economyManager.java @@ -1,82 +1,35 @@ package com.Acrobot.iConomyChestShop; -import com.iConomy.*; -import com.iConomy.system.Holdings; -import cosine.boseconomy.BOSEconomy; +import com.nijikokun.register.payment.Method; /** * * @author Acrobot */ public class EconomyManager { - private static iConomy iConomy = null; - public static BOSEconomy BOSEconomy = null; - - public static iConomy getiConomy() { - return iConomy; - } - public static boolean hasAccount(String p){ - if(iConomy != null){ - return iConomy.hasAccount(p); - } - return BOSEconomy != null; - } + public static Method economy; - public static void setiConomy(iConomy plugin) { - if (iConomy == null) { - iConomy = plugin; - } + public static boolean hasAccount(String p){ + return economy.hasAccount(p); } public static void add(String name, float amount){ - if(iConomy != null){ - Holdings balance = iConomy.getAccount(name).getHoldings(); - balance.add(amount); - } - if(BOSEconomy != null){ - int intAmount = Math.round(amount); - BOSEconomy.addPlayerMoney(name, intAmount, false); - } + economy.getAccount(name).add(amount); } public static void substract(String name, float amount){ - if(iConomy != null){ - Holdings balance = iConomy.getAccount(name).getHoldings(); - balance.subtract(amount); - } - if(BOSEconomy != null){ - int intAmount = Math.round(amount); - BOSEconomy.addPlayerMoney(name, -intAmount, false); - } + economy.getAccount(name).subtract(amount); } public static boolean hasEnough(String name, float amount) { - if (iConomy != null) { - Holdings balance = iConomy.getAccount(name).getHoldings(); - return balance.hasEnough(amount); - } - return BOSEconomy != null && (BOSEconomy.getPlayerMoney(name) >= amount); + return economy.getAccount(name).hasEnough(amount); } public static double balance(String name){ - if(iConomy != null){ - return iConomy.getAccount(name).getHoldings().balance(); - } - if(BOSEconomy != null){ - return BOSEconomy.getPlayerMoney(name); - } - return 0; + return economy.getAccount(name).balance(); } public static String formatedBalance(double amount){ - if(iConomy != null){ - return iConomy.format(amount); - } - String stringAmount = amount + ""; - stringAmount = stringAmount.replace(".0", ""); - if(BOSEconomy != null){ - return stringAmount + " " + BOSEconomy.getMoneyNameCaps(); - } - return amount + ""; + return economy.format(amount); } } diff --git a/com/Acrobot/iConomyChestShop/iConomyChestShopPlayerListener.java b/com/Acrobot/iConomyChestShop/iConomyChestShopPlayerListener.java index 1dd97a9..bb74a2d 100644 --- a/com/Acrobot/iConomyChestShop/iConomyChestShopPlayerListener.java +++ b/com/Acrobot/iConomyChestShop/iConomyChestShopPlayerListener.java @@ -1,10 +1,10 @@ package com.Acrobot.iConomyChestShop; import java.util.HashMap; + import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.block.Block; -import org.bukkit.block.BlockFace; import org.bukkit.block.Sign; import org.bukkit.entity.Player; import org.bukkit.event.block.Action; @@ -54,7 +54,7 @@ public void onPlayerInteract(PlayerInteractEvent event) { } Sign sign = (Sign) event.getClickedBlock().getState(); String name = sign.getLine(0); - if (EconomyManager.getiConomy() == null && EconomyManager.BOSEconomy == null) { + if (EconomyManager.economy == null) { System.out.println("[iConomyChestShop] No economy plugin found!"); player.sendMessage("[iConomyChestShop] No economy plugin found!"); return; diff --git a/com/Acrobot/iConomyChestShop/iConomyChestShopPluginListener.java b/com/Acrobot/iConomyChestShop/iConomyChestShopPluginListener.java index 190c255..7ff0e92 100644 --- a/com/Acrobot/iConomyChestShop/iConomyChestShopPluginListener.java +++ b/com/Acrobot/iConomyChestShop/iConomyChestShopPluginListener.java @@ -2,8 +2,7 @@ import com.griefcraft.lwc.LWCPlugin; import com.nijikokun.bukkit.Permissions.Permissions; -import cosine.boseconomy.BOSEconomy; -import com.iConomy.*; +import com.nijikokun.register.payment.Methods; import info.somethingodd.bukkit.OddItem.OddItem; import org.bukkit.event.server.PluginEnableEvent; import org.bukkit.event.server.ServerListener; @@ -17,19 +16,17 @@ */ public class iConomyChestShopPluginListener extends ServerListener{ + private Methods Methods = new Methods(); public iConomyChestShopPluginListener() { } @Override public void onPluginEnable(PluginEnableEvent event) { - //iConomy - if(EconomyManager.getiConomy() == null) { - Plugin iConomy = iConomyChestShop.getBukkitServer().getPluginManager().getPlugin("iConomy"); - if (iConomy != null) { - if (iConomy.isEnabled()) { - EconomyManager.setiConomy((iConomy) iConomy); - PluginDescriptionFile pDesc = iConomy.getDescription(); - System.out.println("[iConomyChestShop] " + pDesc.getName() + " version " + pDesc.getVersion() + " loaded."); - } + + //Economy plugins + if(!this.Methods.hasMethod()){ + if(this.Methods.setMethod(event.getPlugin())){ + EconomyManager.economy = this.Methods.getMethod(); + System.out.println("[iConomyChestShop] " + EconomyManager.economy.getName() + " version " + EconomyManager.economy.getVersion() + " loaded."); } } @@ -38,7 +35,7 @@ public void onPluginEnable(PluginEnableEvent event) { Plugin permissions = iConomyChestShop.getBukkitServer().getPluginManager().getPlugin("Permissions"); if (permissions != null) { - iConomyChestShop.getBukkitServer().getPluginManager().enablePlugin(permissions); + //iConomyChestShop.getBukkitServer().getPluginManager().enablePlugin(permissions); PermissionManager.Permissions = ((Permissions) permissions).getHandler(); PluginDescriptionFile pDesc = permissions.getDescription(); System.out.println("[iConomyChestShop] " + pDesc.getName() + " version " + pDesc.getVersion() + " loaded."); @@ -77,15 +74,5 @@ public void onPluginEnable(PluginEnableEvent event) { System.out.println("[iConomyChestShop] " + pDesc.getName() + " version " + pDesc.getVersion() + " loaded."); } } - - //BOSEconomy - if (EconomyManager.BOSEconomy == null) { - Plugin tmp = iConomyChestShop.getBukkitServer().getPluginManager().getPlugin("BOSEconomy"); - if (tmp != null) { - EconomyManager.BOSEconomy = (BOSEconomy) tmp; - PluginDescriptionFile pDesc = tmp.getDescription(); - System.out.println("[iConomyChestShop] " + pDesc.getName() + " version " + pDesc.getVersion() + " loaded."); - } - } } } diff --git a/com/nijikokun/register/payment/Method.java b/com/nijikokun/register/payment/Method.java new file mode 100644 index 0000000..935a9bb --- /dev/null +++ b/com/nijikokun/register/payment/Method.java @@ -0,0 +1,48 @@ +package com.nijikokun.register.payment; + +public interface Method { + public Object getPlugin(); + public String getName(); + public String getVersion(); + public String format(double amount); + public boolean hasBanks(); + public boolean hasBank(String bank); + public boolean hasAccount(String name); + public boolean hasBankAccount(String bank, String name); + public MethodAccount getAccount(String name); + public MethodBankAccount getBankAccount(String bank, String name); + + public interface MethodAccount { + public double balance(); + public boolean add(double amount); + public boolean subtract(double amount); + public boolean multiply(double amount); + public boolean divide(double amount); + public boolean hasEnough(double amount); + public boolean hasOver(double amount); + public boolean hasUnder(double amount); + public boolean isNegative(); + public boolean remove(); + + @Override + public String toString(); + } + + public interface MethodBankAccount { + public double balance(); + public String getBankName(); + public int getBankId(); + public boolean add(double amount); + public boolean subtract(double amount); + public boolean multiply(double amount); + public boolean divide(double amount); + public boolean hasEnough(double amount); + public boolean hasOver(double amount); + public boolean hasUnder(double amount); + public boolean isNegative(); + public boolean remove(); + + @Override + public String toString(); + } +} diff --git a/com/nijikokun/register/payment/MethodBOSEconomy.java b/com/nijikokun/register/payment/MethodBOSEconomy.java new file mode 100644 index 0000000..c0433ee --- /dev/null +++ b/com/nijikokun/register/payment/MethodBOSEconomy.java @@ -0,0 +1,180 @@ +package com.nijikokun.register.payment; + +import cosine.boseconomy.BOSEconomy; + +public class MethodBOSEconomy implements Method { + private BOSEconomy BOSEconomy; + + public MethodBOSEconomy(BOSEconomy BOSEconomy) { + this.BOSEconomy = BOSEconomy; + } + + public BOSEconomy getPlugin() { + return this.BOSEconomy; + } + + public String getName() { + return "BOSEconomy"; + } + + public String getVersion() { + return "0.6.2"; + } + + public String format(double amount) { + String currency = this.BOSEconomy.getMoneyNamePlural(); + if(amount == 1) currency = this.BOSEconomy.getMoneyName(); + return amount + " " + currency; + } + + public boolean hasBanks() { + return true; + } + + public boolean hasBank(String bank) { + return this.BOSEconomy.bankExists(bank); + } + + public boolean hasAccount(String name) { + return this.BOSEconomy.playerRegistered(name, false); + } + + public boolean hasBankAccount(String bank, String name) { + return this.BOSEconomy.isBankOwner(bank, name); + } + + public MethodAccount getAccount(String name) { + if(!hasAccount(name)) return null; + return new BOSEAccount(name, this.BOSEconomy); + } + + public MethodBankAccount getBankAccount(String bank, String name) { + return new BOSEBankAccount(bank, name, BOSEconomy); + } + + public class BOSEAccount implements MethodAccount { + private String name; + private BOSEconomy BOSEconomy; + + public BOSEAccount(String name, BOSEconomy bOSEconomy) { + this.name = name; + this.BOSEconomy = bOSEconomy; + } + + public double balance() { + return (double) this.BOSEconomy.getPlayerMoney(this.name); + } + + public boolean add(double amount) { + int IntAmount = (int)amount; + //int balance = (int)this.balance(); + return this.BOSEconomy.addPlayerMoney(this.name, IntAmount, false); + } + + public boolean subtract(double amount) { + int IntAmount = (int)amount; + int balance = (int)this.balance(); + return this.BOSEconomy.setPlayerMoney(this.name, (balance - IntAmount), false); + } + + public boolean multiply(double amount) { + int IntAmount = (int)amount; + int balance = (int)this.balance(); + return this.BOSEconomy.setPlayerMoney(this.name, (balance * IntAmount), false); + } + + public boolean divide(double amount) { + int IntAmount = (int)amount; + int balance = (int)this.balance(); + return this.BOSEconomy.setPlayerMoney(this.name, (balance / IntAmount), false); + } + + public boolean hasEnough(double amount) { + return (this.balance() >= amount); + } + + public boolean hasOver(double amount) { + return (this.balance() > amount); + } + + public boolean hasUnder(double amount) { + return (this.balance() < amount); + } + + public boolean isNegative() { + return (this.balance() < 0); + } + + public boolean remove() { + return false; + } + } + + public class BOSEBankAccount implements MethodBankAccount { + private String bank; + private String name; + private BOSEconomy BOSEconomy; + + public BOSEBankAccount(String bank, String name, BOSEconomy bOSEconomy) { + this.name = name; + this.bank = bank; + this.BOSEconomy = bOSEconomy; + } + + public String getBankName() { + return this.bank; + } + + public int getBankId() { + return -1; + } + + public double balance() { + return (double) this.BOSEconomy.getBankMoney(name); + } + + public boolean add(double amount) { + int IntAmount = (int)amount; + int balance = (int)this.balance(); + return this.BOSEconomy.setBankMoney(this.name, (balance + IntAmount), false); + } + + public boolean subtract(double amount) { + int IntAmount = (int)amount; + int balance = (int)this.balance(); + return this.BOSEconomy.setBankMoney(this.name, (balance - IntAmount), false); + } + + public boolean multiply(double amount) { + int IntAmount = (int)amount; + int balance = (int)this.balance(); + return this.BOSEconomy.setBankMoney(this.name, (balance * IntAmount), false); + } + + public boolean divide(double amount) { + int IntAmount = (int)amount; + int balance = (int)this.balance(); + return this.BOSEconomy.setBankMoney(this.name, (balance / IntAmount), false); + } + + public boolean hasEnough(double amount) { + return (this.balance() >= amount); + } + + public boolean hasOver(double amount) { + return (this.balance() > amount); + } + + public boolean hasUnder(double amount) { + return (this.balance() < amount); + } + + public boolean isNegative() { + return (this.balance() < 0); + } + + public boolean remove() { + return this.BOSEconomy.removeBank(bank); + } + } +} \ No newline at end of file diff --git a/com/nijikokun/register/payment/MethodEEco.java b/com/nijikokun/register/payment/MethodEEco.java new file mode 100644 index 0000000..e8e52a9 --- /dev/null +++ b/com/nijikokun/register/payment/MethodEEco.java @@ -0,0 +1,106 @@ +package com.nijikokun.register.payment; + +import com.earth2me.essentials.Essentials; +import com.earth2me.essentials.api.Economy; + +public class MethodEEco implements Method { + private Essentials Essentials; + + public MethodEEco(Essentials Essentials) { + this.Essentials = Essentials; + } + + public Essentials getPlugin() { + return this.Essentials; + } + + public String getName() { + return "EssentialsEco"; + } + + public String getVersion() { + return "2.2"; + } + + public String format(double amount) { + return Economy.format(amount); + } + + public boolean hasBanks() { + return false; + } + + public boolean hasBank(String bank) { + return false; + } + + public boolean hasAccount(String name) { + return Economy.accountExist(name); + } + + public boolean hasBankAccount(String bank, String name) { + return false; + } + + public MethodAccount getAccount(String name) { + if(!hasAccount(name)) return null; + return new EEcoAccount(name); + } + + public MethodBankAccount getBankAccount(String bank, String name) { + return null; + } + + public class EEcoAccount implements MethodAccount { + private String name; + + public EEcoAccount(String name) { + this.name = name; + } + + public double balance() { + return Economy.getMoney(this.name); + } + + public boolean add(double amount) { + Economy.add(name, amount); + return true; + } + + public boolean subtract(double amount) { + Economy.subtract(name, amount); + return true; + } + + public boolean multiply(double amount) { + Economy.multiply(name, amount); + return true; + } + + public boolean divide(double amount) { + Economy.divide(name, amount); + return true; + } + + public boolean hasEnough(double amount) { + return Economy.hasEnough(name, amount); + } + + public boolean hasOver(double amount) { + return Economy.hasMore(name, amount); + } + + public boolean hasUnder(double amount) { + return Economy.hasLess(name, amount); + } + + public boolean isNegative() { + return Economy.isNegative(name); + } + + public boolean remove() { + Economy.removeAccount(name); + return true; + } + } +} \ No newline at end of file diff --git a/com/nijikokun/register/payment/MethodiCo4.java b/com/nijikokun/register/payment/MethodiCo4.java new file mode 100644 index 0000000..47448de --- /dev/null +++ b/com/nijikokun/register/payment/MethodiCo4.java @@ -0,0 +1,114 @@ +package com.nijikokun.register.payment; + +import com.nijiko.coelho.iConomy.iConomy; +import com.nijiko.coelho.iConomy.system.Account; + +public class MethodiCo4 implements Method { + private iConomy iConomy; + + public MethodiCo4(iConomy iConomy) { + this.iConomy = iConomy; + } + + public iConomy getPlugin() { + return this.iConomy; + } + + public String getName() { + return "iConomy"; + } + + public String getVersion() { + return "4"; + } + + public String format(double amount) { + return this.iConomy.getBank().format(amount); + } + + public boolean hasBanks() { + return false; + } + + public boolean hasBank(String bank) { + return false; + } + + public boolean hasAccount(String name) { + return this.iConomy.getBank().hasAccount(name); + } + + public boolean hasBankAccount(String bank, String name) { + return false; + } + + public MethodAccount getAccount(String name) { + return new iCoAccount(this.iConomy.getBank().getAccount(name)); + } + + public MethodBankAccount getBankAccount(String bank, String name) { + return null; + } + + public class iCoAccount implements MethodAccount { + private Account account; + + public iCoAccount(Account account) { + this.account = account; + } + + public Account getiCoAccount() { + return account; + } + + public double balance() { + return this.account.getBalance(); + } + + public boolean add(double amount) { + if(this.account == null) return false; + this.account.add(amount); + return true; + } + + public boolean subtract(double amount) { + if(this.account == null) return false; + this.account.subtract(amount); + return true; + } + + public boolean multiply(double amount) { + if(this.account == null) return false; + this.account.multiply(amount); + return true; + } + + public boolean divide(double amount) { + if(this.account == null) return false; + this.account.divide(amount); + return true; + } + + public boolean hasEnough(double amount) { + return this.account.hasEnough(amount); + } + + public boolean hasOver(double amount) { + return this.account.hasOver(amount); + } + + public boolean hasUnder(double amount) { + return (this.balance() < amount); + } + + public boolean isNegative() { + return this.account.isNegative(); + } + + public boolean remove() { + if(this.account == null) return false; + this.account.remove(); + return true; + } + } +} diff --git a/com/nijikokun/register/payment/MethodiCo5.java b/com/nijikokun/register/payment/MethodiCo5.java new file mode 100644 index 0000000..60bda5f --- /dev/null +++ b/com/nijikokun/register/payment/MethodiCo5.java @@ -0,0 +1,191 @@ +package com.nijikokun.register.payment; + +import com.iConomy.iConomy; +import com.iConomy.system.Account; +import com.iConomy.system.BankAccount; +import com.iConomy.system.Holdings; +import com.iConomy.util.Constants; + +public class MethodiCo5 implements Method { + private iConomy iConomy; + + public MethodiCo5(iConomy iConomy) { + this.iConomy = iConomy; + } + + public iConomy getPlugin() { + return this.iConomy; + } + + public String getName() { + return "iConomy"; + } + + public String getVersion() { + return "5"; + } + + public String format(double amount) { + return this.iConomy.format(amount); + } + + public boolean hasBanks() { + return Constants.Banking; + } + + public boolean hasBank(String bank) { + return (hasBanks()) && this.iConomy.Banks.exists(bank); + } + + public boolean hasAccount(String name) { + return this.iConomy.hasAccount(name); + } + + public boolean hasBankAccount(String bank, String name) { + return (!hasBank(bank)) && this.iConomy.getBank(name).hasAccount(name); + } + + public MethodAccount getAccount(String name) { + return new iCoAccount(this.iConomy.getAccount(name)); + } + + public MethodBankAccount getBankAccount(String bank, String name) { + return new iCoBankAccount(this.iConomy.getBank(bank).getAccount(name)); + } + + public class iCoAccount implements MethodAccount { + private Account account; + private Holdings holdings; + + public iCoAccount(Account account) { + this.account = account; + this.holdings = account.getHoldings(); + } + + public Account getiCoAccount() { + return account; + } + + public double balance() { + return this.holdings.balance(); + } + + public boolean add(double amount) { + if(this.holdings == null) return false; + this.holdings.add(amount); + return true; + } + + public boolean subtract(double amount) { + if(this.holdings == null) return false; + this.holdings.subtract(amount); + return true; + } + + public boolean multiply(double amount) { + if(this.holdings == null) return false; + this.holdings.multiply(amount); + return true; + } + + public boolean divide(double amount) { + if(this.holdings == null) return false; + this.holdings.divide(amount); + return true; + } + + public boolean hasEnough(double amount) { + return this.holdings.hasEnough(amount); + } + + public boolean hasOver(double amount) { + return this.holdings.hasOver(amount); + } + + public boolean hasUnder(double amount) { + return this.holdings.hasUnder(amount); + } + + public boolean isNegative() { + return this.holdings.isNegative(); + } + + public boolean remove() { + if(this.account == null) return false; + this.account.remove(); + return true; + } + } + + public class iCoBankAccount implements MethodBankAccount { + private BankAccount account; + private Holdings holdings; + + public iCoBankAccount(BankAccount account) { + this.account = account; + this.holdings = account.getHoldings(); + } + + public BankAccount getiCoBankAccount() { + return account; + } + + public String getBankName() { + return this.account.getBankName(); + } + + public int getBankId() { + return this.account.getBankId(); + } + + public double balance() { + return this.holdings.balance(); + } + + public boolean add(double amount) { + if(this.holdings == null) return false; + this.holdings.add(amount); + return true; + } + + public boolean subtract(double amount) { + if(this.holdings == null) return false; + this.holdings.subtract(amount); + return true; + } + + public boolean multiply(double amount) { + if(this.holdings == null) return false; + this.holdings.multiply(amount); + return true; + } + + public boolean divide(double amount) { + if(this.holdings == null) return false; + this.holdings.divide(amount); + return true; + } + + public boolean hasEnough(double amount) { + return this.holdings.hasEnough(amount); + } + + public boolean hasOver(double amount) { + return this.holdings.hasOver(amount); + } + + public boolean hasUnder(double amount) { + return this.holdings.hasUnder(amount); + } + + public boolean isNegative() { + return this.holdings.isNegative(); + } + + public boolean remove() { + if(this.account == null) return false; + this.account.remove(); + return true; + } + } +} diff --git a/com/nijikokun/register/payment/Methods.java b/com/nijikokun/register/payment/Methods.java new file mode 100644 index 0000000..6d4190a --- /dev/null +++ b/com/nijikokun/register/payment/Methods.java @@ -0,0 +1,76 @@ +package com.nijikokun.register.payment; + +import com.iConomy.iConomy; +import cosine.boseconomy.BOSEconomy; +import com.earth2me.essentials.Essentials; + +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.PluginManager; +import org.bukkit.plugin.PluginDescriptionFile; + +public class Methods { + private Method Method = null; + public Plugin method = null; + + public boolean setMethod(Plugin method) { + PluginManager loader = method.getServer().getPluginManager(); + + if(method.isEnabled()) { + PluginDescriptionFile info = method.getDescription(); + String name = info.getName(); + + if(name.equalsIgnoreCase("iconomy")) { + if(method.getClass().getName().equals("com.iConomy.iConomy")) + Method = new MethodiCo5((iConomy)method); + else { Method = new MethodiCo4((com.nijiko.coelho.iConomy.iConomy)method); } + } else if(name.equalsIgnoreCase("boseconomy")) { + Method = new MethodBOSEconomy((BOSEconomy)method); + } else if(name.equalsIgnoreCase("essentials")) { + if(!((Essentials)method).isIConomyFallbackEnabled()) Method = new MethodEEco((Essentials)method); + } + } + + if(!hasMethod()) { + if(loader.getPlugin("iConomy") != null) { + method = loader.getPlugin("iConomy"); + if(method.getClass().getName().equals("com.iConomy.iConomy")) + Method = new MethodiCo5((iConomy)method); + else { Method = new MethodiCo4((com.nijiko.coelho.iConomy.iConomy)method); } + } else if(loader.getPlugin("BOSEconomy") != null) { + method = loader.getPlugin("BOSEconomy"); + Method = new MethodBOSEconomy((BOSEconomy)method); + } else if(loader.getPlugin("Essentials") != null) { + method = loader.getPlugin("Essentials"); + if(!((Essentials)method).isIConomyFallbackEnabled()) Method = new MethodEEco((Essentials)method); + } + } + + return hasMethod(); + } + + public boolean checkDisabled(Plugin method) { + PluginDescriptionFile info = method.getDescription(); + String name = info.getName(); + + if(name.equalsIgnoreCase("iconomy")) { + if(method.getClass().getName().equals("com.iConomy.iConomy")) + Method = null; + else { Method = null; } + } else if(name.equalsIgnoreCase("boseconomy")) { + Method = null; + } else if(name.equalsIgnoreCase("essentials")) { + Method = null; + } + + return (Method == null); + } + + public boolean hasMethod() { + return (Method != null); + } + + public Method getMethod() { + return Method; + } + +} diff --git a/plugin.yml b/plugin.yml index cc483a9..b44e6ed 100644 --- a/plugin.yml +++ b/plugin.yml @@ -3,7 +3,7 @@ name: iConomyChestShop main: com.Acrobot.iConomyChestShop.iConomyChestShop database: true -version: 2.72 +version: 2.73 author: Acrobot