From 46f0137aac947532cd4b4e1419723600999e7afc Mon Sep 17 00:00:00 2001 From: Brian Ronald Date: Tue, 13 May 2014 18:48:54 +0100 Subject: [PATCH] Renamed OtherPlugins to DetectBuildLimiter, and added a bunch of comments Closes #1 --- README.md | 12 ++++------- ...erPlugins.java => DetectBuildLimiter.java} | 20 ++++++++++++++++--- .../listeners/StickyLocksClick.java | 10 +++++----- .../listeners/StickyLocksCreateDestroy.java | 6 +++--- src/main/resources/config.yml | 5 +---- 5 files changed, 30 insertions(+), 23 deletions(-) rename src/main/java/net/simplycrafted/StickyLocks/{OtherPlugins.java => DetectBuildLimiter.java} (72%) diff --git a/README.md b/README.md index e5f57d0..98104df 100644 --- a/README.md +++ b/README.md @@ -27,10 +27,7 @@ Factions or WorldGuard regions. ## Building StickyLocks ## StickyLocks is a Maven project. It was developed using IntelliJ Idea, but -should work fine in Eclipse, and shell junkies won't have any trouble. You -will also need Towny in your libraries, and you'll need to either place -Towny.jar into the /libs folder of your project, or update pom.xml with the -location of your Towny.jar before Maven can build. +should work fine in Eclipse, and shell junkies won't have any trouble. ## Installation: ## @@ -44,9 +41,9 @@ The plugin will create and use StickyLocks.db, which is an [SQLite](http://www.sqlite.org/) database that can be queried with any SQLite3 compatible client. -Towny and WorldGuard are supported, although more are to come. This integration -can be disabled, but StickyLocks will automatically detect these plugins -with the default settings. +The plugin tried to detect whether other plugins are protecting against +building. If they are, it won't allow a player to add a lock where they +cannot build. This is to prevent grief-by-locking. ## Permissions @@ -85,7 +82,6 @@ Fairly simple, and the defaults are likely to be suitable. **chatprefix** is a short text added to the start of chat lines in square brackets. So, the default setting of *SL* would look like *\[SL] Some output here*. -The **integration** section includes keys for plugin integration. Currently there are two, **towny** and **worldguard**. If set to true, the plugin will attempt to detect these plugins, and will use them to help determine whether a player should be allowed to lock a protectable block. If set to false, that plugin will be ignored. ## License ## StickyLocks is [GPL](http://www.gnu.org/copyleft/gpl.html). diff --git a/src/main/java/net/simplycrafted/StickyLocks/OtherPlugins.java b/src/main/java/net/simplycrafted/StickyLocks/DetectBuildLimiter.java similarity index 72% rename from src/main/java/net/simplycrafted/StickyLocks/OtherPlugins.java rename to src/main/java/net/simplycrafted/StickyLocks/DetectBuildLimiter.java index ed99667..56eebdd 100644 --- a/src/main/java/net/simplycrafted/StickyLocks/OtherPlugins.java +++ b/src/main/java/net/simplycrafted/StickyLocks/DetectBuildLimiter.java @@ -23,10 +23,10 @@ * GNU General Public License for more details. */ -public class OtherPlugins implements Listener { +public class DetectBuildLimiter implements Listener { private static StickyLocks stickylocks = StickyLocks.getInstance(); BlockPlaceEvent blockPlaceEvent; - Boolean canPlace = true; + Boolean canPlace; // This class's job is to talk to other plugins, so that StickyLocks can find out // whether a player's attempt to lock an object should be prevented on the grounds @@ -34,10 +34,19 @@ public class OtherPlugins implements Listener { // // It does so by launching a fake build event, then seeing if anything cancelled it. - public OtherPlugins() { + // This class is an event Listener, and in this constructor registers itself as + // a listener for the event it's going to fire. + + public DetectBuildLimiter() { stickylocks.getServer().getPluginManager().registerEvents(this,stickylocks); } + // This catches the event, at sufficiently high priority that everything else + // should be done with it, but before MONITOR handlers (such as LogBlock or + // CoreProtect) might see it. It checks if anything has cancelled the event, + // and sets the class member canPlace to false if it has been. Then it cancels + // it anyway, to avoid anti-grief plugins logging it. + @EventHandler (priority= EventPriority.HIGHEST) public void onBlockPlaceEvent (BlockPlaceEvent event) { if (event.equals(blockPlaceEvent)) { @@ -48,6 +57,10 @@ public void onBlockPlaceEvent (BlockPlaceEvent event) { } } + // To be called once whatever instantiated the class is done with it. This + // unregisters the event handler now, rather than waiting for GC to get + // around to it, cluttering the place up. + public void cleanup () { BlockPlaceEvent.getHandlerList().unregister(this); } @@ -56,6 +69,7 @@ public void cleanup () { // be able to build here. Used to decide whether they should be allowed to lock something. public Boolean canBuildHere(Player player, Block block) { + canPlace = true; blockPlaceEvent = new BlockPlaceEvent(block, block.getState(),block,new ItemStack(block.getType()),player,true); stickylocks.getServer().getPluginManager().callEvent(blockPlaceEvent); return canPlace; diff --git a/src/main/java/net/simplycrafted/StickyLocks/listeners/StickyLocksClick.java b/src/main/java/net/simplycrafted/StickyLocks/listeners/StickyLocksClick.java index cc5f3a5..027c366 100644 --- a/src/main/java/net/simplycrafted/StickyLocks/listeners/StickyLocksClick.java +++ b/src/main/java/net/simplycrafted/StickyLocks/listeners/StickyLocksClick.java @@ -1,7 +1,7 @@ package net.simplycrafted.StickyLocks.listeners; import net.simplycrafted.StickyLocks.Database; -import net.simplycrafted.StickyLocks.OtherPlugins; +import net.simplycrafted.StickyLocks.DetectBuildLimiter; import net.simplycrafted.StickyLocks.Protection; import net.simplycrafted.StickyLocks.StickyLocks; import org.bukkit.ChatColor; @@ -32,7 +32,7 @@ public class StickyLocksClick implements Listener { Material tool; Database db = new Database(); static StickyLocks stickylocks; - OtherPlugins otherPlugins; + DetectBuildLimiter detectBuildLimiter; // Get the tool item Material from the config public StickyLocksClick() { @@ -105,14 +105,14 @@ public void onPlayerInteract(PlayerInteractEvent event) { stickylocks.sendMessage(player, "You do not own this object", false); } } else if (protection.getType() != null) { - otherPlugins = new OtherPlugins(); - if (otherPlugins.canBuildHere(player, target)) { + detectBuildLimiter = new DetectBuildLimiter(); + if (detectBuildLimiter.canBuildHere(player, target)) { stickylocks.sendMessage(player, "Locking...", true); db.lockBlock(target, player); } else { stickylocks.sendMessage(player, "You don't have permission to lock things here", false); } - otherPlugins.cleanup(); + detectBuildLimiter.cleanup(); } } else { stickylocks.sendMessage(player, "You don't have permission to lock or unlock objects", false); diff --git a/src/main/java/net/simplycrafted/StickyLocks/listeners/StickyLocksCreateDestroy.java b/src/main/java/net/simplycrafted/StickyLocks/listeners/StickyLocksCreateDestroy.java index b1bf44b..69c34d1 100644 --- a/src/main/java/net/simplycrafted/StickyLocks/listeners/StickyLocksCreateDestroy.java +++ b/src/main/java/net/simplycrafted/StickyLocks/listeners/StickyLocksCreateDestroy.java @@ -1,7 +1,7 @@ package net.simplycrafted.StickyLocks.listeners; import net.simplycrafted.StickyLocks.Database; -import net.simplycrafted.StickyLocks.OtherPlugins; +import net.simplycrafted.StickyLocks.DetectBuildLimiter; import net.simplycrafted.StickyLocks.Protection; import net.simplycrafted.StickyLocks.StickyLocks; import org.bukkit.block.Block; @@ -32,7 +32,7 @@ public class StickyLocksCreateDestroy implements Listener { Database db = new Database(); private StickyLocks stickylocks = StickyLocks.getInstance(); - private OtherPlugins otherPlugins = new OtherPlugins(); + private DetectBuildLimiter detectBuildLimiter = new DetectBuildLimiter(); // this event handler responds to a block place event, and either // informs the player that they can lock a chest with the tool, @@ -41,7 +41,7 @@ public class StickyLocksCreateDestroy implements Listener { @EventHandler (priority = EventPriority.MONITOR) public void onBlockPlaceEvent(BlockPlaceEvent event) { // Quit if we can't build here - if (event.isCancelled() || !otherPlugins.canBuildHere(event.getPlayer(),event.getBlock())) return; + if (event.isCancelled() || !detectBuildLimiter.canBuildHere(event.getPlayer(),event.getBlock())) return; Player player = event.getPlayer(); Block target = event.getBlock(); if (player.hasPermission("stickylocks.lock")) { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 68c75ff..738d989 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -23,7 +23,4 @@ protectables: - WOODEN_DOOR tool: STICK chatprefix: SL -autolock: false -integration: - towny: true - worldguard: true \ No newline at end of file +autolock: false \ No newline at end of file