diff options
author | snowleo <schneeleo@gmail.com> | 2011-06-06 22:29:08 +0200 |
---|---|---|
committer | snowleo <schneeleo@gmail.com> | 2011-06-06 22:29:08 +0200 |
commit | a45e620946c8e748b35258b7f8813d1c788bae37 (patch) | |
tree | 76b100b6dda862e6d714f85ef9bd40f68bd3b51c /EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java | |
parent | d8778801711ef6c78445baa5da38f4591db0b6c2 (diff) | |
download | Essentials-a45e620946c8e748b35258b7f8813d1c788bae37.tar Essentials-a45e620946c8e748b35258b7f8813d1c788bae37.tar.gz Essentials-a45e620946c8e748b35258b7f8813d1c788bae37.tar.lz Essentials-a45e620946c8e748b35258b7f8813d1c788bae37.tar.xz Essentials-a45e620946c8e748b35258b7f8813d1c788bae37.zip |
Major cleanup of the Protect code
Diffstat (limited to 'EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java')
-rw-r--r-- | EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java | 185 |
1 files changed, 114 insertions, 71 deletions
diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java index 53dee66d2..2fd32b026 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java @@ -4,34 +4,39 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Map.Entry; import java.util.Set; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.plugin.Plugin; -public class ProtectedBlockMemory implements IProtectedBlock { - List<String> worlds = new ArrayList<String>(); - List<String> playerNames = new ArrayList<String>(); - IProtectedBlock storage; - Plugin plugin; +public class ProtectedBlockMemory implements IProtectedBlock +{ + private final transient List<String> worlds = new ArrayList<String>(); + private final transient List<String> playerNames = new ArrayList<String>(); + private final transient IProtectedBlock storage; + private final transient Plugin plugin; - class ProtectedLocation { - int x; - int y; - int z; - int w; + static class ProtectedLocation + { + private final transient int x; + private final transient int y; + private final transient int z; + private final transient int w; - private ProtectedLocation(Block block, int worldId) { + public ProtectedLocation(final Block block, final int worldId) + { this.x = block.getX(); this.y = block.getY(); this.z = block.getZ(); this.w = worldId; } - private ProtectedLocation(OwnedBlock ownedBlock, int worldId) { + public ProtectedLocation(final OwnedBlock ownedBlock, final int worldId) + { this.x = ownedBlock.x; this.y = ownedBlock.y; this.z = ownedBlock.z; @@ -39,33 +44,39 @@ public class ProtectedBlockMemory implements IProtectedBlock { } @Override - public boolean equals(Object o) { - if (o instanceof ProtectedLocation) { - ProtectedLocation pl = (ProtectedLocation) o; - return x == pl.x && y == pl.y && z == pl.z && w == pl.w; + public boolean equals(final Object object) + { + if (object instanceof ProtectedLocation) + { + final ProtectedLocation pLoc = (ProtectedLocation)object; + return x == pLoc.x && y == pLoc.y && z == pLoc.z && w == pLoc.w; } return false; } @Override - public int hashCode() { + public int hashCode() + { return x ^ y ^ z ^ w; } } - class ProtectedBy { - private int playerId = -1; - private Set<Integer> playerIds; + static class ProtectedBy + { + private transient int playerId = -1; + private transient Set<Integer> playerIds; - private ProtectedBy() { - } - - private void add(int playerId) { - if (this.playerId == -1 || this.playerId == playerId) { + public void add(final int playerId) + { + if (this.playerId == -1 || this.playerId == playerId) + { this.playerId = playerId; - } else { - if (playerIds == null) { + } + else + { + if (playerIds == null) + { playerIds = new HashSet<Integer>(4); playerIds.add(this.playerId); } @@ -73,130 +84,162 @@ public class ProtectedBlockMemory implements IProtectedBlock { } } - private boolean contains(int playerId) { - if (playerIds == null) { + public boolean contains(final int playerId) + { + if (playerIds == null) + { return this.playerId == playerId; } return playerIds.contains(playerId); } - private List<String> getPlayers(List<String> playerNames) { - if (playerIds == null) { - List<String> list = new ArrayList<String>(2); + public List<String> getPlayers(final List<String> playerNames) + { + final List<String> list = new ArrayList<String>(2); + if (playerIds == null) + { list.add(playerNames.get(playerId)); - return list; } - List<String> list = new ArrayList<String>(playerIds.size()); - for (Integer integer : playerIds) { - list.add(playerNames.get(integer)); + else + { + for (Integer integer : playerIds) + { + list.add(playerNames.get(integer)); + } } return list; } - private int size() { - if (playerIds == null) { + public int size() + { + if (playerIds == null) + { return 1; } return playerIds.size(); } } - HashMap<ProtectedLocation, ProtectedBy> blocks = new HashMap<ProtectedLocation, ProtectedBy>(); + private final transient Map<ProtectedLocation, ProtectedBy> blocks = new HashMap<ProtectedLocation, ProtectedBy>(); - public ProtectedBlockMemory(IProtectedBlock storage) { + public ProtectedBlockMemory(final IProtectedBlock storage, final Plugin plugin) + { this.storage = storage; + this.plugin = plugin; importProtections(storage.exportProtections()); } - public void clearProtections() { + public void clearProtections() + { blocks.clear(); } - public final void importProtections(List<OwnedBlock> blocks) { - for (OwnedBlock ownedBlock : blocks) { - ProtectedLocation pl = new ProtectedLocation(ownedBlock, getWorldId(ownedBlock.world)); - if (ownedBlock.playerName == null) { + public final void importProtections(final List<OwnedBlock> blocks) + { + for (OwnedBlock ownedBlock : blocks) + { + final ProtectedLocation pl = new ProtectedLocation(ownedBlock, getWorldId(ownedBlock.world)); + if (ownedBlock.playerName == null) + { continue; } protectBlock(pl, ownedBlock.playerName); } } - public List<OwnedBlock> exportProtections() { - List<OwnedBlock> blockList = new ArrayList<OwnedBlock>(blocks.size()); - for (Entry<ProtectedLocation, ProtectedBy> entry : blocks.entrySet()) { - for (String name : entry.getValue().getPlayers(playerNames)) { - OwnedBlock ob = new OwnedBlock(); - ob.x = entry.getKey().x; - ob.y = entry.getKey().y; - ob.z = entry.getKey().z; - ob.world = worlds.get(entry.getKey().w); - ob.playerName = name; + public List<OwnedBlock> exportProtections() + { + final List<OwnedBlock> blockList = new ArrayList<OwnedBlock>(blocks.size()); + for (Entry<ProtectedLocation, ProtectedBy> entry : blocks.entrySet()) + { + for (String name : entry.getValue().getPlayers(playerNames)) + { + final OwnedBlock ob = new OwnedBlock( + entry.getKey().x, + entry.getKey().y, + entry.getKey().z, + worlds.get(entry.getKey().w), + name); blockList.add(ob); } } return blockList; } - public void protectBlock(final Block block, final String playerName) { - ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld())); + public void protectBlock(final Block block, final String playerName) + { + final ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld())); protectBlock(pl, playerName); - plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() { - public void run() { + plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() + { + public void run() + { storage.protectBlock(block, playerName); } }); } - private void protectBlock(ProtectedLocation pl, String playerName) { + private final void protectBlock(ProtectedLocation pl, String playerName) + { int playerId = getPlayerId(playerName); ProtectedBy pb = blocks.get(pl); - if (pb == null) { + if (pb == null) + { pb = new ProtectedBy(); blocks.put(pl, pb); } pb.add(playerId); } - public boolean isProtected(Block block, String playerName) { + public boolean isProtected(Block block, String playerName) + { int playerId = getPlayerId(playerName); ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld())); ProtectedBy pb = blocks.get(pl); return !pb.contains(playerId); } - public List<String> getOwners(Block block) { + public List<String> getOwners(Block block) + { ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld())); ProtectedBy pb = blocks.get(pl); return pb.getPlayers(playerNames); } - public int unprotectBlock(final Block block) { + public int unprotectBlock(final Block block) + { ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld())); ProtectedBy pb = blocks.remove(pl); - plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() { - public void run() { + plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() + { + public void run() + { storage.unprotectBlock(block); } }); return pb.size(); } - private int getPlayerId(String playername) { + private int getPlayerId(String playername) + { int id = playerNames.indexOf(playername); - if (id < 0) { + if (id < 0) + { playerNames.add(playername); id = playerNames.indexOf(playername); } return id; } - private int getWorldId(World world) { + private int getWorldId(World world) + { return getWorldId(world.getName()); } - private int getWorldId(String name) { + private int getWorldId(String name) + { int id = worlds.indexOf(name); - if (id < 0) { + if (id < 0) + { worlds.add(name); id = worlds.indexOf(name); } |