From 51296f6ac9958aa74504fdfec5db353e0d24d1e9 Mon Sep 17 00:00:00 2001 From: Acrobot Date: Sun, 19 Jun 2011 23:52:36 +0200 Subject: [PATCH] - Fix for infinite items - Tried to fix the NPE during transaction - Fixed NPE during Admin Shop setup - Speeded up protection system - Made configuration system more flexible --- com/Acrobot/ChestShop/ChestShop.java | 8 ++++++-- com/Acrobot/ChestShop/Config/Config.java | 5 +++-- .../ChestShop/Listeners/playerInteract.java | 4 +--- com/Acrobot/ChestShop/Listeners/signChange.java | 2 +- com/Acrobot/ChestShop/Options/Option.java | 10 ++++++---- com/Acrobot/ChestShop/Protection/Default.java | 8 ++++++-- com/Acrobot/ChestShop/Shop/Shop.java | 14 ++++++++------ com/Acrobot/ChestShop/Shop/ShopManagement.java | 16 ++++++++++++++-- com/Acrobot/ChestShop/Utils/SearchForBlock.java | 9 ++++++--- com/Acrobot/ChestShop/Utils/SignUtil.java | 5 +++-- plugin.yml | 2 +- 11 files changed, 55 insertions(+), 28 deletions(-) diff --git a/com/Acrobot/ChestShop/ChestShop.java b/com/Acrobot/ChestShop/ChestShop.java index 04e7835..eba034f 100644 --- a/com/Acrobot/ChestShop/ChestShop.java +++ b/com/Acrobot/ChestShop/ChestShop.java @@ -43,6 +43,8 @@ public class ChestShop extends JavaPlugin { private static PluginDescriptionFile desc; private static Server server; + public static String mainWorldName; + public void onEnable() { PluginManager pm = getServer().getPluginManager(); @@ -56,12 +58,14 @@ public class ChestShop extends JavaPlugin { desc = this.getDescription(); server = getServer(); + mainWorldName = server.getWorlds().get(0).getName(); + + //Yep, set up our folder! + folder = getDataFolder(); //Set up our config file! Config.setUp(); - //Yep, set up our folder! - folder = getDataFolder(); //Now set up our database for storing transactions! setupDBfile(); diff --git a/com/Acrobot/ChestShop/Config/Config.java b/com/Acrobot/ChestShop/Config/Config.java index d025eb8..2d3908b 100644 --- a/com/Acrobot/ChestShop/Config/Config.java +++ b/com/Acrobot/ChestShop/Config/Config.java @@ -1,5 +1,6 @@ package com.Acrobot.ChestShop.Config; +import com.Acrobot.ChestShop.ChestShop; import com.Acrobot.ChestShop.Logging.Logging; import org.bukkit.util.config.Configuration; @@ -11,8 +12,8 @@ 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 File configFile = new File(ChestShop.folder, "config.yml"); + private static File langFile = new File(ChestShop.folder, "local.yml"); private static Configuration config = new Configuration(configFile); private static Configuration language = new Configuration(langFile); diff --git a/com/Acrobot/ChestShop/Listeners/playerInteract.java b/com/Acrobot/ChestShop/Listeners/playerInteract.java index 08539ec..577dcbc 100644 --- a/com/Acrobot/ChestShop/Listeners/playerInteract.java +++ b/com/Acrobot/ChestShop/Listeners/playerInteract.java @@ -5,7 +5,6 @@ import com.Acrobot.ChestShop.Config.Language; import com.Acrobot.ChestShop.Config.Property; import com.Acrobot.ChestShop.Permission; import com.Acrobot.ChestShop.Protection.Default; -import com.Acrobot.ChestShop.Protection.Security; import com.Acrobot.ChestShop.Shop.ShopManagement; import com.Acrobot.ChestShop.Utils.SearchForBlock; import com.Acrobot.ChestShop.Utils.SignUtil; @@ -45,7 +44,7 @@ public class playerInteract extends PlayerListener { if (Config.getBoolean(Property.USE_BUILT_IN_PROTECTION) && block.getType() == Material.CHEST) { Default defProtection = new Default(); - if (!Permission.has(player, Permission.ADMIN) && (defProtection.isProtected(block) && !defProtection.canAccess(player, block)) || (Security.isProtected(block) && !Security.canAccess(player, block))) { + if (!Permission.has(player, Permission.ADMIN) && (defProtection.isProtected(block) && !defProtection.canAccess(player, block))) { player.sendMessage(Config.getLocal(Language.ACCESS_DENIED)); event.setCancelled(true); return; @@ -72,7 +71,6 @@ public class playerInteract extends PlayerListener { if (player.getName().equals(sign.getLine(0))) { Chest chest1 = SearchForBlock.findChest(sign); - if (chest1 == null) { player.sendMessage(Config.getLocal(Language.NO_CHEST_DETECTED)); return; diff --git a/com/Acrobot/ChestShop/Listeners/signChange.java b/com/Acrobot/ChestShop/Listeners/signChange.java index 99ac7bd..78792cd 100644 --- a/com/Acrobot/ChestShop/Listeners/signChange.java +++ b/com/Acrobot/ChestShop/Listeners/signChange.java @@ -121,7 +121,7 @@ public class signChange extends BlockListener { } } - if (Config.getBoolean(Property.PROTECT_CHEST_WITH_LWC) && Security.protect(player.getName(), chest.getBlock())) { + if (Config.getBoolean(Property.PROTECT_CHEST_WITH_LWC) && chest != null && Security.protect(player.getName(), chest.getBlock())) { if(Config.getBoolean(Property.PROTECT_SIGN_WITH_LWC)){ Security.protect(player.getName(), signBlock); } diff --git a/com/Acrobot/ChestShop/Options/Option.java b/com/Acrobot/ChestShop/Options/Option.java index 355335a..0568972 100644 --- a/com/Acrobot/ChestShop/Options/Option.java +++ b/com/Acrobot/ChestShop/Options/Option.java @@ -8,15 +8,17 @@ import java.util.Map; * @author Acrobot */ public enum Option { - BALANCE(true), - OUT_OF_STOCK(true), - SOMEONE_BOUGHT(true); + BALANCE("balance", true), + OUT_OF_STOCK("outOfStock", true), + SOMEONE_BOUGHT("someoneBought", true); private boolean enabled; + private String name; private static final Map names = new HashMap(); - private Option(boolean enabled) { + private Option(String name, boolean enabled) { this.enabled = enabled; + this.name = name; } public boolean isEnabled() { diff --git a/com/Acrobot/ChestShop/Protection/Default.java b/com/Acrobot/ChestShop/Protection/Default.java index 520546b..5c991ad 100644 --- a/com/Acrobot/ChestShop/Protection/Default.java +++ b/com/Acrobot/ChestShop/Protection/Default.java @@ -3,6 +3,7 @@ package com.Acrobot.ChestShop.Protection; import com.Acrobot.ChestShop.Utils.SearchForBlock; import com.Acrobot.ChestShop.Utils.SignUtil; import org.bukkit.block.Block; +import org.bukkit.block.Chest; import org.bukkit.block.Sign; import org.bukkit.entity.Player; @@ -12,12 +13,15 @@ import org.bukkit.entity.Player; public class Default implements Protection { public boolean isProtected(Block block) { Sign sign = SearchForBlock.findSign(block); - return (block != null) && ((SignUtil.isSign(block) && SignUtil.isValid((Sign) block.getState())) || sign != null); + Chest nChest = SearchForBlock.findChest(block); + return ((SignUtil.isSign(block) && SignUtil.isValid((Sign) block.getState())) || sign != null) || (nChest != null && SearchForBlock.findSign(nChest.getBlock()) != null); } public boolean canAccess(Player player, Block block) { Sign sign = SearchForBlock.findSign(block); - return (block != null) && (SignUtil.isSign(block) && SignUtil.isValid((Sign) block.getState()) && ((Sign) block.getState()).getLine(0).equals(player.getName())) || (sign != null && sign.getLine(0).equals(player.getName())); + Chest nChest = SearchForBlock.findNeighbor(block); + Sign nSign = (nChest != null ? SearchForBlock.findSign(nChest.getBlock()) : null); + return ((SignUtil.isSign(block) && SignUtil.isValid((Sign) block.getState()) && ((Sign) block.getState()).getLine(0).equals(player.getName())) || (sign != null && sign.getLine(0).equals(player.getName()))) || (nSign != null && nSign.getLine(0).equals(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 6709806..5c32d54 100644 --- a/com/Acrobot/ChestShop/Shop/Shop.java +++ b/com/Acrobot/ChestShop/Shop/Shop.java @@ -18,6 +18,7 @@ import org.bukkit.inventory.ItemStack; */ public class Shop { public ItemStack stock; + public short durability; public int stockAmount; public ChestObject chest; public float buyPrice; @@ -26,6 +27,7 @@ public class Shop { public Shop(ChestObject chest, Sign sign, ItemStack... itemStacks) { this.stock = itemStacks[0]; + this.durability = stock.getDurability(); this.chest = chest; this.buyPrice = SignUtil.buyPrice(sign.getLine(2)); this.sellPrice = SignUtil.sellPrice(sign.getLine(2)); @@ -67,7 +69,7 @@ public class Shop { Economy.substract(playerName, buyPrice); if (!isAdminShop()) { - chest.removeItem(stock, stock.getDurability(), stockAmount); + chest.removeItem(stock, durability, stockAmount); } String formatedPrice = Economy.formatBalance(buyPrice); player.sendMessage(Config.getLocal(Language.YOU_BOUGHT_FROM_SHOP) @@ -111,7 +113,7 @@ public class Shop { return false; } - if (InventoryUtil.amount(player.getInventory(), stock, stock.getDurability()) < stockAmount) { + if (InventoryUtil.amount(player.getInventory(), stock, durability) < stockAmount) { player.sendMessage(Config.getLocal(Language.NOT_ENOUGH_ITEMS_TO_SELL)); return false; } @@ -136,7 +138,7 @@ public class Shop { .replace("%buyer", owner) .replace("%price", formatedBalance)); - InventoryUtil.remove(player.getInventory(), stock, stockAmount, stock.getDurability()); + InventoryUtil.remove(player.getInventory(), stock, stockAmount, durability); Logging.logTransaction(false, this, player); player.updateInventory(); @@ -163,15 +165,15 @@ public class Shop { } private boolean hasEnoughStock() { - return chest.hasEnough(stock, stockAmount, stock.getDurability()); + return chest.hasEnough(stock, stockAmount, durability); } private boolean stockFitsPlayer(Player player) { - return InventoryUtil.fits(player.getInventory(), stock, stockAmount, stock.getDurability()) <= 0; + return InventoryUtil.fits(player.getInventory(), stock, stockAmount, durability) <= 0; } private boolean stockFitsChest(ChestObject chest) { - return chest.fits(stock, stockAmount, stock.getDurability()); + return chest.fits(stock, stockAmount, durability); } private void sendMessageToOwner(String msg) { diff --git a/com/Acrobot/ChestShop/Shop/ShopManagement.java b/com/Acrobot/ChestShop/Shop/ShopManagement.java index 8860399..44a6049 100644 --- a/com/Acrobot/ChestShop/Shop/ShopManagement.java +++ b/com/Acrobot/ChestShop/Shop/ShopManagement.java @@ -3,9 +3,11 @@ 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.ChatColor; import org.bukkit.block.Chest; import org.bukkit.block.Sign; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; /** * @author Acrobot @@ -13,14 +15,24 @@ import org.bukkit.entity.Player; 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))); + ItemStack item = Items.getItemStack(sign.getLine(3)); + if(item == null){ + player.sendMessage(ChatColor.RED + "[Shop] The item is not recognised!"); + return false; + } + Shop shop = new Shop(chestMc != null ? new MinecraftChest(chestMc) : null, sign, item); return shop.buy(player); } 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))); + ItemStack item = Items.getItemStack(sign.getLine(3)); + if(item == null){ + player.sendMessage(ChatColor.RED + "[Shop] The item is not recognised!"); + return false; + } + Shop shop = new Shop(chestMc != null ? new MinecraftChest(chestMc) : null, sign, item); return shop.sell(player); } diff --git a/com/Acrobot/ChestShop/Utils/SearchForBlock.java b/com/Acrobot/ChestShop/Utils/SearchForBlock.java index be581ad..aa4cb92 100644 --- a/com/Acrobot/ChestShop/Utils/SearchForBlock.java +++ b/com/Acrobot/ChestShop/Utils/SearchForBlock.java @@ -39,15 +39,18 @@ public class SearchForBlock { return null; } - public static Chest findNeighbor(Chest chest) { + public static Chest findNeighbor(Block block) { BlockFace[] bf = {BlockFace.EAST, BlockFace.NORTH, BlockFace.WEST, BlockFace.SOUTH}; - Block chestBlock = chest.getBlock(); for (BlockFace blockFace : bf) { - Block neighborBlock = chestBlock.getFace(blockFace); + Block neighborBlock = block.getFace(blockFace); if (neighborBlock.getType() == Material.CHEST) { return (Chest) neighborBlock.getState(); } } return null; //Shame, we didn't find double chest :/ } + + public static Chest findNeighbor(Chest chest){ + return findNeighbor(chest.getBlock()); + } } diff --git a/com/Acrobot/ChestShop/Utils/SignUtil.java b/com/Acrobot/ChestShop/Utils/SignUtil.java index 251de89..b17ce1e 100644 --- a/com/Acrobot/ChestShop/Utils/SignUtil.java +++ b/com/Acrobot/ChestShop/Utils/SignUtil.java @@ -80,9 +80,10 @@ public class SignUtil { public static int itemAmount(String text) { if (Numerical.isInteger(text)) { - return Integer.parseInt(text); + int amount = Integer.parseInt(text); + return (amount >= 1 ? amount : 1); } else { - return 0; + return 1; } } } diff --git a/plugin.yml b/plugin.yml index e86dc32..6ac0595 100644 --- a/plugin.yml +++ b/plugin.yml @@ -3,7 +3,7 @@ name: ChestShop main: com.Acrobot.ChestShop.ChestShop database: true -version: 3.00 BETA 3 +version: 3.00 BETA 4 author: Acrobot