From b28a0778b40cbc1cc550312841690aa23177620b Mon Sep 17 00:00:00 2001 From: JeromSar Date: Tue, 27 Aug 2013 11:52:28 +0200 Subject: [PATCH] Optimized RollbackEntry for memory --- appinfo.properties | 6 +- buildnumber.properties | 4 +- .../Listener/TFM_PlayerListener.java | 5 +- .../TotalFreedomMod/TFM_RollbackManager.java | 75 ++++++++++++++----- 4 files changed, 62 insertions(+), 28 deletions(-) diff --git a/appinfo.properties b/appinfo.properties index 1d87ba8f..e3a24b50 100644 --- a/appinfo.properties +++ b/appinfo.properties @@ -1,5 +1,5 @@ -#Mon, 26 Aug 2013 17:47:26 +0200 +#Tue, 27 Aug 2013 11:50:34 +0200 program.VERSION=3.1 -program.BUILDNUM=507 -program.BUILDDATE=08/26/2013 05\:47 PM +program.BUILDNUM=513 +program.BUILDDATE=08/27/2013 11\:50 AM diff --git a/buildnumber.properties b/buildnumber.properties index bb75ebf9..aad2fa2a 100644 --- a/buildnumber.properties +++ b/buildnumber.properties @@ -1,3 +1,3 @@ #Build Number for ANT. Do not edit! -#Mon Aug 26 17:47:26 CEST 2013 -build.number=508 +#Tue Aug 27 11:50:34 CEST 2013 +build.number=514 diff --git a/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java b/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java index f8eb0dcb..a58720c2 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java +++ b/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java @@ -102,11 +102,10 @@ public class TFM_PlayerListener implements Listener break; } - TFM_Util.playerMsg(player, "Block edits at (" + ChatColor.WHITE + "X" + location.getBlockX() + ", Y" + location.getBlockY() + ", Z" + location.getBlockZ() + ChatColor.BLUE + ")" + ChatColor.WHITE + ":", ChatColor.BLUE); + TFM_Util.playerMsg(player, "Block edits at (" + ChatColor.WHITE + "x" + location.getBlockX() + ", y" + location.getBlockY() + ", z" + location.getBlockZ() + ChatColor.BLUE + ")" + ChatColor.WHITE + ":", ChatColor.BLUE); for (RollbackEntry entry : entries) { - String material = (entry.getType() == EntryType.BLOCK_BREAK ? String.valueOf(entry.getFromMaterial()) : entry.getToMaterial() + (entry.getData() != 0 ? ":" + String.valueOf(entry.getData()) : "")); - TFM_Util.playerMsg(player, " - " + ChatColor.BLUE + entry.getAuthor() + " " + entry.getType() + " " + material); + TFM_Util.playerMsg(player, " - " + ChatColor.BLUE + entry.getAuthor() + " " + entry.getType() + " " + StringUtils.capitalize(entry.getMaterial().toString().toLowerCase()) + (entry.getData() == 0 ? "" : ":" + entry.getData())); } break; diff --git a/src/me/StevenLawson/TotalFreedomMod/TFM_RollbackManager.java b/src/me/StevenLawson/TotalFreedomMod/TFM_RollbackManager.java index 07a09310..591fe35d 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TFM_RollbackManager.java +++ b/src/me/StevenLawson/TotalFreedomMod/TFM_RollbackManager.java @@ -5,6 +5,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; @@ -140,28 +141,37 @@ public class TFM_RollbackManager public static class RollbackEntry { + // Use of primitives to decrease overhead private final String author; - private final Location location; - private final int toBlockId; // ints have less overhead than Materials - private final int fromBlockId; + private final String worldName; + private final int x; + private final short y; + private final int z; + private final short blockId; private final byte data; + private final boolean isBreak; private RollbackEntry(String author, Block block, EntryType entryType) { - this.location = block.getLocation().clone(); + final Location location = block.getLocation(); + + this.x = location.getBlockX(); + this.y = (short) location.getBlockY(); + this.z = location.getBlockZ(); + this.worldName = location.getWorld().getName(); this.author = author; if (entryType == EntryType.BLOCK_BREAK) { - fromBlockId = block.getTypeId(); - toBlockId = Material.AIR.getId(); - data = block.getData(); + this.blockId = (short) block.getTypeId(); + this.data = block.getData(); + this.isBreak = true; } else { - fromBlockId = Material.AIR.getId(); - toBlockId = block.getTypeId(); + blockId = (short) block.getTypeId(); data = 0; + this.isBreak = false; } } @@ -172,19 +182,37 @@ public class TFM_RollbackManager public Location getLocation() { - return location; + try + { + return new Location(Bukkit.getWorld(worldName), (double) x, (double) y, (double) z); + } + catch (Exception ex) + { + TFM_Log.warning("Could not get location of rollback entry at (" + worldName + ":" + x + "," + y + "," + x + ")!"); + } + return null; } - public Material getFromMaterial() + public Material getMaterial() { - return Material.getMaterial(fromBlockId); + return Material.getMaterial(blockId); } - - public Material getToMaterial() + + public int getX() { - return Material.getMaterial(toBlockId); + return x; } - + + public int getY() + { + return y; + } + + public int getZ() + { + return z; + } + public byte getData() { return data; @@ -192,14 +220,21 @@ public class TFM_RollbackManager public EntryType getType() { - return (getFromMaterial() == Material.AIR ? EntryType.BLOCK_PLACE : EntryType.BLOCK_BREAK); + return (isBreak ? EntryType.BLOCK_BREAK : EntryType.BLOCK_PLACE); } public void restore() { - Block block = location.getWorld().getBlockAt(location); - block.setType(getFromMaterial()); - block.setData(data); + Block block = Bukkit.getWorld(worldName).getBlockAt(x, y, z); + if (isBreak) + { + block.setType(getMaterial()); + block.setData(data); + } + else + { + block.setType(Material.AIR); + } } } }