From ec8c778f685de92c6cacd92d492009477c0f5768 Mon Sep 17 00:00:00 2001 From: Iaccidentally Date: Tue, 12 Feb 2013 18:37:40 -0500 Subject: Clean up protect :: transient is not needed :: remove unnecessary returns :: @Override all the things! --- .../essentials/protect/EssentialsConnect.java | 199 +++-- .../essentials/protect/EssentialsProtect.java | 295 +++---- .../protect/EssentialsProtectBlockListener.java | 821 ++++++++++---------- .../protect/EssentialsProtectEntityListener.java | 673 ++++++++-------- .../protect/EssentialsProtectPlayerListener.java | 104 +-- .../protect/EssentialsProtectWeatherListener.java | 97 ++- .../protect/data/ProtectedBlockJDBC.java | 852 ++++++++++----------- .../protect/data/ProtectedBlockMemory.java | 516 ++++++------- 8 files changed, 1771 insertions(+), 1786 deletions(-) (limited to 'EssentialsProtect/src/com/earth2me/essentials/protect') diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsConnect.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsConnect.java index e17264ca9..d52e12444 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsConnect.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsConnect.java @@ -1,102 +1,97 @@ -package com.earth2me.essentials.protect; - -import com.earth2me.essentials.IConf; -import com.earth2me.essentials.IEssentials; -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 static com.earth2me.essentials.I18n._; -import java.util.logging.Level; -import java.util.logging.Logger; -import org.bukkit.plugin.Plugin; - - -public class EssentialsConnect -{ - private static final Logger LOGGER = Logger.getLogger("Minecraft"); - private final transient IEssentials ess; - private final transient IProtect protect; - - public EssentialsConnect(Plugin essPlugin, Plugin essProtect) - { - if (!essProtect.getDescription().getVersion().equals(essPlugin.getDescription().getVersion())) - { - LOGGER.log(Level.WARNING, _("versionMismatchAll")); - } - ess = (IEssentials)essPlugin; - protect = (IProtect)essProtect; - ProtectReloader pr = new ProtectReloader(); - pr.reloadConfig(); - ess.addReloadListener(pr); - } - - public void onDisable() - { - } - - public IEssentials getEssentials() - { - return ess; - } - - - private class ProtectReloader implements IConf - { - @Override - public void reloadConfig() - { - if (protect.getStorage() != null) - { - protect.getStorage().onPluginDeactivation(); - } - for (ProtectConfig protectConfig : ProtectConfig.values()) - { - if (protectConfig.isList()) - { - protect.getSettingsList().put(protectConfig, ess.getSettings().getProtectList(protectConfig.getConfigName())); - } - else if (protectConfig.isString()) - { - protect.getSettingsString().put(protectConfig, ess.getSettings().getProtectString(protectConfig.getConfigName())); - } - else - { - protect.getSettingsBoolean().put(protectConfig, ess.getSettings().getProtectBoolean(protectConfig.getConfigName(), protectConfig.getDefaultValueBoolean())); - } - - } - - if (protect.getSettingString(ProtectConfig.datatype).equalsIgnoreCase("mysql")) - { - try - { - protect.setStorage(new ProtectedBlockMySQL( - protect.getSettingString(ProtectConfig.mysqlDB), - protect.getSettingString(ProtectConfig.dbUsername), - protect.getSettingString(ProtectConfig.dbPassword))); - } - catch (PropertyVetoException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - } - } - else - { - try - { - protect.setStorage(new ProtectedBlockSQLite("jdbc:sqlite:plugins/Essentials/EssentialsProtect.db")); - } - catch (PropertyVetoException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - } - } - if (protect.getSettingBool(ProtectConfig.memstore)) - { - protect.setStorage(new ProtectedBlockMemory(protect.getStorage(), protect)); - } - - } - } -} +package com.earth2me.essentials.protect; + +import static com.earth2me.essentials.I18n._; +import com.earth2me.essentials.IConf; +import com.earth2me.essentials.IEssentials; +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.logging.Level; +import java.util.logging.Logger; +import org.bukkit.plugin.Plugin; + + +public class EssentialsConnect +{ + private static final Logger LOGGER = Logger.getLogger("Minecraft"); + private final IEssentials ess; + private final IProtect protect; + + public EssentialsConnect(Plugin essPlugin, Plugin essProtect) + { + if (!essProtect.getDescription().getVersion().equals(essPlugin.getDescription().getVersion())) + { + LOGGER.log(Level.WARNING, _("versionMismatchAll")); + } + ess = (IEssentials)essPlugin; + protect = (IProtect)essProtect; + ProtectReloader pr = new ProtectReloader(); + pr.reloadConfig(); + ess.addReloadListener(pr); + } + + public IEssentials getEssentials() + { + return ess; + } + + private class ProtectReloader implements IConf + { + @Override + public void reloadConfig() + { + if (protect.getStorage() != null) + { + protect.getStorage().onPluginDeactivation(); + } + for (ProtectConfig protectConfig : ProtectConfig.values()) + { + if (protectConfig.isList()) + { + protect.getSettingsList().put(protectConfig, ess.getSettings().getProtectList(protectConfig.getConfigName())); + } + else if (protectConfig.isString()) + { + protect.getSettingsString().put(protectConfig, ess.getSettings().getProtectString(protectConfig.getConfigName())); + } + else + { + protect.getSettingsBoolean().put(protectConfig, ess.getSettings().getProtectBoolean(protectConfig.getConfigName(), protectConfig.getDefaultValueBoolean())); + } + + } + + if (protect.getSettingString(ProtectConfig.datatype).equalsIgnoreCase("mysql")) + { + try + { + protect.setStorage(new ProtectedBlockMySQL( + protect.getSettingString(ProtectConfig.mysqlDB), + protect.getSettingString(ProtectConfig.dbUsername), + protect.getSettingString(ProtectConfig.dbPassword))); + } + catch (PropertyVetoException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + else + { + try + { + protect.setStorage(new ProtectedBlockSQLite("jdbc:sqlite:plugins/Essentials/EssentialsProtect.db")); + } + catch (PropertyVetoException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + if (protect.getSettingBool(ProtectConfig.memstore)) + { + protect.setStorage(new ProtectedBlockMemory(protect.getStorage(), protect)); + } + + } + } +} diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java index 0ccf6917f..712de076d 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java @@ -1,145 +1,150 @@ -package com.earth2me.essentials.protect; - -import com.earth2me.essentials.protect.data.IProtectedBlock; -import com.mchange.v2.log.MLevel; -import java.util.EnumMap; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.logging.Filter; -import java.util.logging.Level; -import java.util.logging.LogRecord; -import java.util.logging.Logger; -import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; -import org.bukkit.plugin.PluginManager; -import org.bukkit.plugin.java.JavaPlugin; - - -public class EssentialsProtect extends JavaPlugin implements IProtect -{ - private static final Logger LOGGER = Logger.getLogger("Minecraft"); - private static com.mchange.v2.log.MLogger C3P0logger; - 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; - private transient EssentialsConnect ess = null; - - @Override - public void onLoad() - { - try - { - // Simple fix for the case that log4j is on the class path by another plugin - Class propertyConfiguratorClass = Class.forName("org.apache.log4j.PropertyConfigurator"); - Properties properties = new Properties(); - properties.load(this.getClass().getResourceAsStream("log4j.properties")); - propertyConfiguratorClass.getMethod("configure", Properties.class).invoke(null, properties); - } - catch (Exception ex) - { - //Ignore me, log4j not found on classloader. - } - C3P0logger = com.mchange.v2.log.MLog.getLogger(com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.class); - C3P0logger.setLevel(MLevel.WARNING); - } - - public void onEnable() - { - final PluginManager pm = this.getServer().getPluginManager(); - final Plugin essPlugin = pm.getPlugin("Essentials"); - if (essPlugin == null || !essPlugin.isEnabled()) - { - enableEmergencyMode(pm); - return; - } - ess = new EssentialsConnect(essPlugin, this); - - final EssentialsProtectPlayerListener playerListener = new EssentialsProtectPlayerListener(this); - pm.registerEvents(playerListener, this); - - final EssentialsProtectBlockListener blockListener = new EssentialsProtectBlockListener(this); - pm.registerEvents(blockListener, this); - - final EssentialsProtectEntityListener entityListener = new EssentialsProtectEntityListener(this); - pm.registerEvents(entityListener, this); - - final EssentialsProtectWeatherListener weatherListener = new EssentialsProtectWeatherListener(this); - pm.registerEvents(weatherListener, this); - } - - private void enableEmergencyMode(final PluginManager pm) - { - final EmergencyListener emListener = new EmergencyListener(); - pm.registerEvents(emListener, this); - - for (Player player : getServer().getOnlinePlayers()) - { - player.sendMessage("Essentials Protect is in emergency mode. Check your log for errors."); - } - LOGGER.log(Level.SEVERE, "Essentials not installed or failed to load. Essenials Protect is in emergency mode now."); - } - - @Override - public IProtectedBlock getStorage() - { - return storage; - } - - @Override - public void setStorage(IProtectedBlock pb) - { - storage = pb; - } - - public EssentialsConnect getEssentialsConnect() - { - return ess; - } - - public Map getSettingsBoolean() - { - return settingsBoolean; - } - - public Map getSettingsString() - { - return settingsString; - } - - public Map> getSettingsList() - { - return settingsList; - } - - @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() - { - if (storage != null) - { - storage.onPluginDeactivation(); - } - // Sleep for a second to allow the database to close. - try - { - Thread.sleep(1000); - } - catch (InterruptedException ex) - { - } - } -} +package com.earth2me.essentials.protect; + +import com.earth2me.essentials.protect.data.IProtectedBlock; +import com.mchange.v2.log.MLevel; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.PluginManager; +import org.bukkit.plugin.java.JavaPlugin; + + +public class EssentialsProtect extends JavaPlugin implements IProtect +{ + private static final Logger LOGGER = Logger.getLogger("Minecraft"); + private static com.mchange.v2.log.MLogger C3P0logger; + private final Map settingsBoolean = new EnumMap(ProtectConfig.class); + private final Map settingsString = new EnumMap(ProtectConfig.class); + private final Map> settingsList = new EnumMap>(ProtectConfig.class); + private IProtectedBlock storage = null; + private EssentialsConnect ess = null; + + @Override + public void onLoad() + { + try + { + // Simple fix for the case that log4j is on the class path by another plugin + Class propertyConfiguratorClass = Class.forName("org.apache.log4j.PropertyConfigurator"); + Properties properties = new Properties(); + properties.load(this.getClass().getResourceAsStream("log4j.properties")); + propertyConfiguratorClass.getMethod("configure", Properties.class).invoke(null, properties); + } + catch (Exception ex) + { + //Ignore me, log4j not found on classloader. + } + C3P0logger = com.mchange.v2.log.MLog.getLogger(com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.class); + C3P0logger.setLevel(MLevel.WARNING); + } + + @Override + public void onEnable() + { + final PluginManager pm = this.getServer().getPluginManager(); + final Plugin essPlugin = pm.getPlugin("Essentials"); + if (essPlugin == null || !essPlugin.isEnabled()) + { + enableEmergencyMode(pm); + return; + } + ess = new EssentialsConnect(essPlugin, this); + + final EssentialsProtectPlayerListener playerListener = new EssentialsProtectPlayerListener(this); + pm.registerEvents(playerListener, this); + + final EssentialsProtectBlockListener blockListener = new EssentialsProtectBlockListener(this); + pm.registerEvents(blockListener, this); + + final EssentialsProtectEntityListener entityListener = new EssentialsProtectEntityListener(this); + pm.registerEvents(entityListener, this); + + final EssentialsProtectWeatherListener weatherListener = new EssentialsProtectWeatherListener(this); + pm.registerEvents(weatherListener, this); + } + + private void enableEmergencyMode(final PluginManager pm) + { + final EmergencyListener emListener = new EmergencyListener(); + pm.registerEvents(emListener, this); + + for (Player player : getServer().getOnlinePlayers()) + { + player.sendMessage("Essentials Protect is in emergency mode. Check your log for errors."); + } + LOGGER.log(Level.SEVERE, "Essentials not installed or failed to load. Essenials Protect is in emergency mode now."); + } + + @Override + public IProtectedBlock getStorage() + { + return storage; + } + + @Override + public void setStorage(IProtectedBlock pb) + { + storage = pb; + } + + @Override + public EssentialsConnect getEssentialsConnect() + { + return ess; + } + + @Override + public Map getSettingsBoolean() + { + return settingsBoolean; + } + + @Override + public Map getSettingsString() + { + return settingsString; + } + + @Override + public Map> getSettingsList() + { + return settingsList; + } + + @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; + } + + @Override + public void onDisable() + { + if (storage != null) + { + storage.onPluginDeactivation(); + } + // Sleep for a second to allow the database to close. + try + { + Thread.sleep(1000); + } + catch (InterruptedException ex) + { + Logger.getLogger(EssentialsProtect.class.getName()).log(Level.SEVERE, null, ex); + } + } +} diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java index 1791a9c5b..cca7cbef9 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java @@ -1,416 +1,405 @@ -package com.earth2me.essentials.protect; - -import com.earth2me.essentials.IEssentials; -import com.earth2me.essentials.User; -import com.earth2me.essentials.protect.data.IProtectedBlock; -import java.util.ArrayList; -import java.util.List; -import org.bukkit.Material; -import org.bukkit.block.Block; -import org.bukkit.block.BlockFace; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.block.*; - - -public class EssentialsProtectBlockListener implements Listener -{ - final private transient IProtect prot; - final private transient IEssentials ess; - - public EssentialsProtectBlockListener(final IProtect parent) - { - this.prot = parent; - this.ess = prot.getEssentialsConnect().getEssentials(); - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onBlockPlace(final BlockPlaceEvent event) - { - final User user = ess.getUser(event.getPlayer()); - - final Block blockPlaced = event.getBlockPlaced(); - final int id = blockPlaced.getTypeId(); - - - final Block below = blockPlaced.getRelative(BlockFace.DOWN); - if ((below.getType() == Material.RAILS || below.getType() == Material.POWERED_RAIL || below.getType() == Material.DETECTOR_RAIL) - && prot.getSettingBool(ProtectConfig.prevent_block_on_rail) - && isProtected(below, user)) - { - event.setCancelled(true); - return; - } - - final List protect = new ArrayList(); - if ((blockPlaced.getType() == Material.RAILS || blockPlaced.getType() == Material.POWERED_RAIL || blockPlaced.getType() == Material.DETECTOR_RAIL) - && prot.getSettingBool(ProtectConfig.protect_rails) - && user.isAuthorized("essentials.protect")) - { - protect.add(blockPlaced); - if (prot.getSettingBool(ProtectConfig.protect_below_rails) - && !isProtected(blockPlaced.getRelative(BlockFace.DOWN), user)) - { - protect.add(blockPlaced.getRelative(BlockFace.DOWN)); - } - } - 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) - && event.getBlockAgainst().getType() != Material.SIGN_POST - && event.getBlockAgainst().getType() != Material.WALL_SIGN - && !isProtected(event.getBlockAgainst(), user)) - { - protect.add(event.getBlockAgainst()); - } - } - for (Block block : protect) - { - prot.getStorage().protectBlock(block, user.getName()); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onBlockIgnite(BlockIgniteEvent event) - { - final Block block = event.getBlock(); - if ((block.getType() == Material.RAILS || block.getType() == Material.POWERED_RAIL || block.getType() == Material.DETECTOR_RAIL) - && prot.getSettingBool(ProtectConfig.protect_rails)) - { - event.setCancelled(true); - return; - } - 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().getRelative(BlockFace.DOWN).getType() == Material.OBSIDIAN) - { - event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_portal_creation)); - return; - } - - if (event.getCause().equals(BlockIgniteEvent.IgniteCause.SPREAD)) - { - event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_fire_spread)); - return; - } - - if (event.getCause().equals(BlockIgniteEvent.IgniteCause.FLINT_AND_STEEL)) - { - event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_flint_fire)); - return; - } - - if (event.getCause().equals(BlockIgniteEvent.IgniteCause.LAVA)) - { - event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_lava_fire_spread)); - return; - } - if (event.getCause().equals(BlockIgniteEvent.IgniteCause.LIGHTNING)) - { - event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_lightning_fire_spread)); - return; - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onBlockFromTo(final BlockFromToEvent event) - { - final Block toBlock = event.getToBlock(); - if ((toBlock.getType() == Material.RAILS || toBlock.getType() == Material.POWERED_RAIL || toBlock.getType() == Material.DETECTOR_RAIL) - && prot.getSettingBool(ProtectConfig.protect_rails)) - { - event.setCancelled(true); - return; - } - 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(prot.getSettingBool(ProtectConfig.prevent_water_flow)); - return; - } - - if (block.getType() == Material.LAVA || block.getType() == Material.STATIONARY_LAVA) - { - event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_lava_flow)); - return; - } - - if (block.getType() == Material.AIR) - { - event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_water_bucket_flow)); - return; - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onBlockBurn(final BlockBurnEvent event) - { - final Block block = event.getBlock(); - if ((block.getType() == Material.RAILS || block.getType() == Material.POWERED_RAIL || block.getType() == Material.DETECTOR_RAIL) && prot.getSettingBool(ProtectConfig.protect_rails)) - { - event.setCancelled(true); - return; - } - if ((block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST) - && prot.getSettingBool(ProtectConfig.protect_signs)) - { - event.setCancelled(true); - return; - } - 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, - BlockFace.UP, - BlockFace.DOWN, - BlockFace.SELF - }; - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onBlockBreak(final BlockBreakEvent event) - { - final User user = ess.getUser(event.getPlayer()); - - final Block block = event.getBlock(); - final int typeId = block.getTypeId(); - - final Material type = block.getType(); - - final IProtectedBlock storage = prot.getStorage(); - - if (user.isAuthorized("essentials.protect.admin")) - { - if (type == Material.WALL_SIGN || type == Material.SIGN_POST || type == Material.RAILS || type == Material.POWERED_RAIL || type == Material.DETECTOR_RAIL) - { - storage.unprotectBlock(block); - if (type == Material.RAILS || type == Material.POWERED_RAIL || type == Material.DETECTOR_RAIL || type == Material.SIGN_POST) - { - final Block below = block.getRelative(BlockFace.DOWN); - storage.unprotectBlock(below); - } - else - { - for (BlockFace blockFace : faces) - { - final Block against = block.getRelative(blockFace); - storage.unprotectBlock(against); - } - } - } - else - { - for (BlockFace blockFace : faces) - { - final Block against = block.getRelative(blockFace); - storage.unprotectBlock(against); - } - } - } - else - { - - final boolean isProtected = isProtected(block, user); - if (isProtected) - { - event.setCancelled(true); - } - else - { - if (type == Material.WALL_SIGN || type == Material.SIGN_POST || type == Material.RAILS || type == Material.POWERED_RAIL || type == Material.DETECTOR_RAIL) - { - storage.unprotectBlock(block); - if (type == Material.RAILS || type == Material.POWERED_RAIL || type == Material.DETECTOR_RAIL || type == Material.SIGN_POST) - { - final Block below = block.getRelative(BlockFace.DOWN); - storage.unprotectBlock(below); - } - else - { - for (BlockFace blockFace : faces) - { - final Block against = block.getRelative(blockFace); - storage.unprotectBlock(against); - } - } - } - else - { - for (BlockFace blockFace : faces) - { - final Block against = block.getRelative(blockFace); - storage.unprotectBlock(against); - } - } - } - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onBlockPistonExtend(BlockPistonExtendEvent event) - { - for (Block block : event.getBlocks()) - { - if ((block.getRelative(BlockFace.UP).getType() == Material.RAILS - || block.getType() == Material.RAILS - || block.getRelative(BlockFace.UP).getType() == Material.POWERED_RAIL - || block.getType() == Material.POWERED_RAIL - || block.getRelative(BlockFace.UP).getType() == Material.DETECTOR_RAIL - || block.getType() == Material.DETECTOR_RAIL) - && prot.getSettingBool(ProtectConfig.protect_rails)) - { - event.setCancelled(true); - return; - } - if (prot.getSettingBool(ProtectConfig.protect_signs)) - { - for (BlockFace blockFace : faces) - { - if (blockFace == BlockFace.DOWN) - { - continue; - } - final Block sign = block.getRelative(blockFace); - if ((blockFace == BlockFace.UP || blockFace == BlockFace.SELF) - && sign.getType() == Material.SIGN_POST) - { - event.setCancelled(true); - return; - } - if ((blockFace == BlockFace.NORTH || blockFace == BlockFace.EAST - || blockFace == BlockFace.SOUTH || blockFace == BlockFace.WEST - || blockFace == BlockFace.SELF) - && sign.getType() == Material.WALL_SIGN) - { - event.setCancelled(true); - return; - } - } - } - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onBlockPistonRetract(BlockPistonRetractEvent event) - { - if (!event.isSticky()) - { - return; - } - final Block block = event.getRetractLocation().getBlock(); - if ((block.getRelative(BlockFace.UP).getType() == Material.RAILS - || block.getType() == Material.RAILS - || block.getRelative(BlockFace.UP).getType() == Material.POWERED_RAIL - || block.getType() == Material.POWERED_RAIL - || block.getRelative(BlockFace.UP).getType() == Material.DETECTOR_RAIL - || block.getType() == Material.DETECTOR_RAIL) - && prot.getSettingBool(ProtectConfig.protect_rails)) - { - event.setCancelled(true); - return; - } - if (prot.getSettingBool(ProtectConfig.protect_signs)) - { - for (BlockFace blockFace : faces) - { - if (blockFace == BlockFace.DOWN) - { - continue; - } - final Block sign = block.getRelative(blockFace); - if ((blockFace == BlockFace.UP || blockFace == BlockFace.SELF) - && sign.getType() == Material.SIGN_POST) - { - event.setCancelled(true); - return; - } - if ((blockFace == BlockFace.NORTH || blockFace == BlockFace.EAST - || blockFace == BlockFace.SOUTH || blockFace == BlockFace.WEST - || blockFace == BlockFace.SELF) - && sign.getType() == Material.WALL_SIGN) - { - event.setCancelled(true); - return; - } - } - } - } - - private boolean isProtected(final Block block, final User user) - { - final Material type = block.getType(); - if (prot.getSettingBool(ProtectConfig.protect_signs)) - { - if (type == Material.WALL_SIGN || type == Material.SIGN_POST) - { - return prot.getStorage().isProtected(block, user.getName()); - } - if (prot.getSettingBool(ProtectConfig.protect_against_signs)) - { - final Block up = block.getRelative(BlockFace.UP); - if (up != null && up.getType() == Material.SIGN_POST) - { - return prot.getStorage().isProtected(block, user.getName()); - } - final BlockFace[] directions = new BlockFace[] - { - BlockFace.NORTH, - BlockFace.EAST, - BlockFace.SOUTH, - BlockFace.WEST - }; - for (BlockFace blockFace : directions) - { - final Block signblock = block.getRelative(blockFace); - if (signblock.getType() == Material.WALL_SIGN) - { - final org.bukkit.material.Sign signMat = (org.bukkit.material.Sign)signblock.getState().getData(); - if (signMat != null && signMat.getFacing() == blockFace) - { - return prot.getStorage().isProtected(block, user.getName()); - } - } - } - } - } - if (prot.getSettingBool(ProtectConfig.protect_rails)) - { - if (type == Material.RAILS || type == Material.POWERED_RAIL || type == Material.DETECTOR_RAIL) - { - return prot.getStorage().isProtected(block, user.getName()); - } - if (prot.getSettingBool(ProtectConfig.protect_below_rails)) - { - final Block up = block.getRelative(BlockFace.UP); - if (up != null && (type == Material.RAILS || type == Material.POWERED_RAIL || type == Material.DETECTOR_RAIL)) - { - return prot.getStorage().isProtected(block, user.getName()); - } - } - } - return false; - } -} +package com.earth2me.essentials.protect; + +import com.earth2me.essentials.IEssentials; +import com.earth2me.essentials.User; +import com.earth2me.essentials.protect.data.IProtectedBlock; +import java.util.ArrayList; +import java.util.List; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.*; + + +public class EssentialsProtectBlockListener implements Listener +{ + final private IProtect prot; + final private IEssentials ess; + + public EssentialsProtectBlockListener(final IProtect parent) + { + this.prot = parent; + this.ess = prot.getEssentialsConnect().getEssentials(); + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onBlockPlace(final BlockPlaceEvent event) + { + final User user = ess.getUser(event.getPlayer()); + final Block blockPlaced = event.getBlockPlaced(); + final Block below = blockPlaced.getRelative(BlockFace.DOWN); + if ((below.getType() == Material.RAILS || below.getType() == Material.POWERED_RAIL || below.getType() == Material.DETECTOR_RAIL) + && prot.getSettingBool(ProtectConfig.prevent_block_on_rail) + && isProtected(below, user)) + { + event.setCancelled(true); + return; + } + + final List protect = new ArrayList(); + if ((blockPlaced.getType() == Material.RAILS || blockPlaced.getType() == Material.POWERED_RAIL || blockPlaced.getType() == Material.DETECTOR_RAIL) + && prot.getSettingBool(ProtectConfig.protect_rails) + && user.isAuthorized("essentials.protect")) + { + protect.add(blockPlaced); + if (prot.getSettingBool(ProtectConfig.protect_below_rails) + && !isProtected(blockPlaced.getRelative(BlockFace.DOWN), user)) + { + protect.add(blockPlaced.getRelative(BlockFace.DOWN)); + } + } + 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) + && event.getBlockAgainst().getType() != Material.SIGN_POST + && event.getBlockAgainst().getType() != Material.WALL_SIGN + && !isProtected(event.getBlockAgainst(), user)) + { + protect.add(event.getBlockAgainst()); + } + } + for (Block block : protect) + { + prot.getStorage().protectBlock(block, user.getName()); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onBlockIgnite(BlockIgniteEvent event) + { + final Block block = event.getBlock(); + if ((block.getType() == Material.RAILS || block.getType() == Material.POWERED_RAIL || block.getType() == Material.DETECTOR_RAIL) + && prot.getSettingBool(ProtectConfig.protect_rails)) + { + event.setCancelled(true); + return; + } + 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().getRelative(BlockFace.DOWN).getType() == Material.OBSIDIAN) + { + event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_portal_creation)); + return; + } + + if (event.getCause().equals(BlockIgniteEvent.IgniteCause.SPREAD)) + { + event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_fire_spread)); + return; + } + + if (event.getCause().equals(BlockIgniteEvent.IgniteCause.FLINT_AND_STEEL)) + { + event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_flint_fire)); + return; + } + + if (event.getCause().equals(BlockIgniteEvent.IgniteCause.LAVA)) + { + event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_lava_fire_spread)); + return; + } + if (event.getCause().equals(BlockIgniteEvent.IgniteCause.LIGHTNING)) + { + event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_lightning_fire_spread)); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onBlockFromTo(final BlockFromToEvent event) + { + final Block toBlock = event.getToBlock(); + if ((toBlock.getType() == Material.RAILS || toBlock.getType() == Material.POWERED_RAIL || toBlock.getType() == Material.DETECTOR_RAIL) + && prot.getSettingBool(ProtectConfig.protect_rails)) + { + event.setCancelled(true); + return; + } + 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(prot.getSettingBool(ProtectConfig.prevent_water_flow)); + return; + } + + if (block.getType() == Material.LAVA || block.getType() == Material.STATIONARY_LAVA) + { + event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_lava_flow)); + return; + } + + if (block.getType() == Material.AIR) + { + event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_water_bucket_flow)); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onBlockBurn(final BlockBurnEvent event) + { + final Block block = event.getBlock(); + if ((block.getType() == Material.RAILS || block.getType() == Material.POWERED_RAIL || block.getType() == Material.DETECTOR_RAIL) && prot.getSettingBool(ProtectConfig.protect_rails)) + { + event.setCancelled(true); + return; + } + if ((block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST) + && prot.getSettingBool(ProtectConfig.protect_signs)) + { + event.setCancelled(true); + return; + } + if (prot.getSettingBool(ProtectConfig.prevent_fire_spread)) + { + event.setCancelled(true); + } + } + private final static BlockFace[] faces = new BlockFace[] + { + BlockFace.NORTH, + BlockFace.EAST, + BlockFace.SOUTH, + BlockFace.WEST, + BlockFace.UP, + BlockFace.DOWN, + BlockFace.SELF + }; + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onBlockBreak(final BlockBreakEvent event) + { + final User user = ess.getUser(event.getPlayer()); + final Block block = event.getBlock(); + final Material type = block.getType(); + final IProtectedBlock storage = prot.getStorage(); + + if (user.isAuthorized("essentials.protect.admin")) + { + if (type == Material.WALL_SIGN || type == Material.SIGN_POST || type == Material.RAILS || type == Material.POWERED_RAIL || type == Material.DETECTOR_RAIL) + { + storage.unprotectBlock(block); + if (type == Material.RAILS || type == Material.POWERED_RAIL || type == Material.DETECTOR_RAIL || type == Material.SIGN_POST) + { + final Block below = block.getRelative(BlockFace.DOWN); + storage.unprotectBlock(below); + } + else + { + for (BlockFace blockFace : faces) + { + final Block against = block.getRelative(blockFace); + storage.unprotectBlock(against); + } + } + } + else + { + for (BlockFace blockFace : faces) + { + final Block against = block.getRelative(blockFace); + storage.unprotectBlock(against); + } + } + } + else + { + + final boolean isProtected = isProtected(block, user); + if (isProtected) + { + event.setCancelled(true); + } + else + { + if (type == Material.WALL_SIGN || type == Material.SIGN_POST || type == Material.RAILS || type == Material.POWERED_RAIL || type == Material.DETECTOR_RAIL) + { + storage.unprotectBlock(block); + if (type == Material.RAILS || type == Material.POWERED_RAIL || type == Material.DETECTOR_RAIL || type == Material.SIGN_POST) + { + final Block below = block.getRelative(BlockFace.DOWN); + storage.unprotectBlock(below); + } + else + { + for (BlockFace blockFace : faces) + { + final Block against = block.getRelative(blockFace); + storage.unprotectBlock(against); + } + } + } + else + { + for (BlockFace blockFace : faces) + { + final Block against = block.getRelative(blockFace); + storage.unprotectBlock(against); + } + } + } + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onBlockPistonExtend(BlockPistonExtendEvent event) + { + for (Block block : event.getBlocks()) + { + if ((block.getRelative(BlockFace.UP).getType() == Material.RAILS + || block.getType() == Material.RAILS + || block.getRelative(BlockFace.UP).getType() == Material.POWERED_RAIL + || block.getType() == Material.POWERED_RAIL + || block.getRelative(BlockFace.UP).getType() == Material.DETECTOR_RAIL + || block.getType() == Material.DETECTOR_RAIL) + && prot.getSettingBool(ProtectConfig.protect_rails)) + { + event.setCancelled(true); + return; + } + if (prot.getSettingBool(ProtectConfig.protect_signs)) + { + for (BlockFace blockFace : faces) + { + if (blockFace == BlockFace.DOWN) + { + continue; + } + final Block sign = block.getRelative(blockFace); + if ((blockFace == BlockFace.UP || blockFace == BlockFace.SELF) + && sign.getType() == Material.SIGN_POST) + { + event.setCancelled(true); + return; + } + if ((blockFace == BlockFace.NORTH || blockFace == BlockFace.EAST + || blockFace == BlockFace.SOUTH || blockFace == BlockFace.WEST + || blockFace == BlockFace.SELF) + && sign.getType() == Material.WALL_SIGN) + { + event.setCancelled(true); + return; + } + } + } + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onBlockPistonRetract(BlockPistonRetractEvent event) + { + if (!event.isSticky()) + { + return; + } + final Block block = event.getRetractLocation().getBlock(); + if ((block.getRelative(BlockFace.UP).getType() == Material.RAILS + || block.getType() == Material.RAILS + || block.getRelative(BlockFace.UP).getType() == Material.POWERED_RAIL + || block.getType() == Material.POWERED_RAIL + || block.getRelative(BlockFace.UP).getType() == Material.DETECTOR_RAIL + || block.getType() == Material.DETECTOR_RAIL) + && prot.getSettingBool(ProtectConfig.protect_rails)) + { + event.setCancelled(true); + return; + } + if (prot.getSettingBool(ProtectConfig.protect_signs)) + { + for (BlockFace blockFace : faces) + { + if (blockFace == BlockFace.DOWN) + { + continue; + } + final Block sign = block.getRelative(blockFace); + if ((blockFace == BlockFace.UP || blockFace == BlockFace.SELF) + && sign.getType() == Material.SIGN_POST) + { + event.setCancelled(true); + return; + } + if ((blockFace == BlockFace.NORTH || blockFace == BlockFace.EAST + || blockFace == BlockFace.SOUTH || blockFace == BlockFace.WEST + || blockFace == BlockFace.SELF) + && sign.getType() == Material.WALL_SIGN) + { + event.setCancelled(true); + return; + } + } + } + } + + private boolean isProtected(final Block block, final User user) + { + final Material type = block.getType(); + if (prot.getSettingBool(ProtectConfig.protect_signs)) + { + if (type == Material.WALL_SIGN || type == Material.SIGN_POST) + { + return prot.getStorage().isProtected(block, user.getName()); + } + if (prot.getSettingBool(ProtectConfig.protect_against_signs)) + { + final Block up = block.getRelative(BlockFace.UP); + if (up != null && up.getType() == Material.SIGN_POST) + { + return prot.getStorage().isProtected(block, user.getName()); + } + final BlockFace[] directions = new BlockFace[] + { + BlockFace.NORTH, + BlockFace.EAST, + BlockFace.SOUTH, + BlockFace.WEST + }; + for (BlockFace blockFace : directions) + { + final Block signblock = block.getRelative(blockFace); + if (signblock.getType() == Material.WALL_SIGN) + { + final org.bukkit.material.Sign signMat = (org.bukkit.material.Sign)signblock.getState().getData(); + if (signMat != null && signMat.getFacing() == blockFace) + { + return prot.getStorage().isProtected(block, user.getName()); + } + } + } + } + } + if (prot.getSettingBool(ProtectConfig.protect_rails)) + { + if (type == Material.RAILS || type == Material.POWERED_RAIL || type == Material.DETECTOR_RAIL) + { + return prot.getStorage().isProtected(block, user.getName()); + } + if (prot.getSettingBool(ProtectConfig.protect_below_rails)) + { + final Block up = block.getRelative(BlockFace.UP); + if (up != null && (type == Material.RAILS || type == Material.POWERED_RAIL || type == Material.DETECTOR_RAIL)) + { + return prot.getStorage().isProtected(block, user.getName()); + } + } + } + return false; + } +} diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectEntityListener.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectEntityListener.java index 4770248b9..d9102c023 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectEntityListener.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectEntityListener.java @@ -1,338 +1,335 @@ -package com.earth2me.essentials.protect; - -import com.earth2me.essentials.IEssentials; -import com.earth2me.essentials.User; -import java.util.Locale; -import org.bukkit.Material; -import org.bukkit.block.Block; -import org.bukkit.block.BlockFace; -import org.bukkit.entity.*; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.entity.*; -import org.bukkit.event.entity.EntityDamageEvent.DamageCause; -import org.bukkit.event.entity.EntityTargetEvent.TargetReason; - - -public class EssentialsProtectEntityListener implements Listener -{ - private final transient IProtect prot; - private final transient IEssentials ess; - - public EssentialsProtectEntityListener(final IProtect prot) - { - this.prot = prot; - this.ess = prot.getEssentialsConnect().getEssentials(); - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onEntityDamage(final EntityDamageEvent event) - { - final Entity target = event.getEntity(); - - if (target instanceof Villager && prot.getSettingBool(ProtectConfig.prevent_villager_death)) - { - event.setCancelled(true); - return; - } - - final User user = ess.getUser(target); - final DamageCause cause = event.getCause(); - - if (event instanceof EntityDamageByBlockEvent) - { - if (prot.getSettingBool(ProtectConfig.disable_contactdmg) - && cause == DamageCause.CONTACT - && !(target instanceof Player && shouldBeDamaged(user, "contact"))) - { - event.setCancelled(true); - return; - } - if (prot.getSettingBool(ProtectConfig.disable_lavadmg) - && cause == DamageCause.LAVA - && !(target instanceof Player && shouldBeDamaged(user, "lava"))) - { - event.setCancelled(true); - return; - } - if (prot.getSettingBool(ProtectConfig.prevent_tnt_explosion) - && cause == DamageCause.BLOCK_EXPLOSION - && !(target instanceof Player && shouldBeDamaged(user, "tnt"))) - { - event.setCancelled(true); - return; - } - } - - if (event instanceof EntityDamageByEntityEvent) - { - final EntityDamageByEntityEvent edEvent = (EntityDamageByEntityEvent)event; - final Entity eAttack = edEvent.getDamager(); - final User attacker = ess.getUser(eAttack); - - //Creeper explode prevention - if (eAttack instanceof Creeper - && (prot.getSettingBool(ProtectConfig.prevent_creeper_explosion) - || prot.getSettingBool(ProtectConfig.prevent_creeper_playerdmg)) - && !(target instanceof Player && shouldBeDamaged(user, "creeper"))) - { - event.setCancelled(true); - return; - } - - if ((event.getEntity() instanceof Fireball || event.getEntity() instanceof SmallFireball) - && prot.getSettingBool(ProtectConfig.prevent_fireball_playerdmg) - && !(target instanceof Player && shouldBeDamaged(user, "fireball"))) - { - event.setCancelled(true); - return; - } - - if (event.getEntity() instanceof WitherSkull - && prot.getSettingBool(ProtectConfig.prevent_witherskull_playerdmg) - && !(target instanceof Player && shouldBeDamaged(user, "witherskull"))) - { - event.setCancelled(true); - return; - } - - if (eAttack instanceof TNTPrimed && prot.getSettingBool(ProtectConfig.prevent_tnt_playerdmg) - && !(target instanceof Player && shouldBeDamaged(user, "tnt"))) - { - event.setCancelled(true); - return; - } - - // PVP Settings - if (target instanceof Player && eAttack instanceof Player - && prot.getSettingBool(ProtectConfig.disable_pvp) - && (!user.isAuthorized("essentials.protect.pvp") || !attacker.isAuthorized("essentials.protect.pvp"))) - { - event.setCancelled(true); - return; - } - - if (edEvent.getDamager() instanceof Projectile - && target instanceof Player - && ((prot.getSettingBool(ProtectConfig.disable_projectiles) && !shouldBeDamaged(user, "projectiles")) - || (((Projectile)edEvent.getDamager()).getShooter() instanceof Player - && prot.getSettingBool(ProtectConfig.disable_pvp) - && (!user.isAuthorized("essentials.protect.pvp") - || !ess.getUser(((Projectile)edEvent.getDamager()).getShooter()).isAuthorized("essentials.protect.pvp"))))) - { - event.setCancelled(true); - return; - } - } - - if (target instanceof Player) - { - if (cause == DamageCause.FALL - && prot.getSettingBool(ProtectConfig.disable_fall) - && !shouldBeDamaged(user, "fall")) - { - event.setCancelled(true); - return; - } - - if (cause == DamageCause.SUFFOCATION - && prot.getSettingBool(ProtectConfig.disable_suffocate) - && !shouldBeDamaged(user, "suffocation")) - { - event.setCancelled(true); - return; - } - if ((cause == DamageCause.FIRE || cause == DamageCause.FIRE_TICK) - && prot.getSettingBool(ProtectConfig.disable_firedmg) - && !shouldBeDamaged(user, "fire")) - { - event.setCancelled(true); - return; - } - if (cause == DamageCause.DROWNING - && prot.getSettingBool(ProtectConfig.disable_drown) - && !shouldBeDamaged(user, "drowning")) - { - event.setCancelled(true); - return; - } - if (cause == DamageCause.LIGHTNING - && prot.getSettingBool(ProtectConfig.disable_lightning) - && !shouldBeDamaged(user, "lightning")) - { - event.setCancelled(true); - return; - } - if (cause == DamageCause.WITHER - && prot.getSettingBool(ProtectConfig.disable_wither) - && !shouldBeDamaged(user, "wither")) - { - event.setCancelled(true); - return; - } - } - } - - private boolean shouldBeDamaged(final User user, final String type) - { - return (user.isAuthorized("essentials.protect.damage.".concat(type)) - && !user.isAuthorized("essentials.protect.damage.disable")); - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onEntityExplode(final EntityExplodeEvent event) - { - if (event.getEntity() == null) - { - return; - } - final int maxHeight = ess.getSettings().getProtectCreeperMaxHeight(); - - if (event.getEntity() instanceof EnderDragon - && prot.getSettingBool(ProtectConfig.prevent_enderdragon_blockdmg)) - { - event.setCancelled(true); - if (prot.getSettingBool(ProtectConfig.enderdragon_fakeexplosions)) - { - event.getLocation().getWorld().createExplosion(event.getLocation(), 0F); - } - return; - } - if (event.getEntity() instanceof Wither - && prot.getSettingBool(ProtectConfig.prevent_wither_spawnexplosion)) - { - event.setCancelled(true); - return; - } - else if (event.getEntity() instanceof Creeper - && (prot.getSettingBool(ProtectConfig.prevent_creeper_explosion) - || prot.getSettingBool(ProtectConfig.prevent_creeper_blockdmg) - || (maxHeight >= 0 && event.getLocation().getBlockY() > maxHeight))) - { - //Nicccccccccce plaaacccccccccce.. - event.setCancelled(true); - event.getLocation().getWorld().createExplosion(event.getLocation(), 0F); - return; - } - else if (event.getEntity() instanceof TNTPrimed - && prot.getSettingBool(ProtectConfig.prevent_tnt_explosion)) - { - event.setCancelled(true); - return; - } - else if ((event.getEntity() instanceof Fireball || event.getEntity() instanceof SmallFireball) - && prot.getSettingBool(ProtectConfig.prevent_fireball_explosion)) - { - event.setCancelled(true); - return; - } - else if ((event.getEntity() instanceof WitherSkull) - && prot.getSettingBool(ProtectConfig.prevent_witherskull_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.getRelative(BlockFace.UP).getType() == Material.RAILS - || block.getType() == Material.RAILS - || block.getRelative(BlockFace.UP).getType() == Material.POWERED_RAIL - || block.getType() == Material.POWERED_RAIL - || block.getRelative(BlockFace.UP).getType() == Material.DETECTOR_RAIL - || block.getType() == Material.DETECTOR_RAIL) - && prot.getSettingBool(ProtectConfig.protect_rails)) - { - event.setCancelled(true); - return; - } - if ((block.getType() == Material.WALL_SIGN - || block.getRelative(BlockFace.NORTH).getType() == Material.WALL_SIGN - || block.getRelative(BlockFace.EAST).getType() == Material.WALL_SIGN - || block.getRelative(BlockFace.SOUTH).getType() == Material.WALL_SIGN - || block.getRelative(BlockFace.WEST).getType() == Material.WALL_SIGN - || block.getType() == Material.SIGN_POST - || block.getRelative(BlockFace.UP).getType() == Material.SIGN_POST) - && prot.getSettingBool(ProtectConfig.protect_signs)) - { - event.setCancelled(true); - return; - } - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onCreatureSpawn(final CreatureSpawnEvent event) - { - if (event.getEntity() instanceof Player) - { - return; - } - final EntityType creature = event.getEntityType(); - if (creature == null) - { - return; - } - final String creatureName = creature.toString().toLowerCase(Locale.ENGLISH); - if (creatureName == null || creatureName.isEmpty()) - { - return; - } - if (ess.getSettings().getProtectPreventSpawn(creatureName)) - { - event.setCancelled(true); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onEntityTarget(final EntityTargetEvent event) - { - if (!(event.getTarget() instanceof Player)) - { - return; - } - 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 - || event.getReason() == TargetReason.RANDOM_TARGET - || event.getReason() == TargetReason.DEFEND_VILLAGE - || event.getReason() == TargetReason.TARGET_ATTACKED_OWNER - || event.getReason() == TargetReason.OWNER_ATTACKED_TARGET) - && prot.getSettingBool(ProtectConfig.prevent_entitytarget) - && !user.isAuthorized("essentials.protect.entitytarget.bypass")) - { - event.setCancelled(true); - return; - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onExplosionPrime(ExplosionPrimeEvent event) - { - if ((event.getEntity() instanceof Fireball || event.getEntity() instanceof SmallFireball) - && prot.getSettingBool(ProtectConfig.prevent_fireball_fire)) - { - event.setFire(false); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onEntityChangeBlock(EntityChangeBlockEvent event) - { - if (event.getEntityType() == EntityType.ENDERMAN && prot.getSettingBool(ProtectConfig.prevent_enderman_pickup)) - { - event.setCancelled(true); - return; - } - if (event.getEntityType() == EntityType.WITHER && prot.getSettingBool(ProtectConfig.prevent_wither_blockreplace)) - { - event.setCancelled(true); - return; - } - } -} +package com.earth2me.essentials.protect; + +import com.earth2me.essentials.IEssentials; +import com.earth2me.essentials.User; +import java.util.Locale; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.entity.*; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.*; +import org.bukkit.event.entity.EntityDamageEvent.DamageCause; +import org.bukkit.event.entity.EntityTargetEvent.TargetReason; + + +public class EssentialsProtectEntityListener implements Listener +{ + private final IProtect prot; + private final IEssentials ess; + + public EssentialsProtectEntityListener(final IProtect prot) + { + this.prot = prot; + this.ess = prot.getEssentialsConnect().getEssentials(); + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onEntityDamage(final EntityDamageEvent event) + { + final Entity target = event.getEntity(); + + if (target instanceof Villager && prot.getSettingBool(ProtectConfig.prevent_villager_death)) + { + event.setCancelled(true); + return; + } + + final User user = ess.getUser(target); + final DamageCause cause = event.getCause(); + + if (event instanceof EntityDamageByBlockEvent) + { + if (prot.getSettingBool(ProtectConfig.disable_contactdmg) + && cause == DamageCause.CONTACT + && !(target instanceof Player && shouldBeDamaged(user, "contact"))) + { + event.setCancelled(true); + return; + } + if (prot.getSettingBool(ProtectConfig.disable_lavadmg) + && cause == DamageCause.LAVA + && !(target instanceof Player && shouldBeDamaged(user, "lava"))) + { + event.setCancelled(true); + return; + } + if (prot.getSettingBool(ProtectConfig.prevent_tnt_explosion) + && cause == DamageCause.BLOCK_EXPLOSION + && !(target instanceof Player && shouldBeDamaged(user, "tnt"))) + { + event.setCancelled(true); + return; + } + } + + if (event instanceof EntityDamageByEntityEvent) + { + final EntityDamageByEntityEvent edEvent = (EntityDamageByEntityEvent)event; + final Entity eAttack = edEvent.getDamager(); + final User attacker = ess.getUser(eAttack); + + //Creeper explode prevention + if (eAttack instanceof Creeper + && (prot.getSettingBool(ProtectConfig.prevent_creeper_explosion) + || prot.getSettingBool(ProtectConfig.prevent_creeper_playerdmg)) + && !(target instanceof Player && shouldBeDamaged(user, "creeper"))) + { + event.setCancelled(true); + return; + } + + if ((event.getEntity() instanceof Fireball || event.getEntity() instanceof SmallFireball) + && prot.getSettingBool(ProtectConfig.prevent_fireball_playerdmg) + && !(target instanceof Player && shouldBeDamaged(user, "fireball"))) + { + event.setCancelled(true); + return; + } + + if (event.getEntity() instanceof WitherSkull + && prot.getSettingBool(ProtectConfig.prevent_witherskull_playerdmg) + && !(target instanceof Player && shouldBeDamaged(user, "witherskull"))) + { + event.setCancelled(true); + return; + } + + if (eAttack instanceof TNTPrimed && prot.getSettingBool(ProtectConfig.prevent_tnt_playerdmg) + && !(target instanceof Player && shouldBeDamaged(user, "tnt"))) + { + event.setCancelled(true); + return; + } + + // PVP Settings + if (target instanceof Player && eAttack instanceof Player + && prot.getSettingBool(ProtectConfig.disable_pvp) + && (!user.isAuthorized("essentials.protect.pvp") || !attacker.isAuthorized("essentials.protect.pvp"))) + { + event.setCancelled(true); + return; + } + + if (edEvent.getDamager() instanceof Projectile + && target instanceof Player + && ((prot.getSettingBool(ProtectConfig.disable_projectiles) && !shouldBeDamaged(user, "projectiles")) + || (((Projectile)edEvent.getDamager()).getShooter() instanceof Player + && prot.getSettingBool(ProtectConfig.disable_pvp) + && (!user.isAuthorized("essentials.protect.pvp") + || !ess.getUser(((Projectile)edEvent.getDamager()).getShooter()).isAuthorized("essentials.protect.pvp"))))) + { + event.setCancelled(true); + return; + } + } + + if (target instanceof Player) + { + if (cause == DamageCause.FALL + && prot.getSettingBool(ProtectConfig.disable_fall) + && !shouldBeDamaged(user, "fall")) + { + event.setCancelled(true); + return; + } + + if (cause == DamageCause.SUFFOCATION + && prot.getSettingBool(ProtectConfig.disable_suffocate) + && !shouldBeDamaged(user, "suffocation")) + { + event.setCancelled(true); + return; + } + if ((cause == DamageCause.FIRE || cause == DamageCause.FIRE_TICK) + && prot.getSettingBool(ProtectConfig.disable_firedmg) + && !shouldBeDamaged(user, "fire")) + { + event.setCancelled(true); + return; + } + if (cause == DamageCause.DROWNING + && prot.getSettingBool(ProtectConfig.disable_drown) + && !shouldBeDamaged(user, "drowning")) + { + event.setCancelled(true); + return; + } + if (cause == DamageCause.LIGHTNING + && prot.getSettingBool(ProtectConfig.disable_lightning) + && !shouldBeDamaged(user, "lightning")) + { + event.setCancelled(true); + return; + } + if (cause == DamageCause.WITHER + && prot.getSettingBool(ProtectConfig.disable_wither) + && !shouldBeDamaged(user, "wither")) + { + event.setCancelled(true); + } + } + } + + private boolean shouldBeDamaged(final User user, final String type) + { + return (user.isAuthorized("essentials.protect.damage.".concat(type)) + && !user.isAuthorized("essentials.protect.damage.disable")); + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onEntityExplode(final EntityExplodeEvent event) + { + if (event.getEntity() == null) + { + return; + } + final int maxHeight = ess.getSettings().getProtectCreeperMaxHeight(); + + if (event.getEntity() instanceof EnderDragon + && prot.getSettingBool(ProtectConfig.prevent_enderdragon_blockdmg)) + { + event.setCancelled(true); + if (prot.getSettingBool(ProtectConfig.enderdragon_fakeexplosions)) + { + event.getLocation().getWorld().createExplosion(event.getLocation(), 0F); + } + return; + } + if (event.getEntity() instanceof Wither + && prot.getSettingBool(ProtectConfig.prevent_wither_spawnexplosion)) + { + event.setCancelled(true); + return; + } + else if (event.getEntity() instanceof Creeper + && (prot.getSettingBool(ProtectConfig.prevent_creeper_explosion) + || prot.getSettingBool(ProtectConfig.prevent_creeper_blockdmg) + || (maxHeight >= 0 && event.getLocation().getBlockY() > maxHeight))) + { + //Nicccccccccce plaaacccccccccce.. + event.setCancelled(true); + event.getLocation().getWorld().createExplosion(event.getLocation(), 0F); + return; + } + else if (event.getEntity() instanceof TNTPrimed + && prot.getSettingBool(ProtectConfig.prevent_tnt_explosion)) + { + event.setCancelled(true); + return; + } + else if ((event.getEntity() instanceof Fireball || event.getEntity() instanceof SmallFireball) + && prot.getSettingBool(ProtectConfig.prevent_fireball_explosion)) + { + event.setCancelled(true); + return; + } + else if ((event.getEntity() instanceof WitherSkull) + && prot.getSettingBool(ProtectConfig.prevent_witherskull_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.getRelative(BlockFace.UP).getType() == Material.RAILS + || block.getType() == Material.RAILS + || block.getRelative(BlockFace.UP).getType() == Material.POWERED_RAIL + || block.getType() == Material.POWERED_RAIL + || block.getRelative(BlockFace.UP).getType() == Material.DETECTOR_RAIL + || block.getType() == Material.DETECTOR_RAIL) + && prot.getSettingBool(ProtectConfig.protect_rails)) + { + event.setCancelled(true); + return; + } + if ((block.getType() == Material.WALL_SIGN + || block.getRelative(BlockFace.NORTH).getType() == Material.WALL_SIGN + || block.getRelative(BlockFace.EAST).getType() == Material.WALL_SIGN + || block.getRelative(BlockFace.SOUTH).getType() == Material.WALL_SIGN + || block.getRelative(BlockFace.WEST).getType() == Material.WALL_SIGN + || block.getType() == Material.SIGN_POST + || block.getRelative(BlockFace.UP).getType() == Material.SIGN_POST) + && prot.getSettingBool(ProtectConfig.protect_signs)) + { + event.setCancelled(true); + return; + } + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onCreatureSpawn(final CreatureSpawnEvent event) + { + if (event.getEntity() instanceof Player) + { + return; + } + final EntityType creature = event.getEntityType(); + if (creature == null) + { + return; + } + final String creatureName = creature.toString().toLowerCase(Locale.ENGLISH); + if (creatureName == null || creatureName.isEmpty()) + { + return; + } + if (ess.getSettings().getProtectPreventSpawn(creatureName)) + { + event.setCancelled(true); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onEntityTarget(final EntityTargetEvent event) + { + if (!(event.getTarget() instanceof Player)) + { + return; + } + 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 + || event.getReason() == TargetReason.RANDOM_TARGET + || event.getReason() == TargetReason.DEFEND_VILLAGE + || event.getReason() == TargetReason.TARGET_ATTACKED_OWNER + || event.getReason() == TargetReason.OWNER_ATTACKED_TARGET) + && prot.getSettingBool(ProtectConfig.prevent_entitytarget) + && !user.isAuthorized("essentials.protect.entitytarget.bypass")) + { + event.setCancelled(true); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onExplosionPrime(ExplosionPrimeEvent event) + { + if ((event.getEntity() instanceof Fireball || event.getEntity() instanceof SmallFireball) + && prot.getSettingBool(ProtectConfig.prevent_fireball_fire)) + { + event.setFire(false); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onEntityChangeBlock(EntityChangeBlockEvent event) + { + if (event.getEntityType() == EntityType.ENDERMAN && prot.getSettingBool(ProtectConfig.prevent_enderman_pickup)) + { + event.setCancelled(true); + return; + } + if (event.getEntityType() == EntityType.WITHER && prot.getSettingBool(ProtectConfig.prevent_wither_blockreplace)) + { + event.setCancelled(true); + } + } +} diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectPlayerListener.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectPlayerListener.java index 393376b6f..9236213d5 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectPlayerListener.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectPlayerListener.java @@ -1,52 +1,52 @@ -package com.earth2me.essentials.protect; - -import static com.earth2me.essentials.I18n._; -import com.earth2me.essentials.IEssentials; -import com.earth2me.essentials.User; -import org.bukkit.block.Block; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.block.Action; -import org.bukkit.event.player.PlayerInteractEvent; - - -public class EssentialsProtectPlayerListener implements Listener -{ - private final transient IProtect prot; - private final transient IEssentials ess; - - public EssentialsProtectPlayerListener(final IProtect prot) - { - this.prot = prot; - this.ess = prot.getEssentialsConnect().getEssentials(); - } - - @EventHandler(priority = EventPriority.LOW) - public void onPlayerInteract(final PlayerInteractEvent event) - { - // Do not return if cancelled, because the interact event has 2 cancelled states. - final User user = ess.getUser(event.getPlayer()); - - if (user.isAuthorized("essentials.protect.ownerinfo") && event.getAction() == Action.RIGHT_CLICK_BLOCK) - { - final StringBuilder stringBuilder = new StringBuilder(); - boolean first = true; - final Block blockClicked = event.getClickedBlock(); - for (String owner : prot.getStorage().getOwners(blockClicked)) - { - if (!first) - { - stringBuilder.append(", "); - } - first = false; - stringBuilder.append(owner); - } - final String ownerNames = stringBuilder.toString(); - if (ownerNames != null && !ownerNames.isEmpty()) - { - user.sendMessage(_("protectionOwner", ownerNames)); - } - } - } -} +package com.earth2me.essentials.protect; + +import static com.earth2me.essentials.I18n._; +import com.earth2me.essentials.IEssentials; +import com.earth2me.essentials.User; +import org.bukkit.block.Block; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEvent; + + +public class EssentialsProtectPlayerListener implements Listener +{ + private final IProtect prot; + private final IEssentials ess; + + public EssentialsProtectPlayerListener(final IProtect prot) + { + this.prot = prot; + this.ess = prot.getEssentialsConnect().getEssentials(); + } + + @EventHandler(priority = EventPriority.LOW) + public void onPlayerInteract(final PlayerInteractEvent event) + { + // Do not return if cancelled, because the interact event has 2 cancelled states. + final User user = ess.getUser(event.getPlayer()); + + if (user.isAuthorized("essentials.protect.ownerinfo") && event.getAction() == Action.RIGHT_CLICK_BLOCK) + { + final StringBuilder stringBuilder = new StringBuilder(); + boolean first = true; + final Block blockClicked = event.getClickedBlock(); + for (String owner : prot.getStorage().getOwners(blockClicked)) + { + if (!first) + { + stringBuilder.append(", "); + } + first = false; + stringBuilder.append(owner); + } + final String ownerNames = stringBuilder.toString(); + if (ownerNames != null && !ownerNames.isEmpty()) + { + user.sendMessage(_("protectionOwner", ownerNames)); + } + } + } +} diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectWeatherListener.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectWeatherListener.java index 1e5fa6da3..ed621a675 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectWeatherListener.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectWeatherListener.java @@ -1,49 +1,48 @@ -package com.earth2me.essentials.protect; - -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.weather.LightningStrikeEvent; -import org.bukkit.event.weather.ThunderChangeEvent; -import org.bukkit.event.weather.WeatherChangeEvent; - - -public class EssentialsProtectWeatherListener implements Listener -{ - private final transient IProtect prot; - - public EssentialsProtectWeatherListener(final IProtect prot) - { - this.prot = prot; - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onWeatherChange(final WeatherChangeEvent event) - { - if (prot.getSettingBool(ProtectConfig.disable_weather_storm) - && event.toWeatherState()) - { - event.setCancelled(true); - } - - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onLightningStrike(final LightningStrikeEvent event) - { - if (prot.getSettingBool(ProtectConfig.disable_weather_lightning)) - { - event.setCancelled(true); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onThunderChange(final ThunderChangeEvent event) - { - if (prot.getSettingBool(ProtectConfig.disable_weather_thunder) - && event.toThunderState()) - { - event.setCancelled(true); - } - } -} +package com.earth2me.essentials.protect; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.weather.LightningStrikeEvent; +import org.bukkit.event.weather.ThunderChangeEvent; +import org.bukkit.event.weather.WeatherChangeEvent; + + +public class EssentialsProtectWeatherListener implements Listener +{ + private final IProtect prot; + + public EssentialsProtectWeatherListener(final IProtect prot) + { + this.prot = prot; + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onWeatherChange(final WeatherChangeEvent event) + { + if (prot.getSettingBool(ProtectConfig.disable_weather_storm) + && event.toWeatherState()) + { + event.setCancelled(true); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onLightningStrike(final LightningStrikeEvent event) + { + if (prot.getSettingBool(ProtectConfig.disable_weather_lightning)) + { + event.setCancelled(true); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onThunderChange(final ThunderChangeEvent event) + { + if (prot.getSettingBool(ProtectConfig.disable_weather_thunder) + && event.toThunderState()) + { + event.setCancelled(true); + } + } +} diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockJDBC.java b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockJDBC.java index e24a71b80..18bb19a48 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockJDBC.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockJDBC.java @@ -1,426 +1,426 @@ -package com.earth2me.essentials.protect.data; - -import com.mchange.v2.c3p0.ComboPooledDataSource; -import java.beans.PropertyVetoException; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; -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 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 - { - this(driver, url, null, null); - } - - public ProtectedBlockJDBC(String driver, String url, String username, String password) throws PropertyVetoException - { - cpds = new ComboPooledDataSource(); - cpds.setDriverClass(driver); - cpds.setJdbcUrl(url); - if (username != null) - { - cpds.setUser(username); - cpds.setPassword(password); - } - cpds.setMaxStatements(20); - createAndConvertTable(); - } - - private void createAndConvertTable() - { - Connection conn = null; - PreparedStatement ps = null; - 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); - } - } - } - } - - 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(); - return rs.next() && 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); - } - } - } - } - - public void onPluginDeactivation() - { - cpds.close(); - } -} +package com.earth2me.essentials.protect.data; + +import com.mchange.v2.c3p0.ComboPooledDataSource; +import java.beans.PropertyVetoException; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +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 final 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 + { + this(driver, url, null, null); + } + + public ProtectedBlockJDBC(String driver, String url, String username, String password) throws PropertyVetoException + { + cpds = new ComboPooledDataSource(); + cpds.setDriverClass(driver); + cpds.setJdbcUrl(url); + if (username != null) + { + cpds.setUser(username); + cpds.setPassword(password); + } + cpds.setMaxStatements(20); + createAndConvertTable(); + } + + private void createAndConvertTable() + { + Connection conn = null; + PreparedStatement ps = null; + 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); + } + } + } + } + + @Override + 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); + } + } + } + } + + @Override + 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); + } + } + + @Override + 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); + } + } + } + } + + @Override + 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); + } + } + } + } + + @Override + 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(); + return rs.next() && 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); + } + } + } + } + + @Override + 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); + } + } + } + } + + @Override + 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); + } + } + } + } + + @Override + public void onPluginDeactivation() + { + cpds.close(); + } +} diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java index e1a61f0cf..348e75515 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java @@ -1,258 +1,258 @@ -package com.earth2me.essentials.protect.data; - -import java.util.*; -import java.util.Map.Entry; -import org.bukkit.World; -import org.bukkit.block.Block; -import org.bukkit.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; - - - static class ProtectedLocation - { - private final transient int x; - private final transient int y; - private final transient int z; - private final transient int w; - - public ProtectedLocation(final Block block, final int worldId) - { - this.x = block.getX(); - this.y = block.getY(); - this.z = block.getZ(); - this.w = worldId; - } - - public ProtectedLocation(final OwnedBlock ownedBlock, final int worldId) - { - this.x = ownedBlock.x; - this.y = ownedBlock.y; - this.z = ownedBlock.z; - this.w = worldId; - } - - @Override - 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() - { - return x ^ y ^ z ^ w; - } - } - - - static class ProtectedBy - { - private transient int playerId = -1; - private transient Set playerIds; - - public void add(final int playerId) - { - if (this.playerId == -1 || this.playerId == playerId) - { - this.playerId = playerId; - } - else - { - if (playerIds == null) - { - playerIds = new HashSet(4); - playerIds.add(this.playerId); - } - playerIds.add(playerId); - } - } - - public boolean contains(final int playerId) - { - if (playerIds == null) - { - return this.playerId == playerId; - } - return playerIds.contains(playerId); - } - - public List getPlayers(final List playerNames) - { - final List list = new ArrayList(2); - if (playerIds == null) - { - list.add(playerNames.get(playerId)); - } - else - { - for (Integer integer : playerIds) - { - list.add(playerNames.get(integer)); - } - } - return list; - } - - public int size() - { - if (playerIds == null) - { - return 1; - } - return playerIds.size(); - } - } - private final transient Map blocks = new HashMap(); - - public ProtectedBlockMemory(final IProtectedBlock storage, final Plugin plugin) - { - this.storage = storage; - this.plugin = plugin; - importProtections(storage.exportProtections()); - } - - @Override - public void clearProtections() - { - blocks.clear(); - } - - @Override - 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); - } - } - - @Override - 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; - } - - @Override - public void protectBlock(final Block block, final String playerName) - { - final ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld())); - protectBlock(pl, playerName); - plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() - { - @Override - public void run() - { - storage.protectBlock(block, playerName); - } - }); - } - - private void protectBlock(ProtectedLocation pl, String playerName) - { - int playerId = getPlayerId(playerName); - ProtectedBy pb = blocks.get(pl); - if (pb == null) - { - pb = new ProtectedBy(); - blocks.put(pl, pb); - } - pb.add(playerId); - } - - @Override - 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); - } - - @Override - public List getOwners(Block block) - { - ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld())); - ProtectedBy pb = blocks.get(pl); - return pb.getPlayers(playerNames); - } - - @Override - public int unprotectBlock(final Block block) - { - ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld())); - ProtectedBy pb = blocks.remove(pl); - plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() - { - @Override - public void run() - { - storage.unprotectBlock(block); - } - }); - return pb.size(); - } - - private int getPlayerId(String playername) - { - int id = playerNames.indexOf(playername); - if (id < 0) - { - playerNames.add(playername); - id = playerNames.indexOf(playername); - } - return id; - } - - private int getWorldId(World world) - { - return getWorldId(world.getName()); - } - - private int getWorldId(String name) - { - int id = worlds.indexOf(name); - if (id < 0) - { - worlds.add(name); - id = worlds.indexOf(name); - } - return id; - } - - @Override - public void onPluginDeactivation() - { - storage.onPluginDeactivation(); - } -} +package com.earth2me.essentials.protect.data; + +import java.util.*; +import java.util.Map.Entry; +import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.plugin.Plugin; + + +public class ProtectedBlockMemory implements IProtectedBlock +{ + private final List worlds = new ArrayList(); + private final List playerNames = new ArrayList(); + private final IProtectedBlock storage; + private final Plugin plugin; + + + static class ProtectedLocation + { + private final int x; + private final int y; + private final int z; + private final int w; + + public ProtectedLocation(final Block block, final int worldId) + { + this.x = block.getX(); + this.y = block.getY(); + this.z = block.getZ(); + this.w = worldId; + } + + public ProtectedLocation(final OwnedBlock ownedBlock, final int worldId) + { + this.x = ownedBlock.x; + this.y = ownedBlock.y; + this.z = ownedBlock.z; + this.w = worldId; + } + + @Override + 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() + { + return x ^ y ^ z ^ w; + } + } + + + static class ProtectedBy + { + private int playerId = -1; + private Set playerIds; + + public void add(final int playerId) + { + if (this.playerId == -1 || this.playerId == playerId) + { + this.playerId = playerId; + } + else + { + if (playerIds == null) + { + playerIds = new HashSet(4); + playerIds.add(this.playerId); + } + playerIds.add(playerId); + } + } + + public boolean contains(final int playerId) + { + if (playerIds == null) + { + return this.playerId == playerId; + } + return playerIds.contains(playerId); + } + + public List getPlayers(final List playerNames) + { + final List list = new ArrayList(2); + if (playerIds == null) + { + list.add(playerNames.get(playerId)); + } + else + { + for (Integer integer : playerIds) + { + list.add(playerNames.get(integer)); + } + } + return list; + } + + public int size() + { + if (playerIds == null) + { + return 1; + } + return playerIds.size(); + } + } + private final Map blocks = new HashMap(); + + public ProtectedBlockMemory(final IProtectedBlock storage, final Plugin plugin) + { + this.storage = storage; + this.plugin = plugin; + importProtections(storage.exportProtections()); + } + + @Override + public void clearProtections() + { + blocks.clear(); + } + + @Override + 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); + } + } + + @Override + 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; + } + + @Override + public void protectBlock(final Block block, final String playerName) + { + final ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld())); + protectBlock(pl, playerName); + plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() + { + @Override + public void run() + { + storage.protectBlock(block, playerName); + } + }); + } + + private void protectBlock(ProtectedLocation pl, String playerName) + { + int playerId = getPlayerId(playerName); + ProtectedBy pb = blocks.get(pl); + if (pb == null) + { + pb = new ProtectedBy(); + blocks.put(pl, pb); + } + pb.add(playerId); + } + + @Override + 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); + } + + @Override + public List getOwners(Block block) + { + ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld())); + ProtectedBy pb = blocks.get(pl); + return pb.getPlayers(playerNames); + } + + @Override + public int unprotectBlock(final Block block) + { + ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld())); + ProtectedBy pb = blocks.remove(pl); + plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() + { + @Override + public void run() + { + storage.unprotectBlock(block); + } + }); + return pb.size(); + } + + private int getPlayerId(String playername) + { + int id = playerNames.indexOf(playername); + if (id < 0) + { + playerNames.add(playername); + id = playerNames.indexOf(playername); + } + return id; + } + + private int getWorldId(World world) + { + return getWorldId(world.getName()); + } + + private int getWorldId(String name) + { + int id = worlds.indexOf(name); + if (id < 0) + { + worlds.add(name); + id = worlds.indexOf(name); + } + return id; + } + + @Override + public void onPluginDeactivation() + { + storage.onPluginDeactivation(); + } +} -- cgit v1.2.3