diff options
Diffstat (limited to 'EssentialsProtect')
11 files changed, 859 insertions, 132 deletions
diff --git a/EssentialsProtect/nbproject/private/private.properties b/EssentialsProtect/nbproject/private/private.properties new file mode 100644 index 000000000..94183418a --- /dev/null +++ b/EssentialsProtect/nbproject/private/private.properties @@ -0,0 +1 @@ +user.properties.file=C:\\Users\\Paul\\.netbeans\\7.0beta2\\build.properties
diff --git a/EssentialsProtect/nbproject/project.properties b/EssentialsProtect/nbproject/project.properties index dcce6d152..3f2895ed5 100644 --- a/EssentialsProtect/nbproject/project.properties +++ b/EssentialsProtect/nbproject/project.properties @@ -26,6 +26,7 @@ dist.jar=${dist.dir}/original-EssentialsProtect.jar dist.javadoc.dir=${dist.dir}/javadoc
endorsed.classpath=
excludes=
+file.reference.c3p0-0.9.1.2.jar=..\\lib\\c3p0-0.9.1.2.jar
file.reference.craftbukkit-0.0.1-SNAPSHOT.jar=..\\lib\\craftbukkit-0.0.1-SNAPSHOT.jar
file.reference.mysql.jar=..\\lib\\mysql.jar
file.reference.sqlite.jar=..\\lib\\sqlite.jar
@@ -37,7 +38,8 @@ javac.classpath=\ ${reference.Essentials.jar}:\
${file.reference.mysql.jar}:\
${file.reference.sqlite.jar}:\
- ${file.reference.craftbukkit-0.0.1-SNAPSHOT.jar}
+ ${file.reference.craftbukkit-0.0.1-SNAPSHOT.jar}:\
+ ${file.reference.c3p0-0.9.1.2.jar}
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java index a2c577789..d6869519b 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java @@ -1,7 +1,13 @@ package com.earth2me.essentials.protect; import com.earth2me.essentials.Essentials; +import com.earth2me.essentials.IConf; import com.earth2me.essentials.User; +import com.earth2me.essentials.protect.data.IProtectedBlock; +import com.earth2me.essentials.protect.data.ProtectedBlockMemory; +import com.earth2me.essentials.protect.data.ProtectedBlockMySQL; +import com.earth2me.essentials.protect.data.ProtectedBlockSQLite; +import java.beans.PropertyVetoException; import java.util.ArrayList; import java.util.HashMap; import java.util.logging.Level; @@ -15,7 +21,7 @@ import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; -public class EssentialsProtect extends JavaPlugin +public class EssentialsProtect extends JavaPlugin implements IConf { private EssentialsProtectBlockListener blockListener = null; private EssentialsProtectPlayerListener playerListener = null; @@ -33,6 +39,8 @@ public class EssentialsProtect extends JavaPlugin public static ArrayList onUseAlert = null; public static ArrayList onBreakAlert = null; + private IProtectedBlock storage = null; + private static EssentialsProtect instance = null; public EssentialsProtect() { @@ -42,27 +50,27 @@ public class EssentialsProtect extends JavaPlugin { PluginManager pm = this.getServer().getPluginManager(); Essentials ess = (Essentials)pm.getPlugin("Essentials"); - if (!ess.isEnabled()) + if (!ess.isEnabled()) { pm.enablePlugin(ess); + } + + instance = this; + reloadConfig(); playerListener = new EssentialsProtectPlayerListener(this); blockListener = new EssentialsProtectBlockListener(this); entityListener = new EssentialsProtectEntityListener(this); - pm.registerEvent(Type.PLAYER_ITEM, playerListener, Priority.Low, this); - // Why is this commented? - //pm.registerEvent(Type.BLOCK_DAMAGED, blockListener, Priority.High, this); - pm.registerEvent(Type.BLOCK_RIGHTCLICKED, blockListener, Priority.Low, this); - pm.registerEvent(Type.BLOCK_PLACED, blockListener, Priority.Highest, this); - pm.registerEvent(Type.BLOCK_INTERACT, blockListener, Priority.Highest, this); - pm.registerEvent(Type.BLOCK_FLOW, blockListener, Priority.Highest, this); + pm.registerEvent(Type.PLAYER_PICKUP_ITEM, playerListener, Priority.Low, this); + pm.registerEvent(Type.PLAYER_INTERACT, playerListener, Priority.Low, this); + pm.registerEvent(Type.BLOCK_PLACE, blockListener, Priority.Highest, this); + pm.registerEvent(Type.BLOCK_FROMTO, blockListener, Priority.Highest, this); pm.registerEvent(Type.BLOCK_IGNITE, blockListener, Priority.Highest, this); pm.registerEvent(Type.BLOCK_BURN, blockListener, Priority.Highest, this); pm.registerEvent(Type.ENTITY_EXPLODE, entityListener, Priority.Highest, this); - pm.registerEvent(Type.ENTITY_DAMAGED, entityListener, Priority.Highest, this); + pm.registerEvent(Type.ENTITY_DAMAGE, entityListener, Priority.Highest, this); pm.registerEvent(Type.BLOCK_BREAK, blockListener, Priority.Highest, this); pm.registerEvent(Type.CREATURE_SPAWN, entityListener, Priority.Highest, this); - loadSettings(); if (!this.getDescription().getVersion().equals(Essentials.getStatic().getDescription().getVersion())) { logger.log(Level.WARNING, "Version mismatch! Please update all Essentials jars to the same version."); } @@ -94,8 +102,23 @@ public class EssentialsProtect extends JavaPlugin onBreakAlert = null; } - public static void loadSettings() + public void alert(User user, String item, String type) { + Location loc = user.getLocation(); + for (Player p : this.getServer().getOnlinePlayers()) + { + User alertUser = User.get(p); + if (alertUser.isAuthorized("essentials.protect.alerts")) + alertUser.sendMessage(ChatColor.DARK_AQUA + "[" + user.getName() + "] " + ChatColor.WHITE + type + ChatColor.GOLD + item + " at: " + formatCoords(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); + } + } + + public static String formatCoords(int x, int y, int z) + { + return x + "," + y + "," + z; + } + + public void reloadConfig() { dataSettings = Essentials.getSettings().getEpDBSettings(); genSettings = Essentials.getSettings().getEpSettings(); guardSettings = Essentials.getSettings().getEpGuardSettings(); @@ -106,17 +129,26 @@ public class EssentialsProtect extends JavaPlugin onUseAlert = Essentials.getSettings().getEpAlertOnUse(); onBreakAlert = Essentials.getSettings().getEpAlertOnBreak(); playerSettings = Essentials.getSettings().getEpPlayerSettings(); - EssentialsProtectData.createSqlTable(); - } - - public void alert(User user, String item, String type) - { - Location loc = user.getLocation(); - for (Player p : this.getServer().getOnlinePlayers()) - { - User alertUser = User.get(p); - if (alertUser.isAuthorized("essentials.protect.alerts")) - alertUser.sendMessage(ChatColor.DARK_AQUA + "[" + user.getName() + "] " + ChatColor.WHITE + type + ChatColor.GOLD + item + " at: " + EssentialsProtectData.formatCoords(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); + + if (dataSettings.get("protect.datatype").equals("mysql")) { + try { + storage = new ProtectedBlockMySQL(dataSettings.get("protect.mysqlDb"), dataSettings.get("protect.username"), dataSettings.get("protect.password")); + } catch (PropertyVetoException ex) { + logger.log(Level.SEVERE, null, ex); + } + } else { + try { + storage = new ProtectedBlockSQLite("jdbc:sqlite:plugins/Essentials/EssentialsProtect.db"); + } catch (PropertyVetoException ex) { + logger.log(Level.SEVERE, null, ex); + } + } + if (genSettings.get("protect.memstore")) { + storage = new ProtectedBlockMemory(storage); } } + + public static IProtectedBlock getStorage() { + return EssentialsProtect.instance.storage; + } } diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java index 43b508a58..2028edf42 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java @@ -2,28 +2,24 @@ package com.earth2me.essentials.protect; import com.earth2me.essentials.Essentials; import com.earth2me.essentials.User; -import org.bukkit.ChatColor; +import java.util.ArrayList; +import java.util.List; import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; import org.bukkit.entity.Player; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockBurnEvent; import org.bukkit.event.block.BlockFromToEvent; import org.bukkit.event.block.BlockIgniteEvent; -import org.bukkit.event.block.BlockInteractEvent; import org.bukkit.event.block.BlockListener; import org.bukkit.event.block.BlockPlaceEvent; -import org.bukkit.event.block.BlockRightClickEvent; import org.bukkit.inventory.ItemStack; public class EssentialsProtectBlockListener extends BlockListener { private EssentialsProtect parent; - private int railBlockX; - private int railBlockY; - private int railBlockZ; - private EssentialsProtectData spData; public EssentialsProtectBlockListener(EssentialsProtect parent) { @@ -31,34 +27,10 @@ public class EssentialsProtectBlockListener extends BlockListener this.parent = parent; } - private void initialize() - { - if (spData != null) return; - spData = new EssentialsProtectData(); - } - - @Override - public void onBlockRightClick(BlockRightClickEvent event) - { - initialize(); - User user = User.get(event.getPlayer()); - if (user.isAuthorized("essentials.protect.admin")) - { - String ownerName = spData.getBlockOwner(user.getWorld().getName(), user.getName(), - event.getBlock()); - if (ownerName != null) - { - user.sendMessage(ChatColor.GOLD + "[EssentialsProtect] Protection owner: " - + ownerName); - } - } - } - @Override public void onBlockPlace(BlockPlaceEvent event) { if (event.isCancelled()) return; - initialize(); ItemStack item = event.getItemInHand(); User user = User.get(event.getPlayer()); @@ -68,7 +40,6 @@ public class EssentialsProtectBlockListener extends BlockListener return; } - Block blockPlaced = event.getBlockAgainst(); int id = event.getBlockPlaced().getTypeId(); if (EssentialsProtect.checkProtectionItems(EssentialsProtect.blackListPlace, id) && !user.isAuthorized("essentials.protect.exemptplacement")) @@ -82,33 +53,48 @@ public class EssentialsProtectBlockListener extends BlockListener parent.alert(user, item.getType().toString(), "placed: "); } - if (spData.isBlockAboveProtectedRail(blockPlaced)) - { + Block blockPlaced = event.getBlockPlaced(); + Block below = blockPlaced.getFace(BlockFace.DOWN); + if (below.getType() == Material.RAILS) { if (EssentialsProtect.genSettings.get("protect.protect.prevent.block-on-rail")) { - event.setCancelled(true); - return; + if (EssentialsProtect.getStorage().isProtected(below, user.getName())) { + event.setCancelled(true); + return; + } } } - if (item.getTypeId() == 66) - { + List<Block> protect = new ArrayList<Block>(); + if (blockPlaced.getType() == Material.RAILS) { if (EssentialsProtect.genSettings.get("protect.protect.rails")) { if (user.isAuthorized("essentials.protect")) { - railBlockX = blockPlaced.getX(); - railBlockY = blockPlaced.getY(); - railBlockZ = blockPlaced.getZ(); - - spData.insertProtectionIntoDb(user.getWorld().getName(), user.getName(), railBlockX, railBlockY + 1, railBlockZ); + protect.add(blockPlaced); + if (EssentialsProtect.genSettings.get("protect.protect.block-below")) + { + protect.add(blockPlaced.getFace(BlockFace.DOWN)); + } + } + } + } + if (blockPlaced.getType() == Material.SIGN_POST || blockPlaced.getType() == Material.WALL_SIGN) { + if (EssentialsProtect.genSettings.get("protect.protect.signs")) + { + if (user.isAuthorized("essentials.protect")) + { + protect.add(blockPlaced); if (EssentialsProtect.genSettings.get("protect.protect.block-below")) { - spData.insertProtectionIntoDb(user.getWorld().getName(), user.getName(), railBlockX, railBlockY, railBlockZ); + protect.add(event.getBlockAgainst()); } } } } + for (Block block : protect) { + EssentialsProtect.getStorage().protectBlock(block, user.getName()); + } } @Override @@ -145,7 +131,7 @@ public class EssentialsProtectBlockListener extends BlockListener } @Override - public void onBlockFlow(BlockFromToEvent event) + public void onBlockFromTo(BlockFromToEvent event) { if (event.isCancelled()) return; Block block = event.getBlock(); @@ -180,22 +166,6 @@ public class EssentialsProtectBlockListener extends BlockListener } @Override - public void onBlockInteract(BlockInteractEvent event) - { - if (event.isCancelled()) return; - if (!event.isPlayer()) return; - - User user = User.get((Player)event.getEntity()); - - if (EssentialsProtect.playerSettings.get("protect.disable.build") && !user.canBuild()) - { - event.setCancelled(true); - return; - } - - } - - @Override public void onBlockBurn(BlockBurnEvent event) { Block block = event.getBlock(); @@ -220,7 +190,6 @@ public class EssentialsProtectBlockListener extends BlockListener public void onBlockBreak(BlockBreakEvent event) { if (event.isCancelled()) return; - initialize(); User user = User.get(event.getPlayer()); Block block = event.getBlock(); if (EssentialsProtect.playerSettings.get("protect.disable.build") && !user.canBuild()) @@ -244,28 +213,57 @@ public class EssentialsProtectBlockListener extends BlockListener { if (block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST || block.getType() == Material.RAILS) { - spData.removeProtectionFromDB(block, true); + EssentialsProtect.getStorage().unprotectBlock(block); + if (block.getType() == Material.RAILS || block.getType() == Material.SIGN_POST) { + Block below = block.getFace(BlockFace.DOWN); + EssentialsProtect.getStorage().unprotectBlock(below); + } else { + BlockFace[] faces = new BlockFace[] { + BlockFace.NORTH, + BlockFace.EAST, + BlockFace.SOUTH, + BlockFace.WEST + }; + for (BlockFace blockFace : faces) { + Block against = block.getFace(blockFace); + EssentialsProtect.getStorage().unprotectBlock(against); + } + } } else { - spData.removeProtectionFromDB(block); + EssentialsProtect.getStorage().unprotectBlock(block); } return; } else { - boolean canDestroy = spData.canDestroy(user.getWorld().getName(), user.getName(), block); - if (canDestroy) - { + + boolean isProtected = EssentialsProtect.getStorage().isProtected(block, user.getName()); + if (!isProtected) { if (block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST || block.getType() == Material.RAILS) - { - spData.removeProtectionFromDB(block, true); + { + EssentialsProtect.getStorage().unprotectBlock(block); + if (block.getType() == Material.RAILS || block.getType() == Material.SIGN_POST) { + Block below = block.getFace(BlockFace.DOWN); + EssentialsProtect.getStorage().unprotectBlock(below); + } else { + BlockFace[] faces = new BlockFace[] { + BlockFace.NORTH, + BlockFace.EAST, + BlockFace.SOUTH, + BlockFace.WEST + }; + for (BlockFace blockFace : faces) { + Block against = block.getFace(blockFace); + EssentialsProtect.getStorage().unprotectBlock(against); + } + } } else { - spData.removeProtectionFromDB(block); + EssentialsProtect.getStorage().unprotectBlock(block); } - return; } event.setCancelled(true); return; diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectPlayerListener.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectPlayerListener.java index 61419f69a..2497a733d 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectPlayerListener.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectPlayerListener.java @@ -2,39 +2,60 @@ package com.earth2me.essentials.protect; import com.earth2me.essentials.Essentials; import com.earth2me.essentials.User; -import org.bukkit.block.Block; -import org.bukkit.event.player.PlayerItemEvent; +import org.bukkit.ChatColor; +import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerListener; +import org.bukkit.event.player.PlayerPickupItemEvent; import org.bukkit.inventory.ItemStack; public class EssentialsProtectPlayerListener extends PlayerListener { private EssentialsProtect parent; - private int signBlockX; - private int signBlockY; - private int signBlockZ; - private EssentialsProtectData spData = null; - + public EssentialsProtectPlayerListener(EssentialsProtect parent) { Essentials.loadClasses(); this.parent = parent; } - public void initialize() + @Override + public void onPlayerInteract(PlayerInteractEvent event) { - if (spData != null) return; - spData = new EssentialsProtectData(); + if (event.isCancelled()) return; + User user = User.get(event.getPlayer()); + + if (EssentialsProtect.playerSettings.get("protect.disable.build") && !user.canBuild()) + { + event.setCancelled(true); + return; + } + + if (user.isAuthorized("essentials.protect.admin")) + { + StringBuilder sb = new StringBuilder(); + boolean first = true; + for (String owner : EssentialsProtect.getStorage().getOwners(event.getClickedBlock())) { + if (!first) { + sb.append(", "); + } + first = false; + sb.append(owner); + } + String ownerNames = sb.toString(); + if (ownerNames != null) + { + user.sendMessage(ChatColor.GOLD + "[EssentialsProtect] Protection owners: " + ownerNames); + } + } } @Override - public void onPlayerItem(PlayerItemEvent event) + public void onPlayerPickupItem(PlayerPickupItemEvent event) { if(event.isCancelled()) return; - ItemStack item = event.getItem(); + ItemStack item = event.getItem().getItemStack(); User user = User.get(event.getPlayer()); - Block blockPlaced = event.getBlockClicked(); if (EssentialsProtect.checkProtectionItems(EssentialsProtect.usageList, item.getTypeId()) && !user.isAuthorized("essentials.protect.exemptusage")) { event.setCancelled(true); @@ -45,29 +66,5 @@ public class EssentialsProtectPlayerListener extends PlayerListener { parent.alert(user, item.getType().toString(), "used: "); } - - if (item.getTypeId() == 323) - { - if (EssentialsProtect.genSettings.get("protect.protect.signs")) - { - if (user.isAuthorized("essentials.protect")) - { - - signBlockX = blockPlaced.getX(); - signBlockY = blockPlaced.getY(); - signBlockZ = blockPlaced.getZ(); - - initialize(); - spData.insertProtectionIntoDb(user.getWorld().getName(), user.getName(), signBlockX, - signBlockY + 1, signBlockZ); - - if (EssentialsProtect.genSettings.get("protect.protect.block-below")) - { - spData.insertProtectionIntoDb(user.getWorld().getName(), user.getName(), signBlockX, - signBlockY, signBlockZ); - } - } - } - } } } diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/data/IProtectedBlock.java b/EssentialsProtect/src/com/earth2me/essentials/protect/data/IProtectedBlock.java new file mode 100644 index 000000000..28b7e425b --- /dev/null +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/data/IProtectedBlock.java @@ -0,0 +1,15 @@ +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); +} diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/data/OwnedBlock.java b/EssentialsProtect/src/com/earth2me/essentials/protect/data/OwnedBlock.java new file mode 100644 index 000000000..b9b036798 --- /dev/null +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/data/OwnedBlock.java @@ -0,0 +1,9 @@ +package com.earth2me.essentials.protect.data; + +public class OwnedBlock { + int x; + int y; + int z; + String world; + String playerName; +} diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockJDBC.java b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockJDBC.java new file mode 100644 index 000000000..dec0231b9 --- /dev/null +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockJDBC.java @@ -0,0 +1,292 @@ +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); + } + } + } + } + +} diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java new file mode 100644 index 000000000..53dee66d2 --- /dev/null +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java @@ -0,0 +1,205 @@ +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; + } +} diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMySQL.java b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMySQL.java new file mode 100644 index 000000000..8824f4040 --- /dev/null +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMySQL.java @@ -0,0 +1,89 @@ +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); + } +} diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockSQLite.java b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockSQLite.java new file mode 100644 index 000000000..ff96a8952 --- /dev/null +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockSQLite.java @@ -0,0 +1,87 @@ +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); + } +} |