git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1037 e251c2fe-e539-e718-e476-b85c1f46cddb
This commit is contained in:
ementalo 2011-03-30 14:31:54 +00:00
parent 243ff48778
commit 2b66459bce
6 changed files with 0 additions and 697 deletions

View File

@ -1,15 +0,0 @@
package com.earth2me.essentials.protect.data;
import java.util.List;
import java.util.Set;
import org.bukkit.block.Block;
public interface IProtectedBlock {
public void clearProtections();
public void importProtections(List<OwnedBlock> blocks);
public List<OwnedBlock> exportProtections();
public void protectBlock(Block block, String playerName);
public boolean isProtected(Block block, String playerName);
public List<String> getOwners(Block block);
public int unprotectBlock(Block block);
}

View File

@ -1,9 +0,0 @@
package com.earth2me.essentials.protect.data;
public class OwnedBlock {
int x;
int y;
int z;
String world;
String playerName;
}

View File

@ -1,292 +0,0 @@
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 ComboPooledDataSource cpds;
protected abstract PreparedStatement getStatementCreateTable(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();
} 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();
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);
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();
rs.next();
return 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);
}
}
}
}
}

View File

@ -1,205 +0,0 @@
package com.earth2me.essentials.protect.data;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
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;
class ProtectedLocation {
int x;
int y;
int z;
int w;
private ProtectedLocation(Block block, int worldId) {
this.x = block.getX();
this.y = block.getY();
this.z = block.getZ();
this.w = worldId;
}
private ProtectedLocation(OwnedBlock ownedBlock, int worldId) {
this.x = ownedBlock.x;
this.y = ownedBlock.y;
this.z = ownedBlock.z;
this.w = worldId;
}
@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;
}
return false;
}
@Override
public int hashCode() {
return x ^ y ^ z ^ w;
}
}
class ProtectedBy {
private int playerId = -1;
private Set<Integer> playerIds;
private ProtectedBy() {
}
private void add(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);
}
}
private boolean contains(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);
list.add(playerNames.get(playerId));
return list;
}
List<String> list = new ArrayList<String>(playerIds.size());
for (Integer integer : playerIds) {
list.add(playerNames.get(integer));
}
return list;
}
private int size() {
if (playerIds == null) {
return 1;
}
return playerIds.size();
}
}
HashMap<ProtectedLocation, ProtectedBy> blocks = new HashMap<ProtectedLocation, ProtectedBy>();
public ProtectedBlockMemory(IProtectedBlock storage) {
this.storage = storage;
importProtections(storage.exportProtections());
}
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) {
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;
blockList.add(ob);
}
}
return blockList;
}
public void protectBlock(final Block block, final String playerName) {
ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld()));
protectBlock(pl, playerName);
plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() {
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);
}
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) {
ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld()));
ProtectedBy pb = blocks.get(pl);
return pb.getPlayers(playerNames);
}
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() {
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;
}
}

View File

@ -1,89 +0,0 @@
package com.earth2me.essentials.protect.data;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class ProtectedBlockMySQL extends ProtectedBlockJDBC {
public ProtectedBlockMySQL(String url, String username, String password) throws PropertyVetoException {
super("com.mysql.jdbc.Driver", url, username, password);
}
private static final String QueryCreateTable =
"CREATE TABLE IF NOT EXISTS `EssentialsProtectedBlocks` ("
+ "`worldName` varchar(150) NOT NULL,"
+ "`x` int(11) NOT NULL, `y` int(11) NOT NULL, `z` int(11) NOT NULL,"
+ "`playerName` varchar(150) NOT NULL,"
+ ") ENGINE=MyISAM DEFAULT CHARSET=utf8";
@Override
protected PreparedStatement getStatementCreateTable(Connection conn) throws SQLException {
return conn.prepareStatement(QueryCreateTable);
}
private static final String QueryDeleteAll = "DELETE FROM EssentialsProtectedBlocks;";
@Override
protected PreparedStatement getStatementDeleteAll(Connection conn) throws SQLException {
return conn.prepareStatement(QueryDeleteAll);
}
private static final String QueryInsert =
"INSERT INTO EssentialsProtectedBlocks (worldName, x, y, z, playerName) VALUES (?, ?, ?, ?, ?);";
@Override
protected PreparedStatement getStatementInsert(Connection conn, String world, int x, int y, int z, String playerName) throws SQLException {
PreparedStatement ps = conn.prepareStatement(QueryInsert);
ps.setString(1, world);
ps.setInt(2, x);
ps.setInt(3, y);
ps.setInt(4, z);
ps.setString(5, playerName);
return ps;
}
private static final String QueryCountByPlayer =
"SELECT COUNT(playerName), SUM(playerName = ?) FROM EssentialsProtectedBlocks "
+ "WHERE worldName = ? AND x = ? AND y = ? AND z = ? GROUP BY x;";
@Override
protected PreparedStatement getStatementPlayerCountByLocation(Connection conn, String world, int x, int y, int z, String playerName) throws SQLException {
PreparedStatement ps = conn.prepareStatement(QueryCountByPlayer);
ps.setString(1, playerName);
ps.setString(2, world);
ps.setInt(3, x);
ps.setInt(4, y);
ps.setInt(5, z);
return ps;
}
private static final String QueryPlayersByLocation =
"SELECT playerName FROM EssentialsProtectedBlocks WHERE worldname = ? AND x = ? AND y = ? AND z = ?;";
@Override
protected PreparedStatement getStatementPlayersByLocation(Connection conn, String world, int x, int y, int z) throws SQLException {
PreparedStatement ps = conn.prepareStatement(QueryPlayersByLocation);
ps.setString(1, world);
ps.setInt(2, x);
ps.setInt(3, y);
ps.setInt(4, z);
return ps;
}
private static final String QueryDeleteByLocation =
"DELETE FROM EssentialsProtectedBlocks WHERE worldName = ? AND x = ? AND y = ? AND z = ?;";
@Override
protected PreparedStatement getStatementDeleteByLocation(Connection conn, String world, int x, int y, int z) throws SQLException {
PreparedStatement ps = conn.prepareStatement(QueryDeleteByLocation);
ps.setString(1, world);
ps.setInt(2, x);
ps.setInt(3, y);
ps.setInt(4, z);
return ps;
}
private static final String QueryAllBlocks =
"SELECT worldName, x, y, z, playerName FROM EssentialsProtectedBlocks;";
@Override
protected PreparedStatement getStatementAllBlocks(Connection conn) throws SQLException {
return conn.prepareStatement(QueryAllBlocks);
}
}

