From c0b983e8e8aa4fc76269d5655037652a84b66f42 Mon Sep 17 00:00:00 2001 From: Philip Cass Date: Wed, 4 Dec 2013 11:21:34 +0000 Subject: [PATCH] Allow for logging of item data greater than 256. Fixes #96, #235 This change involves refactoring many instances of "byte" to "short and altering a few ItemStack calls to use the int,int,short form instead of int,int,0,byte Significantly it also involves altering the MaterialName system to allow shorts instead of bytes, and altering the rawdata() function to return the durability of an ItemStack rather than the data value of its Material. One side effect of this is that tool durability is now logged, which will make for more accurate rollbacks. --- .../java/de/diddiz/LogBlock/ChestAccess.java | 5 ++--- .../de/diddiz/LogBlock/CommandsHandler.java | 2 +- .../java/de/diddiz/LogBlock/Consumer.java | 4 ++-- src/main/java/de/diddiz/LogBlock/Updater.java | 22 ++++++++++++++++++- .../java/de/diddiz/LogBlock/WorldEditor.java | 6 ++--- src/main/java/de/diddiz/util/BukkitUtils.java | 10 ++++----- .../java/de/diddiz/util/MaterialName.java | 14 ++++++------ src/main/java/de/diddiz/util/Utils.java | 9 ++++++++ src/main/resources/plugin.yml | 2 +- 9 files changed, 51 insertions(+), 23 deletions(-) diff --git a/src/main/java/de/diddiz/LogBlock/ChestAccess.java b/src/main/java/de/diddiz/LogBlock/ChestAccess.java index a59080b..f31b735 100644 --- a/src/main/java/de/diddiz/LogBlock/ChestAccess.java +++ b/src/main/java/de/diddiz/LogBlock/ChestAccess.java @@ -2,10 +2,9 @@ package de.diddiz.LogBlock; public class ChestAccess { - final short itemType, itemAmount; - final byte itemData; + final short itemType, itemAmount, itemData; - public ChestAccess(short itemType, short itemAmount, byte itemData) { + public ChestAccess(short itemType, short itemAmount, short itemData) { this.itemType = itemType; this.itemAmount = itemAmount; this.itemData = itemData >= 0 ? itemData : 0; diff --git a/src/main/java/de/diddiz/LogBlock/CommandsHandler.java b/src/main/java/de/diddiz/LogBlock/CommandsHandler.java index 16cbf56..a7ecb43 100755 --- a/src/main/java/de/diddiz/LogBlock/CommandsHandler.java +++ b/src/main/java/de/diddiz/LogBlock/CommandsHandler.java @@ -607,7 +607,7 @@ public class CommandsHandler implements CommandExecutor final WorldEditor editor = new WorldEditor(logblock, params.world); while (rs.next()) - editor.queueEdit(rs.getInt("x"), rs.getInt("y"), rs.getInt("z"), rs.getInt("replaced"), rs.getInt("type"), rs.getByte("data"), rs.getString("signtext"), rs.getShort("itemtype"), rs.getShort("itemamount"), rs.getByte("itemdata")); + editor.queueEdit(rs.getInt("x"), rs.getInt("y"), rs.getInt("z"), rs.getInt("replaced"), rs.getInt("type"), rs.getByte("data"), rs.getString("signtext"), rs.getShort("itemtype"), rs.getShort("itemamount"), rs.getShort("itemdata")); final int changes = editor.getSize(); if (changes > 10000) { editor.setSender(sender); diff --git a/src/main/java/de/diddiz/LogBlock/Consumer.java b/src/main/java/de/diddiz/LogBlock/Consumer.java index 4aa7558..5247b89 100644 --- a/src/main/java/de/diddiz/LogBlock/Consumer.java +++ b/src/main/java/de/diddiz/LogBlock/Consumer.java @@ -122,7 +122,7 @@ public class Consumer extends TimerTask * @param container * The respective container. Must be an instance of an InventoryHolder. */ - public void queueChestAccess(String playerName, BlockState container, short itemType, short itemAmount, byte itemData) { + public void queueChestAccess(String playerName, BlockState container, short itemType, short itemAmount, short itemData) { if (!(container instanceof InventoryHolder)) return; queueChestAccess(playerName, new Location(container.getWorld(), container.getX(), container.getY(), container.getZ()), container.getTypeId(), itemType, itemAmount, itemData); @@ -132,7 +132,7 @@ public class Consumer extends TimerTask * @param type * Type id of the container. */ - public void queueChestAccess(String playerName, Location loc, int type, short itemType, short itemAmount, byte itemData) { + public void queueChestAccess(String playerName, Location loc, int type, short itemType, short itemAmount, short itemData) { queueBlock(playerName, loc, type, type, (byte)0, null, new ChestAccess(itemType, itemAmount, itemData)); } diff --git a/src/main/java/de/diddiz/LogBlock/Updater.java b/src/main/java/de/diddiz/LogBlock/Updater.java index 7691715..195af4f 100644 --- a/src/main/java/de/diddiz/LogBlock/Updater.java +++ b/src/main/java/de/diddiz/LogBlock/Updater.java @@ -235,6 +235,26 @@ class Updater } config.set("version", "1.71"); } + if (config.getString("version").compareTo("1.72") < 0) { + getLogger().info("Updating tables to 1.72 ..."); + final Connection conn = logblock.getConnection(); + try { + conn.setAutoCommit(true); + final Statement st = conn.createStatement(); + for (final WorldConfig wcfg : getLoggedWorlds()) { + if (wcfg.isLogging(Logging.CHESTACCESS)) { + st.execute("ALTER TABLE `"+wcfg.table+"-chest` CHANGE itemdata itemdata SMALLINT UNSIGNED NOT NULL"); + getLogger().info("Table "+wcfg.table+"-chest modified"); + } + } + st.close(); + conn.close(); + } catch (final SQLException ex) { + Bukkit.getLogger().log(Level.SEVERE, "[Updater] Error: ", ex); + return false; + } + config.set("version", "1.72"); + } logblock.saveConfig(); return true; @@ -257,7 +277,7 @@ class Updater for (final WorldConfig wcfg : getLoggedWorlds()) { createTable(dbm, state, wcfg.table, "(id INT UNSIGNED NOT NULL AUTO_INCREMENT, date DATETIME NOT NULL, playerid INT UNSIGNED NOT NULL, replaced TINYINT UNSIGNED NOT NULL, type TINYINT UNSIGNED NOT NULL, data TINYINT UNSIGNED NOT NULL, x MEDIUMINT NOT NULL, y SMALLINT UNSIGNED NOT NULL, z MEDIUMINT NOT NULL, PRIMARY KEY (id), KEY coords (x, z, y), KEY date (date), KEY playerid (playerid))"); createTable(dbm, state, wcfg.table + "-sign", "(id INT UNSIGNED NOT NULL, signtext VARCHAR(255) NOT NULL, PRIMARY KEY (id)) DEFAULT CHARSET utf8"); - createTable(dbm, state, wcfg.table + "-chest", "(id INT UNSIGNED NOT NULL, itemtype SMALLINT UNSIGNED NOT NULL, itemamount SMALLINT NOT NULL, itemdata TINYINT UNSIGNED NOT NULL, PRIMARY KEY (id))"); + createTable(dbm, state, wcfg.table + "-chest", "(id INT UNSIGNED NOT NULL, itemtype SMALLINT UNSIGNED NOT NULL, itemamount SMALLINT NOT NULL, itemdata SMALLINT UNSIGNED NOT NULL, PRIMARY KEY (id))"); if (wcfg.isLogging(Logging.KILL)) createTable(dbm, state, wcfg.table + "-kills", "(id INT UNSIGNED NOT NULL AUTO_INCREMENT, date DATETIME NOT NULL, killer INT UNSIGNED, victim INT UNSIGNED NOT NULL, weapon SMALLINT UNSIGNED NOT NULL, x MEDIUMINT NOT NULL, y SMALLINT NOT NULL, z MEDIUMINT NOT NULL, PRIMARY KEY (id))"); } diff --git a/src/main/java/de/diddiz/LogBlock/WorldEditor.java b/src/main/java/de/diddiz/LogBlock/WorldEditor.java index 4654224..cecbfc4 100644 --- a/src/main/java/de/diddiz/LogBlock/WorldEditor.java +++ b/src/main/java/de/diddiz/LogBlock/WorldEditor.java @@ -71,7 +71,7 @@ public class WorldEditor implements Runnable this.sender = sender; } - public void queueEdit(int x, int y, int z, int replaced, int type, byte data, String signtext, short itemType, short itemAmount, byte itemData) { + public void queueEdit(int x, int y, int z, int replaced, int type, byte data, String signtext, short itemType, short itemAmount, short itemData) { edits.add(new Edit(0, new Location(world, x, y, z), null, replaced, type, data, signtext, new ChestAccess(itemType, itemAmount, itemData))); } @@ -165,11 +165,11 @@ public class WorldEditor implements Runnable } else if (ca != null && (type == 23 || type == 54 || type == 61 || type == 62)) { int leftover; try { - leftover = modifyContainer(state, new ItemStack(ca.itemType, -ca.itemAmount, (short)0, ca.itemData)); + leftover = modifyContainer(state, new ItemStack(ca.itemType, -ca.itemAmount, ca.itemData)); if (leftover > 0) for (final BlockFace face : new BlockFace[]{BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}) if (block.getRelative(face).getTypeId() == 54) - leftover = modifyContainer(block.getRelative(face).getState(), new ItemStack(ca.itemType, ca.itemAmount < 0 ? leftover : -leftover, (short)0, ca.itemData)); + leftover = modifyContainer(block.getRelative(face).getState(), new ItemStack(ca.itemType, ca.itemAmount < 0 ? leftover : -leftover, ca.itemData)); } catch (final Exception ex) { throw new WorldEditorException(ex.getMessage(), block.getLocation()); } diff --git a/src/main/java/de/diddiz/util/BukkitUtils.java b/src/main/java/de/diddiz/util/BukkitUtils.java index 59f45a7..c930cf4 100644 --- a/src/main/java/de/diddiz/util/BukkitUtils.java +++ b/src/main/java/de/diddiz/util/BukkitUtils.java @@ -242,7 +242,7 @@ public class BukkitUtils for (final ItemStack item : items) if (item != null) { final int type = item.getTypeId(); - final byte data = rawData(item); + final short data = rawData(item); boolean found = false; for (final ItemStack item2 : compressed) if (type == item2.getTypeId() && data == rawData(item2)) { @@ -251,7 +251,7 @@ public class BukkitUtils break; } if (!found) - compressed.add(new ItemStack(type, item.getAmount(), (short)0, data)); + compressed.add(new ItemStack(type, item.getAmount(), data)); } Collections.sort(compressed, new ItemStackComparator()); return compressed.toArray(new ItemStack[compressed.size()]); @@ -322,8 +322,8 @@ public class BukkitUtils } } - public static byte rawData(ItemStack item) { - return item.getType() != null ? item.getData() != null ? item.getData().getData() : 0 : 0; + public static short rawData(ItemStack item) { + return item.getType() != null ? item.getData() != null ? item.getDurability() : 0 : 0; } public static int saveSpawnHeight(Location loc) { @@ -381,7 +381,7 @@ public class BukkitUtils return -1; if (aType > bType) return 1; - final byte aData = rawData(a), bData = rawData(b); + final short aData = rawData(a), bData = rawData(b); if (aData < bData) return -1; if (aData > bData) diff --git a/src/main/java/de/diddiz/util/MaterialName.java b/src/main/java/de/diddiz/util/MaterialName.java index ca9c92f..4855316 100644 --- a/src/main/java/de/diddiz/util/MaterialName.java +++ b/src/main/java/de/diddiz/util/MaterialName.java @@ -1,7 +1,7 @@ package de.diddiz.util; -import static de.diddiz.util.Utils.isByte; import static de.diddiz.util.Utils.isInt; +import static de.diddiz.util.Utils.isShort; import static org.bukkit.Bukkit.getLogger; import java.io.File; import java.io.IOException; @@ -16,7 +16,7 @@ import org.bukkit.material.MaterialData; public class MaterialName { private static final Map materialNames = new HashMap(); - private static final Map> materialDataNames = new HashMap>(); + private static final Map> materialDataNames = new HashMap>(); static { // Add all known materials @@ -57,13 +57,13 @@ public class MaterialName if (cfg.isString(entry)) materialNames.put(Integer.valueOf(entry), cfg.getString(entry)); else if (cfg.isConfigurationSection(entry)) { - final Map dataNames = new HashMap(); + final Map dataNames = new HashMap(); materialDataNames.put(Integer.valueOf(entry), dataNames); final ConfigurationSection sec = cfg.getConfigurationSection(entry); for (final String data : sec.getKeys(false)) - if (isByte(data)) { + if (isShort(data)) { if (sec.isString(data)) - dataNames.put(Byte.valueOf(data), sec.getString(data)); + dataNames.put(Short.valueOf(data), sec.getString(data)); else getLogger().warning("Parsing materials.yml: '" + data + "' is not a string."); } else @@ -84,8 +84,8 @@ public class MaterialName /** * @return Name of the material regarding it's data, or if it's unknown, the basic name. */ - public static String materialName(int type, byte data) { - final Map dataNames = materialDataNames.get(type); + public static String materialName(int type, short data) { + final Map dataNames = materialDataNames.get(type); if (dataNames != null) if (dataNames.containsKey(data)) return dataNames.get(data); diff --git a/src/main/java/de/diddiz/util/Utils.java b/src/main/java/de/diddiz/util/Utils.java index 167dec7..7959b63 100644 --- a/src/main/java/de/diddiz/util/Utils.java +++ b/src/main/java/de/diddiz/util/Utils.java @@ -19,6 +19,15 @@ public class Utils return false; } + public static boolean isShort(String str) { + try { + Short.parseShort(str); + return true; + } catch (final NumberFormatException ex) { + } + return false; + } + public static boolean isByte(String str) { try { Byte.parseByte(str); diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index ce1095d..fea01ee 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: ${project.name} -version: '1.71' +version: '1.72' author: DiddiZ authors: [md_5, ammar2] website: http://dev.bukkit.org/server-mods/logblock/