From a45e620946c8e748b35258b7f8813d1c788bae37 Mon Sep 17 00:00:00 2001 From: snowleo Date: Mon, 6 Jun 2011 22:29:08 +0200 Subject: Major cleanup of the Protect code --- .../essentials/protect/data/IProtectedBlock.java | 1 - .../essentials/protect/data/OwnedBlock.java | 19 +- .../protect/data/ProtectedBlockJDBC.java | 392 ++++++++++++++------- .../protect/data/ProtectedBlockMemory.java | 185 ++++++---- .../protect/data/ProtectedBlockMySQL.java | 20 +- 5 files changed, 405 insertions(+), 212 deletions(-) (limited to 'EssentialsProtect/src/com/earth2me/essentials/protect/data') diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/data/IProtectedBlock.java b/EssentialsProtect/src/com/earth2me/essentials/protect/data/IProtectedBlock.java index 28b7e425b..6580ce7f8 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/data/IProtectedBlock.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/data/IProtectedBlock.java @@ -1,7 +1,6 @@ package com.earth2me.essentials.protect.data; import java.util.List; -import java.util.Set; import org.bukkit.block.Block; public interface IProtectedBlock { diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/data/OwnedBlock.java b/EssentialsProtect/src/com/earth2me/essentials/protect/data/OwnedBlock.java index b9b036798..dea124b58 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/data/OwnedBlock.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/data/OwnedBlock.java @@ -1,9 +1,18 @@ package com.earth2me.essentials.protect.data; public class OwnedBlock { - int x; - int y; - int z; - String world; - String playerName; + final int x; + final int y; + final int z; + final String world; + final String playerName; + + public OwnedBlock(int x, int y, int z, String world, String playerName) + { + this.x = x; + this.y = y; + this.z = z; + this.world = world; + this.playerName = playerName; + } } diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockJDBC.java b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockJDBC.java index 567c7e23b..1d09f95c4 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockJDBC.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockJDBC.java @@ -12,285 +12,411 @@ 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 ComboPooledDataSource cpds; + +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 { + + 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 { + + public ProtectedBlockJDBC(String driver, String url, String username, String password) throws PropertyVetoException + { cpds = new ComboPooledDataSource(); cpds.setDriverClass(driver); cpds.setJdbcUrl(url); - if (username != null) { + if (username != null) + { cpds.setUser(username); cpds.setPassword(password); } cpds.setMaxStatements(20); createAndConvertTable(); } - - - private void createAndConvertTable() { + + private void createAndConvertTable() + { Connection conn = null; PreparedStatement ps = null; - try { + 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 { + } + 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); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); } } - if (conn != null) { - try { + if (conn != null) + { + try + { conn.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); } } } } - - public void clearProtections() { + + public void clearProtections() + { Connection conn = null; PreparedStatement ps = null; - try { + try + { conn = cpds.getConnection(); ps = getStatementDeleteAll(conn); ps.executeUpdate(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); - } finally { - if (ps != null) { - try { + } + 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); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); } } - if (conn != null) { - try { + if (conn != null) + { + try + { conn.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); } } } } - - public void importProtections(List blocks) { - for (OwnedBlock ownedBlock : blocks) { - if (ownedBlock.playerName == null) { + + public void importProtections(List blocks) + { + for (OwnedBlock ownedBlock : blocks) + { + if (ownedBlock.playerName == null) + { continue; } protectBlock(ownedBlock.world, ownedBlock.x, ownedBlock.y, ownedBlock.z, ownedBlock.playerName); } } - - public List exportProtections() { + + public List exportProtections() + { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; List blocks = new ArrayList(); - try { + try + { conn = cpds.getConnection(); ps = getStatementAllBlocks(conn); rs = ps.executeQuery(); - while (rs.next()) { - OwnedBlock ob = new OwnedBlock(); - ob.world = rs.getString(1); - ob.x = rs.getInt(2); - ob.y = rs.getInt(3); - ob.z = rs.getInt(4); - ob.playerName = rs.getString(5); + 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); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); return blocks; - } finally { - if (rs != null) { - try { + } + finally + { + if (rs != null) + { + try + { rs.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); } } - if (ps != null) { - try { + if (ps != null) + { + try + { ps.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); } } - if (conn != null) { - try { + if (conn != null) + { + try + { conn.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); } } } } - - public void protectBlock(Block block, String playerName) { + + 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) { + + private void protectBlock(String world, int x, int y, int z, String playerName) + { Connection conn = null; PreparedStatement ps = null; - try { + 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 { + } + 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); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); } } - if (conn != null) { - try { + if (conn != null) + { + try + { conn.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); } } } } - - public boolean isProtected(Block block, String playerName) { + + public boolean isProtected(Block block, String playerName) + { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; - try { + try + { conn = cpds.getConnection(); ps = getStatementPlayerCountByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), playerName); rs = ps.executeQuery(); rs.next(); return rs.getInt(1) > 0 && rs.getInt(2) == 0; - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); return true; - } finally { - if (rs != null) { - try { + } + finally + { + if (rs != null) + { + try + { rs.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); } } - if (ps != null) { - try { + if (ps != null) + { + try + { ps.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); } } - if (conn != null) { - try { + if (conn != null) + { + try + { conn.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); } } } } - - public List getOwners(Block block) { + + public List getOwners(Block block) + { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; List owners = new ArrayList(); - try { + try + { conn = cpds.getConnection(); ps = getStatementPlayersByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ()); rs = ps.executeQuery(); - while (rs.next()) { + while (rs.next()) + { owners.add(rs.getString(1)); } return owners; - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); return owners; - } finally { - if (rs != null) { - try { + } + finally + { + if (rs != null) + { + try + { rs.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); } } - if (ps != null) { - try { + if (ps != null) + { + try + { ps.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); } } - if (conn != null) { - try { + if (conn != null) + { + try + { conn.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); } } } } - public int unprotectBlock(Block block) { + public int unprotectBlock(Block block) + { Connection conn = null; PreparedStatement ps = null; - try { + 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); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); return 0; - } finally { - if (ps != null) { - try { + } + finally + { + if (ps != null) + { + try + { ps.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); } } - if (conn != null) { - try { + if (conn != null) + { + try + { conn.close(); - } catch (SQLException ex) { - logger.log(Level.SEVERE, null, ex); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); } } } } - } 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 worlds = new ArrayList(); - List playerNames = new ArrayList(); - IProtectedBlock storage; - Plugin plugin; +public class ProtectedBlockMemory implements IProtectedBlock +{ + private final transient List worlds = new ArrayList(); + private final transient List playerNames = new ArrayList(); + 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 playerIds; + static class ProtectedBy + { + private transient int playerId = -1; + private transient Set 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(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 getPlayers(List playerNames) { - if (playerIds == null) { - List list = new ArrayList(2); + public List getPlayers(final List playerNames) + { + final List list = new ArrayList(2); + if (playerIds == null) + { list.add(playerNames.get(playerId)); - return list; } - List list = new ArrayList(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 blocks = new HashMap(); + private final transient Map blocks = new HashMap(); - 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 blocks) { - for (OwnedBlock ownedBlock : blocks) { - ProtectedLocation pl = new ProtectedLocation(ownedBlock, getWorldId(ownedBlock.world)); - if (ownedBlock.playerName == null) { + public final void importProtections(final List 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 exportProtections() { - List blockList = new ArrayList(blocks.size()); - for (Entry 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 exportProtections() + { + final List blockList = new ArrayList(blocks.size()); + for (Entry 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 getOwners(Block block) { + public List 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); } diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMySQL.java b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMySQL.java index c246aca91..8e50ce248 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMySQL.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMySQL.java @@ -5,6 +5,8 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.logging.Level; +import java.util.logging.Logger; public class ProtectedBlockMySQL extends ProtectedBlockJDBC { @@ -50,10 +52,24 @@ public class ProtectedBlockMySQL extends ProtectedBlockJDBC { } } finally { if (testRS != null) { - testRS.close(); + try + { + testRS.close(); + } + catch (SQLException ex) + { + Logger.getLogger(ProtectedBlockMySQL.class.getName()).log(Level.SEVERE, null, ex); + } } if (testPS != null) { - testPS.close(); + try + { + testPS.close(); + } + catch (SQLException ex) + { + Logger.getLogger(ProtectedBlockMySQL.class.getName()).log(Level.SEVERE, null, ex); + } } } } -- cgit v1.2.3