From e5eed6f7f8347455ccff6da57b61188605384aaf Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Wed, 16 Jul 2014 12:39:58 +0100 Subject: [PATCH] A couple fixes, plus range replace mode --- .../java/com/archivesmc/painter/Painter.java | 14 +++-- .../painter/listeners/BlockBreakListener.java | 7 ++- .../painter/listeners/CommandRunner.java | 29 +++++++-- .../listeners/PlayerInteractListener.java | 61 +++++++++++++++++++ src/main/resources/config.yml | 18 +++--- src/main/resources/plugin.yml | 17 +++--- 6 files changed, 117 insertions(+), 29 deletions(-) create mode 100644 src/main/java/com/archivesmc/painter/listeners/PlayerInteractListener.java diff --git a/src/main/java/com/archivesmc/painter/Painter.java b/src/main/java/com/archivesmc/painter/Painter.java index 418051e..0139df7 100644 --- a/src/main/java/com/archivesmc/painter/Painter.java +++ b/src/main/java/com/archivesmc/painter/Painter.java @@ -2,6 +2,7 @@ import com.archivesmc.painter.listeners.BlockBreakListener; import com.archivesmc.painter.listeners.CommandRunner; +import com.archivesmc.painter.listeners.PlayerInteractListener; import com.archivesmc.painter.loggers.*; import net.milkbowl.vault.permission.Permission; @@ -29,9 +30,7 @@ public class Painter extends JavaPlugin { boolean useTool = false; public Set painters; - - private BlockBreakListener breakListener; - private CommandRunner commands; + public Set range_painters; public Permission permissions; @@ -43,6 +42,7 @@ public void onEnable() { // Assign painters set here in case we're reloading, instead of in the class definition this.painters = new HashSet<>(); + this.range_painters = new HashSet<>(); // First, let's start with the paint tool material this.toolString = this.getConfig().getString("paint_tool", null); @@ -121,11 +121,13 @@ public void onEnable() { } // Now that that's done, let's register events and commands - this.breakListener = new BlockBreakListener(this); - this.commands = new CommandRunner(this); + BlockBreakListener breakListener = new BlockBreakListener(this); + PlayerInteractListener interactListener = new PlayerInteractListener(this); + CommandRunner commands = new CommandRunner(this); this.getServer().getPluginManager().registerEvents(breakListener, this); - getCommand("painter").setExecutor(this.commands); + this.getServer().getPluginManager().registerEvents(interactListener, this); + getCommand("painter").setExecutor(commands); } private void setupLogBlock() { diff --git a/src/main/java/com/archivesmc/painter/listeners/BlockBreakListener.java b/src/main/java/com/archivesmc/painter/listeners/BlockBreakListener.java index d804bfa..0e92da7 100644 --- a/src/main/java/com/archivesmc/painter/listeners/BlockBreakListener.java +++ b/src/main/java/com/archivesmc/painter/listeners/BlockBreakListener.java @@ -31,6 +31,7 @@ public void onBlockBreakEvent(BlockBreakEvent event) { if (this.plugin.painters.contains(player.getUniqueId())) { if (! this.plugin.permissions.has(player, "painter.replace")) { this.plugin.painters.remove(player.getUniqueId()); + Map args = new HashMap<>(); args.put("permission", "painter.replace"); args.put("name", player.getName()); @@ -39,14 +40,16 @@ public void onBlockBreakEvent(BlockBreakEvent event) { return; } - Block block = event.getBlock(); - BlockState oldBlockState = block.getState(); ItemStack items = player.getItemInHand(); Material heldMat = items.getType(); if (heldMat.isBlock()) { + Block block = event.getBlock(); + BlockState oldBlockState = block.getState(); + block.setType(heldMat); block.setData(items.getData().getData()); + event.setCancelled(true); // Log it if it's being logged diff --git a/src/main/java/com/archivesmc/painter/listeners/CommandRunner.java b/src/main/java/com/archivesmc/painter/listeners/CommandRunner.java index d08c83d..4b544f5 100644 --- a/src/main/java/com/archivesmc/painter/listeners/CommandRunner.java +++ b/src/main/java/com/archivesmc/painter/listeners/CommandRunner.java @@ -26,7 +26,7 @@ public boolean onCommand(CommandSender commandSender, Command command, String s, this.plugin.sendMessage(commandSender, "command_player_only", args); } else { UUID id = ((Player) commandSender).getUniqueId(); - if (strings.length < 1) { + if (strings.length < 1 || "toggle".equalsIgnoreCase(strings[0])) { if (this.plugin.permissions.has(commandSender, "painter.replace")) { if (this.plugin.painters.contains(id)) { this.plugin.painters.remove(id); @@ -50,9 +50,30 @@ public boolean onCommand(CommandSender commandSender, Command command, String s, this.plugin.sendMessage(commandSender, "command_replace_no_permission", args); } - } else { - // TODO: Paint tool - commandSender.sendMessage("Paint tool isn't implemented yet!"); + } else if ("range".equalsIgnoreCase(strings[0])) { + if (this.plugin.permissions.has(commandSender, "painter.replace.range")) { + if (this.plugin.range_painters.contains(id)) { + this.plugin.range_painters.remove(id); + + Map args = new HashMap<>(); + args.put("name", commandSender.getName()); + + this.plugin.sendMessage(commandSender, "range_replace_mode_disable", args); + } else { + this.plugin.range_painters.add(id); + + Map args = new HashMap<>(); + args.put("name", commandSender.getName()); + + this.plugin.sendMessage(commandSender, "range_replace_mode_enable", args); + } + } else { + Map args = new HashMap<>(); + args.put("permission", "painter.replace.range"); + args.put("name", commandSender.getName()); + + this.plugin.sendMessage(commandSender, "command_range_replace_no_permission", args); + } } } diff --git a/src/main/java/com/archivesmc/painter/listeners/PlayerInteractListener.java b/src/main/java/com/archivesmc/painter/listeners/PlayerInteractListener.java new file mode 100644 index 0000000..d853de7 --- /dev/null +++ b/src/main/java/com/archivesmc/painter/listeners/PlayerInteractListener.java @@ -0,0 +1,61 @@ +package com.archivesmc.painter.listeners; + +import com.archivesmc.painter.Painter; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockState; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.ItemStack; + +import java.util.HashMap; +import java.util.Map; + +public class PlayerInteractListener implements Listener { + Painter plugin; + + public PlayerInteractListener(Painter plugin) { + this.plugin = plugin; + } + + @EventHandler(priority = EventPriority.HIGHEST) + public void onPlayerInteractEvent(PlayerInteractEvent event) { + if(! event.isCancelled()) { + Player player = event.getPlayer(); + + if (this.plugin.range_painters.contains(player.getUniqueId()) + && event.getAction() == Action.LEFT_CLICK_AIR) { + if (! this.plugin.permissions.has(player, "painter.replace.range")) { + this.plugin.range_painters.remove(player.getUniqueId()); + + Map args = new HashMap<>(); + args.put("permission", "painter.replace.range"); + args.put("name", player.getName()); + + this.plugin.sendMessage(player, "range_replace_perm_lost", args); + return; + } + + ItemStack items = player.getItemInHand(); + Material heldMat = items.getType(); + + if (heldMat.isBlock()) { + Block block = player.getTargetBlock(null, 100); + BlockState oldBlockState = block.getState(); + + block.setType(heldMat); + block.setData(items.getData().getData()); + + event.setCancelled(true); + + // Log it if it's being logged + this.plugin.blockPainted(player, oldBlockState, block.getState(), block); + } + } + } + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 2880437..a18404b 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,12 +1,7 @@ -# Paint tool - the item to use -# See here for names: http://jd.bukkit.org/dev/apidocs/org/bukkit/Material.html -# Comment out this entry to disable the wool/clay paint tool -paint_tool: WOOD_SWORD - # This can be coreprotect, logblock, hawkeye or prism # See the plugin documentation for more information on how this works # Comment out this entry to disable block logging -logger: logblock +logger: prism # The rest of this file contains various message strings for you to customize. # You can't leave any of these out! Make sure they're all here! @@ -19,12 +14,19 @@ messages: # Tokens: {NAME} replace_mode_disable: "&dCMP &5\u00BB&6 Replace mode disabled, you are no longer replacing blocks" + # Tokens: {PERMISSION}, {NAME} + range_replace_perm_lost: "&dCMP &5\u00BB&6 Range replace disabled, your access to it has been &crevoked" + # Tokens: {NAME} + range_replace_mode_enable: "&dCMP &5\u00BB&6 Range replace enabled, you are now replacing blocks at a distance" + # Tokens: {NAME} + range_replace_mode_disable: "&dCMP &5\u00BB&6 Range replace disabled, you are no longer replacing blocks at a distance" + # Tokens: {NAME} command_player_only: "&dCMP &5\u00BB&6 This command can only be run by a player" # Tokens: {PERMISSION}, {NAME} command_replace_no_permission: "&dCMP &5\u00BB&6 You do not have permission to toggle replace mode" # Tokens: {PERMISSION}, {NAME} - command_paint_tool_no_permission: "&dCMP &5\u00BB&6 You do not have permission to use the paint tool" + command_range_replace_no_permission: "&dCMP &5\u00BB&6 You do not have permission to toggle range replace mode" # Don't change this! You'll break stuff! -version: 0.0.1pre4 \ No newline at end of file +version: 0.0.1pre5 \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 7361619..e5cd5e7 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: Painter -version: 0.0.1pre4 +version: 0.0.1pre5 description: Allows the painting of wool and clay, and the replacing of blocks author: Gareth Coles @@ -7,22 +7,21 @@ author: Gareth Coles main: com.archivesmc.painter.Painter database: false depend: [Vault] -softdepend: [LogBlock, CoreProtect, Prism, Hawkeye] +softdepend: [LogBlock, CoreProtect, Prism, HawkEye] commands: painter: aliases: [paint, ptr, pt, p] - description: "Enter or leave block replacement mode, or change the colours on your wool/clay paint tool" - usage: "/ [left-click colour] [right-click colour]\n/ \u00BB Toggle block replacement mode\n/ red white \u00BB Set colours on the paint tool\n/ - black \u00BB Set only the right-click colour on the paint tool" + description: "Toggle replacement ("toggle" or no arguments) and range replacement modes" + usage: "/ [toggle|range]" permissions: painter.*: children: - painter.tool: true painter.replace: true - painter.command: true + painter.replace.range: true description: All the Painter permissions - painter.tool: - description: Use the wool/clay paint tool painter.replace: - description: Replace blocks by breaking them \ No newline at end of file + description: Replace blocks by breaking them + painter.replace.range: + description: Replace blocks at range by left-clicking \ No newline at end of file