- 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
This commit is contained in:
Acrobot 2011-06-19 23:52:36 +02:00
parent d919818bb6
commit 51296f6ac9
11 changed files with 55 additions and 28 deletions

View File

@ -43,6 +43,8 @@ public class ChestShop extends JavaPlugin {
private static PluginDescriptionFile desc; private static PluginDescriptionFile desc;
private static Server server; private static Server server;
public static String mainWorldName;
public void onEnable() { public void onEnable() {
PluginManager pm = getServer().getPluginManager(); PluginManager pm = getServer().getPluginManager();
@ -56,12 +58,14 @@ public class ChestShop extends JavaPlugin {
desc = this.getDescription(); desc = this.getDescription();
server = getServer(); server = getServer();
mainWorldName = server.getWorlds().get(0).getName();
//Yep, set up our folder!
folder = getDataFolder();
//Set up our config file! //Set up our config file!
Config.setUp(); Config.setUp();
//Yep, set up our folder!
folder = getDataFolder();
//Now set up our database for storing transactions! //Now set up our database for storing transactions!
setupDBfile(); setupDBfile();

View File

@ -1,5 +1,6 @@
package com.Acrobot.ChestShop.Config; package com.Acrobot.ChestShop.Config;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Logging.Logging; import com.Acrobot.ChestShop.Logging.Logging;
import org.bukkit.util.config.Configuration; import org.bukkit.util.config.Configuration;
@ -11,8 +12,8 @@ import java.util.HashMap;
* @author Acrobot * @author Acrobot
*/ */
public class Config { public class Config {
private static File configFile = new File("plugins/ChestShop/config.yml"); private static File configFile = new File(ChestShop.folder, "config.yml");
private static File langFile = new File("plugins/ChestShop/local.yml"); private static File langFile = new File(ChestShop.folder, "local.yml");
private static Configuration config = new Configuration(configFile); private static Configuration config = new Configuration(configFile);
private static Configuration language = new Configuration(langFile); private static Configuration language = new Configuration(langFile);

View File

@ -5,7 +5,6 @@ import com.Acrobot.ChestShop.Config.Language;
import com.Acrobot.ChestShop.Config.Property; import com.Acrobot.ChestShop.Config.Property;
import com.Acrobot.ChestShop.Permission; import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Protection.Default; import com.Acrobot.ChestShop.Protection.Default;
import com.Acrobot.ChestShop.Protection.Security;
import com.Acrobot.ChestShop.Shop.ShopManagement; import com.Acrobot.ChestShop.Shop.ShopManagement;
import com.Acrobot.ChestShop.Utils.SearchForBlock; import com.Acrobot.ChestShop.Utils.SearchForBlock;
import com.Acrobot.ChestShop.Utils.SignUtil; 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) { if (Config.getBoolean(Property.USE_BUILT_IN_PROTECTION) && block.getType() == Material.CHEST) {
Default defProtection = new Default(); 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)); player.sendMessage(Config.getLocal(Language.ACCESS_DENIED));
event.setCancelled(true); event.setCancelled(true);
return; return;
@ -72,7 +71,6 @@ public class playerInteract extends PlayerListener {
if (player.getName().equals(sign.getLine(0))) { if (player.getName().equals(sign.getLine(0))) {
Chest chest1 = SearchForBlock.findChest(sign); Chest chest1 = SearchForBlock.findChest(sign);
if (chest1 == null) { if (chest1 == null) {
player.sendMessage(Config.getLocal(Language.NO_CHEST_DETECTED)); player.sendMessage(Config.getLocal(Language.NO_CHEST_DETECTED));
return; return;

View File

@ -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)){ if(Config.getBoolean(Property.PROTECT_SIGN_WITH_LWC)){
Security.protect(player.getName(), signBlock); Security.protect(player.getName(), signBlock);
} }

View File

@ -8,15 +8,17 @@ import java.util.Map;
* @author Acrobot * @author Acrobot
*/ */
public enum Option { public enum Option {
BALANCE(true), BALANCE("balance", true),
OUT_OF_STOCK(true), OUT_OF_STOCK("outOfStock", true),
SOMEONE_BOUGHT(true); SOMEONE_BOUGHT("someoneBought", true);
private boolean enabled; private boolean enabled;
private String name;
private static final Map<String, Option> names = new HashMap<String, Option>(); private static final Map<String, Option> names = new HashMap<String, Option>();
private Option(boolean enabled) { private Option(String name, boolean enabled) {
this.enabled = enabled; this.enabled = enabled;
this.name = name;
} }
public boolean isEnabled() { public boolean isEnabled() {

View File

@ -3,6 +3,7 @@ package com.Acrobot.ChestShop.Protection;
import com.Acrobot.ChestShop.Utils.SearchForBlock; import com.Acrobot.ChestShop.Utils.SearchForBlock;
import com.Acrobot.ChestShop.Utils.SignUtil; import com.Acrobot.ChestShop.Utils.SignUtil;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.Chest;
import org.bukkit.block.Sign; import org.bukkit.block.Sign;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -12,12 +13,15 @@ import org.bukkit.entity.Player;
public class Default implements Protection { public class Default implements Protection {
public boolean isProtected(Block block) { public boolean isProtected(Block block) {
Sign sign = SearchForBlock.findSign(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) { public boolean canAccess(Player player, Block block) {
Sign sign = SearchForBlock.findSign(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) { public boolean protect(String name, Block block) {

View File

@ -18,6 +18,7 @@ import org.bukkit.inventory.ItemStack;
*/ */
public class Shop { public class Shop {
public ItemStack stock; public ItemStack stock;
public short durability;
public int stockAmount; public int stockAmount;
public ChestObject chest; public ChestObject chest;
public float buyPrice; public float buyPrice;
@ -26,6 +27,7 @@ public class Shop {
public Shop(ChestObject chest, Sign sign, ItemStack... itemStacks) { public Shop(ChestObject chest, Sign sign, ItemStack... itemStacks) {
this.stock = itemStacks[0]; this.stock = itemStacks[0];
this.durability = stock.getDurability();
this.chest = chest; this.chest = chest;
this.buyPrice = SignUtil.buyPrice(sign.getLine(2)); this.buyPrice = SignUtil.buyPrice(sign.getLine(2));
this.sellPrice = SignUtil.sellPrice(sign.getLine(2)); this.sellPrice = SignUtil.sellPrice(sign.getLine(2));
@ -67,7 +69,7 @@ public class Shop {
Economy.substract(playerName, buyPrice); Economy.substract(playerName, buyPrice);
if (!isAdminShop()) { if (!isAdminShop()) {
chest.removeItem(stock, stock.getDurability(), stockAmount); chest.removeItem(stock, durability, stockAmount);
} }
String formatedPrice = Economy.formatBalance(buyPrice); String formatedPrice = Economy.formatBalance(buyPrice);
player.sendMessage(Config.getLocal(Language.YOU_BOUGHT_FROM_SHOP) player.sendMessage(Config.getLocal(Language.YOU_BOUGHT_FROM_SHOP)
@ -111,7 +113,7 @@ public class Shop {
return false; 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)); player.sendMessage(Config.getLocal(Language.NOT_ENOUGH_ITEMS_TO_SELL));
return false; return false;
} }
@ -136,7 +138,7 @@ public class Shop {
.replace("%buyer", owner) .replace("%buyer", owner)
.replace("%price", formatedBalance)); .replace("%price", formatedBalance));
InventoryUtil.remove(player.getInventory(), stock, stockAmount, stock.getDurability()); InventoryUtil.remove(player.getInventory(), stock, stockAmount, durability);
Logging.logTransaction(false, this, player); Logging.logTransaction(false, this, player);
player.updateInventory(); player.updateInventory();
@ -163,15 +165,15 @@ public class Shop {
} }
private boolean hasEnoughStock() { private boolean hasEnoughStock() {
return chest.hasEnough(stock, stockAmount, stock.getDurability()); return chest.hasEnough(stock, stockAmount, durability);
} }
private boolean stockFitsPlayer(Player player) { 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) { private boolean stockFitsChest(ChestObject chest) {
return chest.fits(stock, stockAmount, stock.getDurability()); return chest.fits(stock, stockAmount, durability);
} }
private void sendMessageToOwner(String msg) { private void sendMessageToOwner(String msg) {

View File

@ -3,9 +3,11 @@ package com.Acrobot.ChestShop.Shop;
import com.Acrobot.ChestShop.Chests.MinecraftChest; import com.Acrobot.ChestShop.Chests.MinecraftChest;
import com.Acrobot.ChestShop.Items.Items; import com.Acrobot.ChestShop.Items.Items;
import com.Acrobot.ChestShop.Utils.SearchForBlock; import com.Acrobot.ChestShop.Utils.SearchForBlock;
import org.bukkit.ChatColor;
import org.bukkit.block.Chest; import org.bukkit.block.Chest;
import org.bukkit.block.Sign; import org.bukkit.block.Sign;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
/** /**
* @author Acrobot * @author Acrobot
@ -13,14 +15,24 @@ import org.bukkit.entity.Player;
public class ShopManagement { public class ShopManagement {
public static boolean buy(Sign sign, Player player) { public static boolean buy(Sign sign, Player player) {
Chest chestMc = SearchForBlock.findChest(sign); 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); return shop.buy(player);
} }
public static boolean sell(Sign sign, Player player) { public static boolean sell(Sign sign, Player player) {
Chest chestMc = SearchForBlock.findChest(sign); 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); return shop.sell(player);
} }

View File

@ -39,15 +39,18 @@ public class SearchForBlock {
return null; return null;
} }
public static Chest findNeighbor(Chest chest) { public static Chest findNeighbor(Block block) {
BlockFace[] bf = {BlockFace.EAST, BlockFace.NORTH, BlockFace.WEST, BlockFace.SOUTH}; BlockFace[] bf = {BlockFace.EAST, BlockFace.NORTH, BlockFace.WEST, BlockFace.SOUTH};
Block chestBlock = chest.getBlock();
for (BlockFace blockFace : bf) { for (BlockFace blockFace : bf) {
Block neighborBlock = chestBlock.getFace(blockFace); Block neighborBlock = block.getFace(blockFace);
if (neighborBlock.getType() == Material.CHEST) { if (neighborBlock.getType() == Material.CHEST) {
return (Chest) neighborBlock.getState(); return (Chest) neighborBlock.getState();
} }
} }
return null; //Shame, we didn't find double chest :/ return null; //Shame, we didn't find double chest :/
} }
public static Chest findNeighbor(Chest chest){
return findNeighbor(chest.getBlock());
}
} }

View File

@ -80,9 +80,10 @@ public class SignUtil {
public static int itemAmount(String text) { public static int itemAmount(String text) {
if (Numerical.isInteger(text)) { if (Numerical.isInteger(text)) {
return Integer.parseInt(text); int amount = Integer.parseInt(text);
return (amount >= 1 ? amount : 1);
} else { } else {
return 0; return 1;
} }
} }
} }

View File

@ -3,7 +3,7 @@ name: ChestShop
main: com.Acrobot.ChestShop.ChestShop main: com.Acrobot.ChestShop.ChestShop
database: true database: true
version: 3.00 BETA 3 version: 3.00 BETA 4
author: Acrobot author: Acrobot