Skip to content

Commit

Permalink
Renamed OtherPlugins to DetectBuildLimiter, and added a bunch of comm…
Browse files Browse the repository at this point in the history
…ents

Closes #1
  • Loading branch information
Brianetta committed May 13, 2014
1 parent 6a76f61 commit 46f0137
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 23 deletions.
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: ##

Expand All @@ -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

Expand Down Expand Up @@ -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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,30 @@
* 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
// that they couldn't have built the object.
//
// 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)) {
Expand All @@ -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);
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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")) {
Expand Down
5 changes: 1 addition & 4 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,4 @@ protectables:
- WOODEN_DOOR
tool: STICK
chatprefix: SL
autolock: false
integration:
towny: true
worldguard: true
autolock: false

0 comments on commit 46f0137

Please sign in to comment.