From e8b6640534869b30d2fb8918f9dcd45daa46fe2f Mon Sep 17 00:00:00 2001 From: Robin Kupper Date: Wed, 10 Aug 2011 14:02:25 +0200 Subject: [PATCH] Added /lb tp to teleport to a lookup element Made rollback errors viewable ingame --- src/de/diddiz/LogBlock/BlockChange.java | 14 ++++++- src/de/diddiz/LogBlock/CommandsHandler.java | 25 ++++++++++++- src/de/diddiz/LogBlock/LBBlockListener.java | 2 +- .../diddiz/LogBlock/LookupCacheElement.java | 10 +++++ src/de/diddiz/LogBlock/Session.java | 2 +- src/de/diddiz/LogBlock/WorldEditor.java | 37 ++++++++++--------- src/de/diddiz/util/BukkitUtils.java | 4 +- 7 files changed, 69 insertions(+), 25 deletions(-) create mode 100644 src/de/diddiz/LogBlock/LookupCacheElement.java diff --git a/src/de/diddiz/LogBlock/BlockChange.java b/src/de/diddiz/LogBlock/BlockChange.java index 430c0dd..6aa3fa4 100644 --- a/src/de/diddiz/LogBlock/BlockChange.java +++ b/src/de/diddiz/LogBlock/BlockChange.java @@ -6,7 +6,7 @@ import java.sql.SQLException; import java.text.SimpleDateFormat; import org.bukkit.Location; -public class BlockChange +public class BlockChange implements LookupCacheElement { private final static SimpleDateFormat formatter = new SimpleDateFormat("MM-dd HH:mm:ss"); public final long id, date; @@ -32,7 +32,7 @@ public class BlockChange public BlockChange(ResultSet rs, QueryParams p) throws SQLException { id = p.needId ? rs.getInt("id") : 0; date = p.needDate ? rs.getTimestamp("date").getTime() : 0; - loc = p.needCoords ? new Location(null, rs.getInt("x"), rs.getInt("y"), rs.getInt("z")) : null; + loc = p.needCoords ? new Location(p.world, rs.getInt("x"), rs.getInt("y"), rs.getInt("z")) : null; playerName = p.needPlayer ? rs.getString("playername") : null; replaced = p.needType ? rs.getInt("replaced") : 0; type = p.needType ? rs.getInt("type") : 0; @@ -80,4 +80,14 @@ public class BlockChange msg.append(" at " + loc.getBlockX() + ":" + loc.getBlockY() + ":" + loc.getBlockZ()); return msg.toString(); } + + @Override + public Location getLocation() { + return loc; + } + + @Override + public String getMessage() { + return toString(); + } } diff --git a/src/de/diddiz/LogBlock/CommandsHandler.java b/src/de/diddiz/LogBlock/CommandsHandler.java index f9bfe26..160fab8 100644 --- a/src/de/diddiz/LogBlock/CommandsHandler.java +++ b/src/de/diddiz/LogBlock/CommandsHandler.java @@ -313,7 +313,24 @@ public class CommandsHandler implements CommandExecutor } else if (command.equals("tp")) { if (sender instanceof Player) { if (logblock.hasPermission(sender, "logblock.tp")) - new CommandTeleport(sender, new QueryParams(logblock, sender, argsToList(args, 1)), true); + if (args.length == 2 || isInt(args[1])) { + final int pos = Integer.parseInt(args[1]) - 1; + final Player player = (Player)sender; + final Session session = logblock.getSession(player.getName()); + if (session.lookupCache != null) + if (pos >= 0 && pos < session.lookupCache.length) { + final Location loc = session.lookupCache[pos].getLocation(); + if (loc != null) { + player.teleport(new Location(loc.getWorld(), loc.getX() + 0.5, saveSpawnHeight(loc), loc.getZ() + 0.5, player.getLocation().getYaw(), 90)); + player.sendMessage(ChatColor.LIGHT_PURPLE + "Teleported to " + loc.getBlockX() + ":" + loc.getBlockY() + ":" + loc.getBlockZ()); + } else + sender.sendMessage(ChatColor.RED + "There is no location associated with that"); + } else + sender.sendMessage(ChatColor.RED + "'" + args[1] + " is out of range"); + else + sender.sendMessage(ChatColor.RED + "You havn't done a lookup yet"); + } else + new CommandTeleport(sender, new QueryParams(logblock, sender, argsToList(args, 1)), true); else sender.sendMessage(ChatColor.RED + "You aren't allowed to do this"); } else @@ -329,6 +346,9 @@ public class CommandsHandler implements CommandExecutor } else sender.sendMessage(ChatColor.RED + "Unknown command '" + args[0] + "'"); } + } catch (final NullPointerException ex) { + sender.sendMessage(ChatColor.RED + "Error, check log"); + log.log(Level.WARNING, "NPE in commandshandler: ", ex); } catch (final Exception ex) { sender.sendMessage(ChatColor.RED + ex.getMessage()); } @@ -345,7 +365,7 @@ public class CommandsHandler implements CommandExecutor if (numberOfPages != 1) sender.sendMessage(ChatColor.DARK_AQUA + "Page " + page + "/" + numberOfPages); for (int i = startpos; i <= stoppos; i++) - sender.sendMessage(ChatColor.GOLD + session.lookupCache[i].toString()); + sender.sendMessage(ChatColor.GOLD + (session.lookupCache[i].getLocation() != null ? "(" + (i + 1) + ") " : "") + session.lookupCache[i].getMessage()); session.page = page; } else sender.sendMessage(ChatColor.RED + "There isn't a page '" + page + "'"); @@ -590,6 +610,7 @@ public class CommandsHandler implements CommandExecutor return; } editor.start(); + logblock.getSession(senderName(sender)).lookupCache = editor.errors; sender.sendMessage(ChatColor.GREEN + "Rollback finished successfully (" + editor.getElapsedTime() + " ms, " + editor.getSuccesses() + "/" + changes + " blocks" + (editor.getErrors() > 0 ? ", " + ChatColor.RED + editor.getErrors() + " errors" + ChatColor.GREEN : "") + (editor.getBlacklistCollisions() > 0 ? ", " + editor.getBlacklistCollisions() + " blacklist collisions" : "") + ")"); if (!params.silent && config.askClearLogAfterRollback && logblock.hasPermission(sender, "logblock.clearlog") && questioner != null && sender instanceof Player) { Thread.sleep(1000); diff --git a/src/de/diddiz/LogBlock/LBBlockListener.java b/src/de/diddiz/LogBlock/LBBlockListener.java index 2bacb27..4c5d8e2 100644 --- a/src/de/diddiz/LogBlock/LBBlockListener.java +++ b/src/de/diddiz/LogBlock/LBBlockListener.java @@ -28,10 +28,10 @@ import org.bukkit.material.MaterialData; class LBBlockListener extends BlockListener { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm:ss"); + private static final Set nonFluidProofBlocks = new HashSet(Arrays.asList(27, 28, 31, 32, 37, 38, 39, 40, 50, 51, 55, 59, 66, 69, 70, 75, 76, 78, 93, 94)); private final Consumer consumer; private final Map worlds; private final List errors = new ArrayList(20); - private final Set nonFluidProofBlocks = new HashSet(Arrays.asList(27, 28, 31, 32, 37, 38, 39, 40, 50, 51, 55, 59, 66, 69, 70, 75, 76, 78, 93, 94)); LBBlockListener(LogBlock logblock) { consumer = logblock.getConsumer(); diff --git a/src/de/diddiz/LogBlock/LookupCacheElement.java b/src/de/diddiz/LogBlock/LookupCacheElement.java new file mode 100644 index 0000000..bf2cdc9 --- /dev/null +++ b/src/de/diddiz/LogBlock/LookupCacheElement.java @@ -0,0 +1,10 @@ +package de.diddiz.LogBlock; + +import org.bukkit.Location; + +public interface LookupCacheElement +{ + public Location getLocation(); + + public String getMessage(); +} diff --git a/src/de/diddiz/LogBlock/Session.java b/src/de/diddiz/LogBlock/Session.java index fdef4ab..42d0464 100644 --- a/src/de/diddiz/LogBlock/Session.java +++ b/src/de/diddiz/LogBlock/Session.java @@ -9,7 +9,7 @@ public class Session public boolean toolBlockEnabled = true; public QueryParams toolBlockQuery; public ToolMode toolBlockMode; - public BlockChange[] lookupCache = null; + public LookupCacheElement[] lookupCache = null; public int page = 1; Session(LogBlock logblock) { diff --git a/src/de/diddiz/LogBlock/WorldEditor.java b/src/de/diddiz/LogBlock/WorldEditor.java index 506d26d..4667d42 100644 --- a/src/de/diddiz/LogBlock/WorldEditor.java +++ b/src/de/diddiz/LogBlock/WorldEditor.java @@ -32,9 +32,9 @@ public class WorldEditor implements Runnable private final Queue edits = new LinkedBlockingQueue(); private final World world; private int taskID; - private int successes = 0, errors = 0, blacklistCollisions = 0; + private int successes = 0, blacklistCollisions = 0; private long elapsedTime = 0; - final List errorList = new ArrayList(); + public LookupCacheElement[] errors; public WorldEditor(LogBlock logblock, World world) { log = logblock.getServer().getLogger(); @@ -52,7 +52,7 @@ public class WorldEditor implements Runnable } public int getErrors() { - return errors; + return errors.length; } public int getBlacklistCollisions() { @@ -67,21 +67,22 @@ public class WorldEditor implements Runnable return elapsedTime; } - synchronized public void start() throws WorldEditorException { + synchronized public void start() throws Exception { final long start = System.currentTimeMillis(); taskID = logblock.getServer().getScheduler().scheduleSyncRepeatingTask(logblock, this, 0, 1); if (taskID == -1) - throw new WorldEditorException("Failed to schedule task"); + throw new Exception("Failed to schedule task"); try { this.wait(); } catch (final InterruptedException ex) { - throw new WorldEditorException("Interrupted"); + throw new Exception("Interrupted"); } elapsedTime = System.currentTimeMillis() - start; } @Override public synchronized void run() { + final List errorList = new ArrayList(); int counter = 0; while (!edits.isEmpty() && counter < 1000) { try { @@ -94,11 +95,8 @@ public class WorldEditor implements Runnable break; } } catch (final WorldEditorException ex) { - errors++; - errorList.add(ex.getMessage()); + errorList.add(ex); } catch (final Exception ex) { - errors++; - errorList.add(ex.getMessage()); log.log(Level.WARNING, "[LogBlock WorldEditor] Exeption: ", ex); } counter++; @@ -110,10 +108,11 @@ public class WorldEditor implements Runnable final File file = new File("plugins/LogBlock/error/WorldEditor-" + new SimpleDateFormat("yy-MM-dd-HH-mm-ss").format(System.currentTimeMillis()) + ".log"); file.getParentFile().mkdirs(); final PrintWriter writer = new PrintWriter(file); - for (final String err : errorList) - writer.println(err); + for (final LookupCacheElement err : errorList) + writer.println(err.getMessage()); writer.close(); } catch (final Exception ex) {} + errors = errorList.toArray(new WorldEditorException[errorList.size()]); notify(); } } @@ -210,18 +209,22 @@ public class WorldEditor implements Runnable } @SuppressWarnings("serial") - public static class WorldEditorException extends Exception + public static class WorldEditorException extends Exception implements LookupCacheElement { - public WorldEditorException(String msg) { - super(msg); - } + private final Location loc; public WorldEditorException(int typeBefore, int typeAfter, Location loc) { this("Failed to replace " + materialName(typeBefore) + " with " + materialName(typeAfter), loc); } public WorldEditorException(String msg, Location loc) { - super(msg + " at " + loc.getBlockX() + ":" + loc.getBlockY() + ":" + loc.getBlockZ()); + super(msg + " at " + loc.getWorld().getName() + ":" + loc.getBlockX() + ":" + loc.getBlockY() + ":" + loc.getBlockZ()); + this.loc = loc; + } + + @Override + public Location getLocation() { + return loc; } } } diff --git a/src/de/diddiz/util/BukkitUtils.java b/src/de/diddiz/util/BukkitUtils.java index b50ff0d..1b6bc05 100644 --- a/src/de/diddiz/util/BukkitUtils.java +++ b/src/de/diddiz/util/BukkitUtils.java @@ -164,7 +164,7 @@ public class BukkitUtils } public static byte rawData(ItemStack item) { - return item.getType() != null ? (item.getData() != null ? item.getData().getData() : 0) : 0; + return item.getType() != null ? item.getData() != null ? item.getData().getData() : 0 : 0; } public static int saveSpawnHeight(Location loc) { @@ -186,7 +186,7 @@ public class BukkitUtils public static int modifyContainer(BlockState b, ItemStack item) throws Exception { if (!(b instanceof ContainerBlock)) - throw new Exception("No container at " + b.getBlock().getLocation().toString()); + throw new Exception("No container"); final Inventory inv = ((ContainerBlock)b).getInventory(); if (item.getAmount() < 0) { item.setAmount(-item.getAmount());