View File

@ -1,87 +0,0 @@
package com.earth2me.essentials.protect.data;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class ProtectedBlockSQLite extends ProtectedBlockJDBC {
public ProtectedBlockSQLite(String url) throws PropertyVetoException {
super("org.sqlite.JDBC", url);
}
private static final String QueryCreateTable =
"CREATE TABLE IF NOT EXISTS EssentialsProtect ("
+ "worldName TEXT ,playerName TEXT, "
+ "x NUMERIC, y NUMERIC, z NUMERIC)";
@Override
protected PreparedStatement getStatementCreateTable(Connection conn) throws SQLException {
return conn.prepareStatement(QueryCreateTable);
}
private static final String QueryDeleteAll = "DELETE FROM EssentialsProtectedBlocks;";
@Override
protected PreparedStatement getStatementDeleteAll(Connection conn) throws SQLException {
return conn.prepareStatement(QueryDeleteAll);
}
private static final String QueryInsert =
"INSERT INTO EssentialsProtectedBlocks (worldName, x, y, z, playerName) VALUES (?, ?, ?, ?, ?);";
@Override
protected PreparedStatement getStatementInsert(Connection conn, String world, int x, int y, int z, String playerName) throws SQLException {
PreparedStatement ps = conn.prepareStatement(QueryInsert);
ps.setString(1, world);
ps.setInt(2, x);
ps.setInt(3, y);
ps.setInt(4, z);
ps.setString(5, playerName);
return ps;
}
private static final String QueryPlayerCountByLocation =
"SELECT COUNT(playerName), SUM(playerName = ?) FROM EssentialsProtectedBlocks "
+ "WHERE worldName = ? AND x = ? AND y = ? AND z = ? GROUP BY x;";
@Override
protected PreparedStatement getStatementPlayerCountByLocation(Connection conn, String world, int x, int y, int z, String playerName) throws SQLException {
PreparedStatement ps = conn.prepareStatement(QueryPlayerCountByLocation);
ps.setString(1, playerName);
ps.setString(2, world);
ps.setInt(3, x);
ps.setInt(4, y);
ps.setInt(5, z);
return ps;
}
private static final String QueryPlayersByLocation =
"SELECT playerName FROM EssentialsProtectedBlocks WHERE worldname = ? AND x = ? AND y = ? AND z = ?;";
@Override
protected PreparedStatement getStatementPlayersByLocation(Connection conn, String world, int x, int y, int z) throws SQLException {
PreparedStatement ps = conn.prepareStatement(QueryPlayersByLocation);
ps.setString(1, world);
ps.setInt(2, x);
ps.setInt(3, y);
ps.setInt(4, z);
return ps;
}
private static final String QueryDeleteByLocation =
"DELETE FROM EssentialsProtectedBlocks WHERE worldName = ? AND x = ? AND y = ? AND z = ?;";
@Override
protected PreparedStatement getStatementDeleteByLocation(Connection conn, String world, int x, int y, int z) throws SQLException {
PreparedStatement ps = conn.prepareStatement(QueryDeleteByLocation);
ps.setString(1, world);
ps.setInt(2, x);
ps.setInt(3, y);
ps.setInt(4, z);
return ps;
}
private static final String QueryAllBlocks =
"SELECT worldName, x, y, z, playerName FROM EssentialsProtectedBlocks;";
@Override
protected PreparedStatement getStatementAllBlocks(Connection conn) throws SQLException {
return conn.prepareStatement(QueryAllBlocks);
}
}