summaryrefslogtreecommitdiffstats
path: root/EssentialsAntiBuild/src
diff options
context:
space:
mode:
authorKHobbits <rob@khobbits.co.uk>2012-08-18 21:19:00 +0100
committerKHobbits <rob@khobbits.co.uk>2012-08-19 14:54:58 +0100
commit444ec11dc173b51c13859526718cd1e0935d3631 (patch)
tree15bf56ef6b47ded27a60af421e6e809f700ebf3a /EssentialsAntiBuild/src
parentc651cc2b6d62ba29c8a6d0217cb8568b92921cff (diff)
downloadEssentials-444ec11dc173b51c13859526718cd1e0935d3631.tar
Essentials-444ec11dc173b51c13859526718cd1e0935d3631.tar.gz
Essentials-444ec11dc173b51c13859526718cd1e0935d3631.tar.lz
Essentials-444ec11dc173b51c13859526718cd1e0935d3631.tar.xz
Essentials-444ec11dc173b51c13859526718cd1e0935d3631.zip
Split Ess protect into EssProtect and EssAntiBuild
New permissions: essentials.build - same as build: true essentials.build.interact.<id> - whitelist single interact block essentials.build.place.<id> - whitelist single place block essentials.build.break.<id> - whitelist single break block essentials.build.use.<id> - whitelist single use item
Diffstat (limited to 'EssentialsAntiBuild/src')
-rw-r--r--EssentialsAntiBuild/src/com/earth2me/essentials/antibuild/AntiBuildConfig.java73
-rw-r--r--EssentialsAntiBuild/src/com/earth2me/essentials/antibuild/EssentialsAntiBuild.java65
-rw-r--r--EssentialsAntiBuild/src/com/earth2me/essentials/antibuild/EssentialsAntiBuildListener.java179
-rw-r--r--EssentialsAntiBuild/src/com/earth2me/essentials/antibuild/EssentialsConnect.java80
-rw-r--r--EssentialsAntiBuild/src/com/earth2me/essentials/antibuild/IAntiBuild.java19
-rw-r--r--EssentialsAntiBuild/src/plugin.yml9
6 files changed, 425 insertions, 0 deletions
diff --git a/EssentialsAntiBuild/src/com/earth2me/essentials/antibuild/AntiBuildConfig.java b/EssentialsAntiBuild/src/com/earth2me/essentials/antibuild/AntiBuildConfig.java
new file mode 100644
index 000000000..1aa33d02a
--- /dev/null
+++ b/EssentialsAntiBuild/src/com/earth2me/essentials/antibuild/AntiBuildConfig.java
@@ -0,0 +1,73 @@
+package com.earth2me.essentials.antibuild;
+
+
+public enum AntiBuildConfig
+{
+ disable_build("protect.disable.build", true),
+ disable_use("protect.disable.use", true),
+ alert_on_placement("protect.alert.on-placement"),
+ alert_on_use("protect.alert.on-use"),
+ alert_on_break("protect.alert.on-break"),
+ blacklist_placement("protect.blacklist.placement"),
+ blacklist_usage("protect.blacklist.usage"),
+ blacklist_break("protect.blacklist.break"),
+ blacklist_piston("protect.blacklist.piston");
+ private final String configName;
+ private final String defValueString;
+ private final boolean defValueBoolean;
+ private final boolean isList;
+ private final boolean isString;
+
+ private AntiBuildConfig(final String configName)
+ {
+ this(configName, null, false, true, false);
+ }
+
+ private AntiBuildConfig(final String configName, final boolean defValueBoolean)
+ {
+ this(configName, null, defValueBoolean, false, false);
+ }
+
+ private AntiBuildConfig(final String configName, final String defValueString, final boolean defValueBoolean, final boolean isList, final boolean isString)
+ {
+ this.configName = configName;
+ this.defValueString = defValueString;
+ this.defValueBoolean = defValueBoolean;
+ this.isList = isList;
+ this.isString = isString;
+ }
+
+ /**
+ * @return the configName
+ */
+ public String getConfigName()
+ {
+ return configName;
+ }
+
+ /**
+ * @return the default value String
+ */
+ public String getDefaultValueString()
+ {
+ return defValueString;
+ }
+
+ /**
+ * @return the default value boolean
+ */
+ public boolean getDefaultValueBoolean()
+ {
+ return defValueBoolean;
+ }
+
+ public boolean isString()
+ {
+ return isString;
+ }
+
+ public boolean isList()
+ {
+ return isList;
+ }
+}
diff --git a/EssentialsAntiBuild/src/com/earth2me/essentials/antibuild/EssentialsAntiBuild.java b/EssentialsAntiBuild/src/com/earth2me/essentials/antibuild/EssentialsAntiBuild.java
new file mode 100644
index 000000000..c7b2eb7dd
--- /dev/null
+++ b/EssentialsAntiBuild/src/com/earth2me/essentials/antibuild/EssentialsAntiBuild.java
@@ -0,0 +1,65 @@
+package com.earth2me.essentials.antibuild;
+
+import java.util.EnumMap;
+import java.util.List;
+import java.util.Map;
+import java.util.logging.Logger;
+import org.bukkit.plugin.Plugin;
+import org.bukkit.plugin.PluginManager;
+import org.bukkit.plugin.java.JavaPlugin;
+
+
+public class EssentialsAntiBuild extends JavaPlugin implements IAntiBuild
+{
+ private static final Logger LOGGER = Logger.getLogger("Minecraft");
+ private final transient Map<AntiBuildConfig, Boolean> settingsBoolean = new EnumMap<AntiBuildConfig, Boolean>(AntiBuildConfig.class);
+ private final transient Map<AntiBuildConfig, List<Integer>> settingsList = new EnumMap<AntiBuildConfig, List<Integer>>(AntiBuildConfig.class);
+ private transient EssentialsConnect ess = null;
+
+ @Override
+ public void onEnable()
+ {
+ final PluginManager pm = this.getServer().getPluginManager();
+ final Plugin essPlugin = pm.getPlugin("Essentials");
+ if (essPlugin == null || !essPlugin.isEnabled())
+ {
+ return;
+ }
+ ess = new EssentialsConnect(essPlugin, this);
+
+ final EssentialsAntiBuildListener blockListener = new EssentialsAntiBuildListener(this);
+ pm.registerEvents(blockListener, this);
+ }
+
+ @Override
+ public boolean checkProtectionItems(final AntiBuildConfig list, final int id)
+ {
+ final List<Integer> itemList = settingsList.get(list);
+ return itemList != null && !itemList.isEmpty() && itemList.contains(id);
+ }
+
+ @Override
+ public EssentialsConnect getEssentialsConnect()
+ {
+ return ess;
+ }
+
+ @Override
+ public Map<AntiBuildConfig, Boolean> getSettingsBoolean()
+ {
+ return settingsBoolean;
+ }
+
+ @Override
+ public Map<AntiBuildConfig, List<Integer>> getSettingsList()
+ {
+ return settingsList;
+ }
+
+ @Override
+ public boolean getSettingBool(final AntiBuildConfig protectConfig)
+ {
+ final Boolean bool = settingsBoolean.get(protectConfig);
+ return bool == null ? protectConfig.getDefaultValueBoolean() : bool;
+ }
+}
diff --git a/EssentialsAntiBuild/src/com/earth2me/essentials/antibuild/EssentialsAntiBuildListener.java b/EssentialsAntiBuild/src/com/earth2me/essentials/antibuild/EssentialsAntiBuildListener.java
new file mode 100644
index 000000000..af6b6329d
--- /dev/null
+++ b/EssentialsAntiBuild/src/com/earth2me/essentials/antibuild/EssentialsAntiBuildListener.java
@@ -0,0 +1,179 @@
+package com.earth2me.essentials.antibuild;
+
+import static com.earth2me.essentials.I18n._;
+import com.earth2me.essentials.IEssentials;
+import com.earth2me.essentials.User;
+import org.bukkit.Material;
+import org.bukkit.block.Block;
+import org.bukkit.event.Event.Result;
+import org.bukkit.event.EventHandler;
+import org.bukkit.event.EventPriority;
+import org.bukkit.event.Listener;
+import org.bukkit.event.block.*;
+import org.bukkit.event.player.PlayerInteractEvent;
+import org.bukkit.inventory.ItemStack;
+
+
+public class EssentialsAntiBuildListener implements Listener
+{
+ final private transient IAntiBuild prot;
+ final private transient IEssentials ess;
+
+ public EssentialsAntiBuildListener(final IAntiBuild parent)
+ {
+ this.prot = parent;
+ this.ess = prot.getEssentialsConnect().getEssentials();
+ }
+
+ @EventHandler(priority = EventPriority.HIGHEST)
+ public void onBlockPlace(final BlockPlaceEvent event)
+ {
+ if (event.isCancelled())
+ {
+ return;
+ }
+
+ final User user = ess.getUser(event.getPlayer());
+
+ if (prot.getSettingBool(AntiBuildConfig.disable_build) && !user.canBuild() && !user.isAuthorized("essentials.build")
+ && (event.getBlock() != null && !user.isAuthorized("essentials.build.place." + event.getBlock().getTypeId())))
+ {
+ event.setCancelled(true);
+ return;
+ }
+
+ final Block blockPlaced = event.getBlockPlaced();
+ final int id = blockPlaced.getTypeId();
+
+ if (prot.checkProtectionItems(AntiBuildConfig.blacklist_placement, id) && !user.isAuthorized("essentials.protect.exemptplacement"))
+ {
+ event.setCancelled(true);
+ return;
+ }
+
+ if (!user.isAuthorized("essentials.protect.alerts.notrigger")
+ && prot.checkProtectionItems(AntiBuildConfig.alert_on_placement, id))
+ {
+ prot.getEssentialsConnect().alert(user, blockPlaced.getType().toString(), _("alertPlaced"));
+ }
+ }
+
+ @EventHandler(priority = EventPriority.HIGHEST)
+ public void onBlockBreak(final BlockBreakEvent event)
+ {
+ if (event.isCancelled())
+ {
+ return;
+ }
+ final User user = ess.getUser(event.getPlayer());
+
+ if (prot.getSettingBool(AntiBuildConfig.disable_build) && !user.canBuild() && !user.isAuthorized("essentials.build")
+ && (event.getBlock() != null && !user.isAuthorized("essentials.build.break." + event.getBlock().getTypeId())))
+ {
+ event.setCancelled(true);
+ return;
+ }
+ final Block block = event.getBlock();
+ final int typeId = block.getTypeId();
+
+ if (prot.checkProtectionItems(AntiBuildConfig.blacklist_break, typeId)
+ && !user.isAuthorized("essentials.protect.exemptbreak"))
+ {
+ event.setCancelled(true);
+ return;
+ }
+ final Material type = block.getType();
+
+ if (!user.isAuthorized("essentials.protect.alerts.notrigger")
+ && prot.checkProtectionItems(AntiBuildConfig.alert_on_break, typeId))
+ {
+ prot.getEssentialsConnect().alert(user, type.toString(), _("alertBroke"));
+ }
+ }
+
+ @EventHandler(priority = EventPriority.HIGHEST)
+ public void onBlockPistonExtend(BlockPistonExtendEvent event)
+ {
+ if (event.isCancelled())
+ {
+ return;
+ }
+ for (Block block : event.getBlocks())
+ {
+ if (prot.checkProtectionItems(AntiBuildConfig.blacklist_piston, block.getTypeId()))
+ {
+ event.setCancelled(true);
+ return;
+ }
+ }
+ }
+
+ @EventHandler(priority = EventPriority.HIGHEST)
+ public void onBlockPistonRetract(BlockPistonRetractEvent event)
+ {
+ if (event.isCancelled() || !event.isSticky())
+ {
+ return;
+ }
+ final Block block = event.getRetractLocation().getBlock();
+ if (prot.checkProtectionItems(AntiBuildConfig.blacklist_piston, block.getTypeId()))
+ {
+ event.setCancelled(true);
+ return;
+ }
+ }
+
+ @EventHandler(priority = EventPriority.LOW)
+ public void onPlayerInteract(final PlayerInteractEvent event)
+ {
+ // Do not return if cancelled, because the interact event has 2 cancelled states.
+ final User user = ess.getUser(event.getPlayer());
+
+ if (event.hasItem()
+ && (event.getItem().getType() == Material.WATER_BUCKET
+ || event.getItem().getType() == Material.LAVA_BUCKET)
+ && prot.getSettingBool(AntiBuildConfig.disable_build) && !user.canBuild() && !user.isAuthorized("essentials.build"))
+ {
+ if (ess.getSettings().warnOnBuildDisallow())
+ {
+ user.sendMessage(_("buildAlert"));
+ }
+ event.setCancelled(true);
+ return;
+ }
+
+ final ItemStack item = event.getItem();
+ if (item != null
+ && prot.checkProtectionItems(AntiBuildConfig.blacklist_usage, item.getTypeId())
+ && !user.isAuthorized("essentials.protect.exemptusage"))
+ {
+ event.setCancelled(true);
+ return;
+ }
+
+ if (item != null
+ && !user.isAuthorized("essentials.protect.alerts.notrigger")
+ && prot.checkProtectionItems(AntiBuildConfig.alert_on_use, item.getTypeId()))
+ {
+ prot.getEssentialsConnect().alert(user, item.getType().toString(), _("alertUsed"));
+ }
+
+ if (prot.getSettingBool(AntiBuildConfig.disable_use) && !user.canBuild() && !user.isAuthorized("essentials.interact") && !user.isAuthorized("essentials.build"))
+ {
+ if (event.getClickedBlock() != null && !user.isAuthorized("essentials.build.interact." + event.getClickedBlock().getTypeId()))
+ {
+ event.setUseInteractedBlock(Result.DENY);
+ if (ess.getSettings().warnOnBuildDisallow())
+ {
+ user.sendMessage(_("buildAlert"));
+ }
+ }
+ if (event.hasItem() && !user.isAuthorized("essentials.build.use." + event.getMaterial().getId()))
+ {
+ event.setUseItemInHand(Result.DENY);
+ }
+
+
+ }
+ }
+}
diff --git a/EssentialsAntiBuild/src/com/earth2me/essentials/antibuild/EssentialsConnect.java b/EssentialsAntiBuild/src/com/earth2me/essentials/antibuild/EssentialsConnect.java
new file mode 100644
index 000000000..c25898f78
--- /dev/null
+++ b/EssentialsAntiBuild/src/com/earth2me/essentials/antibuild/EssentialsConnect.java
@@ -0,0 +1,80 @@
+package com.earth2me.essentials.antibuild;
+
+import com.earth2me.essentials.IConf;
+import com.earth2me.essentials.IEssentials;
+import com.earth2me.essentials.User;
+import static com.earth2me.essentials.I18n._;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import org.bukkit.Location;
+import org.bukkit.entity.Player;
+import org.bukkit.plugin.Plugin;
+
+
+public class EssentialsConnect
+{
+ private static final Logger LOGGER = Logger.getLogger("Minecraft");
+ private final transient IEssentials ess;
+ private final transient IAntiBuild protect;
+
+ public EssentialsConnect(Plugin essPlugin, Plugin essProtect)
+ {
+ if (!essProtect.getDescription().getVersion().equals(essPlugin.getDescription().getVersion()))
+ {
+ LOGGER.log(Level.WARNING, _("versionMismatchAll"));
+ }
+ ess = (IEssentials)essPlugin;
+ protect = (IAntiBuild)essProtect;
+ AntiBuildReloader pr = new AntiBuildReloader();
+ pr.reloadConfig();
+ ess.addReloadListener(pr);
+ }
+
+ public void onDisable()
+ {
+ }
+
+ public IEssentials getEssentials()
+ {
+ return ess;
+ }
+
+ public void alert(final User user, final String item, final String type)
+ {
+ final Location loc = user.getLocation();
+ final String warnMessage = _("alertFormat", user.getName(), type, item,
+ loc.getWorld().getName() + "," + loc.getBlockX() + ","
+ + loc.getBlockY() + "," + loc.getBlockZ());
+ LOGGER.log(Level.WARNING, warnMessage);
+ for (Player p : ess.getServer().getOnlinePlayers())
+ {
+ final User alertUser = ess.getUser(p);
+ if (alertUser.isAuthorized("essentials.protect.alerts"))
+ {
+ alertUser.sendMessage(warnMessage);
+ }
+ }
+ }
+
+
+ private class AntiBuildReloader implements IConf
+ {
+ @Override
+ public void reloadConfig()
+ {
+ for (AntiBuildConfig protectConfig : AntiBuildConfig.values())
+ {
+ if (protectConfig.isList())
+ {
+ protect.getSettingsList().put(protectConfig, ess.getSettings().getProtectList(protectConfig.getConfigName()));
+ }
+ else
+ {
+ protect.getSettingsBoolean().put(protectConfig, ess.getSettings().getProtectBoolean(protectConfig.getConfigName(), protectConfig.getDefaultValueBoolean()));
+ }
+
+ }
+
+ }
+ }
+}
diff --git a/EssentialsAntiBuild/src/com/earth2me/essentials/antibuild/IAntiBuild.java b/EssentialsAntiBuild/src/com/earth2me/essentials/antibuild/IAntiBuild.java
new file mode 100644
index 000000000..c7b370006
--- /dev/null
+++ b/EssentialsAntiBuild/src/com/earth2me/essentials/antibuild/IAntiBuild.java
@@ -0,0 +1,19 @@
+package com.earth2me.essentials.antibuild;
+
+import java.util.List;
+import java.util.Map;
+import org.bukkit.plugin.Plugin;
+
+
+public interface IAntiBuild extends Plugin
+{
+ boolean checkProtectionItems(final AntiBuildConfig list, final int id);
+
+ boolean getSettingBool(final AntiBuildConfig protectConfig);
+
+ EssentialsConnect getEssentialsConnect();
+
+ Map<AntiBuildConfig, Boolean> getSettingsBoolean();
+
+ Map<AntiBuildConfig, List<Integer>> getSettingsList();
+}
diff --git a/EssentialsAntiBuild/src/plugin.yml b/EssentialsAntiBuild/src/plugin.yml
new file mode 100644
index 000000000..ea880af14
--- /dev/null
+++ b/EssentialsAntiBuild/src/plugin.yml
@@ -0,0 +1,9 @@
+# This determines the command prefix when there are conflicts (/name:home, /name:help, etc.)
+name: EssentialsAntiBuild
+main: com.earth2me.essentials.antibuild.EssentialsAntiBuild
+# Note to developers: This next line cannot change, or the automatic versioning system will break.
+version: TeamCity
+website: http://www.earth2me.net:8001/
+description: Provides build protection.
+authors: [Zenexer, ementalo, Aelux, Brettflan, KimKandor, snowleo, ceulemans, Xeology, KHobbits]
+softdepend: [Essentials] \ No newline at end of file