diff options
Diffstat (limited to 'EssentialsProtect/src/com/earth2me')
13 files changed, 885 insertions, 559 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 569123cd5..38c6cc3f9 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsConnect.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsConnect.java @@ -1,13 +1,13 @@ package com.earth2me.essentials.protect; -import com.earth2me.essentials.IConf; -import com.earth2me.essentials.IEssentials; -import com.earth2me.essentials.User; +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.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.Location; @@ -29,8 +29,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); LOGGER.info(_("loadinfo", essProtect.getDescription().getName(), essProtect.getDescription().getVersion(), "essentials team")); @@ -45,7 +46,7 @@ public class EssentialsConnect return ess; } - public void alert(final User user, final String item, final String type) + public void alert(final IUser user, final String item, final String type) { final Location loc = user.getLocation(); final String warnMessage = _("alertFormat", user.getName(), type, item, @@ -54,7 +55,7 @@ public class EssentialsConnect LOGGER.log(Level.WARNING, warnMessage); for (Player p : ess.getServer().getOnlinePlayers()) { - final User alertUser = ess.getUser(p); + final IUser alertUser = ess.getUser(p); if (alertUser.isAuthorized("essentials.protect.alerts")) { alertUser.sendMessage(warnMessage); @@ -63,62 +64,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 e313298cd..43975216e 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; @@ -20,11 +17,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() @@ -95,12 +93,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() @@ -119,7 +117,7 @@ public class EssentialsProtect extends JavaPlugin implements IProtect return ess; } - public Map<ProtectConfig, Boolean> getSettingsBoolean() + /*public Map<ProtectConfig, Boolean> getSettingsBoolean() { return settingsBoolean; } @@ -146,7 +144,7 @@ public class EssentialsProtect extends JavaPlugin implements IProtect { final String str = settingsString.get(protectConfig); return str == null ? protectConfig.getDefaultValueString() : str; - } + }*/ public void onDisable() { @@ -163,4 +161,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 a1d16af25..1d90384c1 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java @@ -1,8 +1,8 @@ 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.api.IUser; import com.earth2me.essentials.protect.data.IProtectedBlock; import java.util.ArrayList; import java.util.List; @@ -31,65 +31,73 @@ public class EssentialsProtectBlockListener extends BlockListener return; } - final User user = ess.getUser(event.getPlayer()); - - if (prot.getSettingBool(ProtectConfig.disable_build) && !user.canBuild()) + final IUser user = ess.getUser(event.getPlayer()); + final ProtectHolder settings = prot.getSettings(); + settings.acquireReadLock(); + try { - event.setCancelled(true); - return; - } + if (!user.isAuthorized(Permissions.BUILD)) + { + 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 (!user.isAuthorized(BlockPlacePermissions.getPermission(blockPlaced.getType()))) + { + event.setCancelled(true); + return; + } - if (prot.checkProtectionItems(ProtectConfig.alert_on_placement, id)) - { - prot.getEssentialsConnect().alert(user, blockPlaced.getType().toString(), _("alertPlaced")); - } + if (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() + && user.isAuthorized("essentials.protect")) { - 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(); } } @@ -100,47 +108,56 @@ public class EssentialsProtectBlockListener extends BlockListener { 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(ess.getUser(event.getPlayer()).isAuthorized(Permissions.USEFLINTSTEEL)); + 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(); } } @@ -151,37 +168,45 @@ public class EssentialsProtectBlockListener extends BlockListener { 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(); } } @@ -192,22 +217,32 @@ public class EssentialsProtectBlockListener extends BlockListener { 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[] @@ -228,9 +263,9 @@ public class EssentialsProtectBlockListener extends BlockListener { return; } - final User user = ess.getUser(event.getPlayer()); + final IUser user = ess.getUser(event.getPlayer()); - if (prot.getSettingBool(ProtectConfig.disable_build) && !user.canBuild()) + if (!user.isAuthorized(Permissions.BUILD)) { event.setCancelled(true); return; @@ -238,29 +273,41 @@ public class EssentialsProtectBlockListener extends BlockListener final Block block = event.getBlock(); final int typeId = block.getTypeId(); - if (prot.checkProtectionItems(ProtectConfig.blacklist_break, typeId) - && !user.isAuthorized("essentials.protect.exemptbreak")) + if (!user.isAuthorized(BlockBreakPermissions.getPermission(block.getType()))) { event.setCancelled(true); return; } - final Material type = block.getType(); - - if (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 (settings.getData().getAlertOnBreak().contains(type)) { - storage.unprotectBlock(block); - if (type == Material.RAILS || type == Material.POWERED_RAIL || type == Material.DETECTOR_RAIL || type == Material.SIGN_POST) + prot.getEssentialsConnect().alert(user, type.toString(), _("alertBroke")); + } + 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) { - 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 { @@ -273,30 +320,30 @@ public class EssentialsProtectBlockListener extends BlockListener } 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 { @@ -307,28 +354,89 @@ public class EssentialsProtectBlockListener extends BlockListener } } } - else + } + } + finally + { + settings.unlock(); + } + } + + @Override + public void onBlockPistonExtend(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(); + } } @Override - public void onBlockPistonExtend(BlockPistonExtendEvent event) + public void onBlockPistonRetract(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; @@ -339,12 +447,12 @@ public class EssentialsProtectBlockListener extends BlockListener || 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) { @@ -370,70 +478,22 @@ public class EssentialsProtectBlockListener extends BlockListener } } } - } - - @Override - 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())) - { - 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)) + finally { - 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 IUser 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) { @@ -458,14 +518,14 @@ public class EssentialsProtectBlockListener extends BlockListener } } } - } + } - 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 89167b2b9..99b0c72e7 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectEntityListener.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectEntityListener.java @@ -1,9 +1,8 @@ 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.api.IUser; import com.earth2me.essentials.craftbukkit.FakeExplosion; -import java.util.Locale; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; @@ -20,6 +19,7 @@ public class EssentialsProtectEntityListener extends EntityListener public EssentialsProtectEntityListener(final IProtect prot) { + super(); this.prot = prot; this.ess = prot.getEssentialsConnect().getEssentials(); } @@ -31,233 +31,219 @@ public class EssentialsProtectEntityListener extends EntityListener { 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 IUser user = target instanceof Player ? ess.getUser((Player)target) : null; + if (target instanceof Player && event instanceof EntityDamageByBlockEvent) { - event.setCancelled(true); - return; + final DamageCause cause = event.getCause(); + + if (cause == DamageCause.CONTACT + && (user.isAuthorized(Permissions.PREVENTDAMAGE_CONTACT) + && !user.isAuthorized(Permissions.PREVENTDAMAGE_NONE))) + { + event.setCancelled(true); + return; + } + if (cause == DamageCause.LAVA + && (user.isAuthorized(Permissions.PREVENTDAMAGE_LAVADAMAGE) + && !user.isAuthorized(Permissions.PREVENTDAMAGE_NONE))) + { + event.setCancelled(true); + return; + } + if (cause == DamageCause.BLOCK_EXPLOSION + && (user.isAuthorized(Permissions.PREVENTDAMAGE_TNT) + && !user.isAuthorized(Permissions.PREVENTDAMAGE_NONE))) + { + 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 IUser attacker = eAttack instanceof Player ? ess.getUser((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 + && (!user.isAuthorized(Permissions.PVP) || !attacker.isAuthorized(Permissions.PVP))) + { + 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() + || (user.isAuthorized(Permissions.PREVENTDAMAGE_CREEPER) + && !user.isAuthorized(Permissions.PREVENTDAMAGE_NONE))) + { + 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) + && (user.isAuthorized(Permissions.PREVENTDAMAGE_FIREBALL) + && !user.isAuthorized(Permissions.PREVENTDAMAGE_NONE))) + { + 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 + && (user.isAuthorized(Permissions.PREVENTDAMAGE_TNT) + && !user.isAuthorized(Permissions.PREVENTDAMAGE_NONE))) + { + 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 + && ((user.isAuthorized(Permissions.PREVENTDAMAGE_PROJECTILES) + && !user.isAuthorized(Permissions.PREVENTDAMAGE_NONE)) + || (((Projectile)edEvent.getDamager()).getShooter() instanceof Player + && (!user.isAuthorized(Permissions.PVP) + || !ess.getUser((Player)((Projectile)edEvent.getDamager()).getShooter()).isAuthorized(Permissions.PVP))))) + { + 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 + && (user.isAuthorized(Permissions.PREVENTDAMAGE_FALL) + && !user.isAuthorized(Permissions.PREVENTDAMAGE_NONE))) + { + 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 + && (user.isAuthorized(Permissions.PREVENTDAMAGE_SUFFOCATION) + && !user.isAuthorized(Permissions.PREVENTDAMAGE_NONE))) + { + event.setCancelled(true); + return; + } + if ((cause == DamageCause.FIRE + || cause == DamageCause.FIRE_TICK) + && (user.isAuthorized(Permissions.PREVENTDAMAGE_FIRE) + && !user.isAuthorized(Permissions.PREVENTDAMAGE_NONE))) + { + event.setCancelled(true); + return; + } + if (cause == DamageCause.DROWNING + && (user.isAuthorized(Permissions.PREVENTDAMAGE_DROWNING) + && !user.isAuthorized(Permissions.PREVENTDAMAGE_NONE))) + { + event.setCancelled(true); + return; + } + if (cause == DamageCause.LIGHTNING + && (user.isAuthorized(Permissions.PREVENTDAMAGE_LIGHTNING) + && !user.isAuthorized(Permissions.PREVENTDAMAGE_NONE))) + { + event.setCancelled(true); + return; + } } } + finally + { + settings.unlock(); + } + } - final DamageCause cause = event.getCause(); - if (target instanceof Player) + @Override + 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; } - } - } - - @Override - 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(); } } @@ -277,37 +263,37 @@ public class EssentialsProtectEntityListener extends EntityListener { 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(); } } @Override 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 IUser user = ess.getUser((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")) + && user.isAuthorized(Permissions.ENTITYTARGET)) { event.setCancelled(true); return; @@ -315,26 +301,43 @@ public class EssentialsProtectEntityListener extends EntityListener } @Override - 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(); } } @Override - public void onEndermanPickup(EndermanPickupEvent event) + public void onEndermanPickup(final EndermanPickupEvent 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 ad7831a53..5e6da11cc 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectPlayerListener.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectPlayerListener.java @@ -1,8 +1,8 @@ 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.api.IUser; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.event.block.Action; @@ -26,64 +26,72 @@ public class EssentialsProtectPlayerListener extends PlayerListener 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 IUser user = ess.getUser(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) + && !user.isAuthorized(Permissions.BUILD)) { - 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 (!user.isAuthorized(Permissions.INTERACT)) { - 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 + && !user.isAuthorized(ItemUsePermissions.getPermission(item.getType()))) + { + 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 (user.isAuthorized("essentials.protect.ownerinfo") && 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 + && settings.getData().getAlertOnUse().contains(item.getType())) { - user.sendMessage(_("protectionOwner", ownerNames)); + prot.getEssentialsConnect().alert(user, item.getType().toString(), _("alertUsed")); } } - if (item != null - && 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 e81efc72b..4097275dd 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectWeatherListener.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectWeatherListener.java @@ -18,33 +18,59 @@ public class EssentialsProtectWeatherListener extends WeatherListener @Override 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(); } - } @Override 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(); } } @Override 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..61ac1b295 --- /dev/null +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/Permissions.java @@ -0,0 +1,69 @@ +package com.earth2me.essentials.protect; + +import com.earth2me.essentials.Util; +import com.earth2me.essentials.api.IPermission; +import java.util.Locale; +import org.bukkit.permissions.Permission; +import org.bukkit.permissions.PermissionDefault; + + +public enum Permissions implements IPermission +{ + BUILD(PermissionDefault.TRUE), + INTERACT(PermissionDefault.TRUE), + USEFLINTSTEEL(PermissionDefault.TRUE), + PVP(PermissionDefault.TRUE), + ENTITYTARGET(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); + 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; + } +} 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"); + } +} |