Reworked rollback manager.

This commit is contained in:
StevenLawson 2013-08-05 15:26:21 -04:00
parent 028d1a07b2
commit e0efb63123
4 changed files with 123 additions and 122 deletions

View File

@ -82,7 +82,7 @@ public class Command_rollback extends TFM_Command
} }
TFM_Util.adminAction(sender.getName(), "Rolling back player: " + p.getName(), false); TFM_Util.adminAction(sender.getName(), "Rolling back player: " + p.getName(), false);
playerMsg("Rolled back " + TFM_RollbackManager.rollback(p) + " blocks"); playerMsg("Rolled back " + TFM_RollbackManager.rollback(p.getName()) + " blocks");
return true; return true;
} }
} }

View File

@ -3,7 +3,6 @@ package me.StevenLawson.TotalFreedomMod.Listener;
import me.StevenLawson.TotalFreedomMod.TFM_Log; import me.StevenLawson.TotalFreedomMod.TFM_Log;
import me.StevenLawson.TotalFreedomMod.TFM_PlayerData; import me.StevenLawson.TotalFreedomMod.TFM_PlayerData;
import me.StevenLawson.TotalFreedomMod.TFM_ProtectedArea; import me.StevenLawson.TotalFreedomMod.TFM_ProtectedArea;
import me.StevenLawson.TotalFreedomMod.TFM_RollbackEntry;
import me.StevenLawson.TotalFreedomMod.TFM_RollbackManager; import me.StevenLawson.TotalFreedomMod.TFM_RollbackManager;
import me.StevenLawson.TotalFreedomMod.TFM_SuperadminList; import me.StevenLawson.TotalFreedomMod.TFM_SuperadminList;
import me.StevenLawson.TotalFreedomMod.TFM_Util; import me.StevenLawson.TotalFreedomMod.TFM_Util;
@ -95,9 +94,15 @@ public class TFM_BlockListener implements Listener
if (TFM_ProtectedArea.isInProtectedArea(block_pos)) if (TFM_ProtectedArea.isInProtectedArea(block_pos))
{ {
event.setCancelled(true); event.setCancelled(true);
return;
} }
} }
} }
if (!event.isCancelled())
{
TFM_RollbackManager.blockBreak(event);
}
} }
@EventHandler(priority = EventPriority.HIGH) @EventHandler(priority = EventPriority.HIGH)
@ -179,6 +184,7 @@ public class TFM_BlockListener implements Listener
p.sendMessage(ChatColor.GRAY + "Lava placement is currently disabled."); p.sendMessage(ChatColor.GRAY + "Lava placement is currently disabled.");
event.setCancelled(true); event.setCancelled(true);
return;
} }
break; break;
} }
@ -197,6 +203,7 @@ public class TFM_BlockListener implements Listener
p.sendMessage(ChatColor.GRAY + "Water placement is currently disabled."); p.sendMessage(ChatColor.GRAY + "Water placement is currently disabled.");
event.setCancelled(true); event.setCancelled(true);
return;
} }
break; break;
} }
@ -214,6 +221,7 @@ public class TFM_BlockListener implements Listener
p.sendMessage(ChatColor.GRAY + "Fire placement is currently disabled."); p.sendMessage(ChatColor.GRAY + "Fire placement is currently disabled.");
event.setCancelled(true); event.setCancelled(true);
return;
} }
break; break;
} }
@ -231,10 +239,16 @@ public class TFM_BlockListener implements Listener
p.sendMessage(ChatColor.GRAY + "TNT is currently disabled."); p.sendMessage(ChatColor.GRAY + "TNT is currently disabled.");
event.setCancelled(true); event.setCancelled(true);
return;
} }
break; break;
} }
} }
if (!event.isCancelled())
{
TFM_RollbackManager.blockPlace(event);
}
} }
@EventHandler(priority = EventPriority.HIGH) @EventHandler(priority = EventPriority.HIGH)
@ -245,19 +259,4 @@ public class TFM_BlockListener implements Listener
event.setCancelled(true); event.setCancelled(true);
} }
} }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockPlaceRollback(BlockPlaceEvent event)
{
TFM_RollbackEntry entry = new TFM_RollbackEntry();
entry.setLocation(event.getBlock().getLocation());
entry.setMaterial(Material.AIR);
TFM_RollbackManager.blockUpdate(event.getPlayer(), entry);
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockBreakRollback(BlockBreakEvent event)
{
TFM_RollbackManager.blockUpdate(event.getPlayer(), event.getBlock());
}
} }

View File

@ -1,52 +0,0 @@
package me.StevenLawson.TotalFreedomMod;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
public class TFM_RollbackEntry
{
private Location location;
private Material material;
private byte data;
public TFM_RollbackEntry()
{
}
public TFM_RollbackEntry(Block block)
{
location = block.getLocation();
material = block.getType();
data = block.getData();
}
public void setBlock(Block block)
{
location = block.getLocation();
material = block.getType();
data = block.getData();
}
public void setLocation(Location location)
{
this.location = location;
}
public void setMaterial(Material material)
{
this.material = material;
}
public void setData(byte data)
{
this.data = data;
}
public void restore()
{
Block b = location.getWorld().getBlockAt(location);
b.setType(material);
b.setData(data);
}
}

View File

