diff options
Diffstat (limited to 'EssentialsProtect')
14 files changed, 903 insertions, 567 deletions
diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/BlockBreakPermissions.java b/EssentialsProtect/src/com/earth2me/essentials/protect/BlockBreakPermissions.java new file mode 100644 index 000000000..74ebe38dc --- /dev/null +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/BlockBreakPermissions.java @@ -0,0 +1,43 @@ +package com.earth2me.essentials.protect; + +import com.earth2me.essentials.api.IPermission; +import com.earth2me.essentials.perm.AbstractSuperpermsPermission; +import java.util.EnumMap; +import java.util.Locale; +import java.util.Map; +import org.bukkit.Material; +import org.bukkit.permissions.PermissionDefault; + +public class BlockBreakPermissions extends AbstractSuperpermsPermission{ + private static Map<Material,IPermission> permissions = new EnumMap<Material, IPermission>(Material.class); + private static final String base = "essentials.protect.blockbreak."; + private final String permission; + + public static IPermission getPermission(Material mat) + { + IPermission perm = permissions.get(mat); + if (perm == null) { + perm = new BlockBreakPermissions(mat.toString().toLowerCase(Locale.ENGLISH)); + permissions.put(mat, perm); + } + return perm; + } + + private BlockBreakPermissions(String matName) + { + this.permission = base + matName; + } + + @Override + public String getPermission() + { + return this.permission; + } + + @Override + public PermissionDefault getPermissionDefault() + { + return PermissionDefault.TRUE; + } +} + diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/BlockPlacePermissions.java b/EssentialsProtect/src/com/earth2me/essentials/protect/BlockPlacePermissions.java new file mode 100644 index 000000000..23ce7e053 --- /dev/null +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/BlockPlacePermissions.java @@ -0,0 +1,42 @@ +package com.earth2me.essentials.protect; + +import com.earth2me.essentials.api.IPermission; +import com.earth2me.essentials.perm.AbstractSuperpermsPermission; +import java.util.EnumMap; +import java.util.Locale; +import java.util.Map; +import org.bukkit.Material; +import org.bukkit.permissions.PermissionDefault; + +public class BlockPlacePermissions extends AbstractSuperpermsPermission{ + private static Map<Material,IPermission> permissions = new EnumMap<Material, IPermission>(Material.class); + private static final String base = "essentials.protect.blockplace."; + private final String permission; + + public static IPermission getPermission(Material mat) + { + IPermission perm = permissions.get(mat); + if (perm == null) { + perm = new BlockPlacePermissions(mat.toString().toLowerCase(Locale.ENGLISH)); + permissions.put(mat, perm); + } + return perm; + } + + private BlockPlacePermissions(String matName) + { + this.permission = base + matName; + } + + @Override + public String getPermission() + { + return this.permission; + } + + @Override + public PermissionDefault getPermissionDefault() + { + return PermissionDefault.TRUE; + } +} diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsConnect.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsConnect.java index 7503c334c..539ff208b 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsConnect.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsConnect.java @@ -1,13 +1,12 @@ package com.earth2me.essentials.protect; -import com.earth2me.essentials.IConf; -import com.earth2me.essentials.IEssentials; -import com.earth2me.essentials.User; -import com.earth2me.essentials.protect.data.ProtectedBlockMemory; +import static com.earth2me.essentials.I18n._; +import com.earth2me.essentials.api.IEssentials; +import com.earth2me.essentials.api.IReload; +import com.earth2me.essentials.api.IUser; 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.Location; @@ -29,8 +28,9 @@ public class EssentialsConnect } ess = (IEssentials)essPlugin; protect = (IProtect)essProtect; + protect.setSettings(new ProtectHolder(ess)); ProtectReloader pr = new ProtectReloader(); - pr.reloadConfig(); + pr.onReload(); ess.addReloadListener(pr); } @@ -43,7 +43,7 @@ public class EssentialsConnect return ess; } - public void alert(final User user, final String item, final String type) + public void alert(final Player user, final String item, final String type) { final Location loc = user.getLocation(); final String warnMessage = _("alertFormat", user.getName(), type, item, @@ -52,8 +52,8 @@ public class EssentialsConnect LOGGER.log(Level.WARNING, warnMessage); for (Player p : ess.getServer().getOnlinePlayers()) { - final User alertUser = ess.getUser(p); - if (alertUser.isAuthorized("essentials.protect.alerts")) + final IUser alertUser = ess.getUser(p); + if (Permissions.ALERTS.isAuthorized(alertUser)) { alertUser.sendMessage(warnMessage); } @@ -61,62 +61,68 @@ public class EssentialsConnect } - private class ProtectReloader implements IConf + private class ProtectReloader implements IReload { @Override - public void reloadConfig() + public void onReload() { 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())); - } - } + /* + * 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")) + ProtectHolder settings = protect.getSettings(); + settings.acquireReadLock(); + try { - try - { - protect.setStorage(new ProtectedBlockMySQL( - protect.getSettingString(ProtectConfig.mysqlDB), - protect.getSettingString(ProtectConfig.dbUsername), - protect.getSettingString(ProtectConfig.dbPassword))); - } - catch (PropertyVetoException ex) + if (settings.getData().getDbtype().equalsIgnoreCase("mysql")) { - LOGGER.log(Level.SEVERE, null, ex); + try + { + protect.setStorage(new ProtectedBlockMySQL( + settings.getData().getDburl(), + settings.getData().getDbuser(), + settings.getData().getDbpassword())); + } + catch (PropertyVetoException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } } - } - else - { - try + else { - protect.setStorage(new ProtectedBlockSQLite("jdbc:sqlite:plugins/Essentials/EssentialsProtect.db")); + try + { + protect.setStorage(new ProtectedBlockSQLite("jdbc:sqlite:plugins/Essentials/EssentialsProtect.db")); + } + catch (PropertyVetoException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } } - catch (PropertyVetoException ex) + /*if (protect.getSettingBool(ProtectConfig.memstore)) { - LOGGER.log(Level.SEVERE, null, ex); - } + protect.setStorage(new ProtectedBlockMemory(protect.getStorage(), protect)); + }*/ + } - if (protect.getSettingBool(ProtectConfig.memstore)) + finally { - protect.setStorage(new ProtectedBlockMemory(protect.getStorage(), protect)); + settings.unlock(); } - } } } diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java index 9c257662d..72000eeba 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java @@ -1,9 +1,6 @@ package com.earth2me.essentials.protect; import com.earth2me.essentials.protect.data.IProtectedBlock; -import java.util.EnumMap; -import java.util.List; -import java.util.Map; import java.util.logging.Filter; import java.util.logging.Level; import java.util.logging.LogRecord; @@ -18,11 +15,12 @@ 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<ProtectConfig, Boolean> settingsBoolean = new EnumMap<ProtectConfig, Boolean>(ProtectConfig.class); - private final transient Map<ProtectConfig, String> settingsString = new EnumMap<ProtectConfig, String>(ProtectConfig.class); - private final transient Map<ProtectConfig, List<Integer>> settingsList = new EnumMap<ProtectConfig, List<Integer>>(ProtectConfig.class); + //private final transient Map<ProtectConfig, Boolean> settingsBoolean = new EnumMap<ProtectConfig, Boolean>(ProtectConfig.class); + //private final transient Map<ProtectConfig, String> settingsString = new EnumMap<ProtectConfig, String>(ProtectConfig.class); + //private final transient Map<ProtectConfig, List<Integer>> settingsList = new EnumMap<ProtectConfig, List<Integer>>(ProtectConfig.class); private transient IProtectedBlock storage = null; private transient EssentialsConnect ess = null; + private transient ProtectHolder settings = null; @Override public void onLoad() @@ -40,7 +38,7 @@ public class EssentialsProtect extends JavaPlugin implements IProtect public void onEnable() { final PluginManager pm = this.getServer().getPluginManager(); - final Plugin essPlugin = pm.getPlugin("Essentials"); + final Plugin essPlugin = pm.getPlugin("Essentials3"); if (essPlugin == null || !essPlugin.isEnabled()) { enableEmergencyMode(pm); @@ -65,7 +63,7 @@ public class EssentialsProtect extends JavaPlugin implements IProtect { 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."); @@ -73,12 +71,12 @@ public class EssentialsProtect extends JavaPlugin implements IProtect LOGGER.log(Level.SEVERE, "Essentials not installed or failed to load. Essenials Protect is in emergency mode now."); } - @Override + /*@Override public boolean checkProtectionItems(final ProtectConfig list, final int id) { final List<Integer> itemList = settingsList.get(list); return itemList != null && !itemList.isEmpty() && itemList.contains(id); - } + }*/ @Override public IProtectedBlock getStorage() @@ -97,7 +95,7 @@ public class EssentialsProtect extends JavaPlugin implements IProtect return ess; } - public Map<ProtectConfig, Boolean> getSettingsBoolean() + /*public Map<ProtectConfig, Boolean> getSettingsBoolean() { return settingsBoolean; } @@ -124,7 +122,7 @@ public class EssentialsProtect extends JavaPlugin implements IProtect { final String str = settingsString.get(protectConfig); return str == null ? protectConfig.getDefaultValueString() : str; - } + }*/ public void onDisable() { @@ -141,4 +139,14 @@ public class EssentialsProtect extends JavaPlugin implements IProtect { } } + + public ProtectHolder getSettings() + { + return settings; + } + + public void setSettings(final ProtectHolder settings) + { + this.settings = settings; + } } diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java index 8e5e9cca8..f69ede53a 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java @@ -1,14 +1,14 @@ package com.earth2me.essentials.protect; import static com.earth2me.essentials.I18n._; -import com.earth2me.essentials.IEssentials; -import com.earth2me.essentials.User; +import com.earth2me.essentials.api.IEssentials; 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.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; @@ -34,66 +34,74 @@ public class EssentialsProtectBlockListener implements Listener return; } - final User user = ess.getUser(event.getPlayer()); - - if (prot.getSettingBool(ProtectConfig.disable_build) && !user.canBuild()) + final Player user = event.getPlayer(); + final ProtectHolder settings = prot.getSettings(); + settings.acquireReadLock(); + try { - event.setCancelled(true); - return; - } + if (!Permissions.BUILD.isAuthorized(user)) + { + event.setCancelled(true); + return; + } - final Block blockPlaced = event.getBlockPlaced(); - final int id = blockPlaced.getTypeId(); + final Block blockPlaced = event.getBlockPlaced(); + final int id = blockPlaced.getTypeId(); - if (prot.checkProtectionItems(ProtectConfig.blacklist_placement, id) && !user.isAuthorized("essentials.protect.exemptplacement")) - { - event.setCancelled(true); - return; - } + if (!BlockPlacePermissions.getPermission(blockPlaced.getType()).isAuthorized(user)) + { + event.setCancelled(true); + return; + } - if (!user.hasPermission("essentials.protect.alerts.notrigger") - && prot.checkProtectionItems(ProtectConfig.alert_on_placement, id)) - { - prot.getEssentialsConnect().alert(user, blockPlaced.getType().toString(), _("alertPlaced")); - } + if (!Permissions.ALERTS_NOTRIGGER.isAuthorized(user) && + settings.getData().getAlertOnPlacement().contains(blockPlaced.getType())) + { + prot.getEssentialsConnect().alert(user, blockPlaced.getType().toString(), _("alertPlaced")); + } - 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 Block below = blockPlaced.getRelative(BlockFace.DOWN); + if ((below.getType() == Material.RAILS || below.getType() == Material.POWERED_RAIL || below.getType() == Material.DETECTOR_RAIL) + && settings.getData().getSignsAndRails().isPreventBlockAboveRails() + && isProtected(below, user, settings)) + { + event.setCancelled(true); + return; + } - final List<Block> protect = new ArrayList<Block>(); - 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)) + final List<Block> protect = new ArrayList<Block>(); + if ((blockPlaced.getType() == Material.RAILS || blockPlaced.getType() == Material.POWERED_RAIL || blockPlaced.getType() == Material.DETECTOR_RAIL) + && settings.getData().getSignsAndRails().isProtectRails() + && Permissions.RAILS.isAuthorized(user)) { - protect.add(blockPlaced.getRelative(BlockFace.DOWN)); + protect.add(blockPlaced); + if (settings.getData().getSignsAndRails().isBlockBelow() + && !isProtected(blockPlaced.getRelative(BlockFace.DOWN), user, settings)) + { + 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)) + /*if ((blockPlaced.getType() == Material.SIGN_POST || blockPlaced.getType() == Material.WALL_SIGN) + && settings.getData().getSignsAndRails().isProtectSigns() + && user.isAuthorized("essentials.protect")) + { + protect.add(blockPlaced); + if (settings.getData().getSignsAndRails().isBlockBelow() + && event.getBlockAgainst().getType() != Material.SIGN_POST + && event.getBlockAgainst().getType() != Material.WALL_SIGN + && !isProtected(event.getBlockAgainst(), user, settings)) + { + protect.add(event.getBlockAgainst()); + } + }*/ + for (Block block : protect) { - protect.add(event.getBlockAgainst()); + prot.getStorage().protectBlock(block, user.getName()); } } - for (Block block : protect) + finally { - prot.getStorage().protectBlock(block, user.getName()); + settings.unlock(); } } @@ -104,47 +112,56 @@ public class EssentialsProtectBlockListener implements Listener { return; } - 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) + final ProtectHolder settings = prot.getSettings(); + settings.acquireReadLock(); + try { - event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_portal_creation)); - return; - } + final Block block = event.getBlock(); + if ((block.getType() == Material.RAILS || block.getType() == Material.POWERED_RAIL || block.getType() == Material.DETECTOR_RAIL) + && settings.getData().getSignsAndRails().isProtectRails()) + { + event.setCancelled(true); + return; + } + if ((block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST) + && settings.getData().getSignsAndRails().isProtectSigns()) + { + event.setCancelled(true); + return; + } + if (event.getBlock().getType() == Material.OBSIDIAN + || event.getBlock().getRelative(BlockFace.DOWN).getType() == Material.OBSIDIAN) + { + event.setCancelled(settings.getData().getPrevent().isPortalCreation()); + return; + } - if (event.getCause().equals(BlockIgniteEvent.IgniteCause.SPREAD)) - { - event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_fire_spread)); - return; - } + if (event.getCause().equals(BlockIgniteEvent.IgniteCause.SPREAD)) + { + event.setCancelled(settings.getData().getPrevent().isFirespread()); + 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.FLINT_AND_STEEL) && event.getPlayer() != null) + { + event.setCancelled(Permissions.USEFLINTSTEEL.isAuthorized(event.getPlayer())); + return; + } - if (event.getCause().equals(BlockIgniteEvent.IgniteCause.LAVA)) - { - event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_lava_fire_spread)); - return; + if (event.getCause().equals(BlockIgniteEvent.IgniteCause.LAVA)) + { + event.setCancelled(settings.getData().getPrevent().isLavaFirespread()); + return; + } + if (event.getCause().equals(BlockIgniteEvent.IgniteCause.LIGHTNING)) + { + event.setCancelled(settings.getData().getPrevent().isLightningFirespread()); + return; + } } - if (event.getCause().equals(BlockIgniteEvent.IgniteCause.LIGHTNING)) + finally { - event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_lightning_fire_spread)); - return; + settings.unlock(); } } @@ -155,37 +172,45 @@ public class EssentialsProtectBlockListener implements Listener { return; } - 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)) + final ProtectHolder settings = prot.getSettings(); + settings.acquireReadLock(); + try { - event.setCancelled(true); - return; - } + final Block toBlock = event.getToBlock(); + if ((toBlock.getType() == Material.RAILS || toBlock.getType() == Material.POWERED_RAIL || toBlock.getType() == Material.DETECTOR_RAIL) + && settings.getData().getSignsAndRails().isProtectRails()) + { + event.setCancelled(true); + return; + } + if ((toBlock.getType() == Material.WALL_SIGN || toBlock.getType() == Material.SIGN_POST) + && settings.getData().getSignsAndRails().isProtectSigns()) + { + 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; - } + final Block block = event.getBlock(); + if (block.getType() == Material.WATER || block.getType() == Material.STATIONARY_WATER) + { + event.setCancelled(settings.getData().getPrevent().isWaterFlow()); + return; + } - if (block.getType() == Material.LAVA || block.getType() == Material.STATIONARY_LAVA) - { - event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_lava_flow)); - return; + if (block.getType() == Material.LAVA || block.getType() == Material.STATIONARY_LAVA) + { + event.setCancelled(settings.getData().getPrevent().isLavaFlow()); + return; + } + // TODO: Test if this still works + /* + * if (block.getType() == Material.AIR) { + * event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_water_bucket_flow)); return; } + */ } - - if (block.getType() == Material.AIR) + finally { - event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_water_bucket_flow)); - return; + settings.unlock(); } } @@ -196,22 +221,32 @@ public class EssentialsProtectBlockListener implements Listener { return; } - 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)) + final ProtectHolder settings = prot.getSettings(); + settings.acquireReadLock(); + try { - event.setCancelled(true); - return; + final Block block = event.getBlock(); + if ((block.getType() == Material.RAILS || block.getType() == Material.POWERED_RAIL || block.getType() == Material.DETECTOR_RAIL) + && settings.getData().getSignsAndRails().isProtectRails()) + { + event.setCancelled(true); + return; + } + if ((block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST) + && settings.getData().getSignsAndRails().isProtectSigns()) + { + event.setCancelled(true); + return; + } + if (settings.getData().getPrevent().isFirespread()) + { + event.setCancelled(true); + return; + } } - if (prot.getSettingBool(ProtectConfig.prevent_fire_spread)) + finally { - event.setCancelled(true); - return; + settings.unlock(); } } private final static BlockFace[] faces = new BlockFace[] @@ -232,9 +267,9 @@ public class EssentialsProtectBlockListener implements Listener { return; } - final User user = ess.getUser(event.getPlayer()); + final Player user = event.getPlayer(); - if (prot.getSettingBool(ProtectConfig.disable_build) && !user.canBuild()) + if (!Permissions.BUILD.isAuthorized(user)) { event.setCancelled(true); return; @@ -242,30 +277,41 @@ public class EssentialsProtectBlockListener implements Listener final Block block = event.getBlock(); final int typeId = block.getTypeId(); - if (prot.checkProtectionItems(ProtectConfig.blacklist_break, typeId) - && !user.isAuthorized("essentials.protect.exemptbreak")) + if (!BlockBreakPermissions.getPermission(block.getType()).isAuthorized(user)) { event.setCancelled(true); return; } - final Material type = block.getType(); - - if (!user.hasPermission("essentials.protect.alerts.notrigger") - && prot.checkProtectionItems(ProtectConfig.alert_on_break, typeId)) + final ProtectHolder settings = prot.getSettings(); + settings.acquireReadLock(); + try { - prot.getEssentialsConnect().alert(user, type.toString(), _("alertBroke")); - } - final IProtectedBlock storage = prot.getStorage(); + final Material type = block.getType(); - 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) + if (!Permissions.ALERTS_NOTRIGGER.isAuthorized(user) && settings.getData().getAlertOnBreak().contains(type)) + { + prot.getEssentialsConnect().alert(user, type.toString(), _("alertBroke")); + } + final IProtectedBlock storage = prot.getStorage(); + + if (Permissions.ADMIN.isAuthorized(user)) { - storage.unprotectBlock(block); - if (type == Material.RAILS || type == Material.POWERED_RAIL || type == Material.DETECTOR_RAIL || type == Material.SIGN_POST) + if (type == Material.WALL_SIGN || type == Material.SIGN_POST || type == Material.RAILS || type == Material.POWERED_RAIL || type == Material.DETECTOR_RAIL) { - final Block below = block.getRelative(BlockFace.DOWN); - storage.unprotectBlock(below); + 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 { @@ -278,30 +324,30 @@ public class EssentialsProtectBlockListener implements Listener } else { - for (BlockFace blockFace : faces) + + final boolean isProtected = isProtected(block, user, settings); + if (isProtected) { - final Block against = block.getRelative(blockFace); - storage.unprotectBlock(against); + event.setCancelled(true); } - } - } - 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) + else { - storage.unprotectBlock(block); - if (type == Material.RAILS || type == Material.POWERED_RAIL || type == Material.DETECTOR_RAIL || type == Material.SIGN_POST) + if (type == Material.WALL_SIGN || type == Material.SIGN_POST || type == Material.RAILS || type == Material.POWERED_RAIL || type == Material.DETECTOR_RAIL) { - final Block below = block.getRelative(BlockFace.DOWN); - storage.unprotectBlock(below); + 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 { @@ -312,28 +358,89 @@ public class EssentialsProtectBlockListener implements Listener } } } - else + } + } + finally + { + settings.unlock(); + } + } + + @EventHandler(priority = EventPriority.HIGHEST) + public void onBlockPistonExtend(final BlockPistonExtendEvent event) + { + if (event.isCancelled()) + { + return; + } + final ProtectHolder settings = prot.getSettings(); + settings.acquireReadLock(); + try + { + for (Block block : event.getBlocks()) + { + if (settings.getData().getPrevent().getPistonPush().contains(block.getType())) + { + event.setCancelled(true); + return; + } + 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) + && settings.getData().getSignsAndRails().isProtectRails()) + { + event.setCancelled(true); + return; + } + if (settings.getData().getSignsAndRails().isProtectSigns()) { for (BlockFace blockFace : faces) { - final Block against = block.getRelative(blockFace); - storage.unprotectBlock(against); + 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; + } } } } } + finally + { + settings.unlock(); + } } @EventHandler(priority = EventPriority.HIGHEST) - public void onBlockPistonExtend(BlockPistonExtendEvent event) + public void onBlockPistonRetract(final BlockPistonRetractEvent event) { - if (event.isCancelled()) + if (event.isCancelled() || !event.isSticky()) { return; } - for (Block block : event.getBlocks()) + final ProtectHolder settings = prot.getSettings(); + settings.acquireReadLock(); + try { - if (prot.checkProtectionItems(ProtectConfig.blacklist_piston, block.getTypeId())) + final Block block = event.getRetractLocation().getBlock(); + if (settings.getData().getPrevent().getPistonPush().contains(block.getType())) { event.setCancelled(true); return; @@ -344,12 +451,12 @@ public class EssentialsProtectBlockListener implements Listener || block.getType() == Material.POWERED_RAIL || block.getRelative(BlockFace.UP).getType() == Material.DETECTOR_RAIL || block.getType() == Material.DETECTOR_RAIL) - && prot.getSettingBool(ProtectConfig.protect_rails)) + && settings.getData().getSignsAndRails().isProtectRails()) { event.setCancelled(true); return; } - if (prot.getSettingBool(ProtectConfig.protect_signs)) + if (settings.getData().getSignsAndRails().isProtectSigns()) { for (BlockFace blockFace : faces) { @@ -375,70 +482,23 @@ public class EssentialsProtectBlockListener implements Listener } } } - } - @EventHandler(priority = EventPriority.HIGHEST) - public void onBlockPistonRetract(BlockPistonRetractEvent event) - { - if (event.isCancelled() || !event.isSticky()) - { - return; - } - final Block block = event.getRetractLocation().getBlock(); - if (prot.checkProtectionItems(ProtectConfig.blacklist_piston, block.getTypeId())) + finally { - event.setCancelled(true); - return; - } - 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; - } - } + settings.unlock(); } } - private boolean isProtected(final Block block, final User user) + private boolean isProtected(final Block block, final Player user, final ProtectHolder settings) { final Material type = block.getType(); - if (prot.getSettingBool(ProtectConfig.protect_signs)) + if (settings.getData().getSignsAndRails().isProtectSigns()) { 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) { @@ -463,15 +523,15 @@ public class EssentialsProtectBlockListener implements Listener } } } - } + } - if (prot.getSettingBool(ProtectConfig.protect_rails)) + if (settings.getData().getSignsAndRails().isProtectRails()) { 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)) + if (settings.getData().getSignsAndRails().isBlockBelow()) { final Block up = block.getRelative(BlockFace.UP); if (up != null && (type == Material.RAILS || type == Material.POWERED_RAIL || type == Material.DETECTOR_RAIL)) diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectEntityListener.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectEntityListener.java index 57302517f..9a754a374 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectEntityListener.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectEntityListener.java @@ -1,9 +1,7 @@ package com.earth2me.essentials.protect; -import com.earth2me.essentials.IEssentials; -import com.earth2me.essentials.User; +import com.earth2me.essentials.api.IEssentials; import com.earth2me.essentials.craftbukkit.FakeExplosion; -import java.util.Locale; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; @@ -23,6 +21,7 @@ public class EssentialsProtectEntityListener implements Listener public EssentialsProtectEntityListener(final IProtect prot) { + super(); this.prot = prot; this.ess = prot.getEssentialsConnect().getEssentials(); } @@ -34,233 +33,219 @@ public class EssentialsProtectEntityListener implements Listener { return; } - final Entity target = event.getEntity(); - - if (target instanceof Villager && prot.getSettingBool(ProtectConfig.prevent_villager_death)) + final ProtectHolder settings = prot.getSettings(); + settings.acquireReadLock(); + try { - event.setCancelled(true); - return; - } - - final User user = ess.getUser(target); - if (event instanceof EntityDamageByBlockEvent) - { - final DamageCause cause = event.getCause(); + final Entity target = event.getEntity(); - if (prot.getSettingBool(ProtectConfig.disable_contactdmg) - && cause == DamageCause.CONTACT - && !(target instanceof Player - && user.isAuthorized("essentials.protect.damage.contact") - && !user.isAuthorized("essentials.protect.damage.disable"))) + if (target instanceof Villager && settings.getData().getPrevent().isVillagerDeath()) { event.setCancelled(true); return; } - if (prot.getSettingBool(ProtectConfig.disable_lavadmg) - && cause == DamageCause.LAVA - && !(target instanceof Player - && user.isAuthorized("essentials.protect.damage.lava") - && !user.isAuthorized("essentials.protect.damage.disable"))) + + final Player user = target instanceof Player ? (Player)target : null; + if (target instanceof Player && event instanceof EntityDamageByBlockEvent) { - event.setCancelled(true); - return; + final DamageCause cause = event.getCause(); + + if (cause == DamageCause.CONTACT + && (Permissions.PREVENTDAMAGE_CONTACT.isAuthorized(user) + && !Permissions.PREVENTDAMAGE_NONE.isAuthorized(user))) + { + event.setCancelled(true); + return; + } + if (cause == DamageCause.LAVA + && (Permissions.PREVENTDAMAGE_LAVADAMAGE.isAuthorized(user) + && !Permissions.PREVENTDAMAGE_NONE.isAuthorized(user))) + { + event.setCancelled(true); + return; + } + if (cause == DamageCause.BLOCK_EXPLOSION + && (Permissions.PREVENTDAMAGE_TNT.isAuthorized(user) + && !Permissions.PREVENTDAMAGE_NONE.isAuthorized(user))) + { + event.setCancelled(true); + return; + } } - if (prot.getSettingBool(ProtectConfig.prevent_tnt_explosion) - && cause == DamageCause.BLOCK_EXPLOSION - && !(target instanceof Player - && user.isAuthorized("essentials.protect.damage.tnt") - && !user.isAuthorized("essentials.protect.damage.disable"))) + + if (target instanceof Player && event instanceof EntityDamageByEntityEvent) { - event.setCancelled(true); - return; - } - } + final EntityDamageByEntityEvent edEvent = (EntityDamageByEntityEvent)event; + final Entity eAttack = edEvent.getDamager(); + final Player attacker = eAttack instanceof Player ? (Player)eAttack : null; - if (event instanceof EntityDamageByEntityEvent) - { - final EntityDamageByEntityEvent edEvent = (EntityDamageByEntityEvent)event; - final Entity eAttack = edEvent.getDamager(); - final User attacker = ess.getUser(eAttack); + // PVP Settings + if (target instanceof Player && eAttack instanceof Player + && (!Permissions.PVP.isAuthorized(user) || !Permissions.PVP.isAuthorized(attacker))) + { + 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; - } + //Creeper explode prevention + if (eAttack instanceof Creeper && settings.getData().getPrevent().isCreeperExplosion() + || (Permissions.PREVENTDAMAGE_CREEPER.isAuthorized(user) + && !Permissions.PREVENTDAMAGE_NONE.isAuthorized(user))) + { + event.setCancelled(true); + return; + } - //Creeper explode prevention - if (eAttack instanceof Creeper && prot.getSettingBool(ProtectConfig.prevent_creeper_explosion) - && !(target instanceof Player - && user.isAuthorized("essentials.protect.damage.creeper") - && !user.isAuthorized("essentials.protect.damage.disable"))) - { - event.setCancelled(true); - return; - } + if ((event.getEntity() instanceof Fireball || event.getEntity() instanceof SmallFireball) + && (Permissions.PREVENTDAMAGE_FIREBALL.isAuthorized(user) + && !Permissions.PREVENTDAMAGE_NONE.isAuthorized(user))) + { + event.setCancelled(true); + return; + } - if (eAttack instanceof Creeper && prot.getSettingBool(ProtectConfig.prevent_creeper_playerdmg) - && !(target instanceof Player - && user.isAuthorized("essentials.protect.damage.creeper") - && !user.isAuthorized("essentials.protect.damage.disable"))) - { - event.setCancelled(true); - return; - } + if (eAttack instanceof TNTPrimed + && (Permissions.PREVENTDAMAGE_TNT.isAuthorized(user) + && !Permissions.PREVENTDAMAGE_NONE.isAuthorized(user))) + { + event.setCancelled(true); + return; + } - if ((event.getEntity() instanceof Fireball || event.getEntity() instanceof SmallFireball) - && prot.getSettingBool(ProtectConfig.prevent_fireball_playerdmg) - && !(target instanceof Player - && user.isAuthorized("essentials.protect.damage.fireball") - && !user.isAuthorized("essentials.protect.damage.disable"))) - { - event.setCancelled(true); - return; + if (edEvent.getDamager() instanceof Projectile + && ((Permissions.PREVENTDAMAGE_PROJECTILES.isAuthorized(user) + && !Permissions.PREVENTDAMAGE_NONE.isAuthorized(user)) + || (((Projectile)edEvent.getDamager()).getShooter() instanceof Player + && (!Permissions.PVP.isAuthorized(user) + || !Permissions.PVP.isAuthorized((Player)((Projectile)edEvent.getDamager()).getShooter()))))) + { + event.setCancelled(true); + return; + } } - if (eAttack instanceof TNTPrimed && prot.getSettingBool(ProtectConfig.prevent_tnt_playerdmg) - && !(target instanceof Player - && user.isAuthorized("essentials.protect.damage.tnt") - && !user.isAuthorized("essentials.protect.damage.disable"))) + final DamageCause cause = event.getCause(); + if (target instanceof Player) { - event.setCancelled(true); - return; - } + if (cause == DamageCause.FALL + && (Permissions.PREVENTDAMAGE_FALL.isAuthorized(user) + && !Permissions.PREVENTDAMAGE_NONE.isAuthorized(user))) + { + event.setCancelled(true); + return; + } - if (edEvent.getDamager() instanceof Projectile - && target instanceof Player - && ((prot.getSettingBool(ProtectConfig.disable_projectiles) - && !(user.isAuthorized("essentials.protect.damage.projectiles") - && !user.isAuthorized("essentials.protect.damage.disable"))) - || (((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 (cause == DamageCause.SUFFOCATION + && (Permissions.PREVENTDAMAGE_SUFFOCATION.isAuthorized(user) + && !Permissions.PREVENTDAMAGE_NONE.isAuthorized(user))) + { + event.setCancelled(true); + return; + } + if ((cause == DamageCause.FIRE + || cause == DamageCause.FIRE_TICK) + && (Permissions.PREVENTDAMAGE_FIRE.isAuthorized(user) + && !Permissions.PREVENTDAMAGE_NONE.isAuthorized(user))) + { + event.setCancelled(true); + return; + } + if (cause == DamageCause.DROWNING + && (Permissions.PREVENTDAMAGE_DROWNING.isAuthorized(user) + && !Permissions.PREVENTDAMAGE_NONE.isAuthorized(user))) + { + event.setCancelled(true); + return; + } + if (cause == DamageCause.LIGHTNING + && (Permissions.PREVENTDAMAGE_LIGHTNING.isAuthorized(user) + && !Permissions.PREVENTDAMAGE_NONE.isAuthorized(user))) + { + event.setCancelled(true); + return; + } } } + finally + { + settings.unlock(); + } + } - final DamageCause cause = event.getCause(); - if (target instanceof Player) + @EventHandler(priority = EventPriority.HIGHEST) + public void onEntityExplode(final EntityExplodeEvent event) + { + if (event.isCancelled()) { - if (cause == DamageCause.FALL - && prot.getSettingBool(ProtectConfig.disable_fall) - && !(user.isAuthorized("essentials.protect.damage.fall") - && !user.isAuthorized("essentials.protect.damage.disable"))) - { - event.setCancelled(true); - return; - } + return; + } + final ProtectHolder settings = prot.getSettings(); + settings.acquireReadLock(); + try + { + final int maxHeight = settings.getData().getCreeperMaxHeight(); - if (cause == DamageCause.SUFFOCATION - && prot.getSettingBool(ProtectConfig.disable_suffocate) - && !(user.isAuthorized("essentials.protect.damage.suffocation") - && !user.isAuthorized("essentials.protect.damage.disable"))) + if (event.getEntity() instanceof EnderDragon + && settings.getData().getPrevent().isEnderdragonBlockdamage()) { event.setCancelled(true); return; } - if ((cause == DamageCause.FIRE - || cause == DamageCause.FIRE_TICK) - && prot.getSettingBool(ProtectConfig.disable_firedmg) - && !(user.isAuthorized("essentials.protect.damage.fire") - && !user.isAuthorized("essentials.protect.damage.disable"))) + else if (event.getEntity() instanceof Creeper + && (settings.getData().getPrevent().isCreeperExplosion() + || settings.getData().getPrevent().isCreeperBlockdamage() + || (maxHeight >= 0 && event.getLocation().getBlockY() > maxHeight))) { + //Nicccccccccce plaaacccccccccce.. + FakeExplosion.createExplosion(event, ess.getServer(), ess.getServer().getOnlinePlayers()); event.setCancelled(true); return; } - if (cause == DamageCause.DROWNING - && prot.getSettingBool(ProtectConfig.disable_drown) - && !(user.isAuthorized("essentials.protect.damage.drowning") - && !user.isAuthorized("essentials.protect.damage.disable"))) + else if (event.getEntity() instanceof TNTPrimed + && settings.getData().getPrevent().isTntExplosion()) { event.setCancelled(true); return; } - if (cause == DamageCause.LIGHTNING - && prot.getSettingBool(ProtectConfig.disable_lightning) - && !(user.isAuthorized("essentials.protect.damage.lightning") - && !user.isAuthorized("essentials.protect.damage.disable"))) + else if ((event.getEntity() instanceof Fireball || event.getEntity() instanceof SmallFireball) + && settings.getData().getPrevent().isFireballExplosion()) { event.setCancelled(true); return; } - } - } - - @EventHandler(priority = EventPriority.HIGHEST) - public void onEntityExplode(final EntityExplodeEvent event) - { - if (event.isCancelled()) - { - return; - } - final int maxHeight = ess.getSettings().getProtectCreeperMaxHeight(); + // This code will prevent explosions near protected rails, signs or protected chests + // TODO: Use protect db instead of this code - if (event.getEntity() instanceof EnderDragon - && prot.getSettingBool(ProtectConfig.prevent_enderdragon_blockdmg)) - { - if (prot.getSettingBool(ProtectConfig.enderdragon_fakeexplosions)) + for (Block block : event.blockList()) { - FakeExplosion.createExplosion(event, ess.getServer(), ess.getServer().getOnlinePlayers()); + 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) + && settings.getData().getSignsAndRails().isProtectRails()) + { + 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) + && settings.getData().getSignsAndRails().isProtectSigns()) + { + event.setCancelled(true); + return; + } } - 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.. - FakeExplosion.createExplosion(event, ess.getServer(), ess.getServer().getOnlinePlayers()); - event.setCancelled(true); - 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)) + finally { - 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; - } + settings.unlock(); } } @@ -280,37 +265,37 @@ public class EssentialsProtectEntityListener implements Listener { return; } - final String creatureName = creature.toString().toLowerCase(Locale.ENGLISH); - if (creatureName == null || creatureName.isEmpty()) + final ProtectHolder settings = prot.getSettings(); + settings.acquireReadLock(); + try { - return; + final Boolean prevent = settings.getData().getPrevent().getSpawn().get(creature); + if (prevent != null && prevent) + { + event.setCancelled(true); + } } - if (ess.getSettings().getProtectPreventSpawn(creatureName)) + finally { - event.setCancelled(true); + settings.unlock(); } } @EventHandler(priority = EventPriority.HIGHEST) public void onEntityTarget(final EntityTargetEvent event) { - if (event.isCancelled()) + if (event.isCancelled() || !(event.getTarget() instanceof Player)) { return; } - if (!(event.getTarget() instanceof Player)) - { - return; - } - final User user = ess.getUser(event.getTarget()); + final Player user = (Player)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.TARGET_ATTACKED_OWNER || event.getReason() == TargetReason.OWNER_ATTACKED_TARGET) - && prot.getSettingBool(ProtectConfig.prevent_entitytarget) - && !user.isAuthorized("essentials.protect.entitytarget.bypass")) + && Permissions.ENTITYTARGET.isAuthorized(user)) { event.setCancelled(true); return; @@ -318,26 +303,43 @@ public class EssentialsProtectEntityListener implements Listener } @EventHandler(priority = EventPriority.HIGHEST) - public void onExplosionPrime(ExplosionPrimeEvent event) + public void onExplosionPrime(final ExplosionPrimeEvent event) { - if ((event.getEntity() instanceof Fireball || event.getEntity() instanceof SmallFireball) - && prot.getSettingBool(ProtectConfig.prevent_fireball_fire)) + final ProtectHolder settings = prot.getSettings(); + settings.acquireReadLock(); + try { - event.setFire(false); + if ((event.getEntity() instanceof Fireball || event.getEntity() instanceof SmallFireball) + && settings.getData().getPrevent().isFireballFire()) + { + event.setFire(false); + } + } + finally + { + settings.unlock(); } } @EventHandler(priority = EventPriority.HIGHEST) - public void onEntityChangeBlock(EntityChangeBlockEvent event) + public void onEntityChangeBlock(final EntityChangeBlockEvent event) { if (event.isCancelled()) { return; } - if (prot.getSettingBool(ProtectConfig.prevent_enderman_pickup)) + final ProtectHolder settings = prot.getSettings(); + settings.acquireReadLock(); + try { - event.setCancelled(true); - return; + if (settings.getData().getPrevent().isEndermanPickup()) + { + event.setCancelled(true); + } + } + finally + { + settings.unlock(); } } } diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectPlayerListener.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectPlayerListener.java index 6e515e56d..a14fde8bc 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectPlayerListener.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectPlayerListener.java @@ -1,10 +1,10 @@ package com.earth2me.essentials.protect; import static com.earth2me.essentials.I18n._; -import com.earth2me.essentials.IEssentials; -import com.earth2me.essentials.User; +import com.earth2me.essentials.api.IEssentials; import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; @@ -28,65 +28,73 @@ public class EssentialsProtectPlayerListener implements Listener 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()); + final Player user = event.getPlayer(); - if (event.hasItem() - && (event.getItem().getType() == Material.WATER_BUCKET - || event.getItem().getType() == Material.LAVA_BUCKET) - && prot.getSettingBool(ProtectConfig.disable_build) && !user.canBuild()) + final ProtectHolder settings = prot.getSettings(); + settings.acquireReadLock(); + try { - if (ess.getSettings().warnOnBuildDisallow()) + if (event.hasItem() + && (event.getItem().getType() == Material.WATER_BUCKET + || event.getItem().getType() == Material.LAVA_BUCKET) + && !Permissions.BUILD.isAuthorized(user)) { - user.sendMessage(_("buildAlert")); + if (settings.getData().isWarnOnBuildDisallow()) + { + user.sendMessage(_("buildAlert")); + } + event.setCancelled(true); + return; } - event.setCancelled(true); - return; - } - if (prot.getSettingBool(ProtectConfig.disable_use) && !user.canBuild()) - { - if (ess.getSettings().warnOnBuildDisallow()) + if (!Permissions.INTERACT.isAuthorized(user)) { - user.sendMessage(_("buildAlert")); + if (settings.getData().isWarnOnBuildDisallow()) + { + user.sendMessage(_("buildAlert")); + } + event.setCancelled(true); + return; } - event.setCancelled(true); - return; - } - final ItemStack item = event.getItem(); - if (item != null - && prot.checkProtectionItems(ProtectConfig.blacklist_usage, item.getTypeId()) - && !user.isAuthorized("essentials.protect.exemptusage")) - { - event.setCancelled(true); - return; - } + final ItemStack item = event.getItem(); + if (item != null + && !ItemUsePermissions.getPermission(item.getType()).isAuthorized(user)) + { + event.setCancelled(true); + return; + } - 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 (Permissions.OWNERINFO.isAuthorized(user) && event.getAction() == Action.RIGHT_CLICK_BLOCK) { - if (!first) + 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()) { - stringBuilder.append(", "); + user.sendMessage(_("protectionOwner", ownerNames)); } - first = false; - stringBuilder.append(owner); } - final String ownerNames = stringBuilder.toString(); - if (ownerNames != null && !ownerNames.isEmpty()) + if (item != null + && !Permissions.ALERTS_NOTRIGGER.isAuthorized(user) + && settings.getData().getAlertOnUse().contains(item.getType())) { - user.sendMessage(_("protectionOwner", ownerNames)); + prot.getEssentialsConnect().alert(user, item.getType().toString(), _("alertUsed")); } } - if (item != null - && !user.hasPermission("essentials.protect.alerts.notrigger") - && prot.checkProtectionItems(ProtectConfig.alert_on_use, item.getTypeId())) + finally { - prot.getEssentialsConnect().alert(user, item.getType().toString(), _("alertUsed")); + settings.unlock(); } } } diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectWeatherListener.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectWeatherListener.java index 1b8dd19ff..babebac71 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectWeatherListener.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectWeatherListener.java @@ -20,33 +20,59 @@ public class EssentialsProtectWeatherListener implements Listener @EventHandler(priority = EventPriority.HIGHEST) public void onWeatherChange(final WeatherChangeEvent event) { - if (!event.isCancelled() - && prot.getSettingBool(ProtectConfig.disable_weather_storm) - && event.toWeatherState()) + final ProtectHolder settings = prot.getSettings(); + settings.acquireReadLock(); + try { - event.setCancelled(true); + if (!event.isCancelled() + && settings.getData().isDisableStorm() + && event.toWeatherState()) + { + event.setCancelled(true); + } + } + finally + { + settings.unlock(); } - } @EventHandler(priority = EventPriority.HIGHEST) public void onLightningStrike(final LightningStrikeEvent event) { - if (!event.isCancelled() - && prot.getSettingBool(ProtectConfig.disable_weather_lightning)) + final ProtectHolder settings = prot.getSettings(); + settings.acquireReadLock(); + try { - event.setCancelled(true); + if (!event.isCancelled() + && settings.getData().isDisableLighting()) + { + event.setCancelled(true); + } + } + finally + { + settings.unlock(); } } @EventHandler(priority = EventPriority.HIGHEST) public void onThunderChange(final ThunderChangeEvent event) { - if (!event.isCancelled() - && prot.getSettingBool(ProtectConfig.disable_weather_thunder) - && event.toThunderState()) + final ProtectHolder settings = prot.getSettings(); + settings.acquireReadLock(); + try + { + if (!event.isCancelled() + && settings.getData().isDisableThunder() + && event.toThunderState()) + { + event.setCancelled(true); + } + } + finally { - event.setCancelled(true); + settings.unlock(); } } } diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/IProtect.java b/EssentialsProtect/src/com/earth2me/essentials/protect/IProtect.java index afcb6e8ce..d25661bf8 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/IProtect.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/IProtect.java @@ -1,28 +1,24 @@ package com.earth2me.essentials.protect; import com.earth2me.essentials.protect.data.IProtectedBlock; -import java.util.List; -import java.util.Map; import org.bukkit.plugin.Plugin; public interface IProtect extends Plugin { - boolean checkProtectionItems(final ProtectConfig list, final int id); - - boolean getSettingBool(final ProtectConfig protectConfig); - - String getSettingString(final ProtectConfig protectConfig); - +// boolean checkProtectionItems(final ProtectConfig list, final int id); +// boolean getSettingBool(final ProtectConfig protectConfig); +// String getSettingString(final ProtectConfig protectConfig); IProtectedBlock getStorage(); void setStorage(IProtectedBlock pb); EssentialsConnect getEssentialsConnect(); - Map<ProtectConfig, Boolean> getSettingsBoolean(); - - Map<ProtectConfig, String> getSettingsString(); +// Map<ProtectConfig, Boolean> getSettingsBoolean(); +// Map<ProtectConfig, String> getSettingsString(); +// Map<ProtectConfig, List<Integer>> getSettingsList(); + ProtectHolder getSettings(); - Map<ProtectConfig, List<Integer>> getSettingsList(); + void setSettings(ProtectHolder settings); } diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/ItemUsePermissions.java b/EssentialsProtect/src/com/earth2me/essentials/protect/ItemUsePermissions.java new file mode 100644 index 000000000..258a93761 --- /dev/null +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/ItemUsePermissions.java @@ -0,0 +1,42 @@ +package com.earth2me.essentials.protect; + +import com.earth2me.essentials.api.IPermission; +import com.earth2me.essentials.perm.AbstractSuperpermsPermission; +import java.util.EnumMap; +import java.util.Locale; +import java.util.Map; +import org.bukkit.Material; +import org.bukkit.permissions.PermissionDefault; + +public class ItemUsePermissions extends AbstractSuperpermsPermission{ + private static Map<Material,IPermission> permissions = new EnumMap<Material, IPermission>(Material.class); + private static final String base = "essentials.protect.itemuse."; + private final String permission; + + public static IPermission getPermission(Material mat) + { + IPermission perm = permissions.get(mat); + if (perm == null) { + perm = new ItemUsePermissions(mat.toString().toLowerCase(Locale.ENGLISH)); + permissions.put(mat, perm); + } + return perm; + } + + private ItemUsePermissions(String matName) + { + this.permission = base + matName; + } + + @Override + public String getPermission() + { + return this.permission; + } + + @Override + public PermissionDefault getPermissionDefault() + { + return PermissionDefault.TRUE; + } +} diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/Permissions.java b/EssentialsProtect/src/com/earth2me/essentials/protect/Permissions.java new file mode 100644 index 000000000..1ed32ddda --- /dev/null +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/Permissions.java @@ -0,0 +1,81 @@ +package com.earth2me.essentials.protect; + +import com.earth2me.essentials.Util; +import com.earth2me.essentials.api.IPermission; +import java.util.Locale; +import org.bukkit.command.CommandSender; +import org.bukkit.permissions.Permission; +import org.bukkit.permissions.PermissionDefault; + + +public enum Permissions implements IPermission +{ + ALERTS, + ALERTS_NOTRIGGER, + ADMIN, + BUILD(PermissionDefault.TRUE), + ENTITYTARGET(PermissionDefault.TRUE), + INTERACT(PermissionDefault.TRUE), + OWNERINFO, + PVP(PermissionDefault.TRUE), + PREVENTDAMAGE_FALL(PermissionDefault.FALSE), + PREVENTDAMAGE_CREEPER(PermissionDefault.FALSE), + PREVENTDAMAGE_CONTACT(PermissionDefault.FALSE), + PREVENTDAMAGE_FIREBALL(PermissionDefault.FALSE), + PREVENTDAMAGE_PROJECTILES(PermissionDefault.FALSE), + PREVENTDAMAGE_LAVADAMAGE(PermissionDefault.FALSE), + PREVENTDAMAGE_TNT(PermissionDefault.FALSE), + PREVENTDAMAGE_SUFFOCATION(PermissionDefault.FALSE), + PREVENTDAMAGE_FIRE(PermissionDefault.FALSE), + PREVENTDAMAGE_DROWNING(PermissionDefault.FALSE), + PREVENTDAMAGE_LIGHTNING(PermissionDefault.FALSE), + PREVENTDAMAGE_NONE(PermissionDefault.FALSE), + RAILS(PermissionDefault.TRUE), + USEFLINTSTEEL(PermissionDefault.TRUE); + private static final String base = "essentials.protect."; + private final String permission; + private final PermissionDefault defaultPerm; + private transient Permission bukkitPerm = null; + + private Permissions() + { + this(PermissionDefault.OP); + } + + private Permissions(final PermissionDefault defaultPerm) + { + permission = base + toString().toLowerCase(Locale.ENGLISH).replace('_', '.'); + this.defaultPerm = defaultPerm; + } + + @Override + public String getPermission() + { + return permission; + } + + @Override + public Permission getBukkitPermission() + { + if (bukkitPerm != null) + { + return bukkitPerm; + } + else + { + return Util.registerPermission(getPermission(), getPermissionDefault()); + } + } + + @Override + public PermissionDefault getPermissionDefault() + { + return this.defaultPerm; + } + + @Override + public boolean isAuthorized(CommandSender sender) + { + return sender.hasPermission(getBukkitPermission()); + } +} diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/ProtectConfig.java b/EssentialsProtect/src/com/earth2me/essentials/protect/ProtectConfig.java index bee8d0f4d..feeeb19df 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/ProtectConfig.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/ProtectConfig.java @@ -1,7 +1,7 @@ package com.earth2me.essentials.protect; -public enum ProtectConfig +/*public enum ProtectConfig { datatype("protect.datatype", "sqlite"), mysqlDB("protect.mysqlDb", "jdbc:mysql://localhost:3306/minecraft"), @@ -87,7 +87,7 @@ public enum ProtectConfig /** * @return the configName - */ + *//* public String getConfigName() { return configName; @@ -95,7 +95,7 @@ public enum ProtectConfig /** * @return the default value String - */ + *//* public String getDefaultValueString() { return defValueString; @@ -103,7 +103,7 @@ public enum ProtectConfig /** * @return the default value boolean - */ + *//* public boolean getDefaultValueBoolean() { return defValueBoolean; @@ -118,4 +118,4 @@ public enum ProtectConfig { return isList; } -} +}*/ diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/ProtectHolder.java b/EssentialsProtect/src/com/earth2me/essentials/protect/ProtectHolder.java new file mode 100644 index 000000000..2e2451397 --- /dev/null +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/ProtectHolder.java @@ -0,0 +1,22 @@ +package com.earth2me.essentials.protect; + +import com.earth2me.essentials.api.IEssentials; +import com.earth2me.essentials.settings.protect.Protect; +import com.earth2me.essentials.storage.AsyncStorageObjectHolder; +import java.io.File; +import java.io.IOException; + + +public class ProtectHolder extends AsyncStorageObjectHolder<Protect> +{ + public ProtectHolder(IEssentials ess) + { + super(ess, Protect.class); + } + + @Override + public File getStorageFile() throws IOException + { + return new File(ess.getDataFolder(), "protect.yml"); + } +} diff --git a/EssentialsProtect/src/plugin.yml b/EssentialsProtect/src/plugin.yml index 6407c45ee..42fe156b9 100644 --- a/EssentialsProtect/src/plugin.yml +++ b/EssentialsProtect/src/plugin.yml @@ -3,7 +3,7 @@ name: EssentialsProtect main: com.earth2me.essentials.protect.EssentialsProtect # Note to developers: This next line cannot change, or the automatic versioning system will break. version: TeamCity -website: http://www.earth2me.net:8001/ +website: http://tiny.cc/EssentialsWiki description: Provides protection for various parts of the world. authors: [Zenexer, ementalo, Aelux, Brettflan, KimKandor, snowleo, ceulemans, Xeology, KHobbits] -softdepend: [Essentials]
\ No newline at end of file +softdepend: [Essentials3]
\ No newline at end of file |