diff options
author | Iaccidentally <coryhuckaby@gmail.com> | 2013-02-12 18:37:40 -0500 |
---|---|---|
committer | Iaccidentally <coryhuckaby@gmail.com> | 2013-02-12 18:37:40 -0500 |
commit | 0c72a7ab991bcc4b2294aa53dba1fc79ade02b8c (patch) | |
tree | 7c40d7d3af53ed60042238b5e83fc508e4e13f3d /EssentialsProtect/src/com/earth2me/essentials/protect/data | |
parent | 416eacf618a4ddf27b99b1984adb47934f693f9e (diff) | |
download | Essentials-0c72a7ab991bcc4b2294aa53dba1fc79ade02b8c.tar Essentials-0c72a7ab991bcc4b2294aa53dba1fc79ade02b8c.tar.gz Essentials-0c72a7ab991bcc4b2294aa53dba1fc79ade02b8c.tar.lz Essentials-0c72a7ab991bcc4b2294aa53dba1fc79ade02b8c.tar.xz Essentials-0c72a7ab991bcc4b2294aa53dba1fc79ade02b8c.zip |
Clean up protect :: transient is not needed :: remove unnecessary returns :: @Override all the things!
Diffstat (limited to 'EssentialsProtect/src/com/earth2me/essentials/protect/data')
-rw-r--r-- | EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockJDBC.java | 852 | ||||
-rw-r--r-- | EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java | 516 |
2 files changed, 684 insertions, 684 deletions
diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockJDBC.java b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockJDBC.java index e24a71b80..18bb19a48 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockJDBC.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockJDBC.java @@ -1,426 +1,426 @@ -package com.earth2me.essentials.protect.data; - -import com.mchange.v2.c3p0.ComboPooledDataSource; -import java.beans.PropertyVetoException; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; -import org.bukkit.block.Block; - - -public abstract class ProtectedBlockJDBC implements IProtectedBlock -{ - protected static final Logger LOGGER = Logger.getLogger("Minecraft"); - protected final transient ComboPooledDataSource cpds; - - protected abstract PreparedStatement getStatementCreateTable(Connection conn) throws SQLException; - - protected abstract PreparedStatement getStatementUpdateFrom2_0Table(Connection conn) throws SQLException; - - protected abstract PreparedStatement getStatementDeleteAll(Connection conn) throws SQLException; - - protected abstract PreparedStatement getStatementInsert(Connection conn, String world, int x, int y, int z, String playerName) throws SQLException; - - protected abstract PreparedStatement getStatementPlayerCountByLocation(Connection conn, String world, int x, int y, int z, String playerName) throws SQLException; - - protected abstract PreparedStatement getStatementPlayersByLocation(Connection conn, String name, int x, int y, int z) throws SQLException; - - protected abstract PreparedStatement getStatementDeleteByLocation(Connection conn, String world, int x, int y, int z) throws SQLException; - - protected abstract PreparedStatement getStatementAllBlocks(Connection conn) throws SQLException; - - public ProtectedBlockJDBC(String driver, String url) throws PropertyVetoException - { - this(driver, url, null, null); - } - - public ProtectedBlockJDBC(String driver, String url, String username, String password) throws PropertyVetoException - { - cpds = new ComboPooledDataSource(); - cpds.setDriverClass(driver); - cpds.setJdbcUrl(url); - if (username != null) - { - cpds.setUser(username); - cpds.setPassword(password); - } - cpds.setMaxStatements(20); - createAndConvertTable(); - } - - private void createAndConvertTable() - { - Connection conn = null; - PreparedStatement ps = null; - try - { - conn = cpds.getConnection(); - ps = getStatementCreateTable(conn); - ps.execute(); - ps.close(); - ps = getStatementUpdateFrom2_0Table(conn); - ps.execute(); - } - catch (SQLException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - } - finally - { - if (ps != null) - { - try - { - ps.close(); - } - catch (SQLException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - } - } - if (conn != null) - { - try - { - conn.close(); - } - catch (SQLException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - } - } - } - } - - public void clearProtections() - { - Connection conn = null; - PreparedStatement ps = null; - try - { - conn = cpds.getConnection(); - ps = getStatementDeleteAll(conn); - ps.executeUpdate(); - } - catch (SQLException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - } - finally - { - if (ps != null) - { - try - { - ps.close(); - } - catch (SQLException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - } - } - if (conn != null) - { - try - { - conn.close(); - } - catch (SQLException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - } - } - } - } - - public void importProtections(List<OwnedBlock> blocks) - { - for (OwnedBlock ownedBlock : blocks) - { - if (ownedBlock.playerName == null) - { - continue; - } - protectBlock(ownedBlock.world, ownedBlock.x, ownedBlock.y, ownedBlock.z, ownedBlock.playerName); - } - } - - public List<OwnedBlock> exportProtections() - { - Connection conn = null; - PreparedStatement ps = null; - ResultSet rs = null; - List<OwnedBlock> blocks = new ArrayList<OwnedBlock>(); - try - { - conn = cpds.getConnection(); - ps = getStatementAllBlocks(conn); - rs = ps.executeQuery(); - while (rs.next()) - { - OwnedBlock ob = new OwnedBlock( - rs.getInt(2), - rs.getInt(3), - rs.getInt(4), - rs.getString(1), - rs.getString(5)); - blocks.add(ob); - } - return blocks; - } - catch (SQLException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - return blocks; - } - finally - { - if (rs != null) - { - try - { - rs.close(); - } - catch (SQLException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - } - } - if (ps != null) - { - try - { - ps.close(); - } - catch (SQLException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - } - } - if (conn != null) - { - try - { - conn.close(); - } - catch (SQLException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - } - } - } - } - - public void protectBlock(Block block, String playerName) - { - protectBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), playerName); - } - - private void protectBlock(String world, int x, int y, int z, String playerName) - { - Connection conn = null; - PreparedStatement ps = null; - try - { - conn = cpds.getConnection(); - ps = getStatementInsert(conn, world, x, y, z, playerName); - ps.executeUpdate(); - } - catch (SQLException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - } - finally - { - if (ps != null) - { - try - { - ps.close(); - } - catch (SQLException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - } - } - if (conn != null) - { - try - { - conn.close(); - } - catch (SQLException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - } - } - } - } - - public boolean isProtected(Block block, String playerName) - { - Connection conn = null; - PreparedStatement ps = null; - ResultSet rs = null; - try - { - conn = cpds.getConnection(); - ps = getStatementPlayerCountByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), playerName); - rs = ps.executeQuery(); - return rs.next() && rs.getInt(1) > 0 && rs.getInt(2) == 0; - } - catch (SQLException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - return true; - } - finally - { - if (rs != null) - { - try - { - rs.close(); - } - catch (SQLException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - } - } - if (ps != null) - { - try - { - ps.close(); - } - catch (SQLException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - } - } - if (conn != null) - { - try - { - conn.close(); - } - catch (SQLException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - } - } - } - } - - public List<String> getOwners(Block block) - { - Connection conn = null; - PreparedStatement ps = null; - ResultSet rs = null; - List<String> owners = new ArrayList<String>(); - try - { - conn = cpds.getConnection(); - ps = getStatementPlayersByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ()); - rs = ps.executeQuery(); - while (rs.next()) - { - owners.add(rs.getString(1)); - } - return owners; - } - catch (SQLException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - return owners; - } - finally - { - if (rs != null) - { - try - { - rs.close(); - } - catch (SQLException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - } - } - if (ps != null) - { - try - { - ps.close(); - } - catch (SQLException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - } - } - if (conn != null) - { - try - { - conn.close(); - } - catch (SQLException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - } - } - } - } - - public int unprotectBlock(Block block) - { - Connection conn = null; - PreparedStatement ps = null; - try - { - conn = cpds.getConnection(); - ps = getStatementDeleteByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ()); - return ps.executeUpdate(); - } - catch (SQLException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - return 0; - } - finally - { - if (ps != null) - { - try - { - ps.close(); - } - catch (SQLException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - } - } - if (conn != null) - { - try - { - conn.close(); - } - catch (SQLException ex) - { - LOGGER.log(Level.SEVERE, null, ex); - } - } - } - } - - public void onPluginDeactivation() - { - cpds.close(); - } -} +package com.earth2me.essentials.protect.data;
+
+import com.mchange.v2.c3p0.ComboPooledDataSource;
+import java.beans.PropertyVetoException;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import org.bukkit.block.Block;
+
+
+public abstract class ProtectedBlockJDBC implements IProtectedBlock
+{
+ protected static final Logger LOGGER = Logger.getLogger("Minecraft");
+ protected final ComboPooledDataSource cpds;
+ protected abstract PreparedStatement getStatementCreateTable(Connection conn) throws SQLException;
+ protected abstract PreparedStatement getStatementUpdateFrom2_0Table(Connection conn) throws SQLException;
+ protected abstract PreparedStatement getStatementDeleteAll(Connection conn) throws SQLException;
+ protected abstract PreparedStatement getStatementInsert(Connection conn, String world, int x, int y, int z, String playerName) throws SQLException;
+ protected abstract PreparedStatement getStatementPlayerCountByLocation(Connection conn, String world, int x, int y, int z, String playerName) throws SQLException;
+ protected abstract PreparedStatement getStatementPlayersByLocation(Connection conn, String name, int x, int y, int z) throws SQLException;
+ protected abstract PreparedStatement getStatementDeleteByLocation(Connection conn, String world, int x, int y, int z) throws SQLException;
+ protected abstract PreparedStatement getStatementAllBlocks(Connection conn) throws SQLException;
+
+ public ProtectedBlockJDBC(String driver, String url) throws PropertyVetoException
+ {
+ this(driver, url, null, null);
+ }
+
+ public ProtectedBlockJDBC(String driver, String url, String username, String password) throws PropertyVetoException
+ {
+ cpds = new ComboPooledDataSource();
+ cpds.setDriverClass(driver);
+ cpds.setJdbcUrl(url);
+ if (username != null)
+ {
+ cpds.setUser(username);
+ cpds.setPassword(password);
+ }
+ cpds.setMaxStatements(20);
+ createAndConvertTable();
+ }
+
+ private void createAndConvertTable()
+ {
+ Connection conn = null;
+ PreparedStatement ps = null;
+ try
+ {
+ conn = cpds.getConnection();
+ ps = getStatementCreateTable(conn);
+ ps.execute();
+ ps.close();
+ ps = getStatementUpdateFrom2_0Table(conn);
+ ps.execute();
+ }
+ catch (SQLException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ }
+ finally
+ {
+ if (ps != null)
+ {
+ try
+ {
+ ps.close();
+ }
+ catch (SQLException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ }
+ }
+ if (conn != null)
+ {
+ try
+ {
+ conn.close();
+ }
+ catch (SQLException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ }
+ }
+ }
+ }
+
+ @Override
+ public void clearProtections()
+ {
+ Connection conn = null;
+ PreparedStatement ps = null;
+ try
+ {
+ conn = cpds.getConnection();
+ ps = getStatementDeleteAll(conn);
+ ps.executeUpdate();
+ }
+ catch (SQLException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ }
+ finally
+ {
+ if (ps != null)
+ {
+ try
+ {
+ ps.close();
+ }
+ catch (SQLException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ }
+ }
+ if (conn != null)
+ {
+ try
+ {
+ conn.close();
+ }
+ catch (SQLException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ }
+ }
+ }
+ }
+
+ @Override
+ public void importProtections(List<OwnedBlock> blocks)
+ {
+ for (OwnedBlock ownedBlock : blocks)
+ {
+ if (ownedBlock.playerName == null)
+ {
+ continue;
+ }
+ protectBlock(ownedBlock.world, ownedBlock.x, ownedBlock.y, ownedBlock.z, ownedBlock.playerName);
+ }
+ }
+
+ @Override
+ public List<OwnedBlock> exportProtections()
+ {
+ Connection conn = null;
+ PreparedStatement ps = null;
+ ResultSet rs = null;
+ List<OwnedBlock> blocks = new ArrayList<OwnedBlock>();
+ try
+ {
+ conn = cpds.getConnection();
+ ps = getStatementAllBlocks(conn);
+ rs = ps.executeQuery();
+ while (rs.next())
+ {
+ OwnedBlock ob = new OwnedBlock(
+ rs.getInt(2),
+ rs.getInt(3),
+ rs.getInt(4),
+ rs.getString(1),
+ rs.getString(5));
+ blocks.add(ob);
+ }
+ return blocks;
+ }
+ catch (SQLException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ return blocks;
+ }
+ finally
+ {
+ if (rs != null)
+ {
+ try
+ {
+ rs.close();
+ }
+ catch (SQLException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ }
+ }
+ if (ps != null)
+ {
+ try
+ {
+ ps.close();
+ }
+ catch (SQLException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ }
+ }
+ if (conn != null)
+ {
+ try
+ {
+ conn.close();
+ }
+ catch (SQLException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ }
+ }
+ }
+ }
+
+ @Override
+ public void protectBlock(Block block, String playerName)
+ {
+ protectBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), playerName);
+ }
+
+ private void protectBlock(String world, int x, int y, int z, String playerName)
+ {
+ Connection conn = null;
+ PreparedStatement ps = null;
+ try
+ {
+ conn = cpds.getConnection();
+ ps = getStatementInsert(conn, world, x, y, z, playerName);
+ ps.executeUpdate();
+ }
+ catch (SQLException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ }
+ finally
+ {
+ if (ps != null)
+ {
+ try
+ {
+ ps.close();
+ }
+ catch (SQLException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ }
+ }
+ if (conn != null)
+ {
+ try
+ {
+ conn.close();
+ }
+ catch (SQLException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ }
+ }
+ }
+ }
+
+ @Override
+ public boolean isProtected(Block block, String playerName)
+ {
+ Connection conn = null;
+ PreparedStatement ps = null;
+ ResultSet rs = null;
+ try
+ {
+ conn = cpds.getConnection();
+ ps = getStatementPlayerCountByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), playerName);
+ rs = ps.executeQuery();
+ return rs.next() && rs.getInt(1) > 0 && rs.getInt(2) == 0;
+ }
+ catch (SQLException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ return true;
+ }
+ finally
+ {
+ if (rs != null)
+ {
+ try
+ {
+ rs.close();
+ }
+ catch (SQLException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ }
+ }
+ if (ps != null)
+ {
+ try
+ {
+ ps.close();
+ }
+ catch (SQLException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ }
+ }
+ if (conn != null)
+ {
+ try
+ {
+ conn.close();
+ }
+ catch (SQLException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ }
+ }
+ }
+ }
+
+ @Override
+ public List<String> getOwners(Block block)
+ {
+ Connection conn = null;
+ PreparedStatement ps = null;
+ ResultSet rs = null;
+ List<String> owners = new ArrayList<String>();
+ try
+ {
+ conn = cpds.getConnection();
+ ps = getStatementPlayersByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ());
+ rs = ps.executeQuery();
+ while (rs.next())
+ {
+ owners.add(rs.getString(1));
+ }
+ return owners;
+ }
+ catch (SQLException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ return owners;
+ }
+ finally
+ {
+ if (rs != null)
+ {
+ try
+ {
+ rs.close();
+ }
+ catch (SQLException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ }
+ }
+ if (ps != null)
+ {
+ try
+ {
+ ps.close();
+ }
+ catch (SQLException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ }
+ }
+ if (conn != null)
+ {
+ try
+ {
+ conn.close();
+ }
+ catch (SQLException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ }
+ }
+ }
+ }
+
+ @Override
+ public int unprotectBlock(Block block)
+ {
+ Connection conn = null;
+ PreparedStatement ps = null;
+ try
+ {
+ conn = cpds.getConnection();
+ ps = getStatementDeleteByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ());
+ return ps.executeUpdate();
+ }
+ catch (SQLException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ return 0;
+ }
+ finally
+ {
+ if (ps != null)
+ {
+ try
+ {
+ ps.close();
+ }
+ catch (SQLException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ }
+ }
+ if (conn != null)
+ {
+ try
+ {
+ conn.close();
+ }
+ catch (SQLException ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ }
+ }
+ }
+ }
+
+ @Override
+ public void onPluginDeactivation()
+ {
+ cpds.close();
+ }
+}
diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java index e1a61f0cf..348e75515 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java @@ -1,258 +1,258 @@ -package com.earth2me.essentials.protect.data; - -import java.util.*; -import java.util.Map.Entry; -import org.bukkit.World; -import org.bukkit.block.Block; -import org.bukkit.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; - - - static class ProtectedLocation - { - private final transient int x; - private final transient int y; - private final transient int z; - private final transient int w; - - public ProtectedLocation(final Block block, final int worldId) - { - this.x = block.getX(); - this.y = block.getY(); - this.z = block.getZ(); - this.w = worldId; - } - - public ProtectedLocation(final OwnedBlock ownedBlock, final int worldId) - { - this.x = ownedBlock.x; - this.y = ownedBlock.y; - this.z = ownedBlock.z; - this.w = worldId; - } - - @Override - 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() - { - return x ^ y ^ z ^ w; - } - } - - - static class ProtectedBy - { - private transient int playerId = -1; - private transient Set<Integer> playerIds; - - public void add(final int playerId) - { - if (this.playerId == -1 || this.playerId == playerId) - { - this.playerId = playerId; - } - else - { - if (playerIds == null) - { - playerIds = new HashSet<Integer>(4); - playerIds.add(this.playerId); - } - playerIds.add(playerId); - } - } - - public boolean contains(final int playerId) - { - if (playerIds == null) - { - return this.playerId == playerId; - } - return playerIds.contains(playerId); - } - - public List<String> getPlayers(final List<String> playerNames) - { - final List<String> list = new ArrayList<String>(2); - if (playerIds == null) - { - list.add(playerNames.get(playerId)); - } - else - { - for (Integer integer : playerIds) - { - list.add(playerNames.get(integer)); - } - } - return list; - } - - public int size() - { - if (playerIds == null) - { - return 1; - } - return playerIds.size(); - } - } - private final transient Map<ProtectedLocation, ProtectedBy> blocks = new HashMap<ProtectedLocation, ProtectedBy>(); - - public ProtectedBlockMemory(final IProtectedBlock storage, final Plugin plugin) - { - this.storage = storage; - this.plugin = plugin; - importProtections(storage.exportProtections()); - } - - @Override - public void clearProtections() - { - blocks.clear(); - } - - @Override - 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); - } - } - - @Override - 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; - } - - @Override - public void protectBlock(final Block block, final String playerName) - { - final ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld())); - protectBlock(pl, playerName); - plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() - { - @Override - public void run() - { - storage.protectBlock(block, playerName); - } - }); - } - - private void protectBlock(ProtectedLocation pl, String playerName) - { - int playerId = getPlayerId(playerName); - ProtectedBy pb = blocks.get(pl); - if (pb == null) - { - pb = new ProtectedBy(); - blocks.put(pl, pb); - } - pb.add(playerId); - } - - @Override - 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); - } - - @Override - public List<String> getOwners(Block block) - { - ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld())); - ProtectedBy pb = blocks.get(pl); - return pb.getPlayers(playerNames); - } - - @Override - public int unprotectBlock(final Block block) - { - ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld())); - ProtectedBy pb = blocks.remove(pl); - plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() - { - @Override - public void run() - { - storage.unprotectBlock(block); - } - }); - return pb.size(); - } - - private int getPlayerId(String playername) - { - int id = playerNames.indexOf(playername); - if (id < 0) - { - playerNames.add(playername); - id = playerNames.indexOf(playername); - } - return id; - } - - private int getWorldId(World world) - { - return getWorldId(world.getName()); - } - - private int getWorldId(String name) - { - int id = worlds.indexOf(name); - if (id < 0) - { - worlds.add(name); - id = worlds.indexOf(name); - } - return id; - } - - @Override - public void onPluginDeactivation() - { - storage.onPluginDeactivation(); - } -} +package com.earth2me.essentials.protect.data;
+
+import java.util.*;
+import java.util.Map.Entry;
+import org.bukkit.World;
+import org.bukkit.block.Block;
+import org.bukkit.plugin.Plugin;
+
+
+public class ProtectedBlockMemory implements IProtectedBlock
+{
+ private final List<String> worlds = new ArrayList<String>();
+ private final List<String> playerNames = new ArrayList<String>();
+ private final IProtectedBlock storage;
+ private final Plugin plugin;
+
+
+ static class ProtectedLocation
+ {
+ private final int x;
+ private final int y;
+ private final int z;
+ private final int w;
+
+ public ProtectedLocation(final Block block, final int worldId)
+ {
+ this.x = block.getX();
+ this.y = block.getY();
+ this.z = block.getZ();
+ this.w = worldId;
+ }
+
+ public ProtectedLocation(final OwnedBlock ownedBlock, final int worldId)
+ {
+ this.x = ownedBlock.x;
+ this.y = ownedBlock.y;
+ this.z = ownedBlock.z;
+ this.w = worldId;
+ }
+
+ @Override
+ 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()
+ {
+ return x ^ y ^ z ^ w;
+ }
+ }
+
+
+ static class ProtectedBy
+ {
+ private int playerId = -1;
+ private Set<Integer> playerIds;
+
+ public void add(final int playerId)
+ {
+ if (this.playerId == -1 || this.playerId == playerId)
+ {
+ this.playerId = playerId;
+ }
+ else
+ {
+ if (playerIds == null)
+ {
+ playerIds = new HashSet<Integer>(4);
+ playerIds.add(this.playerId);
+ }
+ playerIds.add(playerId);
+ }
+ }
+
+ public boolean contains(final int playerId)
+ {
+ if (playerIds == null)
+ {
+ return this.playerId == playerId;
+ }
+ return playerIds.contains(playerId);
+ }
+
+ public List<String> getPlayers(final List<String> playerNames)
+ {
+ final List<String> list = new ArrayList<String>(2);
+ if (playerIds == null)
+ {
+ list.add(playerNames.get(playerId));
+ }
+ else
+ {
+ for (Integer integer : playerIds)
+ {
+ list.add(playerNames.get(integer));
+ }
+ }
+ return list;
+ }
+
+ public int size()
+ {
+ if (playerIds == null)
+ {
+ return 1;
+ }
+ return playerIds.size();
+ }
+ }
+ private final Map<ProtectedLocation, ProtectedBy> blocks = new HashMap<ProtectedLocation, ProtectedBy>();
+
+ public ProtectedBlockMemory(final IProtectedBlock storage, final Plugin plugin)
+ {
+ this.storage = storage;
+ this.plugin = plugin;
+ importProtections(storage.exportProtections());
+ }
+
+ @Override
+ public void clearProtections()
+ {
+ blocks.clear();
+ }
+
+ @Override
+ 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);
+ }
+ }
+
+ @Override
+ 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;
+ }
+
+ @Override
+ public void protectBlock(final Block block, final String playerName)
+ {
+ final ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld()));
+ protectBlock(pl, playerName);
+ plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable()
+ {
+ @Override
+ public void run()
+ {
+ storage.protectBlock(block, playerName);
+ }
+ });
+ }
+
+ private void protectBlock(ProtectedLocation pl, String playerName)
+ {
+ int playerId = getPlayerId(playerName);
+ ProtectedBy pb = blocks.get(pl);
+ if (pb == null)
+ {
+ pb = new ProtectedBy();
+ blocks.put(pl, pb);
+ }
+ pb.add(playerId);
+ }
+
+ @Override
+ 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);
+ }
+
+ @Override
+ public List<String> getOwners(Block block)
+ {
+ ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld()));
+ ProtectedBy pb = blocks.get(pl);
+ return pb.getPlayers(playerNames);
+ }
+
+ @Override
+ public int unprotectBlock(final Block block)
+ {
+ ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld()));
+ ProtectedBy pb = blocks.remove(pl);
+ plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable()
+ {
+ @Override
+ public void run()
+ {
+ storage.unprotectBlock(block);
+ }
+ });
+ return pb.size();
+ }
+
+ private int getPlayerId(String playername)
+ {
+ int id = playerNames.indexOf(playername);
+ if (id < 0)
+ {
+ playerNames.add(playername);
+ id = playerNames.indexOf(playername);
+ }
+ return id;
+ }
+
+ private int getWorldId(World world)
+ {
+ return getWorldId(world.getName());
+ }
+
+ private int getWorldId(String name)
+ {
+ int id = worlds.indexOf(name);
+ if (id < 0)
+ {
+ worlds.add(name);
+ id = worlds.indexOf(name);
+ }
+ return id;
+ }
+
+ @Override
+ public void onPluginDeactivation()
+ {
+ storage.onPluginDeactivation();
+ }
+}
|