@ -2,85 +2,139 @@ package me.StevenLawson.TotalFreedomMod;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.bukkit.OfflinePlayer; import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.Player;
public class TFM_RollbackManager public class TFM_RollbackManager
{ {
public static Map<String, List<TFM_RollbackEntry>> entries = new HashMap<String, List<TFM_RollbackEntry>>(); private static final Map<String, List<TFM_RollbackManager_Entry>> PLAYER_HISTORY_MAP = new HashMap<String, List<TFM_RollbackManager_Entry>>();
public static void blockUpdate(OfflinePlayer player, Block block) private TFM_RollbackManager()
{ {
List<TFM_RollbackEntry> e; throw new AssertionError();
if (entries.containsKey(player.getName()))
{
e = entries.get(player.getName());
}
else
{
e = new ArrayList<TFM_RollbackEntry>();
}
e.add(0, new TFM_RollbackEntry(block));
entries.put(player.getName(), e);
} }
public static void blockUpdate(OfflinePlayer player, TFM_RollbackEntry entry) public static void blockPlace(org.bukkit.event.block.BlockPlaceEvent event)
{ {
List<TFM_RollbackEntry> e; storeEntry(event.getPlayer(), new TFM_RollbackManager_Entry(event.getBlock(), TFM_RollbackManager_EntryType.BLOCK_PLACE));
if (entries.containsKey(player.getName()))
{
e = entries.get(player.getName());
}
else
{
e = new ArrayList<TFM_RollbackEntry>();
}
e.add(0, entry);
entries.put(player.getName(), e);
} }
public static int rollback(OfflinePlayer player) public static void blockBreak(org.bukkit.event.block.BlockBreakEvent event)
{ {
if (!canRollback(player.getName())) storeEntry(event.getPlayer(), new TFM_RollbackManager_Entry(event.getBlock(), TFM_RollbackManager_EntryType.BLOCK_BREAK));
{
TFM_Log.severe("Could not rollback player: " + player.getName() + "! No entries are set");
return 0;
} }
List<TFM_RollbackEntry> e = entries.get(player.getName()); private static void storeEntry(Player player, TFM_RollbackManager_Entry entry)
int counter = 0;
for (TFM_RollbackEntry entry : e)
{ {
entry.restore(); List<TFM_RollbackManager_Entry> playerEntryList = getPlayerEntryList(player.getName());
counter++; if (playerEntryList != null)
}
entries.remove(player.getName());
return counter;
}
public static boolean canRollback(String player)
{ {
return entries.containsKey(player); playerEntryList.add(0, entry);
}
} }
public static int purgeEntries() public static int purgeEntries()
{ {
int counter = entries.size(); int count = 0;
entries.clear(); Iterator<List<TFM_RollbackManager_Entry>> it = PLAYER_HISTORY_MAP.values().iterator();
return counter; while (it.hasNext())
{
List<TFM_RollbackManager_Entry> playerEntryList = it.next();
if (playerEntryList != null)
{
count += playerEntryList.size();
playerEntryList.clear();
}
}
return count;
} }
public static int purgeEntries(String player) public static int purgeEntries(String playerName)
{ {
if (!canRollback(player)) List<TFM_RollbackManager_Entry> playerEntryList = getPlayerEntryList(playerName);
if (playerEntryList != null)
{ {
int count = playerEntryList.size();
playerEntryList.clear();
return count;
}
return 0; return 0;
} }
int counter = entries.get(player).size(); public static boolean canRollback(String playerName)
entries.remove(player); {
return counter; return PLAYER_HISTORY_MAP.containsKey(playerName.toLowerCase());
}
public static int rollback(String playerName)
{
List<TFM_RollbackManager_Entry> playerEntryList = getPlayerEntryList(playerName);
if (playerEntryList != null)
{
int count = playerEntryList.size();
Iterator<TFM_RollbackManager_Entry> it = playerEntryList.iterator();
while (it.hasNext())
{
TFM_RollbackManager_Entry entry = it.next();
if (entry != null)
{
entry.restore();
}
it.remove();
}
return count;
}
return 0;
}
private static List<TFM_RollbackManager_Entry> getPlayerEntryList(String playerName)
{
playerName = playerName.toLowerCase();
List<TFM_RollbackManager_Entry> playerEntryList = PLAYER_HISTORY_MAP.get(playerName);
if (playerEntryList == null)
{
playerEntryList = new ArrayList<TFM_RollbackManager_Entry>();
PLAYER_HISTORY_MAP.put(playerName, playerEntryList);
}
return playerEntryList;
}
private enum TFM_RollbackManager_EntryType
{
BLOCK_PLACE, BLOCK_BREAK
}
private static class TFM_RollbackManager_Entry
{
private final Location location;
private final Material material;
private final byte data;
public TFM_RollbackManager_Entry(Block block, TFM_RollbackManager_EntryType entryType)
{
this.location = block.getLocation();
if (entryType == TFM_RollbackManager_EntryType.BLOCK_BREAK)
{
this.material = block.getType();
this.data = block.getData();
}
else
{
this.material = Material.AIR;
this.data = 0;
}
}
public void restore()
{
Block b = this.location.getWorld().getBlockAt(this.location);
b.setType(this.material);
b.setData(this.data);
}
} }
} }