diff --git a/src/Blacklist.java b/src/Blacklist.java index fb396a87..4b71c52d 100644 --- a/src/Blacklist.java +++ b/src/Blacklist.java @@ -92,6 +92,28 @@ public boolean onDestroy(final Block block, final Player player) { return ret; } + /** + * Called on block break. Returns true to let the action pass + * through. + * + * @param block + * @param player + * @return + */ + public boolean onBreak(final Block block, final Player player) { + List entries = getEntries(block.getType()); + if (entries == null) { + return true; + } + boolean ret = true; + for (BlacklistEntry entry : entries) { + if (!entry.onBreak(block, player)) { + ret = false; + } + } + return ret; + } + /** * Called on left click. Returns true to let the action pass through. * @@ -269,6 +291,8 @@ public void load(File file) throws IOException { entry.setIgnoreGroups(parts[1].split(",")); } else if (parts[0].equalsIgnoreCase("on-destroy")) { entry.setDestroyActions(parts[1].split(",")); + } else if (parts[0].equalsIgnoreCase("on-break")) { + entry.setBreakActions(parts[1].split(",")); } else if (parts[0].equalsIgnoreCase("on-left") || parts[0].equalsIgnoreCase("on-destroy-with")) { entry.setDestroyWithActions(parts[1].split(",")); diff --git a/src/BlacklistEntry.java b/src/BlacklistEntry.java index c63dedb4..f015e605 100644 --- a/src/BlacklistEntry.java +++ b/src/BlacklistEntry.java @@ -46,6 +46,10 @@ public class BlacklistEntry { * List of actions to perform on destruction. */ private String[] destroyActions; + /** + * List of actions to perform on break. + */ + private String[] breakActions; /** * List of actions to perform on left click. */ @@ -104,6 +108,20 @@ public void setDestroyActions(String[] actions) { this.destroyActions = actions; } + /** + * @return + */ + public String[] getBreakActions() { + return breakActions; + } + + /** + * @param actions + */ + public void setBreakActions(String[] actions) { + this.breakActions = actions; + } + /** * @return */ @@ -243,6 +261,42 @@ public void tell(String itemName) { return process(block.getType(), player, destroyActions, handler); } + /** + * Called on block break. Returns true to let the action pass + * through. + * + * @param block + * @param player + * @return + */ + public boolean onBreak(final Block block, final Player player) { + if (breakActions == null) { + return true; + } + + final BlacklistEntry entry = this; + + ActionHandler handler = new ActionHandler() { + public void log(String itemName) { + blacklist.getLogger().logBreakAttempt(player, block); + } + public void kick(String itemName) { + player.kick("You are not allowed to break " + itemName); + } + public void ban(String itemName) { + entry.banPlayer(player, "Banned: You are not allowed to break " + itemName); + } + public void notifyAdmins(String itemName) { + entry.notifyAdmins(player.getName() + " tried to break " + itemName + "."); + } + public void tell(String itemName) { + player.sendMessage(Colors.Yellow + "You are not allowed to break " + itemName + "."); + } + }; + + return process(block.getType(), player, breakActions, handler); + } + /** * Called on left click. Returns true to let the action pass through. * diff --git a/src/BlacklistLogger.java b/src/BlacklistLogger.java index 13d289c2..26652a3f 100644 --- a/src/BlacklistLogger.java +++ b/src/BlacklistLogger.java @@ -70,6 +70,18 @@ public void logDestroyAttempt(Player player, Block block) { } } + /** + * Log a block break attempt. + * + * @param player + * @param block + */ + public void logBreakAttempt(Player player, Block block) { + for (BlacklistLoggerHandler handler : handlers) { + handler.logBreakAttempt(player, block); + } + } + /** * Log a right click on attempt. * diff --git a/src/BlacklistLoggerHandler.java b/src/BlacklistLoggerHandler.java index 98c7560c..3cb85c16 100644 --- a/src/BlacklistLoggerHandler.java +++ b/src/BlacklistLoggerHandler.java @@ -30,6 +30,13 @@ public interface BlacklistLoggerHandler { * @param block */ public void logDestroyAttempt(Player player, Block block); + /** + * Log a block break attempt. + * + * @param player + * @param block + */ + public void logBreakAttempt(Player player, Block block); /** * Log a right click on attempt. * diff --git a/src/ConsoleLoggerHandler.java b/src/ConsoleLoggerHandler.java index c0db6e61..9a3e746f 100644 --- a/src/ConsoleLoggerHandler.java +++ b/src/ConsoleLoggerHandler.java @@ -29,7 +29,7 @@ public class ConsoleLoggerHandler implements BlacklistLoggerHandler { * Logger. */ private static final Logger logger = Logger.getLogger("Minecraft.WorldGuard"); - + /** * Log a block destroy attempt. * @@ -41,6 +41,17 @@ public void logDestroyAttempt(Player player, Block block) { + " tried to destroy " + getFriendlyItemName(block.getType())); } + /** + * Log a block break attempt. + * + * @param player + * @param block + */ + public void logBreakAttempt(Player player, Block block) { + logger.log(Level.INFO, "WorldGuard: " + player.getName() + + " tried to break " + getFriendlyItemName(block.getType())); + } + /** * Log a right click on attempt. * diff --git a/src/DatabaseLoggerHandler.java b/src/DatabaseLoggerHandler.java index 3987e29b..2df0f4f3 100644 --- a/src/DatabaseLoggerHandler.java +++ b/src/DatabaseLoggerHandler.java @@ -112,7 +112,7 @@ private void logEvent(String event, String name, int x, int y, int z, int item) + e.getMessage()); } } - + /** * Log a block destroy attempt. * @@ -124,6 +124,17 @@ public void logDestroyAttempt(Player player, Block block) { block.getX(), block.getY(), block.getZ(), block.getType()); } + /** + * Log a block break attempt. + * + * @param player + * @param block + */ + public void logBreakAttempt(Player player, Block block) { + logEvent("BREAK", player.getName(), + block.getX(), block.getY(), block.getZ(), block.getType()); + } + /** * Log a right click on attempt. * diff --git a/src/FileLoggerHandler.java b/src/FileLoggerHandler.java index d78835e5..d08f61e1 100644 --- a/src/FileLoggerHandler.java +++ b/src/FileLoggerHandler.java @@ -215,6 +215,16 @@ public void logDestroyAttempt(Player player, Block block) { log(player, "Tried to destroy " + getFriendlyItemName(block.getType())); } + /** + * Log a block break attempt. + * + * @param player + * @param block + */ + public void logBreakAttempt(Player player, Block block) { + log(player, "Tried to break " + getFriendlyItemName(block.getType())); + } + /** * Log a right click on attempt. * diff --git a/src/WorldGuard.java b/src/WorldGuard.java index f6ab4a54..757d59bd 100644 --- a/src/WorldGuard.java +++ b/src/WorldGuard.java @@ -71,6 +71,8 @@ public void initialize() { PluginListener.Priority.HIGH); loader.addListener(PluginLoader.Hook.BLOCK_DESTROYED, listener, this, PluginListener.Priority.CRITICAL); + loader.addListener(PluginLoader.Hook.BLOCK_BROKEN, listener, this, + PluginListener.Priority.HIGH); loader.addListener(PluginLoader.Hook.DISCONNECT, listener, this, PluginListener.Priority.HIGH); loader.addListener(PluginLoader.Hook.ITEM_DROP , listener, this, diff --git a/src/WorldGuardListener.java b/src/WorldGuardListener.java index e69c8afe..a18a0785 100644 --- a/src/WorldGuardListener.java +++ b/src/WorldGuardListener.java @@ -516,6 +516,23 @@ else if (type == 18) { // Leaves return false; } + /** + * Called when a person actually breaks the block. + * + * @param player + * @param block + * @return + */ + public boolean onBlockBreak(Player player, Block block) { + if (blacklist != null) { + if (!blacklist.onBreak(block, player)) { + return true; + } + } + + return false; + } + /** * Called when either a sign, chest or furnace is changed. * diff --git a/worldguard-blacklist.txt b/worldguard-blacklist.txt index 9ed315c9..f22f0a55 100644 --- a/worldguard-blacklist.txt +++ b/worldguard-blacklist.txt @@ -18,6 +18,7 @@ # # Events: # - on-destroy (when a block of this type is being destroyed) +# - on-break (when a block of this type is about to be broken) # - on-destroy-with (the item/block held by the user while destroying) # - on-create (the item/block in the user's inventory is being created) # - on-use (the block is right clicked) @@ -67,4 +68,7 @@ #on-destroy=notify,deny,log #[cobblestone] -#on-create=deny,tell,log \ No newline at end of file +#on-create=deny,tell,log + +#[lever] +#on-break=deny \ No newline at end of file