From a45e620946c8e748b35258b7f8813d1c788bae37 Mon Sep 17 00:00:00 2001 From: snowleo Date: Mon, 6 Jun 2011 22:29:08 +0200 Subject: [PATCH] Major cleanup of the Protect code --- .../src/com/earth2me/essentials/Settings.java | 215 ++----- .../essentials/protect/EssentialsProtect.java | 154 ++--- .../EssentialsProtectBlockListener.java | 249 ++++---- .../EssentialsProtectEntityListener.java | 248 ++++---- .../EssentialsProtectPlayerListener.java | 48 +- .../protect/EssentialsProtectRegions.java | 5 - .../EssentialsProtectWeatherListener.java | 38 +- .../earth2me/essentials/protect/IProtect.java | 21 + .../essentials/protect/ProtectConfig.java | 111 ++++ .../protect/data/IProtectedBlock.java | 1 - .../essentials/protect/data/OwnedBlock.java | 19 +- .../protect/data/ProtectedBlockJDBC.java | 586 +++++++++++------- .../protect/data/ProtectedBlockMemory.java | 185 +++--- .../protect/data/ProtectedBlockMySQL.java | 20 +- 14 files changed, 1046 insertions(+), 854 deletions(-) delete mode 100644 EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectRegions.java create mode 100644 EssentialsProtect/src/com/earth2me/essentials/protect/IProtect.java create mode 100644 EssentialsProtect/src/com/earth2me/essentials/protect/ProtectConfig.java diff --git a/Essentials/src/com/earth2me/essentials/Settings.java b/Essentials/src/com/earth2me/essentials/Settings.java index 25e2786d3..64b2372a3 100644 --- a/Essentials/src/com/earth2me/essentials/Settings.java +++ b/Essentials/src/com/earth2me/essentials/Settings.java @@ -198,167 +198,7 @@ public class Settings implements IConf return config.getBoolean("non-ess-in-help", true); } - public Map getEpSettings() - { - Map epSettings = new HashMap(); - - epSettings.put("protect.protect.signs", config.getBoolean("protect.protect.signs", true)); - epSettings.put("protect.protect.rails", config.getBoolean("protect.protect.rails", true)); - epSettings.put("protect.protect.block-below", config.getBoolean("protect.protect.block-below", true)); - epSettings.put("protect.protect.prevent-block-on-rails", config.getBoolean("protect.protect.prevent-block-on-rails", false)); - epSettings.put("protect.memstore", config.getBoolean("protect.memstore", false)); - return epSettings; - } - - public Map getEpDBSettings() - { - Map epSettings = new HashMap(); - epSettings.put("protect.datatype", config.getString("protect.datatype", "sqlite")); - epSettings.put("protect.username", config.getString("protect.username", "root")); - epSettings.put("protect.password", config.getString("protect.password", "root")); - epSettings.put("protect.mysqlDb", config.getString("protect.mysqlDb", "jdbc:mysql://localhost:3306/minecraft")); - return epSettings; - } - - public List getEpAlertOnPlacement() - { - final List epAlertPlace = new ArrayList(); - for (String itemName : config.getString("protect.alert.on-placement", "").split(",")) { - itemName = itemName.trim(); - if (itemName.isEmpty()) { - continue; - } - ItemStack is; - try { - is = ItemDb.get(itemName); - epAlertPlace.add(is.getTypeId()); - } catch (Exception ex) { - logger.log(Level.SEVERE, Util.format("unknownItemInList", itemName, "alert.on-placement")); - } - } - return epAlertPlace; - } - - public List getEpAlertOnUse() - { - final List epAlertUse = new ArrayList(); - for (String itemName : config.getString("protect.alert.on-use", "").split(",")) { - itemName = itemName.trim(); - if (itemName.isEmpty()) { - continue; - } - ItemStack is; - try { - is = ItemDb.get(itemName); - epAlertUse.add(is.getTypeId()); - } catch (Exception ex) { - logger.log(Level.SEVERE, Util.format("unknownItemInList", itemName, "alert.on-use")); - } - } - return epAlertUse; - } - - public List getEpAlertOnBreak() - { - final List epAlertPlace = new ArrayList(); - for (String itemName : config.getString("protect.alert.on-break", "").split(",")) { - itemName = itemName.trim(); - if (itemName.isEmpty()) { - continue; - } - ItemStack is; - try { - is = ItemDb.get(itemName); - epAlertPlace.add(is.getTypeId()); - } catch (Exception ex) { - logger.log(Level.SEVERE, Util.format("unknownItemInList", itemName, "alert.on-break")); - } - } - return epAlertPlace; - } - - public List epBlackListPlacement() - { - final List epBlacklistPlacement = new ArrayList(); - for (String itemName : config.getString("protect.blacklist.placement", "").split(",")) { - itemName = itemName.trim(); - if (itemName.isEmpty()) { - continue; - } - ItemStack is; - try { - is = ItemDb.get(itemName); - epBlacklistPlacement.add(is.getTypeId()); - } catch (Exception ex) { - logger.log(Level.SEVERE, Util.format("unknownItemInList", itemName, "blacklist.placement")); - } - } - return epBlacklistPlacement; - } - - public List epBlackListUsage() - { - final List epBlackListUsage = new ArrayList(); - for (String itemName : config.getString("protect.blacklist.usage", "").split(",")) { - itemName = itemName.trim(); - if (itemName.isEmpty()) { - continue; - } - ItemStack is; - try { - is = ItemDb.get(itemName); - epBlackListUsage.add(is.getTypeId()); - } catch (Exception ex) { - logger.log(Level.SEVERE, Util.format("unknownItemInList", itemName, "blacklist.usage")); - } - } - return epBlackListUsage; - } - - public Map getEpGuardSettings() - { - final Map epSettings = new HashMap(); - epSettings.put("protect.prevent.lava-flow", config.getBoolean("protect.prevent.lava-flow", false)); - epSettings.put("protect.prevent.water-flow", config.getBoolean("protect.prevent.water-flow", false)); - epSettings.put("protect.prevent.water-bucket-flow", config.getBoolean("protect.prevent.water-bucket-flow", false)); - epSettings.put("protect.prevent.fire-spread", config.getBoolean("protect.prevent.fire-spread", true)); - epSettings.put("protect.prevent.flint-fire", config.getBoolean("protect.prevent.flint-fire", false)); - epSettings.put("protect.prevent.portal-creation", config.getBoolean("protect.prevent.portal-creation", false)); - epSettings.put("protect.prevent.lava-fire-spread", config.getBoolean("protect.prevent.lava-fire-spread", true)); - epSettings.put("protect.prevent.tnt-explosion", config.getBoolean("protect.prevent.tnt-explosion", false)); - epSettings.put("protect.prevent.creeper-explosion", config.getBoolean("protect.prevent.creeper-explosion", false)); - epSettings.put("protect.prevent.creeper-playerdamage", config.getBoolean("protect.prevent.creeper-playerdamage", false)); - epSettings.put("protect.prevent.creeper-blockdamage", config.getBoolean("protect.prevent.creeper-blockdamage", false)); - epSettings.put("protect.prevent.entitytarget", config.getBoolean("protect.prevent.entitytarget", false)); - for (CreatureType ct : CreatureType.values()) { - final String name = ct.toString().toLowerCase(); - epSettings.put("protect.prevent.spawn."+name, config.getBoolean("protect.prevent.spawn."+name, false)); - } - epSettings.put("protect.prevent.lightning-fire-spread", config.getBoolean("protect.prevent.lightning-fire-spread", true)); - return epSettings; - } - - public Map getEpPlayerSettings() - { - final Map epPlayerSettings = new HashMap(); - epPlayerSettings.put("protect.disable.fall", config.getBoolean("protect.disable.fall", false)); - epPlayerSettings.put("protect.disable.pvp", config.getBoolean("protect.disable.pvp", false)); - epPlayerSettings.put("protect.disable.drown", config.getBoolean("protect.disable.drown", false)); - epPlayerSettings.put("protect.disable.suffocate", config.getBoolean("protect.disable.suffocate", false)); - epPlayerSettings.put("protect.disable.lavadmg", config.getBoolean("protect.disable.lavadmg", false)); - epPlayerSettings.put("protect.disable.projectiles", config.getBoolean("protect.disable.projectiles", false)); - epPlayerSettings.put("protect.disable.contactdmg", config.getBoolean("protect.disable.contactdmg", false)); - epPlayerSettings.put("protect.disable.firedmg", config.getBoolean("protect.disable.firedmg", false)); - epPlayerSettings.put("protect.disable.build", config.getBoolean("protect.disable.build", false)); - epPlayerSettings.put("protect.disable.lightning", config.getBoolean("protect.disable.lightning", false)); - epPlayerSettings.put("protect.disable.weather.lightning", config.getBoolean("protect.disable.weather.lightning", false)); - epPlayerSettings.put("protect.disable.weather.storm", config.getBoolean("protect.disable.weather.storm", false)); - epPlayerSettings.put("protect.disable.weather.thunder", config.getBoolean("protect.disable.weather.thunder", false)); - return epPlayerSettings; - - } - - public int getEpCreeperMaxHeight() + public int getProtectCreeperMaxHeight() { return config.getInt("protect.creeper.max-height", -1); } @@ -441,25 +281,6 @@ public class Settings implements IConf return epItemSpwn; } - public List epBlockBreakingBlacklist() - { - final List epBreakList = new ArrayList(); - for (String itemName : config.getString("protect.blacklist.break", "").split(",")) { - itemName = itemName.trim(); - if (itemName.isEmpty()) { - continue; - } - ItemStack is; - try { - is = ItemDb.get(itemName); - epBreakList.add(is.getTypeId()); - } catch (Exception ex) { - logger.log(Level.SEVERE, Util.format("unknownItemInList", itemName, "blacklist.break")); - } - } - return epBreakList; - } - public boolean spawnIfNoHome() { return config.getBoolean("spawn-if-no-home", false); @@ -517,4 +338,38 @@ public class Settings implements IConf { return config.getBoolean("disable-eco", false); } + + public boolean getProtectPreventSpawn(final String creatureName) + { + return config.getBoolean("protect.prevent.spawn."+creatureName, false); + } + + public List getProtectList(final String configName) + { + final List list = new ArrayList(); + for (String itemName : config.getString(configName, "").split(",")) { + itemName = itemName.trim(); + if (itemName.isEmpty()) { + continue; + } + ItemStack itemStack; + try { + itemStack = ItemDb.get(itemName); + list.add(itemStack.getTypeId()); + } catch (Exception ex) { + logger.log(Level.SEVERE, Util.format("unknownItemInList", itemName, configName)); + } + } + return list; + } + + public String getProtectString(final String configName) + { + return config.getString(configName, null); + } + + public boolean getProtectBoolean(final String configName, boolean def) + { + return config.getBoolean(configName, def); + } } diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java index 5eb6b00f8..c8a8bcfc2 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java @@ -10,6 +10,7 @@ import com.earth2me.essentials.protect.data.ProtectedBlockMemory; import com.earth2me.essentials.protect.data.ProtectedBlockMySQL; import com.earth2me.essentials.protect.data.ProtectedBlockSQLite; import java.beans.PropertyVetoException; +import java.util.EnumMap; import java.util.List; import java.util.Map; import java.util.logging.Level; @@ -22,125 +23,104 @@ import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; -public class EssentialsProtect extends JavaPlugin implements IConf +public class EssentialsProtect extends JavaPlugin implements IConf, IProtect { - private EssentialsProtectBlockListener blockListener = null; - private EssentialsProtectPlayerListener playerListener = null; - private EssentialsProtectEntityListener entityListener = null; - private EssentialsProtectWeatherListener weatherListener = null; - public static final String AUTHORS = Essentials.AUTHORS; - private static final Logger logger = Logger.getLogger("Minecraft"); - public static Map genSettings = null; - public static Map dataSettings = null; - public static Map guardSettings = null; - public static Map playerSettings = null; - public static List usageList = null; - public static List blackListPlace = null; - public static List breakBlackList = null; - public static List onPlaceAlert = null; - public static List onUseAlert = null; - public static List onBreakAlert = null; - private IProtectedBlock storage = null; - IEssentials ess = null; - private static EssentialsProtect instance = null; + private static final Logger LOGGER = Logger.getLogger("Minecraft"); - public EssentialsProtect() - { - } + private final transient Map settingsBoolean = new EnumMap(ProtectConfig.class); + private final transient Map settingsString = new EnumMap(ProtectConfig.class); + private final transient Map> settingsList = new EnumMap>(ProtectConfig.class); + private transient IProtectedBlock storage = null; + public transient IEssentials ess = null; public void onEnable() { ess = Essentials.getStatic(); ess.getDependancyChecker().checkProtectDependancies(); - PluginManager pm = this.getServer().getPluginManager(); - playerListener = new EssentialsProtectPlayerListener(this); - blockListener = new EssentialsProtectBlockListener(this); - entityListener = new EssentialsProtectEntityListener(this); - weatherListener = new EssentialsProtectWeatherListener(this); + final PluginManager pm = this.getServer().getPluginManager(); + + final EssentialsProtectPlayerListener playerListener = new EssentialsProtectPlayerListener(this); pm.registerEvent(Type.PLAYER_INTERACT, playerListener, Priority.Low, this); + + final EssentialsProtectBlockListener blockListener = new EssentialsProtectBlockListener(this); pm.registerEvent(Type.BLOCK_PLACE, blockListener, Priority.Highest, this); pm.registerEvent(Type.BLOCK_FROMTO, blockListener, Priority.Highest, this); pm.registerEvent(Type.BLOCK_IGNITE, blockListener, Priority.Highest, this); pm.registerEvent(Type.BLOCK_BURN, blockListener, Priority.Highest, this); + pm.registerEvent(Type.BLOCK_BREAK, blockListener, Priority.Highest, this); + + final EssentialsProtectEntityListener entityListener = new EssentialsProtectEntityListener(this); pm.registerEvent(Type.ENTITY_EXPLODE, entityListener, Priority.Highest, this); pm.registerEvent(Type.ENTITY_DAMAGE, entityListener, Priority.Highest, this); - pm.registerEvent(Type.BLOCK_BREAK, blockListener, Priority.Highest, this); pm.registerEvent(Type.CREATURE_SPAWN, entityListener, Priority.Highest, this); + pm.registerEvent(Type.ENTITY_TARGET, entityListener, Priority.Highest, this); + + final EssentialsProtectWeatherListener weatherListener = new EssentialsProtectWeatherListener(this); pm.registerEvent(Type.LIGHTNING_STRIKE, weatherListener, Priority.Highest, this); pm.registerEvent(Type.THUNDER_CHANGE, weatherListener, Priority.Highest, this); pm.registerEvent(Type.WEATHER_CHANGE, weatherListener, Priority.Highest, this); - pm.registerEvent(Type.ENTITY_TARGET, entityListener, Priority.Highest, this); + reloadConfig(); ess.addReloadListener(this); - if (!this.getDescription().getVersion().equals(Essentials.getStatic().getDescription().getVersion())) + if (!this.getDescription().getVersion().equals(ess.getDescription().getVersion())) { - logger.log(Level.WARNING, Util.i18n("versionMismatchAll")); + LOGGER.log(Level.WARNING, Util.i18n("versionMismatchAll")); } - logger.info(Util.format("loadinfo", this.getDescription().getName(), this.getDescription().getVersion(), Essentials.AUTHORS)); - } - - public static boolean checkProtectionItems(List itemList, int id) - { - return !itemList.isEmpty() && itemList.contains(id); + LOGGER.info(Util.format("loadinfo", this.getDescription().getName(), this.getDescription().getVersion(), Essentials.AUTHORS)); } @Override - public void onDisable() + public boolean checkProtectionItems(final ProtectConfig list, final int id) { - genSettings.clear(); - dataSettings.clear(); - blockListener = null; - playerListener = null; - entityListener = null; - genSettings = null; - dataSettings = null; - guardSettings = null; - playerSettings = null; - usageList = null; - blackListPlace = null; - onPlaceAlert = null; - onUseAlert = null; - onBreakAlert = null; + final List itemList = settingsList.get(list); + return itemList != null && !itemList.isEmpty() && itemList.contains(id); } - public void alert(User user, String item, String type) + @Override + public void alert(final User user, final String item, final String type) { - Location loc = user.getLocation(); + final Location loc = user.getLocation(); for (Player p : this.getServer().getOnlinePlayers()) { - User alertUser = ess.getUser(p); + final User alertUser = ess.getUser(p); if (alertUser.isAuthorized("essentials.protect.alerts")) + { alertUser.sendMessage(Util.format("alertFormat", user.getName(), type, item, formatCoords(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()))); + } } } - public static String formatCoords(int x, int y, int z) + public static String formatCoords(final int x, final int y, final int z) { return x + "," + y + "," + z; } public void reloadConfig() { - dataSettings = ess.getSettings().getEpDBSettings(); - genSettings = ess.getSettings().getEpSettings(); - guardSettings = ess.getSettings().getEpGuardSettings(); - usageList = ess.getSettings().epBlackListUsage(); - blackListPlace = ess.getSettings().epBlackListPlacement(); - breakBlackList = ess.getSettings().epBlockBreakingBlacklist(); - onPlaceAlert = ess.getSettings().getEpAlertOnPlacement(); - onUseAlert = ess.getSettings().getEpAlertOnUse(); - onBreakAlert = ess.getSettings().getEpAlertOnBreak(); - playerSettings = ess.getSettings().getEpPlayerSettings(); + for (ProtectConfig protectConfig : ProtectConfig.values()) + { + if (protectConfig.isList()) { + settingsList.put(protectConfig, ess.getSettings().getProtectList(protectConfig.getConfigName())); + } else if (protectConfig.isString()) { + settingsString.put(protectConfig, ess.getSettings().getProtectString(protectConfig.getConfigName())); + } else { + settingsBoolean.put(protectConfig, ess.getSettings().getProtectBoolean(protectConfig.getConfigName(), protectConfig.getDefaultValueBoolean())); + } + + } - if (dataSettings.get("protect.datatype").equals("mysql")) + if (getSettingString(ProtectConfig.datatype).equalsIgnoreCase("mysql")) { try { - storage = new ProtectedBlockMySQL(dataSettings.get("protect.mysqlDb"), dataSettings.get("protect.username"), dataSettings.get("protect.password")); + storage = new ProtectedBlockMySQL( + getSettingString(ProtectConfig.mysqlDB), + getSettingString(ProtectConfig.dbUsername), + getSettingString(ProtectConfig.dbPassword)); } catch (PropertyVetoException ex) { - logger.log(Level.SEVERE, null, ex); + LOGGER.log(Level.SEVERE, null, ex); } } else @@ -151,17 +131,41 @@ public class EssentialsProtect extends JavaPlugin implements IConf } catch (PropertyVetoException ex) { - logger.log(Level.SEVERE, null, ex); + LOGGER.log(Level.SEVERE, null, ex); } } - if (genSettings.get("protect.memstore")) + if (getSettingBool(ProtectConfig.memstore)) { - storage = new ProtectedBlockMemory(storage); + storage = new ProtectedBlockMemory(storage, this); } } - public static IProtectedBlock getStorage() + @Override + public IProtectedBlock getStorage() { - return EssentialsProtect.instance.storage; + return storage; + } + + @Override + public boolean getSettingBool(final ProtectConfig protectConfig) + { + final Boolean bool = settingsBoolean.get(protectConfig); + return bool == null ? protectConfig.getDefaultValueBoolean() : bool; + } + + @Override + public String getSettingString(final ProtectConfig protectConfig) + { + final String str = settingsString.get(protectConfig); + return str == null ? protectConfig.getDefaultValueString() : str; + } + + public void onDisable() + { + } + + public IEssentials getEssentials() + { + return ess; } } diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java index 4029e273f..56f175897 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java @@ -1,8 +1,9 @@ package com.earth2me.essentials.protect; -import com.earth2me.essentials.Essentials; +import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.User; import com.earth2me.essentials.Util; +import com.earth2me.essentials.protect.data.IProtectedBlock; import java.util.ArrayList; import java.util.List; import org.bukkit.Material; @@ -14,85 +15,82 @@ import org.bukkit.event.block.BlockFromToEvent; import org.bukkit.event.block.BlockIgniteEvent; import org.bukkit.event.block.BlockListener; import org.bukkit.event.block.BlockPlaceEvent; -import org.bukkit.inventory.ItemStack; public class EssentialsProtectBlockListener extends BlockListener { - private EssentialsProtect parent; + final private transient IProtect prot; + final private transient IEssentials ess; - public EssentialsProtectBlockListener(EssentialsProtect parent) + public EssentialsProtectBlockListener(final IProtect parent) { - this.parent = parent; + this.prot = parent; + this.ess = prot.getEssentials(); } @Override - public void onBlockPlace(BlockPlaceEvent event) + public void onBlockPlace(final BlockPlaceEvent event) { - if (event.isCancelled()) return; - final ItemStack item = event.getItemInHand(); - final User user = parent.ess.getUser(event.getPlayer()); + if (event.isCancelled()) + { + return; + } - if (EssentialsProtect.playerSettings.get("protect.disable.build") && !user.canBuild()) + final User user = ess.getUser(event.getPlayer()); + + if (prot.getSettingBool(ProtectConfig.disable_build) && !user.canBuild()) { event.setCancelled(true); return; } - int id = event.getBlockPlaced().getTypeId(); + final Block blockPlaced = event.getBlockPlaced(); + final int id = blockPlaced.getTypeId(); - if (EssentialsProtect.checkProtectionItems(EssentialsProtect.blackListPlace, id) && !user.isAuthorized("essentials.protect.exemptplacement")) + if (prot.checkProtectionItems(ProtectConfig.blacklist_placement, id) && !user.isAuthorized("essentials.protect.exemptplacement")) { event.setCancelled(true); return; } - if (!EssentialsProtect.onPlaceAlert.isEmpty() && EssentialsProtect.onPlaceAlert.contains(String.valueOf(item.getTypeId()))) + if (prot.checkProtectionItems(ProtectConfig.alert_on_placement, id)) { - parent.alert(user, item.getType().toString(), Util.i18n("alertPlaced")); + prot.alert(user, blockPlaced.getType().toString(), Util.i18n("alertPlaced")); } - Block blockPlaced = event.getBlockPlaced(); - Block below = blockPlaced.getFace(BlockFace.DOWN); - if (below.getType() == Material.RAILS) { - if (EssentialsProtect.genSettings.get("protect.protect.prevent.block-on-rail")) - { - if (EssentialsProtect.getStorage().isProtected(below, user.getName())) { - event.setCancelled(true); - return; - } - } + final Block below = blockPlaced.getFace(BlockFace.DOWN); + if (below.getType() == Material.RAILS + && prot.getSettingBool(ProtectConfig.prevent_block_on_rail) + && prot.getStorage().isProtected(below, user.getName())) + { + event.setCancelled(true); + return; } - List protect = new ArrayList(); - if (blockPlaced.getType() == Material.RAILS) { - if (EssentialsProtect.genSettings.get("protect.protect.rails")) + final List protect = new ArrayList(); + if (blockPlaced.getType() == Material.RAILS + && prot.getSettingBool(ProtectConfig.protect_rails) + && user.isAuthorized("essentials.protect")) + { + protect.add(blockPlaced); + if (prot.getSettingBool(ProtectConfig.protect_below_rails)) { - if (user.isAuthorized("essentials.protect")) - { - protect.add(blockPlaced); - if (EssentialsProtect.genSettings.get("protect.protect.block-below")) - { - protect.add(blockPlaced.getFace(BlockFace.DOWN)); - } - } + protect.add(blockPlaced.getFace(BlockFace.DOWN)); } } - if (blockPlaced.getType() == Material.SIGN_POST || blockPlaced.getType() == Material.WALL_SIGN) { - if (EssentialsProtect.genSettings.get("protect.protect.signs")) + if ((blockPlaced.getType() == Material.SIGN_POST || blockPlaced.getType() == Material.WALL_SIGN) + && prot.getSettingBool(ProtectConfig.protect_signs) + && user.isAuthorized("essentials.protect")) + { + protect.add(blockPlaced); + if (prot.getSettingBool(ProtectConfig.protect_against_signs)) { - if (user.isAuthorized("essentials.protect")) - { - protect.add(blockPlaced); - if (EssentialsProtect.genSettings.get("protect.protect.block-below")) - { - protect.add(event.getBlockAgainst()); - } - } + protect.add(event.getBlockAgainst()); } } - for (Block block : protect) { - EssentialsProtect.getStorage().protectBlock(block, user.getName()); + for (Block block : protect) + { + prot.getStorage().protectBlock(block, user.getName()); } } @@ -100,180 +98,201 @@ public class EssentialsProtectBlockListener extends BlockListener public void onBlockIgnite(BlockIgniteEvent event) { Block block = event.getBlock(); - if (block.getType() == Material.RAILS && EssentialsProtect.genSettings.get("protect.protect.rails")) + if (block.getType() == Material.RAILS + && prot.getSettingBool(ProtectConfig.protect_rails)) { event.setCancelled(true); return; } - if ((block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST) && EssentialsProtect.genSettings.get("protect.protect.signs")) + if ((block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST) + && prot.getSettingBool(ProtectConfig.protect_signs)) { event.setCancelled(true); return; } - if (event.getBlock().getType() == Material.OBSIDIAN || - event.getBlock().getFace(BlockFace.DOWN).getType() == Material.OBSIDIAN) + if (event.getBlock().getType() == Material.OBSIDIAN + || event.getBlock().getFace(BlockFace.DOWN).getType() == Material.OBSIDIAN) { - event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.portal-creation")); + event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_portal_creation)); return; } - - if ((event.getCause().equals(BlockIgniteEvent.IgniteCause.SPREAD))) + + if (event.getCause().equals(BlockIgniteEvent.IgniteCause.SPREAD)) { - event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.fire-spread")); + event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_fire_spread)); return; } if (event.getCause().equals(BlockIgniteEvent.IgniteCause.FLINT_AND_STEEL)) { - event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.flint-fire")); + event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_flint_fire)); return; } if (event.getCause().equals(BlockIgniteEvent.IgniteCause.LAVA)) { - event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.lava-fire-spread")); + event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_lava_fire_spread)); return; } if (event.getCause().equals(BlockIgniteEvent.IgniteCause.LIGHTNING)) { - event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.lightning-fire-spread")); + event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_lightning_fire_spread)); return; } } @Override - public void onBlockFromTo(BlockFromToEvent event) + public void onBlockFromTo(final BlockFromToEvent event) { - if (event.isCancelled()) return; - Block block = event.getBlock(); - Block toBlock = event.getToBlock(); - if (toBlock.getType() == Material.RAILS && EssentialsProtect.genSettings.get("protect.protect.rails")) + if (event.isCancelled()) + { + return; + } + final Block toBlock = event.getToBlock(); + if (toBlock.getType() == Material.RAILS + && prot.getSettingBool(ProtectConfig.protect_rails)) { event.setCancelled(true); return; } - if ((toBlock.getType() == Material.WALL_SIGN || toBlock.getType() == Material.SIGN_POST) && EssentialsProtect.genSettings.get("protect.protect.signs")) + if ((toBlock.getType() == Material.WALL_SIGN || toBlock.getType() == Material.SIGN_POST) + && prot.getSettingBool(ProtectConfig.protect_signs)) { event.setCancelled(true); return; } + + final Block block = event.getBlock(); if (block.getType() == Material.WATER || block.getType() == Material.STATIONARY_WATER) { - event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.water-flow")); + event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_water_flow)); return; } if (block.getType() == Material.LAVA || block.getType() == Material.STATIONARY_LAVA) { - event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.lava-flow")); + event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_lava_flow)); return; } if (block.getType() == Material.AIR) { - event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.water-bucket-flow")); + event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_water_bucket_flow)); return; } } @Override - public void onBlockBurn(BlockBurnEvent event) + public void onBlockBurn(final BlockBurnEvent event) { - Block block = event.getBlock(); - if (block.getType() == Material.RAILS && EssentialsProtect.genSettings.get("protect.protect.rails")) + final Block block = event.getBlock(); + if (block.getType() == Material.RAILS && prot.getSettingBool(ProtectConfig.protect_rails)) { event.setCancelled(true); return; } - if ((block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST) && EssentialsProtect.genSettings.get("protect.protect.signs")) + if ((block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST) + && prot.getSettingBool(ProtectConfig.protect_signs)) { event.setCancelled(true); return; } - if (EssentialsProtect.guardSettings.get("protect.prevent.fire-spread")) + if (prot.getSettingBool(ProtectConfig.prevent_fire_spread)) { event.setCancelled(true); return; } } + private final static BlockFace[] faces = new BlockFace[] + { + BlockFace.NORTH, + BlockFace.EAST, + BlockFace.SOUTH, + BlockFace.WEST + }; @Override - public void onBlockBreak(BlockBreakEvent event) + public void onBlockBreak(final BlockBreakEvent event) { - if (event.isCancelled()) return; - User user = parent.ess.getUser(event.getPlayer()); - Block block = event.getBlock(); - if (EssentialsProtect.playerSettings.get("protect.disable.build") && !user.canBuild()) + if (event.isCancelled()) + { + return; + } + final User user = ess.getUser(event.getPlayer()); + + if (prot.getSettingBool(ProtectConfig.disable_build) && !user.canBuild()) { event.setCancelled(true); return; } + final Block block = event.getBlock(); + final int typeId = block.getTypeId(); - if(EssentialsProtect.breakBlackList.contains(String.valueOf(block.getTypeId())) && !user.isAuthorized("essentials.protect.exemptbreak")) + if (prot.checkProtectionItems(ProtectConfig.blacklist_break, typeId) + && !user.isAuthorized("essentials.protect.exemptbreak")) { event.setCancelled(true); return; } + final Material type = block.getType(); - if (!EssentialsProtect.onBreakAlert.isEmpty() && EssentialsProtect.onBreakAlert.contains(String.valueOf(block.getTypeId()))) + if (prot.checkProtectionItems(ProtectConfig.alert_on_break, typeId)) { - parent.alert(user, block.getType().toString(), Util.i18n("alertBroke")); + prot.alert(user, type.toString(), Util.i18n("alertBroke")); } + final IProtectedBlock storage = prot.getStorage(); if (user.isAuthorized("essentials.protect.admin")) { - if (block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST || block.getType() == Material.RAILS) + if (type == Material.WALL_SIGN || type == Material.SIGN_POST || type == Material.RAILS) { - EssentialsProtect.getStorage().unprotectBlock(block); - if (block.getType() == Material.RAILS || block.getType() == Material.SIGN_POST) { - Block below = block.getFace(BlockFace.DOWN); - EssentialsProtect.getStorage().unprotectBlock(below); - } else { - BlockFace[] faces = new BlockFace[] { - BlockFace.NORTH, - BlockFace.EAST, - BlockFace.SOUTH, - BlockFace.WEST - }; - for (BlockFace blockFace : faces) { - Block against = block.getFace(blockFace); - EssentialsProtect.getStorage().unprotectBlock(against); + storage.unprotectBlock(block); + if (type == Material.RAILS || type == Material.SIGN_POST) + { + final Block below = block.getFace(BlockFace.DOWN); + storage.unprotectBlock(below); + } + else + { + for (BlockFace blockFace : faces) + { + final Block against = block.getFace(blockFace); + storage.unprotectBlock(against); } } } else { - EssentialsProtect.getStorage().unprotectBlock(block); + storage.unprotectBlock(block); } return; } else { - boolean isProtected = EssentialsProtect.getStorage().isProtected(block, user.getName()); - if (!isProtected) { - if (block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST || block.getType() == Material.RAILS) - { - EssentialsProtect.getStorage().unprotectBlock(block); - if (block.getType() == Material.RAILS || block.getType() == Material.SIGN_POST) { - Block below = block.getFace(BlockFace.DOWN); - EssentialsProtect.getStorage().unprotectBlock(below); - } else { - BlockFace[] faces = new BlockFace[] { - BlockFace.NORTH, - BlockFace.EAST, - BlockFace.SOUTH, - BlockFace.WEST - }; - for (BlockFace blockFace : faces) { - Block against = block.getFace(blockFace); - EssentialsProtect.getStorage().unprotectBlock(against); + final boolean isProtected = storage.isProtected(block, user.getName()); + if (!isProtected) + { + if (type == Material.WALL_SIGN || type == Material.SIGN_POST || type == Material.RAILS) + { + storage.unprotectBlock(block); + if (type == Material.RAILS || type == Material.SIGN_POST) + { + final Block below = block.getFace(BlockFace.DOWN); + storage.unprotectBlock(below); + } + else + { + for (BlockFace blockFace : faces) + { + final Block against = block.getFace(blockFace); + storage.unprotectBlock(against); } } } else { - EssentialsProtect.getStorage().unprotectBlock(block); + storage.unprotectBlock(block); } } event.setCancelled(true); diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectEntityListener.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectEntityListener.java index ab49944d2..cd3977b71 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectEntityListener.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectEntityListener.java @@ -1,11 +1,10 @@ package com.earth2me.essentials.protect; -import com.earth2me.essentials.Essentials; import com.earth2me.essentials.EssentialsBlockListener; +import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.User; -import java.util.ArrayList; import java.util.HashSet; -import java.util.List; +import java.util.Set; import net.minecraft.server.ChunkPosition; import net.minecraft.server.Packet60Explosion; import org.bukkit.Location; @@ -18,7 +17,6 @@ import org.bukkit.craftbukkit.entity.CraftPlayer; import org.bukkit.entity.Creeper; import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Monster; import org.bukkit.entity.Player; import org.bukkit.event.entity.CreatureSpawnEvent; import org.bukkit.event.entity.EntityDamageByBlockEvent; @@ -34,11 +32,13 @@ import org.bukkit.event.entity.EntityTargetEvent.TargetReason; public class EssentialsProtectEntityListener extends EntityListener { - private EssentialsProtect parent; + private final transient IProtect prot; + private final transient IEssentials ess; - public EssentialsProtectEntityListener(EssentialsProtect parent) + public EssentialsProtectEntityListener(final IProtect prot) { - this.parent = parent; + this.prot = prot; + this.ess = prot.getEssentials(); } @Override @@ -48,30 +48,35 @@ public class EssentialsProtectEntityListener extends EntityListener { return; } + final Entity target = event.getEntity(); + final User user = ess.getUser(target); if (event instanceof EntityDamageByBlockEvent) { - DamageCause cause = event.getCause(); + final DamageCause cause = event.getCause(); - if (EssentialsProtect.playerSettings.get("protect.disable.contactdmg") && cause == DamageCause.CONTACT - && !(event.getEntity() instanceof Player - && Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.contact") - && !Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.disable"))) + if (prot.getSettingBool(ProtectConfig.disable_contactdmg) + && cause == DamageCause.CONTACT + && !(target instanceof Player + && user.isAuthorized("essentials.protect.damage.contact") + && !user.isAuthorized("essentials.protect.damage.disable"))) { event.setCancelled(true); return; } - if (EssentialsProtect.playerSettings.get("protect.disable.lavadmg") && cause == DamageCause.LAVA - && !(event.getEntity() instanceof Player - && Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.lava") - && !Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.disable"))) + if (prot.getSettingBool(ProtectConfig.disable_lavadmg) + && cause == DamageCause.LAVA + && !(target instanceof Player + && user.isAuthorized("essentials.protect.damage.lava") + && !user.isAuthorized("essentials.protect.damage.disable"))) { event.setCancelled(true); return; } - if (EssentialsProtect.guardSettings.get("protect.prevent.tnt-explosion") && cause == DamageCause.BLOCK_EXPLOSION - && !(event.getEntity() instanceof Player - && Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.tnt") - && !Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.disable"))) + if (prot.getSettingBool(ProtectConfig.prevent_tnt_explosion) + && cause == DamageCause.BLOCK_EXPLOSION + && !(target instanceof Player + && user.isAuthorized("essentials.protect.damage.tnt") + && !user.isAuthorized("essentials.protect.damage.disable"))) { event.setCancelled(true); return; @@ -80,97 +85,90 @@ public class EssentialsProtectEntityListener extends EntityListener if (event instanceof EntityDamageByEntityEvent) { - EntityDamageByEntityEvent edEvent = (EntityDamageByEntityEvent)event; - Entity eAttack = edEvent.getDamager(); - Entity eDefend = edEvent.getEntity(); + final EntityDamageByEntityEvent edEvent = (EntityDamageByEntityEvent)event; + final Entity eAttack = edEvent.getDamager(); + final User attacker = ess.getUser(eAttack); // PVP Settings - if (eDefend instanceof Player && eAttack instanceof Player) + if (target instanceof Player && eAttack instanceof Player + && prot.getSettingBool(ProtectConfig.disable_pvp) + && (!user.isAuthorized("essentials.protect.pvp") || !attacker.isAuthorized("essentials.protect.pvp"))) { - if (EssentialsProtect.playerSettings.get("protect.disable.pvp")) - { - User defender = parent.ess.getUser(eDefend); - User attacker = parent.ess.getUser(eAttack); - - if (!defender.isAuthorized("essentials.protect.pvp") || !attacker.isAuthorized("essentials.protect.pvp")) - { - event.setCancelled(true); - return; - } - } + event.setCancelled(true); + return; } + //Creeper explode prevention - if (eAttack != null && eAttack instanceof Monster) + if (eAttack instanceof Creeper && prot.getSettingBool(ProtectConfig.prevent_creeper_explosion) + && !(target instanceof Player + && user.isAuthorized("essentials.protect.damage.creeper") + && !user.isAuthorized("essentials.protect.damage.disable"))) { - if (eAttack instanceof Creeper && EssentialsProtect.guardSettings.get("protect.prevent.creeper-explosion") - && !(event.getEntity() instanceof Player - && Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.creeper") - && !Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.disable"))) - { - event.setCancelled(true); - return; - } - - if (eAttack instanceof Creeper && EssentialsProtect.guardSettings.get("protect.prevent.creeper-playerdamage") - && !(event.getEntity() instanceof Player - && Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.creeper") - && !Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.disable"))) - { - event.setCancelled(true); - return; - } + event.setCancelled(true); + return; } - } - if (event instanceof EntityDamageByProjectileEvent) - { - if (event.getEntity() instanceof Player - && EssentialsProtect.playerSettings.get("protect.disable.projectiles") - && !Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.projectiles") - && !Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.disable")) + if (eAttack instanceof Creeper && prot.getSettingBool(ProtectConfig.prevent_creeper_playerdmg) + && !(target instanceof Player + && user.isAuthorized("essentials.protect.damage.creeper") + && !user.isAuthorized("essentials.protect.damage.disable"))) { event.setCancelled(true); return; } } - DamageCause cause = event.getCause(); - Entity casualty = event.getEntity(); - if (casualty instanceof Player) + if (event instanceof EntityDamageByProjectileEvent + && target instanceof Player + && prot.getSettingBool(ProtectConfig.disable_projectiles) + && !(user.isAuthorized("essentials.protect.damage.projectiles") + && !user.isAuthorized("essentials.protect.damage.disable"))) { - if (EssentialsProtect.playerSettings.get("protect.disable.fall") && cause == DamageCause.FALL - && !(Essentials.getStatic().getUser(casualty).isAuthorized("essentials.protect.damage.fall") - && !Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.disable"))) + event.setCancelled(true); + return; + } + + final DamageCause cause = event.getCause(); + if (target instanceof Player) + { + if (cause == DamageCause.FALL + && prot.getSettingBool(ProtectConfig.disable_fall) + && !(user.isAuthorized("essentials.protect.damage.fall") + && !user.isAuthorized("essentials.protect.damage.disable"))) { event.setCancelled(true); return; } - if (EssentialsProtect.playerSettings.get("protect.disable.suffocate") && cause == DamageCause.SUFFOCATION - && !(Essentials.getStatic().getUser(casualty).isAuthorized("essentials.protect.damage.suffocation") - && !Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.disable"))) + if (cause == DamageCause.SUFFOCATION + && prot.getSettingBool(ProtectConfig.disable_suffocate) + && !(user.isAuthorized("essentials.protect.damage.suffocation") + && !user.isAuthorized("essentials.protect.damage.disable"))) { event.setCancelled(true); return; } - if (EssentialsProtect.playerSettings.get("protect.disable.firedmg") && (cause == DamageCause.FIRE - || cause == DamageCause.FIRE_TICK) - && !(parent.ess.getUser(casualty).isAuthorized("essentials.protect.damage.fire") - && !parent.ess.getUser(event.getEntity()).isAuthorized("essentials.protect.damage.disable"))) + if ((cause == DamageCause.FIRE + || cause == DamageCause.FIRE_TICK) + && prot.getSettingBool(ProtectConfig.disable_firedmg) + && !(user.isAuthorized("essentials.protect.damage.fire") + && !user.isAuthorized("essentials.protect.damage.disable"))) { event.setCancelled(true); return; } - if (EssentialsProtect.playerSettings.get("protect.disable.drown") && cause == DamageCause.DROWNING - && !(parent.ess.getUser(casualty).isAuthorized("essentials.protect.damage.drowning") - && !parent.ess.getUser(event.getEntity()).isAuthorized("essentials.protect.damage.disable"))) + if (cause == DamageCause.DROWNING + && prot.getSettingBool(ProtectConfig.disable_drown) + && !(user.isAuthorized("essentials.protect.damage.drowning") + && !user.isAuthorized("essentials.protect.damage.disable"))) { event.setCancelled(true); return; } - if (EssentialsProtect.playerSettings.get("protect.disable.lightning") && cause == DamageCause.LIGHTNING - && !(parent.ess.getUser(casualty).isAuthorized("essentials.protect.damage.lightning") - && !parent.ess.getUser(event.getEntity()).isAuthorized("essentials.protect.damage.disable"))) + if (cause == DamageCause.LIGHTNING + && prot.getSettingBool(ProtectConfig.disable_lightning) + && !(user.isAuthorized("essentials.protect.damage.lightning") + && !user.isAuthorized("essentials.protect.damage.disable"))) { event.setCancelled(true); return; @@ -185,58 +183,56 @@ public class EssentialsProtectEntityListener extends EntityListener { return; } - if (event.getEntity() instanceof LivingEntity) + final int maxHeight = ess.getSettings().getProtectCreeperMaxHeight(); + //Nicccccccccce plaaacccccccccce.. + if (event.getEntity() instanceof LivingEntity + && (prot.getSettingBool(ProtectConfig.prevent_creeper_explosion) + || prot.getSettingBool(ProtectConfig.prevent_creeper_blockdmg) + || (maxHeight >= 0 && event.getLocation().getBlockY() > maxHeight))) { - //Nicccccccccce plaaacccccccccce.. - int maxHeight = parent.ess.getSettings().getEpCreeperMaxHeight(); - if (EssentialsProtect.guardSettings.get("protect.prevent.creeper-explosion") - || EssentialsProtect.guardSettings.get("protect.prevent.creeper-blockdamage") - || (maxHeight >= 0 && event.getLocation().getBlockY() > maxHeight)) + final Set set = new HashSet(event.blockList().size()); + final Player[] players = ess.getServer().getOnlinePlayers(); + final Set blocksUnderPlayers = new HashSet(players.length); + final Location loc = event.getLocation(); + for (Player player : players) { - HashSet set = new HashSet(event.blockList().size()); - Player[] players = parent.getServer().getOnlinePlayers(); - List blocksUnderPlayers = new ArrayList(players.length); - final Location loc = event.getLocation(); - for (Player player : players) + if (player.getWorld().equals(loc.getWorld())) { - if (player.getWorld().equals(loc.getWorld())) - { - blocksUnderPlayers.add( - new ChunkPosition( - player.getLocation().getBlockX(), - player.getLocation().getBlockY() - 1, - player.getLocation().getBlockZ())); - } + blocksUnderPlayers.add( + new ChunkPosition( + player.getLocation().getBlockX(), + player.getLocation().getBlockY() - 1, + player.getLocation().getBlockZ())); } - ChunkPosition cp; - for (Block block : event.blockList()) - { + } + ChunkPosition cp; + for (Block block : event.blockList()) + { cp = new ChunkPosition(block.getX(), block.getY(), block.getZ()); - if (!blocksUnderPlayers.contains(cp)) - { - set.add(cp); - } + if (!blocksUnderPlayers.contains(cp)) + { + set.add(cp); } + } - ((CraftServer)parent.getServer()).getHandle().a(loc.getX(), loc.getY(), loc.getZ(), 64.0D, ((CraftWorld)loc.getWorld()).getHandle().worldProvider.dimension, - new Packet60Explosion(loc.getX(), loc.getY(), loc.getZ(), 3.0f, set)); - event.setCancelled(true); - return; - } + ((CraftServer)ess.getServer()).getHandle().a(loc.getX(), loc.getY(), loc.getZ(), 64.0D, ((CraftWorld)loc.getWorld()).getHandle().worldProvider.dimension, + new Packet60Explosion(loc.getX(), loc.getY(), loc.getZ(), 3.0f, set)); + event.setCancelled(true); + return; } - else - { //OH NOES TNT - if (EssentialsProtect.guardSettings.get("protect.prevent.tnt-explosion")) - { - event.setCancelled(true); - return; - } + else if (!(event.getEntity() instanceof LivingEntity) + && prot.getSettingBool(ProtectConfig.prevent_tnt_explosion)) + { + event.setCancelled(true); + return; } // This code will prevent explosions near protected rails, signs or protected chests // TODO: Use protect db instead of this code + for (Block block : event.blockList()) { - if ((block.getType() == Material.RAILS || block.getFace(BlockFace.UP).getType() == Material.RAILS) && EssentialsProtect.genSettings.get("protect.protect.rails")) + if ((block.getType() == Material.RAILS || block.getFace(BlockFace.UP).getType() == Material.RAILS) + && prot.getSettingBool(ProtectConfig.protect_rails)) { event.setCancelled(true); return; @@ -248,7 +244,7 @@ public class EssentialsProtectEntityListener extends EntityListener || block.getFace(BlockFace.WEST).getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST || block.getFace(BlockFace.UP).getType() == Material.SIGN_POST) - && EssentialsProtect.genSettings.get("protect.protect.signs")) + && prot.getSettingBool(ProtectConfig.protect_signs)) { event.setCancelled(true); return; @@ -263,7 +259,7 @@ public class EssentialsProtectEntityListener extends EntityListener } @Override - public void onCreatureSpawn(CreatureSpawnEvent event) + public void onCreatureSpawn(final CreatureSpawnEvent event) { if (event.getEntity() instanceof CraftPlayer) { @@ -273,29 +269,29 @@ public class EssentialsProtectEntityListener extends EntityListener { return; } - String creatureName = event.getCreatureType().toString().toLowerCase(); + final String creatureName = event.getCreatureType().toString().toLowerCase(); if (creatureName == null || creatureName.isEmpty()) { return; } - if (EssentialsProtect.guardSettings.get("protect.prevent.spawn." + creatureName)) + if (ess.getSettings().getProtectPreventSpawn(creatureName)) { event.setCancelled(true); } } - + @Override - public void onEntityTarget(EntityTargetEvent event) + public void onEntityTarget(final EntityTargetEvent event) { if (!(event.getTarget() instanceof Player)) { return; } - User user = Essentials.getStatic().getUser(event.getTarget()); + final User user = ess.getUser(event.getTarget()); if ((event.getReason() == TargetReason.CLOSEST_PLAYER || event.getReason() == TargetReason.TARGET_ATTACKED_ENTITY || event.getReason() == TargetReason.PIG_ZOMBIE_TARGET) - && EssentialsProtect.guardSettings.get("protect.prevent.entitytarget") + && prot.getSettingBool(ProtectConfig.prevent_entitytarget) && !user.isAuthorized("essentials.protect.entitytarget.bypass")) { event.setCancelled(true); diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectPlayerListener.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectPlayerListener.java index 4c9b2c5fd..4d69fb31f 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectPlayerListener.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectPlayerListener.java @@ -1,39 +1,45 @@ package com.earth2me.essentials.protect; +import com.earth2me.essentials.IEssentials; import org.bukkit.block.Block; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerListener; import org.bukkit.inventory.ItemStack; -import com.earth2me.essentials.Essentials; import com.earth2me.essentials.User; import com.earth2me.essentials.Util; + public class EssentialsProtectPlayerListener extends PlayerListener { - private EssentialsProtect parent; + private final transient IProtect prot; + private final transient IEssentials ess; - public EssentialsProtectPlayerListener(EssentialsProtect parent) + public EssentialsProtectPlayerListener(final IProtect prot) { - - this.parent = parent; + this.prot = prot; + this.ess = prot.getEssentials(); } @Override - public void onPlayerInteract(PlayerInteractEvent event) + public void onPlayerInteract(final PlayerInteractEvent event) { - if (event.isCancelled()) return; - ItemStack item = event.getItem(); - User user = parent.ess.getUser(event.getPlayer()); - Block blockClicked = event.getClickedBlock(); + if (event.isCancelled()) + { + return; + } - if (EssentialsProtect.playerSettings.get("protect.disable.build") && !user.canBuild()) + final User user = ess.getUser(event.getPlayer()); + + if (prot.getSettingBool(ProtectConfig.disable_build) && !user.canBuild()) { event.setCancelled(true); return; } - - if (item != null && EssentialsProtect.checkProtectionItems(EssentialsProtect.usageList, item.getTypeId()) && !user.isAuthorized("essentials.protect.exemptusage")) + final ItemStack item = event.getItem(); + if (item != null + && prot.checkProtectionItems(ProtectConfig.blacklist_usage, item.getTypeId()) + && !user.isAuthorized("essentials.protect.exemptusage")) { event.setCancelled(true); return; @@ -41,26 +47,28 @@ public class EssentialsProtectPlayerListener extends PlayerListener if (user.isAuthorized("essentials.protect.admin")) { - StringBuilder sb = new StringBuilder(); + final StringBuilder stringBuilder = new StringBuilder(); boolean first = true; - for (String owner : EssentialsProtect.getStorage().getOwners(blockClicked)) + final Block blockClicked = event.getClickedBlock(); + for (String owner : prot.getStorage().getOwners(blockClicked)) { if (!first) { - sb.append(", "); + stringBuilder.append(", "); } first = false; - sb.append(owner); + stringBuilder.append(owner); } - String ownerNames = sb.toString(); + final String ownerNames = stringBuilder.toString(); if (ownerNames != null && !ownerNames.isEmpty()) { user.sendMessage(Util.format("protectionOwner", ownerNames)); } } - if (item != null && EssentialsProtect.onUseAlert.contains(String.valueOf(item.getTypeId()))) + if (item != null + && prot.checkProtectionItems(ProtectConfig.alert_on_use, item.getTypeId())) { - parent.alert(user, item.getType().toString(), Util.i18n("alertUsed")); + prot.alert(user, item.getType().toString(), Util.i18n("alertUsed")); } } } diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectRegions.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectRegions.java deleted file mode 100644 index 6a640f3e1..000000000 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectRegions.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.earth2me.essentials.protect; - -public class EssentialsProtectRegions { - -} diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectWeatherListener.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectWeatherListener.java index 901d3d8c7..e81efc72b 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectWeatherListener.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectWeatherListener.java @@ -8,53 +8,43 @@ import org.bukkit.event.weather.WeatherListener; public class EssentialsProtectWeatherListener extends WeatherListener { - private EssentialsProtect parent; + private final transient IProtect prot; - public EssentialsProtectWeatherListener(EssentialsProtect parent) + public EssentialsProtectWeatherListener(final IProtect prot) { - this.parent = parent; + this.prot = prot; } @Override - public void onWeatherChange(WeatherChangeEvent event) + public void onWeatherChange(final WeatherChangeEvent event) { - if (event.isCancelled()) - { - return; - } - if (EssentialsProtect.playerSettings.get("protect.disable.weather.storm") && event.toWeatherState()) + if (!event.isCancelled() + && prot.getSettingBool(ProtectConfig.disable_weather_storm) + && event.toWeatherState()) { event.setCancelled(true); - return; } } @Override - public void onLightningStrike(LightningStrikeEvent event) + public void onLightningStrike(final LightningStrikeEvent event) { - if (event.isCancelled()) - { - return; - } - if (EssentialsProtect.playerSettings.get("protect.disable.weather.lightning")) + if (!event.isCancelled() + && prot.getSettingBool(ProtectConfig.disable_weather_lightning)) { event.setCancelled(true); - return; } } @Override - public void onThunderChange(ThunderChangeEvent event) + public void onThunderChange(final ThunderChangeEvent event) { - if (event.isCancelled()) - { - return; - } - if (EssentialsProtect.playerSettings.get("protect.disable.weather.thunder") && event.toThunderState()) + if (!event.isCancelled() + && prot.getSettingBool(ProtectConfig.disable_weather_thunder) + && event.toThunderState()) { event.setCancelled(true); - return; } } } diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/IProtect.java b/EssentialsProtect/src/com/earth2me/essentials/protect/IProtect.java new file mode 100644 index 000000000..ac095e33d --- /dev/null +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/IProtect.java @@ -0,0 +1,21 @@ +package com.earth2me.essentials.protect; + +import com.earth2me.essentials.IEssentials; +import com.earth2me.essentials.User; +import com.earth2me.essentials.protect.data.IProtectedBlock; + + +public interface IProtect +{ + void alert(final User user, final String item, final String type); + + boolean checkProtectionItems(final ProtectConfig list, final int id); + + boolean getSettingBool(final ProtectConfig protectConfig); + + String getSettingString(final ProtectConfig protectConfig); + + IProtectedBlock getStorage(); + + IEssentials getEssentials(); +} diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/ProtectConfig.java b/EssentialsProtect/src/com/earth2me/essentials/protect/ProtectConfig.java new file mode 100644 index 000000000..d9161bda8 --- /dev/null +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/ProtectConfig.java @@ -0,0 +1,111 @@ +package com.earth2me.essentials.protect; + + +public enum ProtectConfig +{ + datatype("protect.datatype", "sqlite"), + mysqlDB("protect.mysqlDb", "jdbc:mysql://localhost:3306/minecraft"), + dbUsername("protect.username", "root"), + dbPassword("protect.password", ""), + memstore("protect.memstore", false), + disable_contactdmg("protect.disable.contactdmg", false), + disable_lavadmg("protect.disable.lavadmg", false), + disable_build("protect.disable.build", false), + disable_pvp("protect.disable.pvp", false), + disable_projectiles("protect.disable.projectiles", false), + disable_fall("protect.disable.fall", false), + disable_suffocate("protect.disable.suffocate", false), + disable_firedmg("protect.disable.firedmg", false), + disable_lightning("protect.disable.lightning", false), + disable_drown("protect.disable.drown", false), + disable_weather_storm("protect.disable.weather.storm", false), + disable_weather_lightning("protect.disable.weather.lightning", false), + disable_weather_thunder("protect.disable.weather.thunder", false), + prevent_fire_spread("protect.prevent.fire-spread", true), + prevent_flint_fire("protect.prevent.flint-fire", false), + prevent_lava_fire_spread("protect.prevent.lava-fire-spread", true), + prevent_lightning_fire_spread("protect.prevent.lightning-fire-spread", true), + prevent_water_flow("protect.prevent.water-flow", false), + prevent_lava_flow("protect.prevent.lava-flow", false), + prevent_water_bucket_flow("protect.prevent.water-bucket-flow", false), + prevent_portal_creation("protect.prevent.portal-creation", false), + prevent_block_on_rail("protect.protect.prevent-block-on-rails", false), + prevent_tnt_explosion("protect.prevent.tnt-explosion", false), + prevent_creeper_explosion("protect.prevent.creeper-explosion", true), + prevent_creeper_playerdmg("protect.prevent.creeper-playerdamage", false), + prevent_creeper_blockdmg("protect.prevent.creeper-blockdamage", false), + prevent_entitytarget("protect.prevent.entitytarget", false), + protect_rails("protect.protect.rails", true), + protect_below_rails("protect.protect.block-below", true), + protect_signs("protect.protect.signs", true), + protect_against_signs("protect.protect.block-below", true), + alert_on_placement("protect.alert.on-placement"), + alert_on_use("protect.alert.on-use"), + alert_on_break("protect.alert.on-break"), + blacklist_placement("protect.blacklist.placement"), + blacklist_usage("protect.blacklist.usage"), + blacklist_break("protect.blacklist.break"); + private final String configName; + private final String defValueString; + private final boolean defValueBoolean; + private final boolean isList; + private final boolean isString; + + private ProtectConfig(final String configName) + { + this(configName, null, false, true, false); + } + + private ProtectConfig(final String configName, final String defValueString) + { + this(configName, defValueString, false, false, true); + } + + private ProtectConfig(final String configName, final boolean defValueBoolean) + { + this(configName, null, defValueBoolean, false, false); + } + + private ProtectConfig(final String configName, final String defValueString, final boolean defValueBoolean, final boolean isList, final boolean isString) + { + this.configName = configName; + this.defValueString = defValueString; + this.defValueBoolean = defValueBoolean; + this.isList = isList; + this.isString = isString; + } + + /** + * @return the configName + */ + public String getConfigName() + { + return configName; + } + + /** + * @return the default value String + */ + public String getDefaultValueString() + { + return defValueString; + } + + /** + * @return the default value boolean + */ + public boolean getDefaultValueBoolean() + { + return defValueBoolean; + } + + public boolean isString() + { + return isString; + } + + public boolean isList() + { + return isList; + } +} diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/data/IProtectedBlock.java b/EssentialsProtect/src/com/earth2me/essentials/protect/data/IProtectedBlock.java index 28b7e425b..6580ce7f8 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/data/IProtectedBlock.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/data/IProtectedBlock.java @@ -1,7 +1,6 @@ package com.earth2me.essentials.protect.data; import java.util.List; -import java.util.Set; import org.bukkit.block.Block; public interface IProtectedBlock { diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/data/OwnedBlock.java b/EssentialsProtect/src/com/earth2me/essentials/protect/data/OwnedBlock.java index b9b036798..dea124b58 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/data/OwnedBlock.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/data/OwnedBlock.java @@ -1,9 +1,18 @@ package com.earth2me.essentials.protect.data; public class OwnedBlock { - int x; - int y; - int z; - String world; - String playerName; + final int x; + final int y; + final int z; + final String world; + final String playerName; + + public OwnedBlock(int x, int y, int z, String world, String playerName) + { + this.x = x; + this.y = y; + this.z = z; + this.world = world; + this.playerName = playerName; + } } diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockJDBC.java b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockJDBC.java index 567c7e23b..1d09f95c4 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockJDBC.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockJDBC.java @@ -12,285 +12,411 @@ import java.util.logging.Level; import java.util.logging.Logger; import org.bukkit.block.Block; -public abstract class ProtectedBlockJDBC implements IProtectedBlock { - protected static final Logger logger = Logger.getLogger("Minecraft"); - protected ComboPooledDataSource cpds; + +public abstract class ProtectedBlockJDBC implements IProtectedBlock +{ + protected static final Logger LOGGER = Logger.getLogger("Minecraft"); + protected final transient ComboPooledDataSource cpds; protected abstract PreparedStatement getStatementCreateTable(Connection conn) throws SQLException; + protected abstract PreparedStatement getStatementUpdateFrom2_0Table(Connection conn) throws SQLException; + protected abstract PreparedStatement getStatementDeleteAll(Connection conn) throws SQLException; + protected abstract PreparedStatement getStatementInsert(Connection conn, String world, int x, int y, int z, String playerName) throws SQLException; + protected abstract PreparedStatement getStatementPlayerCountByLocation(Connection conn, String world, int x, int y, int z, String playerName) throws SQLException; + protected abstract PreparedStatement getStatementPlayersByLocation(Connection conn, String name, int x, int y, int z) throws SQLException; + protected abstract PreparedStatement getStatementDeleteByLocation(Connection conn, String world, int x, int y, int z) throws SQLException; + protected abstract PreparedStatement getStatementAllBlocks(Connection conn) throws SQLException; - - public ProtectedBlockJDBC(String driver, String url) throws PropertyVetoException { + + public ProtectedBlockJDBC(String driver, String url) throws PropertyVetoException + { this(driver, url, null, null); } - - public ProtectedBlockJDBC(String driver, String url, String username, String password) throws PropertyVetoException { + + public ProtectedBlockJDBC(String driver, String url, String username, String password) throws PropertyVetoException + { cpds = new ComboPooledDataSource(); cpds.setDriverClass(driver); cpds.setJdbcUrl(url); - if (username != null) { + if (username != null) + { cpds.setUser(username); cpds.setPassword(password); } cpds.setMaxStatements(20); createAndConvertTable(); } - - - private void createAndConvertTable() { + + private void createAndConvertTable() + { Connection conn = null; PreparedStatement ps = null; - try { + try + { conn = cpds.getConnection(); ps = getStatementCreateTable(conn); ps.execute(); ps.close(); ps = getStatementUpdateFrom2_0Table(conn); ps.execute(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); - } finally { - if (ps != null) { - try { - ps.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); - } - } } - } - - public void clearProtections() { - Connection conn = null; - PreparedStatement ps = null; - try { - conn = cpds.getConnection(); - ps = getStatementDeleteAll(conn); - ps.executeUpdate(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); - } finally { - if (ps != null) { - try { - ps.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); - } - } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); } - } - - public void importProtections(List blocks) { - for (OwnedBlock ownedBlock : blocks) { - if (ownedBlock.playerName == null) { - continue; - } - protectBlock(ownedBlock.world, ownedBlock.x, ownedBlock.y, ownedBlock.z, ownedBlock.playerName); - } - } - - public List exportProtections() { - Connection conn = null; - PreparedStatement ps = null; - ResultSet rs = null; - List blocks = new ArrayList(); - try { - conn = cpds.getConnection(); - ps = getStatementAllBlocks(conn); - rs = ps.executeQuery(); - while (rs.next()) { - OwnedBlock ob = new OwnedBlock(); - ob.world = rs.getString(1); - ob.x = rs.getInt(2); - ob.y = rs.getInt(3); - ob.z = rs.getInt(4); - ob.playerName = rs.getString(5); - blocks.add(ob); - } - return blocks; - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); - return blocks; - } finally { - if (rs != null) { - try { - rs.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); - } - } - if (ps != null) { - try { + finally + { + if (ps != null) + { + try + { ps.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); } } - if (conn != null) { - try { + if (conn != null) + { + try + { conn.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); } - } - } - } - - public void protectBlock(Block block, String playerName) { - protectBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), playerName); - } - - private void protectBlock(String world, int x, int y, int z, String playerName) { - Connection conn = null; - PreparedStatement ps = null; - try { - conn = cpds.getConnection(); - ps = getStatementInsert(conn, world, x, y, z, playerName); - ps.executeUpdate(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); - } finally { - if (ps != null) { - try { - ps.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); - } - } - } - } - - public boolean isProtected(Block block, String playerName) { - Connection conn = null; - PreparedStatement ps = null; - ResultSet rs = null; - try { - conn = cpds.getConnection(); - ps = getStatementPlayerCountByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), playerName); - rs = ps.executeQuery(); - rs.next(); - return rs.getInt(1) > 0 && rs.getInt(2) == 0; - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); - return true; - } finally { - if (rs != null) { - try { - rs.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); - } - } - if (ps != null) { - try { - ps.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); - } - } - } - } - - public List getOwners(Block block) { - Connection conn = null; - PreparedStatement ps = null; - ResultSet rs = null; - List owners = new ArrayList(); - try { - conn = cpds.getConnection(); - ps = getStatementPlayersByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ()); - rs = ps.executeQuery(); - while (rs.next()) { - owners.add(rs.getString(1)); - } - return owners; - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); - return owners; - } finally { - if (rs != null) { - try { - rs.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); - } - } - if (ps != null) { - try { - ps.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); } } } } - public int unprotectBlock(Block block) { + public void clearProtections() + { Connection conn = null; PreparedStatement ps = null; - try { + try + { conn = cpds.getConnection(); - ps = getStatementDeleteByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ()); - return ps.executeUpdate(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); - return 0; - } finally { - if (ps != null) { - try { + ps = getStatementDeleteAll(conn); + ps.executeUpdate(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + finally + { + if (ps != null) + { + try + { ps.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); } } - if (conn != null) { - try { + if (conn != null) + { + try + { conn.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + } + } + + public void importProtections(List blocks) + { + for (OwnedBlock ownedBlock : blocks) + { + if (ownedBlock.playerName == null) + { + continue; + } + protectBlock(ownedBlock.world, ownedBlock.x, ownedBlock.y, ownedBlock.z, ownedBlock.playerName); + } + } + + public List exportProtections() + { + Connection conn = null; + PreparedStatement ps = null; + ResultSet rs = null; + List blocks = new ArrayList(); + try + { + conn = cpds.getConnection(); + ps = getStatementAllBlocks(conn); + rs = ps.executeQuery(); + while (rs.next()) + { + OwnedBlock ob = new OwnedBlock( + rs.getInt(2), + rs.getInt(3), + rs.getInt(4), + rs.getString(1), + rs.getString(5)); + blocks.add(ob); + } + return blocks; + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + return blocks; + } + finally + { + if (rs != null) + { + try + { + rs.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + if (ps != null) + { + try + { + ps.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + if (conn != null) + { + try + { + conn.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + } + } + + public void protectBlock(Block block, String playerName) + { + protectBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), playerName); + } + + private void protectBlock(String world, int x, int y, int z, String playerName) + { + Connection conn = null; + PreparedStatement ps = null; + try + { + conn = cpds.getConnection(); + ps = getStatementInsert(conn, world, x, y, z, playerName); + ps.executeUpdate(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + finally + { + if (ps != null) + { + try + { + ps.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + if (conn != null) + { + try + { + conn.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + } + } + + public boolean isProtected(Block block, String playerName) + { + Connection conn = null; + PreparedStatement ps = null; + ResultSet rs = null; + try + { + conn = cpds.getConnection(); + ps = getStatementPlayerCountByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), playerName); + rs = ps.executeQuery(); + rs.next(); + return rs.getInt(1) > 0 && rs.getInt(2) == 0; + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + return true; + } + finally + { + if (rs != null) + { + try + { + rs.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + if (ps != null) + { + try + { + ps.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + if (conn != null) + { + try + { + conn.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + } + } + + public List getOwners(Block block) + { + Connection conn = null; + PreparedStatement ps = null; + ResultSet rs = null; + List owners = new ArrayList(); + try + { + conn = cpds.getConnection(); + ps = getStatementPlayersByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ()); + rs = ps.executeQuery(); + while (rs.next()) + { + owners.add(rs.getString(1)); + } + return owners; + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + return owners; + } + finally + { + if (rs != null) + { + try + { + rs.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + if (ps != null) + { + try + { + ps.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + if (conn != null) + { + try + { + conn.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + } + } + + public int unprotectBlock(Block block) + { + Connection conn = null; + PreparedStatement ps = null; + try + { + conn = cpds.getConnection(); + ps = getStatementDeleteByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ()); + return ps.executeUpdate(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + return 0; + } + finally + { + if (ps != null) + { + try + { + ps.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + if (conn != null) + { + try + { + conn.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); } } } } - } diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java index 53dee66d2..2fd32b026 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java @@ -4,34 +4,39 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Map.Entry; import java.util.Set; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.plugin.Plugin; -public class ProtectedBlockMemory implements IProtectedBlock { - List worlds = new ArrayList(); - List playerNames = new ArrayList(); - IProtectedBlock storage; - Plugin plugin; +public class ProtectedBlockMemory implements IProtectedBlock +{ + private final transient List worlds = new ArrayList(); + private final transient List playerNames = new ArrayList(); + private final transient IProtectedBlock storage; + private final transient Plugin plugin; - class ProtectedLocation { - int x; - int y; - int z; - int w; + static class ProtectedLocation + { + private final transient int x; + private final transient int y; + private final transient int z; + private final transient int w; - private ProtectedLocation(Block block, int worldId) { + public ProtectedLocation(final Block block, final int worldId) + { this.x = block.getX(); this.y = block.getY(); this.z = block.getZ(); this.w = worldId; } - private ProtectedLocation(OwnedBlock ownedBlock, int worldId) { + public ProtectedLocation(final OwnedBlock ownedBlock, final int worldId) + { this.x = ownedBlock.x; this.y = ownedBlock.y; this.z = ownedBlock.z; @@ -39,33 +44,39 @@ public class ProtectedBlockMemory implements IProtectedBlock { } @Override - public boolean equals(Object o) { - if (o instanceof ProtectedLocation) { - ProtectedLocation pl = (ProtectedLocation) o; - return x == pl.x && y == pl.y && z == pl.z && w == pl.w; + public boolean equals(final Object object) + { + if (object instanceof ProtectedLocation) + { + final ProtectedLocation pLoc = (ProtectedLocation)object; + return x == pLoc.x && y == pLoc.y && z == pLoc.z && w == pLoc.w; } return false; } @Override - public int hashCode() { + public int hashCode() + { return x ^ y ^ z ^ w; } } - class ProtectedBy { - private int playerId = -1; - private Set playerIds; + static class ProtectedBy + { + private transient int playerId = -1; + private transient Set playerIds; - private ProtectedBy() { - } - - private void add(int playerId) { - if (this.playerId == -1 || this.playerId == playerId) { + public void add(final int playerId) + { + if (this.playerId == -1 || this.playerId == playerId) + { this.playerId = playerId; - } else { - if (playerIds == null) { + } + else + { + if (playerIds == null) + { playerIds = new HashSet(4); playerIds.add(this.playerId); } @@ -73,130 +84,162 @@ public class ProtectedBlockMemory implements IProtectedBlock { } } - private boolean contains(int playerId) { - if (playerIds == null) { + public boolean contains(final int playerId) + { + if (playerIds == null) + { return this.playerId == playerId; } return playerIds.contains(playerId); } - private List getPlayers(List playerNames) { - if (playerIds == null) { - List list = new ArrayList(2); + public List getPlayers(final List playerNames) + { + final List list = new ArrayList(2); + if (playerIds == null) + { list.add(playerNames.get(playerId)); - return list; } - List list = new ArrayList(playerIds.size()); - for (Integer integer : playerIds) { - list.add(playerNames.get(integer)); + else + { + for (Integer integer : playerIds) + { + list.add(playerNames.get(integer)); + } } return list; } - private int size() { - if (playerIds == null) { + public int size() + { + if (playerIds == null) + { return 1; } return playerIds.size(); } } - HashMap blocks = new HashMap(); + private final transient Map blocks = new HashMap(); - public ProtectedBlockMemory(IProtectedBlock storage) { + public ProtectedBlockMemory(final IProtectedBlock storage, final Plugin plugin) + { this.storage = storage; + this.plugin = plugin; importProtections(storage.exportProtections()); } - public void clearProtections() { + public void clearProtections() + { blocks.clear(); } - public final void importProtections(List blocks) { - for (OwnedBlock ownedBlock : blocks) { - ProtectedLocation pl = new ProtectedLocation(ownedBlock, getWorldId(ownedBlock.world)); - if (ownedBlock.playerName == null) { + public final void importProtections(final List blocks) + { + for (OwnedBlock ownedBlock : blocks) + { + final ProtectedLocation pl = new ProtectedLocation(ownedBlock, getWorldId(ownedBlock.world)); + if (ownedBlock.playerName == null) + { continue; } protectBlock(pl, ownedBlock.playerName); } } - public List exportProtections() { - List blockList = new ArrayList(blocks.size()); - for (Entry entry : blocks.entrySet()) { - for (String name : entry.getValue().getPlayers(playerNames)) { - OwnedBlock ob = new OwnedBlock(); - ob.x = entry.getKey().x; - ob.y = entry.getKey().y; - ob.z = entry.getKey().z; - ob.world = worlds.get(entry.getKey().w); - ob.playerName = name; + public List exportProtections() + { + final List blockList = new ArrayList(blocks.size()); + for (Entry entry : blocks.entrySet()) + { + for (String name : entry.getValue().getPlayers(playerNames)) + { + final OwnedBlock ob = new OwnedBlock( + entry.getKey().x, + entry.getKey().y, + entry.getKey().z, + worlds.get(entry.getKey().w), + name); blockList.add(ob); } } return blockList; } - public void protectBlock(final Block block, final String playerName) { - ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld())); + public void protectBlock(final Block block, final String playerName) + { + final ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld())); protectBlock(pl, playerName); - plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() { - public void run() { + plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() + { + public void run() + { storage.protectBlock(block, playerName); } }); } - private void protectBlock(ProtectedLocation pl, String playerName) { + private final void protectBlock(ProtectedLocation pl, String playerName) + { int playerId = getPlayerId(playerName); ProtectedBy pb = blocks.get(pl); - if (pb == null) { + if (pb == null) + { pb = new ProtectedBy(); blocks.put(pl, pb); } pb.add(playerId); } - public boolean isProtected(Block block, String playerName) { + public boolean isProtected(Block block, String playerName) + { int playerId = getPlayerId(playerName); ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld())); ProtectedBy pb = blocks.get(pl); return !pb.contains(playerId); } - public List getOwners(Block block) { + public List getOwners(Block block) + { ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld())); ProtectedBy pb = blocks.get(pl); return pb.getPlayers(playerNames); } - public int unprotectBlock(final Block block) { + public int unprotectBlock(final Block block) + { ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld())); ProtectedBy pb = blocks.remove(pl); - plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() { - public void run() { + plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() + { + public void run() + { storage.unprotectBlock(block); } }); return pb.size(); } - private int getPlayerId(String playername) { + private int getPlayerId(String playername) + { int id = playerNames.indexOf(playername); - if (id < 0) { + if (id < 0) + { playerNames.add(playername); id = playerNames.indexOf(playername); } return id; } - private int getWorldId(World world) { + private int getWorldId(World world) + { return getWorldId(world.getName()); } - private int getWorldId(String name) { + private int getWorldId(String name) + { int id = worlds.indexOf(name); - if (id < 0) { + if (id < 0) + { worlds.add(name); id = worlds.indexOf(name); } diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMySQL.java b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMySQL.java index c246aca91..8e50ce248 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMySQL.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMySQL.java @@ -5,6 +5,8 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.logging.Level; +import java.util.logging.Logger; public class ProtectedBlockMySQL extends ProtectedBlockJDBC { @@ -50,10 +52,24 @@ public class ProtectedBlockMySQL extends ProtectedBlockJDBC { } } finally { if (testRS != null) { - testRS.close(); + try + { + testRS.close(); + } + catch (SQLException ex) + { + Logger.getLogger(ProtectedBlockMySQL.class.getName()).log(Level.SEVERE, null, ex); + } } if (testPS != null) { - testPS.close(); + try + { + testPS.close(); + } + catch (SQLException ex) + { + Logger.getLogger(ProtectedBlockMySQL.class.getName()).log(Level.SEVERE, null, ex); + } } } }