diff --git a/com/Acrobot/ChestShop/ChestShop.java b/com/Acrobot/ChestShop/ChestShop.java index fadbde0..f5febe3 100644 --- a/com/Acrobot/ChestShop/ChestShop.java +++ b/com/Acrobot/ChestShop/ChestShop.java @@ -1,8 +1,9 @@ package com.Acrobot.ChestShop; -import com.Acrobot.ChestShop.Items.ItemName; +import com.Acrobot.ChestShop.Items.Items; import com.Acrobot.ChestShop.Listeners.*; import com.Acrobot.ChestShop.Utils.Config; +import com.Acrobot.ChestShop.Utils.Defaults; import org.bukkit.Server; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -41,12 +42,13 @@ public class ChestShop extends JavaPlugin { server = getServer(); Config.setUp(); + Defaults.set(); - System.out.println("[" + desc.getName() + "] version " + desc.getVersion() + " initialized!"); + System.out.println('[' + desc.getName() + "] version " + desc.getVersion() + " initialized!"); } public void onDisable() { - System.out.println("[" + desc.getName() + "] version " + desc.getVersion() + " shutting down!"); + System.out.println('[' + desc.getName() + "] version " + desc.getVersion() + " shutting down!"); } public static Server getBukkitServer() { @@ -71,11 +73,11 @@ public class ChestShop extends JavaPlugin { //ItemInfo if(commandName.equals("iteminfo")){ if(argCount == 0){ - p.sendMessage(ItemName.getItemID(p.getItemInHand().getType().name()) + " " + ItemName.getItemName(p.getItemInHand())); + p.sendMessage(Items.getItemID(p.getItemInHand().getType().name()) + " " + Items.getItemName(p.getItemInHand())); return true; } if(argCount == 1){ - String itemName = ItemName.getItemID(ItemName.getItemName(args[0])) + " " + ItemName.getItemName(args[0]); + String itemName = Items.getItemID(Items.getItemName(args[0])) + " " + Items.getItemName(args[0]); p.sendMessage(itemName); return true; } diff --git a/com/Acrobot/ChestShop/Chests/MinecraftChest.java b/com/Acrobot/ChestShop/Chests/MinecraftChest.java index 33e1c16..cf743fc 100644 --- a/com/Acrobot/ChestShop/Chests/MinecraftChest.java +++ b/com/Acrobot/ChestShop/Chests/MinecraftChest.java @@ -1,5 +1,6 @@ package com.Acrobot.ChestShop.Chests; +import com.Acrobot.ChestShop.Utils.InventoryUtil; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; @@ -72,7 +73,8 @@ public class MinecraftChest implements ChestObject{ } public boolean fits(ItemStack item, int amount, short durability) { - return fits(item, amount, durability, main) && (neighbor == null || fits(item, amount, durability, neighbor)); + int firstChest = fits(item, amount, durability, main); + return (firstChest > 0 && neighbor != null ? fits(item, amount, durability, neighbor) <= 0 : firstChest <= 0); } public int getSize() { @@ -91,131 +93,22 @@ public class MinecraftChest implements ChestObject{ return null; //Shame, we didn't find double chest :/ } - private int amount(ItemStack item, short durability, Chest chest){ - ItemStack[] contents = chest.getInventory().getContents(); - int amount = 0; - - for(ItemStack i : contents){ - if(i != null){ - if(i.getType() == item.getType() && (durability == -1 || i.getDurability() == durability || (durability == 0 && i.getDurability() == -1))){ - amount += i.getAmount(); - } - } - } - return amount; + private static int amount(ItemStack item, short durability, Chest chest){ + return InventoryUtil.amount(chest.getInventory(), item, durability); } - private boolean fits(ItemStack item, int amount, short durability, Chest chest){ + private static int fits(ItemStack item, int amount, short durability, Chest chest){ Inventory inv = chest.getInventory(); - Material itemMaterial = item.getType(); - int maxStackSize = item.getMaxStackSize(); - - int amountLeft = amount; - - for(int slot = 0; slot < inv.getSize(); slot++){ - - if(amountLeft <= 0){ - return true; - } - - ItemStack currentItem = inv.getItem(slot); - - if(currentItem == null || currentItem.getType() == Material.AIR){ - amountLeft -= maxStackSize; - continue; - } - - if(currentItem.getType() == itemMaterial && (currentItem.getDurability() == durability)){ - int currentAmount = currentItem.getAmount(); - if(amountLeft == currentAmount){ - amountLeft = 0; - } else if(amountLeft < currentAmount){ - amountLeft = 0; - } else{ - amountLeft -= currentAmount; - } - } - } - - return amountLeft <= 0; + return InventoryUtil.fits(inv, item, amount, durability); } - private int addItem(ItemStack item, short durability, int amount, Chest chest){ + private static int addItem(ItemStack item, short durability, int amount, Chest chest){ Inventory inv = chest.getInventory(); - ItemStack[] contents = inv.getContents(); - Material itemMaterial = item.getType(); - - int amountLeft = amount; - int maxStackSize = item.getMaxStackSize(); - ItemStack baseItem = item.clone(); - - for(int slot = 0; slot < inv.getSize(); slot++){ - ItemStack itemStack = contents[slot]; - if(amountLeft <= 0){ - return 0; - } - if(itemStack != null && itemStack.getType() != Material.AIR){ //Our slot is not free - int currentAmount = itemStack.getAmount(); - Material currentMaterial = itemStack.getType(); - short currentDurability = itemStack.getDurability(); - if(currentMaterial == itemMaterial && (currentDurability == durability)){ - if((currentAmount + amountLeft) <= maxStackSize){ - baseItem.setAmount(currentAmount + amountLeft); - amountLeft = 0; - } else{ - baseItem.setAmount(maxStackSize); - amountLeft -= (maxStackSize - currentAmount); - } - inv.setItem(slot, baseItem); - } - }else{ //Free slot - if(amountLeft <= maxStackSize){ //There is less to add than whole stack - baseItem.setAmount(amountLeft); - inv.setItem(slot, baseItem); - amountLeft = 0; - } else{ //We add whole stack - baseItem.setAmount(maxStackSize); - inv.setItem(slot, baseItem); - amountLeft -= maxStackSize; - } - } - } - return amountLeft; + return InventoryUtil.add(inv, item, amount); } - private int removeItem(ItemStack item, short durability, int amount, Chest chest){ + private static int removeItem(ItemStack item, short durability, int amount, Chest chest){ Inventory inv = chest.getInventory(); - Material itemMaterial = item.getType(); - - int amountLeft = amount; - - for(int slot = 0; slot < inv.getSize(); slot++){ - - if(amountLeft <= 0){ - return 0; - } - - ItemStack currentItem = inv.getItem(slot); - - if(currentItem == null || currentItem.getType() == Material.AIR){ - continue; - } - if(currentItem.getType() == itemMaterial && (currentItem.getDurability() == durability)){ - int currentAmount = currentItem.getAmount(); - if(amountLeft == currentAmount){ - currentItem = null; - amountLeft = 0; - } else if(amountLeft < currentAmount){ - currentItem.setAmount(currentAmount - amountLeft); - amountLeft = 0; - } else{ - currentItem = null; - amountLeft -= currentAmount; - } - inv.setItem(slot, currentItem); - } - } - - return amountLeft; + return InventoryUtil.remove(inv, item, amount, durability); } } diff --git a/com/Acrobot/ChestShop/Items/ItemName.java b/com/Acrobot/ChestShop/Items/Items.java similarity index 56% rename from com/Acrobot/ChestShop/Items/ItemName.java rename to com/Acrobot/ChestShop/Items/Items.java index 7aef487..c8f18a5 100644 --- a/com/Acrobot/ChestShop/Items/ItemName.java +++ b/com/Acrobot/ChestShop/Items/Items.java @@ -1,5 +1,6 @@ package com.Acrobot.ChestShop.Items; +import com.Acrobot.ChestShop.Utils.Numerical; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; @@ -7,7 +8,7 @@ import org.bukkit.inventory.ItemStack; * @author Acrobot * Manages ItemStack names and ID's */ -public class ItemName { +public class Items { public static String getItemName(ItemStack itemStack){ return getItemName(itemStack.getType().name()); @@ -34,4 +35,24 @@ public class ItemName { public static int getItemID(String itemName){ return getMaterial(itemName).getId(); } + + public static ItemStack getItemStack(String itemName){ + if(Odd.isInitialized()){ + ItemStack odd = Odd.returnItemStack(itemName.replace(":", ";")); + if(odd != null){ + return odd; + } + } + String[] split = itemName.split(":"); + itemName = split[0]; + short dataValue = (short) (split.length > 1 && Numerical.isInteger(split[1]) ? Integer.parseInt(split[1]) : 0); + + if(Numerical.isInteger(itemName)){ + return new ItemStack(Material.getMaterial(Integer.parseInt(itemName)), 1, dataValue); + } + + Material mat = getMaterial(itemName); + + return (mat != null ? new ItemStack(mat, 1, dataValue) : null); + } } diff --git a/com/Acrobot/ChestShop/Items/Odd.java b/com/Acrobot/ChestShop/Items/Odd.java index 767bdd7..3533f09 100644 --- a/com/Acrobot/ChestShop/Items/Odd.java +++ b/com/Acrobot/ChestShop/Items/Odd.java @@ -1,6 +1,7 @@ package com.Acrobot.ChestShop.Items; -import info.somethingodd.bukkit.odd.item.OddItem; +import info.somethingodd.bukkit.OddItem.OddItem; +import org.bukkit.inventory.ItemStack; /** * @author Acrobot @@ -8,4 +9,13 @@ import info.somethingodd.bukkit.odd.item.OddItem; public class Odd { public static OddItem oddItem; + public static boolean isInitialized(){ + return oddItem != null; + } + + public static ItemStack returnItemStack(String name){ + try{ + return oddItem.getItemStack(name); + } catch (Exception ignored){return null;} + } } diff --git a/com/Acrobot/ChestShop/Listeners/playerInteract.java b/com/Acrobot/ChestShop/Listeners/playerInteract.java index 90213d5..55c5ddb 100644 --- a/com/Acrobot/ChestShop/Listeners/playerInteract.java +++ b/com/Acrobot/ChestShop/Listeners/playerInteract.java @@ -2,6 +2,7 @@ package com.Acrobot.ChestShop.Listeners; import com.Acrobot.ChestShop.Messaging.Message; import com.Acrobot.ChestShop.Protection.Security; +import com.Acrobot.ChestShop.Shop.ShopManagement; import com.Acrobot.ChestShop.Utils.Config; import com.Acrobot.ChestShop.Utils.SignUtil; import org.bukkit.Material; @@ -42,13 +43,15 @@ public class playerInteract extends PlayerListener{ if(!SignUtil.isValid(sign)){ return; } - + Action buy = (Config.getBoolean("reverse_buttons") ? Action.LEFT_CLICK_BLOCK : Action.RIGHT_CLICK_BLOCK); + + if(action == buy){ - player.sendMessage("You are buying!"); + ShopManagement.buy(sign, player); } else{ - player.sendMessage("You are selling!"); + ShopManagement.sell(sign, player); } } } diff --git a/com/Acrobot/ChestShop/Listeners/pluginEnable.java b/com/Acrobot/ChestShop/Listeners/pluginEnable.java index d72c243..f29ad63 100644 --- a/com/Acrobot/ChestShop/Listeners/pluginEnable.java +++ b/com/Acrobot/ChestShop/Listeners/pluginEnable.java @@ -10,7 +10,7 @@ import com.Acrobot.ChestShop.Protection.Security; import com.griefcraft.lwc.LWCPlugin; import com.nijikokun.bukkit.Permissions.Permissions; import com.nijikokun.register.payment.Methods; -import info.somethingodd.bukkit.odd.item.OddItem; +import info.somethingodd.bukkit.OddItem.OddItem; import org.bukkit.event.server.PluginEnableEvent; import org.bukkit.event.server.ServerListener; import org.bukkit.plugin.Plugin; @@ -31,7 +31,7 @@ public class pluginEnable extends ServerListener{ if(!this.methods.hasMethod()){ if(methods.setMethod(event.getPlugin())){ Economy.economy = methods.getMethod(); - System.out.println("[ChestShop] " + Economy.economy.getName() + " " + Economy.economy.getVersion() + " loaded."); + System.out.println("[ChestShop] " + Economy.economy.getName() + ' ' + Economy.economy.getVersion() + " loaded."); } } diff --git a/com/Acrobot/ChestShop/Protection/Default.java b/com/Acrobot/ChestShop/Protection/Default.java index 1f59acd..4f48fea 100644 --- a/com/Acrobot/ChestShop/Protection/Default.java +++ b/com/Acrobot/ChestShop/Protection/Default.java @@ -1,6 +1,8 @@ package com.Acrobot.ChestShop.Protection; +import com.Acrobot.ChestShop.Utils.SignUtil; import org.bukkit.block.Block; +import org.bukkit.block.Sign; import org.bukkit.entity.Player; /** @@ -8,11 +10,11 @@ import org.bukkit.entity.Player; */ public class Default implements Protection{ public boolean isProtected(Block block) { - return false; //TODO: Make it check the sign's first line + return (block != null) && SignUtil.isSign(block) && SignUtil.isValid((Sign) block.getState()); } public boolean canAccess(Player player, Block block) { - return false; //TODO: Make it check the sign's first line + return (block != null) && SignUtil.isSign(block) && SignUtil.isValid((Sign) block.getState()) && ((Sign) block.getState()).getLine(0).startsWith(player.getName()); } public boolean protect(String name, Block block) { diff --git a/com/Acrobot/ChestShop/Shop/Shop.java b/com/Acrobot/ChestShop/Shop/Shop.java index 7e900d8..0dd670b 100644 --- a/com/Acrobot/ChestShop/Shop/Shop.java +++ b/com/Acrobot/ChestShop/Shop/Shop.java @@ -1,16 +1,161 @@ package com.Acrobot.ChestShop.Shop; +import com.Acrobot.ChestShop.ChestShop; import com.Acrobot.ChestShop.Chests.ChestObject; +import com.Acrobot.ChestShop.Economy; +import com.Acrobot.ChestShop.Utils.Config; +import com.Acrobot.ChestShop.Utils.InventoryUtil; +import com.Acrobot.ChestShop.Utils.SignUtil; import org.bukkit.block.Sign; +import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; /** * @author Acrobot */ public class Shop { - private ItemStack[] items; + private ItemStack stock; + private int stockAmount; + private ChestObject chest; + private float buyPrice; + private float sellPrice; + private String owner; public Shop(ChestObject chest, Sign sign, ItemStack ... itemStacks){ - items = itemStacks; + this.stock = itemStacks[0]; + this.chest = chest; + this.buyPrice = SignUtil.buyPrice(sign.getLine(2)); + this.sellPrice = SignUtil.sellPrice(sign.getLine(2)); + this.owner = sign.getLine(0); + this.stockAmount = SignUtil.itemAmount(sign.getLine(1)); + } + + public boolean buy(Player player){ + if(buyPrice == -1){ + player.sendMessage(Config.getLocal("NO_BUYING_HERE")); + return false; + } + + if(!fits(stock, player)){ + player.sendMessage(Config.getLocal("NOT_ENOUGH_SPACE_IN_INVENTORY")); + return false; + } + + String materialName = stock.getType().name(); + + if(!isAdminShop() && !hasEnoughStock()){ + player.sendMessage(Config.getLocal("NOT_ENOUGH_STOCK")); + sendMessageToOwner(Config.getLocal("NOT_ENOUGH_STOCK_IN_YOUR_SHOP").replace("%material", materialName)); + return false; + } + + if(!getOwner().isEmpty() && Economy.hasAccount(getOwner())){ + Economy.add(getOwner(), buyPrice); + } + Economy.substract(player.getName(), buyPrice); + + if(!isAdminShop()){ + chest.removeItem(stock, stock.getDurability(), stockAmount); + } + InventoryUtil.add(player.getInventory(), stock, stockAmount); + + player.updateInventory(); + + String formatedPrice = Economy.formatBalance(buyPrice); + + player.sendMessage(Config.getLocal("YOU_BOUGHT_FROM_SHOP") + .replace("%amount", String.valueOf(stockAmount)) + .replace("%item", materialName) + .replace("%owner", owner) + .replace("%price", formatedPrice)); + + sendMessageToOwner(Config.getLocal("SOMEBODY_BOUGHT_FROM_YOUR_SHOP") + .replace("%amount", String.valueOf(stockAmount)) + .replace("%item", materialName) + .replace("%buyer", player.getName()) + .replace("%price", formatedPrice)); + + return true; + } + + public boolean sell(Player player){ + if(sellPrice == -1){ + player.sendMessage(Config.getLocal("NO_SELLING_HERE")); + return false; + } + + if(!isAdminShop() && !fits(stock, chest)){ + player.sendMessage(Config.getLocal("NOT_ENOUGH_SPACE_IN_CHEST")); + return false; + } + + if(InventoryUtil.amount(player.getInventory(), stock, stock.getDurability()) < stockAmount){ + player.sendMessage(Config.getLocal("NOT_ENOUGH_ITEMS_TO_SELL")); + return false; + } + + if(!getOwner().isEmpty() && Economy.hasAccount(getOwner())){ + Economy.substract(getOwner(), sellPrice); + } + + if(!isAdminShop()){ + chest.addItem(stock, stock.getDurability(), stockAmount); + } + + InventoryUtil.remove(player.getInventory(), stock, stockAmount, stock.getDurability()); + + player.updateInventory(); + + Economy.add(player.getName(), sellPrice); + + String materialName = stock.getType().name(); + String formatedBalance = Economy.formatBalance(sellPrice); + + player.sendMessage(Config.getLocal("YOU_SOLD_TO_SHOP") + .replace("%amount", String.valueOf(stockAmount)) + .replace("%item", materialName) + .replace("%buyer", owner) + .replace("%price", formatedBalance)); + + sendMessageToOwner(Config.getLocal("SOMEBODY_SOLD_TO_YOUR_SHOP") + .replace("%amount", String.valueOf(stockAmount)) + .replace("%item", materialName) + .replace("%seller", player.getName()) + .replace("%price", formatedBalance)); + + return true; + } + + private String getOwner(){ + if(SignUtil.isAdminShop(owner)){ + return Config.getString("shopEconomyAccount"); + } else{ + return owner; + } + } + + private boolean isAdminShop(){ + return SignUtil.isAdminShop(owner); + } + + private boolean hasEnoughStock(){ + return chest.hasEnough(stock, stockAmount, stock.getDurability()); + } + + private static boolean fits(ItemStack item, Player player){ + return InventoryUtil.fits(player.getInventory(), item, item.getAmount(), item.getDurability()) <= 0; + } + + private static boolean fits(ItemStack item, ChestObject chest){ + return chest.fits(item, item.getAmount(), item.getDurability()); + } + + private void sendMessageToOwner(String msg){ + if(!isAdminShop()){ + Player player = ChestShop.getBukkitServer().getPlayer(owner); + if(player != null){ + player.sendMessage(msg); + } + } } } diff --git a/com/Acrobot/ChestShop/Shop/ShopManagement.java b/com/Acrobot/ChestShop/Shop/ShopManagement.java new file mode 100644 index 0000000..850540e --- /dev/null +++ b/com/Acrobot/ChestShop/Shop/ShopManagement.java @@ -0,0 +1,29 @@ +package com.Acrobot.ChestShop.Shop; + +import com.Acrobot.ChestShop.Chests.MinecraftChest; +import com.Acrobot.ChestShop.Items.Items; +import com.Acrobot.ChestShop.Utils.SearchForBlock; +import org.bukkit.block.Chest; +import org.bukkit.block.Sign; +import org.bukkit.entity.Player; + +/** + * @author Acrobot + */ +public class ShopManagement { + public static boolean buy(Sign sign, Player player){ + Chest chestMc = SearchForBlock.findChest(sign); + Shop shop = new Shop(chestMc != null ? new MinecraftChest(chestMc) : null, sign, Items.getItemStack(sign.getLine(3))); + + shop.buy(player); + return true; + } + + public static boolean sell(Sign sign, Player player){ + Chest chestMc = SearchForBlock.findChest(sign); + Shop shop = new Shop(chestMc != null ? new MinecraftChest(chestMc) : null, sign, Items.getItemStack(sign.getLine(3))); + + shop.sell(player); + return true; + } +} diff --git a/com/Acrobot/ChestShop/Utils/Config.java b/com/Acrobot/ChestShop/Utils/Config.java index 58df5a2..7477978 100644 --- a/com/Acrobot/ChestShop/Utils/Config.java +++ b/com/Acrobot/ChestShop/Utils/Config.java @@ -4,13 +4,21 @@ import com.Acrobot.ChestShop.Logging.Logging; import org.bukkit.util.config.Configuration; import java.io.File; +import java.io.FileWriter; +import java.util.HashMap; /** * @author Acrobot */ public class Config { private static File configFile = new File("plugins/ChestShop/config.yml"); + private static File langFile = new File("plugins/ChestShop/local.yml"); + private static Configuration config = new Configuration(configFile); + private static Configuration language = new Configuration(langFile); + + public static HashMap defaultValues = new HashMap(); + private static String langChar = Character.toString((char) 167); @@ -23,26 +31,56 @@ public class Config { Logging.log("Couldn't create configuration file!"); } } - load(); - } - - public static void load(){ config.load(); + language.load(); } public static boolean getBoolean(String node){ - return config.getBoolean(node, false); + return config.getBoolean(node, (Boolean) getDefaultValue(node)); } public static String getString(String node){ - return config.getString(node, ""); + return getColored(config.getString(node, (String) getDefaultValue(node))); } public static int getInteger(String node){ - return config.getInt(node, 0); + return config.getInt(node, Integer.parseInt(getDefaultValue(node).toString())); } public static double getDouble(String node){ return config.getDouble(node, -1); } + + public static Object getDefaultValue(String node, Configuration configuration, File file){ + if(configuration.getProperty(node) == null){ + try{ + Object defaultValue = defaultValues.get(node); + if(defaultValue != null){ + FileWriter fw = new FileWriter(file, true); + fw.write('\n' + node+": " + defaultValue); + fw.close(); + } + } catch (Exception e){ + Logging.log("Failed to update config file!"); + } + } + configuration.load(); + return configuration.getProperty(node); + } + + public static String getColored(String msg){ + return msg.replaceAll("&", langChar); + } + + public static String getLocal(String node){ + return getColored(language.getString("prefix",(String) getDefaultLocal("prefix")) + language.getString(node, (String) getDefaultLocal(node))); + } + + public static Object getDefaultValue(String node){ + return getDefaultValue(node, config, configFile); + } + + public static Object getDefaultLocal(String node){ + return getDefaultValue(node, language, langFile); + } } diff --git a/com/Acrobot/ChestShop/Utils/Defaults.java b/com/Acrobot/ChestShop/Utils/Defaults.java new file mode 100644 index 0000000..5f1e033 --- /dev/null +++ b/com/Acrobot/ChestShop/Utils/Defaults.java @@ -0,0 +1,26 @@ +package com.Acrobot.ChestShop.Utils; + +/** + * @author Acrobot + */ +public class Defaults { + + public static void set(){ + Config.defaultValues.put("reverse_buttons", "false #If true, people buy with left click and sell with right click"); + Config.defaultValues.put("shopEconomyAccount", "\"\" #Place economy account you want Admin Shops to be assigned to"); + + //LANGUAGE: + Config.defaultValues.put("prefix", "\"&2[Shop]&f\""); + Config.defaultValues.put("NO_BUYING_HERE", "\"You can't buy here!\""); + Config.defaultValues.put("NO_SELLING_HERE", "\"You can't sell here!\""); + Config.defaultValues.put("NOT_ENOUGH_SPACE_IN_INVENTORY", "\"You haven't got enough space in inventory!\""); + Config.defaultValues.put("NOT_ENOUGH_STOCK", "\"This shop has not enough stock.\""); + Config.defaultValues.put("NOT_ENOUGH_STOCK_IN_YOUR_SHOP", "\"Your %material shop is out of stock!\""); + + Config.defaultValues.put("YOU_BOUGHT_FROM_SHOP", "\"You bought %amount %item from %owner for %price.\""); + Config.defaultValues.put("SOMEBODY_BOUGHT_FROM_YOUR_SHOP", "\"%buyer bought %amount %item for %price from you.\""); + + Config.defaultValues.put("YOU_SOLD_TO_SHOP", "\"You sold %amount %item to %buyer for %price.\""); + Config.defaultValues.put("SOMEBODY_SOLD_TO_YOUR_SHOP", "\"%seller sold %amount %item for %price to you.\""); + } +} diff --git a/com/Acrobot/ChestShop/Utils/InventoryUtil.java b/com/Acrobot/ChestShop/Utils/InventoryUtil.java new file mode 100644 index 0000000..b9ae33e --- /dev/null +++ b/com/Acrobot/ChestShop/Utils/InventoryUtil.java @@ -0,0 +1,131 @@ +package com.Acrobot.ChestShop.Utils; + +import org.bukkit.Material; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +/** + * @author Acrobot + */ +public class InventoryUtil { + + public static int remove(Inventory inv, ItemStack item, int amount, short durability){ + Material itemMaterial = item.getType(); + int amountLeft = amount; + + for(int slot = 0; slot < inv.getSize(); slot++){ + if(amountLeft <= 0){ + return 0; + } + + ItemStack currentItem = inv.getItem(slot); + if(currentItem == null || currentItem.getType() == Material.AIR){ + continue; + } + + if(currentItem.getType() == itemMaterial && (durability == -1 || currentItem.getDurability() == durability)){ + int currentAmount = currentItem.getAmount(); + if(amountLeft == currentAmount){ + currentItem = null; + amountLeft = 0; + } else if(amountLeft < currentAmount){ + currentItem.setAmount(currentAmount - amountLeft); + amountLeft = 0; + } else{ + currentItem = null; + amountLeft -= currentAmount; + } + inv.setItem(slot, currentItem); + } + } + + return amountLeft; + } + + public static int add(Inventory inv, ItemStack item, int amount){ + ItemStack[] contents = inv.getContents(); + Material itemMaterial = item.getType(); + short durability = item.getDurability(); + + int amountLeft = amount; + int maxStackSize = itemMaterial.getMaxStackSize(); + ItemStack baseItem = item.clone(); + + for(int slot = 0; slot++ <= inv.getSize();){ + ItemStack itemStack = contents[slot]; + if(amountLeft <= 0){ + return 0; + } + if(itemStack != null && itemStack.getType() != Material.AIR){ //Our slot is not free + int currentAmount = itemStack.getAmount(); + Material currentMaterial = itemStack.getType(); + short currentDurability = itemStack.getDurability(); + if(currentMaterial == itemMaterial && (currentDurability == durability)){ + if((currentAmount + amountLeft) <= maxStackSize){ + baseItem.setAmount(currentAmount + amountLeft); + amountLeft = 0; + } else{ + baseItem.setAmount(maxStackSize); + amountLeft -= (maxStackSize - currentAmount); + } + inv.setItem(slot, baseItem); + } + }else{ //Free slot + if(amountLeft <= maxStackSize){ //There is less to add than whole stack + baseItem.setAmount(amountLeft); + inv.setItem(slot, baseItem); + amountLeft = 0; + } else{ //We add whole stack + baseItem.setAmount(maxStackSize); + inv.setItem(slot, baseItem); + amountLeft -= maxStackSize; + } + } + } + return amountLeft; + } + + public static int amount(Inventory inv, ItemStack item, short durability){ + int amount = 0; + ItemStack[] contents = inv.getContents(); + for(ItemStack i : contents){ + if(i != null){ + if(i.getType() == item.getType() && (durability == -1 || i.getDurability() == durability || (durability == 0 && i.getDurability() == -1))){ + amount += i.getAmount(); + } + } + } + return amount; + } + + public static int fits(Inventory inv, ItemStack item, int amount, short durability){ + Material itemMaterial = item.getType(); + int maxStackSize = itemMaterial.getMaxStackSize(); + + int amountLeft = amount; + + for(ItemStack currentItem : inv.getContents()){ + if(amountLeft <= 0){ + return 0; + } + + if(currentItem == null || currentItem.getType() == Material.AIR){ + amountLeft -= maxStackSize; + continue; + } + + int currentAmount = currentItem.getAmount(); + if(currentAmount == itemMaterial.getMaxStackSize()){ + continue; + } + + if(currentItem.getType() == itemMaterial && (durability == -1 || currentItem.getDurability() == durability)){ + currentAmount = currentAmount < 1 ? 1 : currentAmount; + amountLeft = (amountLeft <= currentAmount ? 0 : currentAmount); + } + } + + return amountLeft; + + } +} diff --git a/com/Acrobot/ChestShop/Utils/SignUtil.java b/com/Acrobot/ChestShop/Utils/SignUtil.java index 5f1df27..989b004 100644 --- a/com/Acrobot/ChestShop/Utils/SignUtil.java +++ b/com/Acrobot/ChestShop/Utils/SignUtil.java @@ -12,6 +12,11 @@ public class SignUtil { public static boolean isSign(Block block){ return (block.getType() == Material.SIGN_POST || block.getType() == Material.WALL_SIGN); } + + public static boolean isAdminShop(String owner){ + return owner.toLowerCase().replace(" ", "").equals("adminshop"); + } + public static boolean isValid(Sign sign){ return isValid(sign.getLines()); } @@ -21,9 +26,56 @@ public class SignUtil { String line2 = lines[1]; String line3 = lines[2]; String line4 = lines[3]; - return !line1.contains("[") && !line1.contains("]") && !line4.equals("") && Numerical.isInteger(line2) && (line3.contains("B") || line3.contains("S")); + return !line1.contains("[") && !line1.contains("]") && !line4.isEmpty() && Numerical.isInteger(line2) && (line3.contains("B") || line3.contains("S")); } catch (Exception e){ return false; } } + + public static float buyPrice(String text){ + text = text.replace(" ", "").toLowerCase(); + + int buyPart = (text.contains("b") ? 0 : -1); + if(buyPart == -1){ + return -1; + } + text = text.replace("b", "").replace("s", ""); + String[] split = text.split(":"); + if(Numerical.isFloat(split[0])){ + float buyPrice = Float.parseFloat(split[0]); + return (buyPrice != 0 ? buyPrice : -1); + }else if(split[0].equals("free")){ + return 0; + } + + return -1; + } + + public static float sellPrice(String text){ + text = text.replace(" ", "").toLowerCase(); + + int sellPart = (text.contains("b") && text.contains("s") ? 1 : (text.contains("s") ? 0 : -1)); + text = text.replace("b", "").replace("s", ""); + String[] split = text.split(":"); + + if(sellPart == -1 || (sellPart == 1 && split.length < 2)){ + return -1; + } + + if(Numerical.isFloat(split[sellPart])){ + Float sellPrice = Float.parseFloat(split[sellPart]); + return (sellPrice != 0 ? sellPrice : -1); + }else if(split[sellPart].equals("free")){ + return 0; + } + return -1; + } + + public static int itemAmount(String text){ + if(Numerical.isInteger(text)){ + return Integer.parseInt(text); + } else{ + return 0; + } + } } diff --git a/com/nijikokun/register/payment/Methods.java b/com/nijikokun/register/payment/Methods.java index 0d9efc4..5a71ca7 100644 --- a/com/nijikokun/register/payment/Methods.java +++ b/com/nijikokun/register/payment/Methods.java @@ -57,7 +57,7 @@ public class Methods { PluginManager manager = method.getServer().getPluginManager(); Plugin plugin = null; - for(String name: this.getDependencies()) { + for(String name: this.Dependencies) { if(hasMethod()) break; if(method.getDescription().getName().equals(name)) plugin = method; else plugin = manager.getPlugin(name); if(plugin == null) continue; diff --git a/com/nijikokun/register/payment/methods/BOSE.java b/com/nijikokun/register/payment/methods/BOSE.java index a392176..c01aa57 100644 --- a/com/nijikokun/register/payment/methods/BOSE.java +++ b/com/nijikokun/register/payment/methods/BOSE.java @@ -68,7 +68,7 @@ public class BOSE implements Method { } public double balance() { - return Double.valueOf(this.BOSEconomy.getPlayerMoney(this.name)); + return (double) this.BOSEconomy.getPlayerMoney(this.name); } public boolean set(double amount) { @@ -141,7 +141,7 @@ public class BOSE implements Method { } public double balance() { - return Double.valueOf(this.BOSEconomy.getBankMoney(name)); + return (double) this.BOSEconomy.getBankMoney(name); } public boolean set(double amount) { diff --git a/com/nijikokun/register/payment/methods/iCo4.java b/com/nijikokun/register/payment/methods/iCo4.java index 5b27ed1..182aa6c 100644 --- a/com/nijikokun/register/payment/methods/iCo4.java +++ b/com/nijikokun/register/payment/methods/iCo4.java @@ -23,7 +23,7 @@ public class iCo4 implements Method { } public String format(double amount) { - return this.iConomy.getBank().format(amount); + return iConomy.getBank().format(amount); } public boolean hasBanks() { @@ -35,7 +35,7 @@ public class iCo4 implements Method { } public boolean hasAccount(String name) { - return this.iConomy.getBank().hasAccount(name); + return iConomy.getBank().hasAccount(name); } public boolean hasBankAccount(String bank, String name) { @@ -43,7 +43,7 @@ public class iCo4 implements Method { } public MethodAccount getAccount(String name) { - return new iCoAccount(this.iConomy.getBank().getAccount(name)); + return new iCoAccount(iConomy.getBank().getAccount(name)); } public MethodBankAccount getBankAccount(String bank, String name) { diff --git a/com/nijikokun/register/payment/methods/iCo5.java b/com/nijikokun/register/payment/methods/iCo5.java index a848ec5..4a92a91 100644 --- a/com/nijikokun/register/payment/methods/iCo5.java +++ b/com/nijikokun/register/payment/methods/iCo5.java @@ -26,7 +26,7 @@ public class iCo5 implements Method { } public String format(double amount) { - return this.iConomy.format(amount); + return iConomy.format(amount); } public boolean hasBanks() { @@ -34,23 +34,23 @@ public class iCo5 implements Method { } public boolean hasBank(String bank) { - return (!hasBanks()) ? false : this.iConomy.Banks.exists(bank); + return (hasBanks()) && iConomy.Banks.exists(bank); } public boolean hasAccount(String name) { - return this.iConomy.hasAccount(name); + return iConomy.hasAccount(name); } public boolean hasBankAccount(String bank, String name) { - return (hasBank(bank)) ? false : this.iConomy.getBank(name).hasAccount(name); + return (!hasBank(bank)) && iConomy.getBank(name).hasAccount(name); } public MethodAccount getAccount(String name) { - return new iCoAccount(this.iConomy.getAccount(name)); + return new iCoAccount(iConomy.getAccount(name)); } public MethodBankAccount getBankAccount(String bank, String name) { - return new iCoBankAccount(this.iConomy.getBank(bank).getAccount(name)); + return new iCoBankAccount(iConomy.getBank(bank).getAccount(name)); } public boolean isCompatible(Plugin plugin) { diff --git a/plugin.yml b/plugin.yml index 34fd42a..c753f52 100644 --- a/plugin.yml +++ b/plugin.yml @@ -8,7 +8,7 @@ version: 3.00 author: Acrobot description: > - A chest shop for iConomy. + A chest shop for economy mods. commands: buy: aliases: chBuy @@ -25,8 +25,8 @@ commands: description: Lists item id and names usage: | / - icsVersion: + csVersion: aliases: - description: Shows the iConomyChestShop's version + description: Shows the ChestShop's version usage: | / \ No newline at end of file