- Fixed protections

This commit is contained in:
Acrobot 2011-07-02 20:34:14 +02:00
parent e522b33d9d
commit 0bde74f3ad
18 changed files with 72 additions and 67 deletions

View File

@ -1,7 +1,7 @@
package com.Acrobot.ChestShop.Chests; package com.Acrobot.ChestShop.Chests;
import com.Acrobot.ChestShop.Utils.InventoryUtil;
import com.Acrobot.ChestShop.Utils.BlockSearch; import com.Acrobot.ChestShop.Utils.BlockSearch;
import com.Acrobot.ChestShop.Utils.InventoryUtil;
import org.bukkit.block.Chest; import org.bukkit.block.Chest;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;

View File

@ -23,8 +23,8 @@ public class Config {
public static void setUp() { public static void setUp() {
config.load(); config.load();
for (Property def : com.Acrobot.ChestShop.Config.Property.values()){ for (Property def : com.Acrobot.ChestShop.Config.Property.values()) {
if(config.getProperty(def.name()) == null){ if (config.getProperty(def.name()) == null) {
writeToFile(def.name() + ": " + def.getValue() + " #" + def.getComment(), configFile); writeToFile(def.name() + ": " + def.getValue() + " #" + def.getComment(), configFile);
} }
} }
@ -49,19 +49,19 @@ public class Config {
} }
} }
public static boolean getBoolean(Property value){ public static boolean getBoolean(Property value) {
return (Boolean) getValue(value.name()); return (Boolean) getValue(value.name());
} }
public static String getString(Property value){ public static String getString(Property value) {
return (String) getValue(value.name()); return (String) getValue(value.name());
} }
public static int getInteger(Property value){ public static int getInteger(Property value) {
return Integer.parseInt(getValue(value.name()).toString()); return Integer.parseInt(getValue(value.name()).toString());
} }
public static double getDouble(Property value){ public static double getDouble(Property value) {
return config.getDouble(value.name(), -1); return config.getDouble(value.name(), -1);
} }

View File

@ -41,7 +41,7 @@ public enum Language {
SHOP_CREATED("Shop successfully created!"), SHOP_CREATED("Shop successfully created!"),
NO_PERMISSION("You don't have permissions to do that!"), NO_PERMISSION("You don't have permissions to do that!"),
NAME_TOO_LONG ("Unfortunately, your name is too long :( Please wait for newer shop version!"), NAME_TOO_LONG("Unfortunately, your name is too long :( Please wait for newer shop version!"),
INCORRECT_ITEM_ID("You have specified invalid item id!"); INCORRECT_ITEM_ID("You have specified invalid item id!");

View File

@ -4,8 +4,8 @@ package com.Acrobot.ChestShop.Config;
* @author Acrobot * @author Acrobot
*/ */
public enum Property { public enum Property {
REVERSE_BUTTONS (false, "If true, people will buy with left-click and sell with right-click."), REVERSE_BUTTONS(false, "If true, people will buy with left-click and sell with right-click."),
SERVER_ECONOMY_ACCOUNT ("", "Economy account's name you want Admin Shops to be assigned to"), SERVER_ECONOMY_ACCOUNT("", "Economy account's name you want Admin Shops to be assigned to"),
LOG_TO_FILE(false, "If true, plugin will log transactions in its own file"), LOG_TO_FILE(false, "If true, plugin will log transactions in its own file"),
LOG_TO_CONSOLE(true, "Do you want ChestShop's messages to show up in console?"), LOG_TO_CONSOLE(true, "Do you want ChestShop's messages to show up in console?"),
USE_DATABASE(false, "If true, plugin will log transactions in EBean database"), USE_DATABASE(false, "If true, plugin will log transactions in EBean database"),
@ -13,11 +13,11 @@ public enum Property {
GENERATE_STATISTICS_PAGE(false, "If true, plugin will generate shop statistics webpage."), GENERATE_STATISTICS_PAGE(false, "If true, plugin will generate shop statistics webpage."),
STATISTICS_PAGE_PATH("plugins/ChestShop/website.html", "Where should your generated website be saved?"), STATISTICS_PAGE_PATH("plugins/ChestShop/website.html", "Where should your generated website be saved?"),
RECORD_TIME_TO_LIVE(600, "How long should transaction information be stored?"), RECORD_TIME_TO_LIVE(600, "How long should transaction information be stored?"),
USE_BUILT_IN_PROTECTION(true, "Do you want to use built-in protection?"), USE_BUILT_IN_PROTECTION(true, "Do you want to use built-in protection against chest destruction?"),
PROTECT_CHEST_WITH_LWC(false, "Do you want to protect shop chests with LWC?"), PROTECT_CHEST_WITH_LWC(false, "Do you want to protect shop chests with LWC?"),
PROTECT_SIGN_WITH_LWC(false, "Do you want to protect shop signs with LWC?"); PROTECT_SIGN_WITH_LWC(false, "Do you want to protect shop signs with LWC?");
private Object value; private Object value;
private String comment; private String comment;

View File

@ -10,17 +10,17 @@ import org.bukkit.material.*;
* @author Acrobot * @author Acrobot
*/ */
public class DataValue { public class DataValue {
public static byte get(String arg, Material material){ public static byte get(String arg, Material material) {
if(material == null){ if (material == null) {
return 0; return 0;
} }
arg = arg.toUpperCase().replace(" ", "_"); arg = arg.toUpperCase().replace(" ", "_");
MaterialData materialData = null; MaterialData materialData = null;
switch (material){ switch (material) {
case SAPLING: case SAPLING:
case LOG: case LOG:
materialData = new Tree(TreeSpecies.valueOf(arg)); materialData = new Tree(TreeSpecies.valueOf(arg));

View File

@ -41,14 +41,14 @@ public class Items {
Material mat; Material mat;
String[] data = itemName.split(" "); String[] data = itemName.split(" ");
if(data.length >= 2){ if (data.length >= 2) {
mat = getMat(itemName.substring(itemName.indexOf(' '))); mat = getMat(itemName.substring(itemName.indexOf(' ')));
byte argData = DataValue.get(data[0], mat); byte argData = DataValue.get(data[0], mat);
if(argData != 0){ if (argData != 0) {
dataValue = argData; dataValue = argData;
} }
} else{ } else {
mat = getMat(itemName); mat = getMat(itemName);
} }

View File

@ -24,14 +24,14 @@ public class blockBreak extends BlockListener {
return; return;
} }
if(SignUtil.isSign(block)){ if (SignUtil.isSign(block)) {
Sign currentSign = (Sign) block.getState(); Sign currentSign = (Sign) block.getState();
if(RestrictedSign.isRestricted(currentSign)){ if (RestrictedSign.isRestricted(currentSign)) {
event.setCancelled(true); event.setCancelled(true);
} }
currentSign.update(true); currentSign.update(true);
} }
Sign sign = BlockSearch.findSign(block); Sign sign = BlockSearch.findSign(block);
if (sign != null) { if (sign != null) {

View File

@ -44,8 +44,8 @@ public class playerInteract extends PlayerListener {
Block block = event.getClickedBlock(); Block block = event.getClickedBlock();
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 protection = new Default();
if (!Permission.has(player, Permission.ADMIN) && !Permission.has(player, Permission.MOD) && (defProtection.isProtected(block) && !defProtection.canAccess(player, block))) { if (!Permission.has(player, Permission.ADMIN) && !Permission.has(player, Permission.MOD) && (protection.isProtected(block) && !protection.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;
@ -93,8 +93,8 @@ public class playerInteract extends PlayerListener {
return; return;
} }
if(RestrictedSign.isRestricted(sign)){ if (RestrictedSign.isRestricted(sign)) {
if(!RestrictedSign.canAccess(sign, player)){ if (!RestrictedSign.canAccess(sign, player)) {
player.sendMessage(Config.getLocal(Language.ACCESS_DENIED)); player.sendMessage(Config.getLocal(Language.ACCESS_DENIED));
return; return;
} }

View File

@ -7,12 +7,12 @@ import org.bukkit.event.server.ServerListener;
/** /**
* @author Acrobot * @author Acrobot
*/ */
public class pluginDisable extends ServerListener{ public class pluginDisable extends ServerListener {
public void onPluginDisable(PluginDisableEvent event){ public void onPluginDisable(PluginDisableEvent event) {
if(Economy.economy != null && pluginEnable.methods.hasMethod()){ if (Economy.economy != null && pluginEnable.methods.hasMethod()) {
boolean check = pluginEnable.methods.checkDisabled(event.getPlugin()); boolean check = pluginEnable.methods.checkDisabled(event.getPlugin());
if(check){ if (check) {
Economy.economy = null; Economy.economy = null;
System.out.println("[ChestShop] Economy plugin disabled!"); System.out.println("[ChestShop] Economy plugin disabled!");
} }

View File

@ -54,7 +54,7 @@ public class pluginEnable extends ServerListener {
PluginDescriptionFile pDesc = lwcPlugin.getDescription(); PluginDescriptionFile pDesc = lwcPlugin.getDescription();
LWCplugin.lwc = ((LWCPlugin) lwcPlugin).getLWC(); LWCplugin.lwc = ((LWCPlugin) lwcPlugin).getLWC();
Security.protection = new LWCplugin(); Security.protection = new LWCplugin();
System.out.println("[ChestShop] " + pDesc.getName() + " version " + pDesc.getVersion() + " loaded."); System.out.println("[ChestShop] " + pDesc.getName() + " version " + pDesc.getVersion() + " loaded.");
} }
} }

View File

@ -43,7 +43,7 @@ public class signChange extends BlockListener {
if (isAlmostReady) { if (isAlmostReady) {
if(player.getName().length() > 15){ if (player.getName().length() > 15) {
player.sendMessage(Config.getLocal(Language.NAME_TOO_LONG)); player.sendMessage(Config.getLocal(Language.NAME_TOO_LONG));
dropSign(event); dropSign(event);
return; return;
@ -54,24 +54,23 @@ public class signChange extends BlockListener {
return; return;
} }
if (!(playerIsAdmin || if (!(playerIsAdmin ||
Permission.has(player, Permission.SHOP_CREATION) || Permission.has(player, Permission.SHOP_CREATION) ||
(Permission.has(player, Permission.SHOP_CREATION + "." + mat.getId()) && (Permission.has(player, Permission.SHOP_CREATION + "." + mat.getId()) &&
!Permission.has(player, Permission.EXCLUDE_ITEM + "." + mat.getId())))) !Permission.has(player, Permission.EXCLUDE_ITEM + "." + mat.getId())))) {
{
player.sendMessage(Config.getLocal(Language.YOU_CANNOT_CREATE_SHOP)); player.sendMessage(Config.getLocal(Language.YOU_CANNOT_CREATE_SHOP));
dropSign(event); dropSign(event);
return; return;
} }
} else { } else {
if(RestrictedSign.isRestricted(event.getLines())){ if (RestrictedSign.isRestricted(event.getLines())) {
if(!playerIsAdmin){ if (!playerIsAdmin) {
player.sendMessage(Config.getLocal(Language.ACCESS_DENIED)); player.sendMessage(Config.getLocal(Language.ACCESS_DENIED));
dropSign(event); dropSign(event);
return; return;
} }
Block secondSign = signBlock.getFace(BlockFace.DOWN); Block secondSign = signBlock.getFace(BlockFace.DOWN);
if(!SignUtil.isSign(secondSign) || !SignUtil.isValid((Sign) secondSign.getState())){ if (!SignUtil.isSign(secondSign) || !SignUtil.isValid((Sign) secondSign.getState())) {
dropSign(event); dropSign(event);
} }
} }
@ -121,29 +120,36 @@ public class signChange extends BlockListener {
dropSign(event); dropSign(event);
return; return;
} else if (!playerIsAdmin) { } else if (!playerIsAdmin) {
boolean canPlaceSign = Security.canPlaceSign(player, signBlock); if (!Security.canPlaceSign(player, signBlock)) {
if (!canPlaceSign) {
player.sendMessage(Config.getLocal(Language.ANOTHER_SHOP_DETECTED)); player.sendMessage(Config.getLocal(Language.ANOTHER_SHOP_DETECTED));
dropSign(event); dropSign(event);
return; return;
} }
Default protection = new Default(); boolean canAccess = true;
Block chestBlock = chest.getBlock(); Block chestBlock = chest.getBlock();
if(Security.isProtected(chestBlock) || protection.isProtected(chestBlock)){ if (Security.isProtected(chestBlock) && !Security.canAccess(player, chestBlock)) {
if(!Security.canAccess(player, chestBlock) || !protection.canAccess(player, chestBlock)){ canAccess = false;
player.sendMessage(Config.getLocal(Language.CANNOT_ACCESS_THE_CHEST)); }
dropSign(event);
return; if (!(Security.protection instanceof Default)) {
Default protection = new Default();
if (protection.isProtected(chestBlock) && !protection.canAccess(player, chestBlock)) {
canAccess = false;
} }
} }
if (!canAccess) {
player.sendMessage(Config.getLocal(Language.CANNOT_ACCESS_THE_CHEST));
dropSign(event);
return;
}
} }
} }
if (Config.getBoolean(Property.PROTECT_CHEST_WITH_LWC) && chest != null && 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);
} }
player.sendMessage(Config.getLocal(Language.PROTECTED_SHOP)); player.sendMessage(Config.getLocal(Language.PROTECTED_SHOP));

View File

@ -12,17 +12,17 @@ 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) {
if((SignUtil.isSign(block) && SignUtil.isValid((Sign) block.getState())) || BlockSearch.findSign(block) != null){ if ((SignUtil.isSign(block) && SignUtil.isValid((Sign) block.getState())) || BlockSearch.findSign(block) != null) {
return true; return true;
} else { } else {
if(!(block.getState() instanceof Chest)){ if (!(block.getState() instanceof Chest)) {
return false; return false;
} }
if(BlockSearch.findSign(block) != null){ if (BlockSearch.findSign(block) != null) {
return true; return true;
} }
Chest neighbor = BlockSearch.findNeighbor(block); Chest neighbor = BlockSearch.findNeighbor(block);
if(neighbor != null && BlockSearch.findSign(neighbor.getBlock()) != null){ if (neighbor != null && BlockSearch.findSign(neighbor.getBlock()) != null) {
return true; return true;
} }
} }

View File

@ -11,18 +11,18 @@ import org.bukkit.entity.Player;
* @author Acrobot * @author Acrobot
*/ */
public class RestrictedSign { public class RestrictedSign {
public static boolean isRestricted(Sign sign){ public static boolean isRestricted(Sign sign) {
Block blockUp = sign.getBlock().getFace(BlockFace.UP); Block blockUp = sign.getBlock().getFace(BlockFace.UP);
return SignUtil.isSign(blockUp) && isRestricted(((Sign) blockUp.getState()).getLines()); return SignUtil.isSign(blockUp) && isRestricted(((Sign) blockUp.getState()).getLines());
} }
public static boolean isRestricted(String[] lines){ public static boolean isRestricted(String[] lines) {
return lines[0].equalsIgnoreCase("[restricted]"); return lines[0].equalsIgnoreCase("[restricted]");
} }
public static boolean canAccess(Sign sign, Player player){ public static boolean canAccess(Sign sign, Player player) {
Block blockUp = sign.getBlock().getFace(BlockFace.UP); Block blockUp = sign.getBlock().getFace(BlockFace.UP);
if(Permission.permissions == null || !SignUtil.isSign(blockUp) || Permission.has(player, Permission.ADMIN)){ if (Permission.permissions == null || !SignUtil.isSign(blockUp) || Permission.has(player, Permission.ADMIN)) {
return true; return true;
} }
String world = blockUp.getWorld().getName(); String world = blockUp.getWorld().getName();
@ -31,7 +31,7 @@ public class RestrictedSign {
sign = (Sign) blockUp.getState(); sign = (Sign) blockUp.getState();
boolean result = false; boolean result = false;
for(int i = 1; i <= 3; i++){ for (int i = 1; i <= 3; i++) {
result = result || Permission.permissions.inGroup(world, playerName, sign.getLine(i)); result = result || Permission.permissions.inGroup(world, playerName, sign.getLine(i));
} }
return result; return result;

View File

@ -45,7 +45,7 @@ public class Shop {
player.sendMessage(Config.getLocal(Language.NO_BUYING_HERE)); player.sendMessage(Config.getLocal(Language.NO_BUYING_HERE));
return false; return false;
} }
if(!Permission.has(player, Permission.BUY)){ if (!Permission.has(player, Permission.BUY)) {
player.sendMessage(Config.getLocal(Language.NO_PERMISSION)); player.sendMessage(Config.getLocal(Language.NO_PERMISSION));
return false; return false;
} }
@ -104,7 +104,7 @@ public class Shop {
player.sendMessage(Config.getLocal(Language.NO_SELLING_HERE)); player.sendMessage(Config.getLocal(Language.NO_SELLING_HERE));
return false; return false;
} }
if(!Permission.has(player, Permission.SELL)){ if (!Permission.has(player, Permission.SELL)) {
player.sendMessage(Config.getLocal(Language.NO_PERMISSION)); player.sendMessage(Config.getLocal(Language.NO_PERMISSION));
return false; return false;
} }

View File

@ -16,7 +16,7 @@ public class ShopManagement {
public static boolean buy(Sign sign, Player player) { public static boolean buy(Sign sign, Player player) {
Chest chestMc = BlockSearch.findChest(sign); Chest chestMc = BlockSearch.findChest(sign);
ItemStack item = Items.getItemStack(sign.getLine(3)); ItemStack item = Items.getItemStack(sign.getLine(3));
if(item == null){ if (item == null) {
player.sendMessage(ChatColor.RED + "[Shop] The item is not recognised!"); player.sendMessage(ChatColor.RED + "[Shop] The item is not recognised!");
return false; return false;
} }
@ -28,7 +28,7 @@ public class ShopManagement {
public static boolean sell(Sign sign, Player player) { public static boolean sell(Sign sign, Player player) {
Chest chestMc = BlockSearch.findChest(sign); Chest chestMc = BlockSearch.findChest(sign);
ItemStack item = Items.getItemStack(sign.getLine(3)); ItemStack item = Items.getItemStack(sign.getLine(3));
if(item == null){ if (item == null) {
player.sendMessage(ChatColor.RED + "[Shop] The item is not recognised!"); player.sendMessage(ChatColor.RED + "[Shop] The item is not recognised!");
return false; return false;
} }

View File

@ -52,7 +52,7 @@ public class BlockSearch {
return null; //Shame, we didn't find double chest :/ return null; //Shame, we didn't find double chest :/
} }
public static Chest findNeighbor(Chest chest){ public static Chest findNeighbor(Chest chest) {
return findNeighbor(chest.getBlock()); return findNeighbor(chest.getBlock());
} }
} }

View File

@ -17,7 +17,7 @@ public class InventoryUtil {
Material itemMaterial = item.getType(); Material itemMaterial = item.getType();
int first = inv.first(itemMaterial); int first = inv.first(itemMaterial);
if(first == -1){ if (first == -1) {
return amount; return amount;
} }
@ -52,7 +52,7 @@ public class InventoryUtil {
public static int add(Inventory inv, ItemStack item, int amount) { public static int add(Inventory inv, ItemStack item, int amount) {
amount = (amount > 0 ? amount : 1); amount = (amount > 0 ? amount : 1);
Material itemMaterial = item.getType(); Material itemMaterial = item.getType();
int maxStackSize = itemMaterial.getMaxStackSize(); int maxStackSize = itemMaterial.getMaxStackSize();
@ -84,10 +84,9 @@ public class InventoryUtil {
public static int amount(Inventory inv, ItemStack item, short durability) { public static int amount(Inventory inv, ItemStack item, short durability) {
int amount = 0; int amount = 0;
if(!inv.contains(item.getType())){ if (!inv.contains(item.getType())) {
return amount; return amount;
} }
ItemStack[] contents = inv.getContents(); ItemStack[] contents = inv.getContents();
for (ItemStack i : contents) { for (ItemStack i : contents) {
if (i != null) { if (i != null) {

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 6 version: 3.00 BETA 7
author: Acrobot author: Acrobot