summaryrefslogtreecommitdiffstats
path: root/EssentialsProtect/src/com
diff options
context:
space:
mode:
Diffstat (limited to 'EssentialsProtect/src/com')
-rw-r--r--EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java122
-rw-r--r--EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java274
-rw-r--r--EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectData.java533
-rw-r--r--EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectEntityListener.java197
-rw-r--r--EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectPlayerListener.java73
-rw-r--r--EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectRegions.java5
-rw-r--r--EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectSqlProperties.java12
7 files changed, 1216 insertions, 0 deletions
diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java
new file mode 100644
index 000000000..a2c577789
--- /dev/null
+++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java
@@ -0,0 +1,122 @@
+package com.earth2me.essentials.protect;
+
+import com.earth2me.essentials.Essentials;
+import com.earth2me.essentials.User;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import org.bukkit.ChatColor;
+import org.bukkit.Location;
+import org.bukkit.entity.Player;
+import org.bukkit.event.Event.Priority;
+import org.bukkit.event.Event.Type;
+import org.bukkit.plugin.PluginManager;
+import org.bukkit.plugin.java.JavaPlugin;
+
+
+public class EssentialsProtect extends JavaPlugin
+{
+ private EssentialsProtectBlockListener blockListener = null;
+ private EssentialsProtectPlayerListener playerListener = null;
+ private EssentialsProtectEntityListener entityListener = null;
+ public static final String AUTHORS = Essentials.AUTHORS;
+ private static final Logger logger = Logger.getLogger("Minecraft");
+ public static HashMap<String, Boolean> genSettings = null;
+ public static HashMap<String, String> dataSettings = null;
+ public static HashMap<String, Boolean> guardSettings = null;
+ public static HashMap<String, Boolean> playerSettings = null;
+ public static ArrayList usageList = null;
+ public static ArrayList blackListPlace = null;
+ public static ArrayList breakBlackList = null;
+ public static ArrayList onPlaceAlert = null;
+ public static ArrayList onUseAlert = null;
+ public static ArrayList onBreakAlert = null;
+
+
+ public EssentialsProtect()
+ {
+ }
+
+ public void onEnable()
+ {
+ PluginManager pm = this.getServer().getPluginManager();
+ Essentials ess = (Essentials)pm.getPlugin("Essentials");
+ if (!ess.isEnabled())
+ pm.enablePlugin(ess);
+
+ 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.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.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.");
+ }
+ logger.info("Loaded " + this.getDescription().getName() + " build " + this.getDescription().getVersion() + " maintained by " + AUTHORS);
+ }
+
+ public static boolean checkProtectionItems(ArrayList itemList, int id)
+ {
+ return !itemList.isEmpty() && itemList.contains(String.valueOf(id));
+ }
+
+ @Override
+ public void onDisable()
+ {
+ genSettings.clear();
+ dataSettings.clear();
+
+ blockListener = null;
+ playerListener = null;
+ entityListener = null;
+ genSettings = null;
+ dataSettings = null;
+ guardSettings = null;
+ playerSettings = null;
+ usageList = null;
+ blackListPlace = null;
+ onPlaceAlert = null;
+ onUseAlert = null;
+ onBreakAlert = null;
+ }
+
+ public static void loadSettings()
+ {
+ dataSettings = Essentials.getSettings().getEpDBSettings();
+ genSettings = Essentials.getSettings().getEpSettings();
+ guardSettings = Essentials.getSettings().getEpGuardSettings();
+ usageList = Essentials.getSettings().epBlackListUsage();
+ blackListPlace = Essentials.getSettings().epBlackListPlacement();
+ breakBlackList = Essentials.getSettings().epBlockBreakingBlacklist();
+ onPlaceAlert = Essentials.getSettings().getEpAlertOnPlacement();
+ 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()));
+ }
+ }
+}
diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java
new file mode 100644
index 000000000..43b508a58
--- /dev/null
+++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java
@@ -0,0 +1,274 @@
+package com.earth2me.essentials.protect;
+
+import com.earth2me.essentials.Essentials;
+import com.earth2me.essentials.User;
+import org.bukkit.ChatColor;
+import org.bukkit.Material;
+import org.bukkit.block.Block;
+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)
+ {
+ Essentials.loadClasses();
+ 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());
+
+ if (EssentialsProtect.playerSettings.get("protect.disable.build") && !user.canBuild())
+ {
+ event.setCancelled(true);
+ return;
+ }
+
+ Block blockPlaced = event.getBlockAgainst();
+ int id = event.getBlockPlaced().getTypeId();
+
+ if (EssentialsProtect.checkProtectionItems(EssentialsProtect.blackListPlace, id) && !user.isAuthorized("essentials.protect.exemptplacement"))
+ {
+ event.setCancelled(true);
+ return;
+ }
+
+ if (!EssentialsProtect.onPlaceAlert.isEmpty() && EssentialsProtect.onPlaceAlert.contains(String.valueOf(item.getTypeId())))
+ {
+ parent.alert(user, item.getType().toString(), "placed: ");
+ }
+
+ if (spData.isBlockAboveProtectedRail(blockPlaced))
+ {
+ if (EssentialsProtect.genSettings.get("protect.protect.prevent.block-on-rail"))
+ {
+ event.setCancelled(true);
+ return;
+ }
+ }
+
+ if (item.getTypeId() == 66)
+ {
+ 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);
+ if (EssentialsProtect.genSettings.get("protect.protect.block-below"))
+ {
+ spData.insertProtectionIntoDb(user.getWorld().getName(), user.getName(), railBlockX, railBlockY, railBlockZ);
+ }
+ }
+ }
+ }
+ }
+
+ @Override
+ public void onBlockIgnite(BlockIgniteEvent event)
+ {
+ Block block = event.getBlock();
+ if (block.getType() == Material.RAILS && EssentialsProtect.genSettings.get("protect.protect.rails"))
+ {
+ event.setCancelled(true);
+ return;
+ }
+ if ((block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST) && EssentialsProtect.genSettings.get("protect.protect.signs"))
+ {
+ event.setCancelled(true);
+ return;
+ }
+ if ((event.getCause().equals(BlockIgniteEvent.IgniteCause.SPREAD)))
+ {
+ event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.fire-spread"));
+ return;
+ }
+
+ if (event.getCause().equals(BlockIgniteEvent.IgniteCause.FLINT_AND_STEEL))
+ {
+ event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.flint-fire"));
+ return;
+ }
+
+ if (event.getCause().equals(BlockIgniteEvent.IgniteCause.LAVA))
+ {
+ event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.lava-fire-spread"));
+ return;
+ }
+ }
+
+ @Override
+ public void onBlockFlow(BlockFromToEvent event)
+ {
+ if (event.isCancelled()) return;
+ Block block = event.getBlock();
+ Block toBlock = event.getToBlock();
+ if (toBlock.getType() == Material.RAILS && EssentialsProtect.genSettings.get("protect.protect.rails"))
+ {
+ event.setCancelled(true);
+ return;
+ }
+ if ((toBlock.getType() == Material.WALL_SIGN || toBlock.getType() == Material.SIGN_POST) && EssentialsProtect.genSettings.get("protect.protect.signs"))
+ {
+ event.setCancelled(true);
+ return;
+ }
+ if (block.getType() == Material.WATER || block.getType() == Material.STATIONARY_WATER)
+ {
+ event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.water-flow"));
+ return;
+ }
+
+ if (block.getType() == Material.LAVA || block.getType() == Material.STATIONARY_LAVA)
+ {
+ event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.lava-flow"));
+ return;
+ }
+
+ if (block.getType() == Material.AIR)
+ {
+ event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.water-bucket-flow"));
+ return;
+ }
+ }
+
+ @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();
+ if (block.getType() == Material.RAILS && EssentialsProtect.genSettings.get("protect.protect.rails"))
+ {
+ event.setCancelled(true);
+ return;
+ }
+ if ((block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST) && EssentialsProtect.genSettings.get("protect.protect.signs"))
+ {
+ event.setCancelled(true);
+ return;
+ }
+ if (EssentialsProtect.guardSettings.get("protect.prevent.fire-spread"))
+ {
+ event.setCancelled(true);
+ return;
+ }
+ }
+
+ @Override
+ 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())
+ {
+ event.setCancelled(true);
+ return;
+ }
+
+ if(EssentialsProtect.breakBlackList.contains(String.valueOf(block.getTypeId())) && !user.isAuthorized("essentials.protect.exemptbreak"))
+ {
+ event.setCancelled(true);
+ return;
+ }
+
+ if (!EssentialsProtect.onBreakAlert.isEmpty() && EssentialsProtect.onBreakAlert.contains(String.valueOf(block.getTypeId())))
+ {
+ parent.alert(user, block.getType().toString(), "broke: ");
+ }
+
+ if (user.isAuthorized("essentials.protect.admin"))
+ {
+ if (block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST || block.getType() == Material.RAILS)
+ {
+ spData.removeProtectionFromDB(block, true);
+ }
+ else
+ {
+ spData.removeProtectionFromDB(block);
+ }
+ return;
+ }
+ else
+ {
+ boolean canDestroy = spData.canDestroy(user.getWorld().getName(), user.getName(), block);
+ if (canDestroy)
+ {
+ if (block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST || block.getType() == Material.RAILS)
+ {
+ spData.removeProtectionFromDB(block, true);
+ }
+ else
+ {
+ spData.removeProtectionFromDB(block);
+ }
+ return;
+ }
+ event.setCancelled(true);
+ return;
+ }
+ }
+}
diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectData.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectData.java
new file mode 100644
index 000000000..76b36d810
--- /dev/null
+++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectData.java
@@ -0,0 +1,533 @@
+package com.earth2me.essentials.protect;
+
+import com.earth2me.essentials.Essentials;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import org.bukkit.block.Block;
+
+
+public class EssentialsProtectData
+{
+ private static final Logger logger = Logger.getLogger("Minecraft");
+ private static final String mysqlDriver = "com.mysql.jdbc.Driver";
+ private static String mysqlUsername;
+ private static String mysqlPassword;
+ private static String mysqlDatabase;
+ private static String dataType;
+ private static final String sqlite = "jdbc:sqlite:plugins/Essentials/EssentialsProtect.db";
+ private static final String mysqlTable;
+ private static final String sqliteTable;
+ private static final String insertQuery;
+ private static final String countByLocationQuery;
+ private static final String countByPlayerLocationQuery;
+ private static final String playerByLocationQuery;
+ private static final String deleteByLocationQuery;
+
+ static
+ {
+ mysqlTable = EssentialsProtectSqlProperties.EssentialsProtect;
+ sqliteTable = EssentialsProtectSqlProperties.EssentialsProtect_sqlite;
+ insertQuery = EssentialsProtectSqlProperties.Insert;
+ countByLocationQuery = EssentialsProtectSqlProperties.CountByLocation;
+ countByPlayerLocationQuery = EssentialsProtectSqlProperties.CountByPLayerLocation;
+ playerByLocationQuery = EssentialsProtectSqlProperties.PlayerByLocation;
+ deleteByLocationQuery = EssentialsProtectSqlProperties.DeleteByLocation;
+ mysqlUsername = EssentialsProtect.dataSettings.get("protect.username");
+ mysqlPassword = EssentialsProtect.dataSettings.get("protect.password");
+ mysqlDatabase = EssentialsProtect.dataSettings.get("protect.mysqlDb");
+ dataType = EssentialsProtect.dataSettings.get("protect.datatype");
+ }
+
+ public EssentialsProtectData()
+ {
+ }
+
+ public static String formatCoords(int x, int y, int z)
+ {
+ return x + "," + y + "," + z;
+ }
+
+ public void insertProtectionIntoDb(String worldname, String playerName, int x, int y, int z)
+ {
+ Connection conn = null;
+ PreparedStatement ps = null;
+ ResultSet rs = null;
+ try
+ {
+ if (dataType.contentEquals("mysql"))
+ {
+ Class.forName(mysqlDriver);
+ conn = DriverManager.getConnection(mysqlDatabase, mysqlUsername, mysqlPassword);
+ }
+ else
+ {
+ Class.forName("org.sqlite.JDBC");
+ conn = DriverManager.getConnection(sqlite);
+ }
+ ps = conn.prepareStatement(insertQuery);
+ ps.setString(1, worldname);
+ ps.setString(2, playerName);
+ ps.setInt(3, x);
+ ps.setInt(4, y);
+ ps.setInt(5, z);
+ ps.executeUpdate();
+
+ }
+ catch (SQLException ex)
+ {
+ logger.log(Level.SEVERE, "[EssentialsProtect] Unable to add protection into SQL", ex);
+ }
+ catch (ClassNotFoundException e)
+ {
+ // TODO Auto-generated catch block
+ logger.log(Level.SEVERE, "[EssentialsProtect] Class not found", e);
+ }
+ finally
+ {
+ try
+ {
+ if (ps != null)
+ {
+ ps.close();
+ }
+ if (rs != null)
+ {
+ rs.close();
+ }
+ if (conn != null)
+ {
+ conn.close();
+ }
+ }
+ catch (SQLException ex)
+ {
+ logger.log(Level.SEVERE, "[EssentialsProtect] Could not close connection to SQL", ex);
+ }
+ }
+ }
+
+ public boolean canDestroy(String worldName, String playerName, Block block)
+ {
+ int x = block.getX();
+ int y = block.getY();
+ int z = block.getZ();
+
+ int rowCount = 0;
+ Connection conn = null;
+ PreparedStatement ps = null;
+ ResultSet rs = null;
+ try
+ {
+ if (dataType.contentEquals("mysql"))
+ {
+ Class.forName(mysqlDriver);
+ conn = DriverManager.getConnection(mysqlDatabase, mysqlUsername, mysqlPassword);
+ }
+ else
+ {
+ Class.forName("org.sqlite.JDBC");
+ conn = DriverManager.getConnection(sqlite);
+ }
+ conn.setAutoCommit(false);
+ ps = conn.prepareStatement(countByLocationQuery);
+ ps.setString(1, worldName);
+ ps.setInt(2, x);
+ ps.setInt(3, y);
+ ps.setInt(4, z);
+ rs = ps.executeQuery();
+ rs.next();
+ rowCount = rs.getInt(1);
+ rs.close();
+ ps.close();
+
+ if (rowCount == 0)
+ {
+ return true;
+ }
+ else
+ {
+ ps = conn.prepareStatement(countByPlayerLocationQuery);
+ ps.setString(1, worldName);
+ ps.setString(2, playerName);
+ ps.setInt(3, x);
+ ps.setInt(4, y);
+ ps.setInt(5, z);
+ rs = ps.executeQuery();
+ rs.next();
+ rowCount = rs.getInt(1);
+
+ if (rowCount == 0)
+ {
+ return false;
+ }
+
+ }
+
+ }
+ catch (SQLException ex)
+ {
+ logger.log(Level.SEVERE, "[EssentialsProtect] Unable to query Protection", ex);
+ }
+ catch (Throwable e)
+ {
+ logger.log(Level.SEVERE, "[EssentialsProtect] Unable to query Protection", e);
+ }
+ finally
+ {
+ try
+ {
+ if (ps != null)
+ {
+ ps.close();
+ }
+ if (rs != null)
+ {
+ rs.close();
+ }
+ if (conn != null)
+ {
+ conn.close();
+ }
+ }
+ catch (SQLException ex)
+ {
+ logger.log(Level.SEVERE, "[EssentialsProtection] Could not close connection to SQL", ex);
+ }
+ }
+ return true;
+ }
+
+ @SuppressWarnings("CallToThreadDumpStack")
+ public static void createSqlTable()
+ {
+ Connection conn = null;
+ Statement st = null;
+
+ try
+ {
+ if (dataType.contentEquals("mysql"))
+ {
+ Class.forName(mysqlDriver);
+ conn = DriverManager.getConnection(mysqlDatabase, mysqlUsername, mysqlPassword);
+ }
+ else
+ {
+ Class.forName("org.sqlite.JDBC");
+ conn = DriverManager.getConnection(sqlite);
+ }
+
+ st = conn.createStatement();
+ st.executeUpdate(dataType.contentEquals("mysql") ? mysqlTable : sqliteTable);
+ }
+ catch (SQLException s)
+ {
+ logger.log(Level.SEVERE, "[EssentialsProtect] Could not create table for " + dataType, s);
+
+ }
+ catch (ClassNotFoundException ex)
+ {
+ logger.log(Level.SEVERE, "[EssentialsProtect] Could not find driver for " + dataType, ex);
+
+ }
+ catch (Throwable e)
+ {
+ logger.log(Level.SEVERE, "[EssentialsProtect] Unexpected error occured whilst creating table ", e);
+ }
+ finally
+ {
+ try
+ {
+ if (conn != null && !conn.isClosed())
+ {
+ try
+ {
+ conn.close();
+ }
+ catch (SQLException e)
+ {
+ logger.log(Level.SEVERE, "[EssentialsProtect] Unexpected error occured whilst closing the connection", e);
+ }
+ }
+ }
+ catch (SQLException e)
+ {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ }
+
+ public String getBlockOwner(String worldName, String playerName, Block block)
+ {
+ String returnPlayerName = null;
+ int x = block.getX();
+ int y = block.getY();
+ int z = block.getZ();
+
+ Connection conn = null;
+ PreparedStatement ps = null;
+ ResultSet rs = null;
+ try
+ {
+ if (dataType.contentEquals("mysql"))
+ {
+ Class.forName(mysqlDriver);
+ conn = DriverManager.getConnection(mysqlDatabase, mysqlUsername, mysqlPassword);
+ }
+ else
+ {
+ Class.forName("org.sqlite.JDBC");
+ conn = DriverManager.getConnection(sqlite);
+ }
+ conn.setAutoCommit(false);
+ ps = conn.prepareStatement(playerByLocationQuery);
+
+ ps.setString(1, worldName);
+ ps.setInt(2, x);
+ ps.setInt(3, y);
+ ps.setInt(4, z);
+ rs = ps.executeQuery();
+ while (rs.next())
+ {
+ returnPlayerName = rs.getString("playerName");
+ }
+ rs.close();
+ ps.close();
+
+ }
+ catch (SQLException ex)
+ {
+ logger.log(Level.SEVERE, "[EssentialsProtect] Unable to query EssentialsProtection", ex);
+ }
+ catch (Throwable e)
+ {
+ logger.log(Level.SEVERE, "[EssentialsProtect] Unable to query EssentialsProtection", e);
+ }
+ finally
+ {
+ try
+ {
+ if (ps != null)
+ {
+ ps.close();
+ }
+ if (rs != null)
+ {
+ rs.close();
+ }
+ if (conn != null)
+ {
+ conn.close();
+ }
+ }
+ catch (SQLException ex)
+ {
+ logger.log(Level.SEVERE, "[EssentialsProtection] Could not close connection to SQL", ex);
+ }
+
+ }
+ return returnPlayerName;
+ }
+
+
+ public void removeProtectionFromDB(Block block)
+ {
+ try
+ {
+ Connection conn = null;
+ if (dataType.contentEquals("mysql"))
+ {
+ Class.forName(mysqlDriver);
+ conn = DriverManager.getConnection(mysqlDatabase, mysqlUsername,
+ mysqlPassword);
+ }
+ else
+ {
+ Class.forName("org.sqlite.JDBC");
+ conn = DriverManager.getConnection(sqlite);
+ }
+ PreparedStatement ps = null;
+ try
+ {
+ ps = conn.prepareStatement(deleteByLocationQuery);
+ ps.setString(1, block.getWorld().getName());
+ ps.setInt(2, block.getX());
+ ps.setInt(3, block.getY());
+ ps.setInt(4, block.getZ());
+ ps.executeUpdate();
+
+ }
+ catch (SQLException ex)
+ {
+ logger.log(Level.WARNING,
+ "[EssentialsProtect] Could not delete block data from database",
+ ex);
+ }
+ finally
+ {
+ if (conn != null && !conn.isClosed())
+ {
+ conn.close();
+ }
+ if (ps != null)
+ {
+ ps.close();
+ }
+ }
+
+ }
+ catch (Throwable e)
+ {
+ logger.log(Level.SEVERE, " [EssentialsProtect] Exception occured whilst trying to delete data from sql", e);
+ }
+
+ }
+
+ public void removeProtectionFromDB(Block block, boolean removeBelow)
+ {
+ try
+ {
+ Connection conn = null;
+ if (dataType.contentEquals("mysql"))
+ {
+ Class.forName(mysqlDriver);
+ conn = DriverManager.getConnection(mysqlDatabase, mysqlUsername, mysqlPassword);
+ }
+ else
+ {
+ Class.forName("org.sqlite.JDBC");
+ conn = DriverManager.getConnection(sqlite);
+ }
+ PreparedStatement ps = null;
+ try
+ {
+ ps = conn.prepareStatement(deleteByLocationQuery);
+ ps.setString(1, block.getWorld().getName());
+ ps.setInt(2, block.getX());
+ ps.setInt(3, block.getY());
+ ps.setInt(4, block.getZ());
+ ps.executeUpdate();
+ if (removeBelow)
+ {
+ ps = conn.prepareStatement(deleteByLocationQuery);
+ ps.setString(1, block.getWorld().getName());
+ ps.setInt(2, block.getX());
+ ps.setInt(3, block.getY() - 1);
+ ps.setInt(4, block.getZ());
+ ps.executeUpdate();
+ }
+
+ }
+ catch (SQLException ex)
+ {
+ logger.log(Level.WARNING, "[EssentialsProtect] Could not delete block data from database", ex);
+ }
+ finally
+ {
+ if (conn != null && !conn.isClosed())
+ {
+ conn.close();
+ }
+ if (ps != null)
+ {
+ ps.close();
+ }
+ }
+
+ }
+ catch (Throwable e)
+ {
+ logger.log(Level.SEVERE, "[EssentialsProtect] Exception occured whilst trying to delete data from sql", e);
+ }
+
+ }
+
+ public boolean isBlockAboveProtectedRail(Block block)
+ {
+ Connection conn = null;
+ PreparedStatement ps = null;
+ ResultSet rs = null;
+ if (block.getTypeId() == 66)
+ {
+ try
+ {
+ if (dataType.contentEquals("mysql"))
+ {
+ Class.forName(mysqlDriver);
+ conn = DriverManager.getConnection(mysqlDatabase, mysqlUsername, mysqlPassword);
+ }
+ else
+ {
+ Class.forName("org.sqlite.JDBC");
+ conn = DriverManager.getConnection(sqlite);
+ }
+ int rowCount = 0;
+ conn.setAutoCommit(false);
+ ps = conn.prepareStatement(countByLocationQuery);
+ ps.setString(1, block.getWorld().getName());
+ ps.setInt(2, block.getX());
+ ps.setInt(3, block.getY());
+ ps.setInt(4, block.getZ());
+ rs = ps.executeQuery();
+ rs.next();
+ rowCount = rs.getInt(1);
+ rs.close();
+ ps.close();
+
+ if (rowCount == 0)
+ {
+
+ return false;
+ }
+ else
+ {
+ return true;
+ }
+ }
+ catch (SQLException s)
+ {
+ logger.log(Level.SEVERE, "[EssentialsProtect] Could not query protection", s);
+
+ }
+ catch (ClassNotFoundException ex)
+ {
+ logger.log(Level.SEVERE, "[EssentialsProtect] Could not find driver for " + dataType, ex);
+
+ }
+ catch (Throwable e)
+ {
+ logger.log(Level.SEVERE, "[EssentialsProtect] Unexpected error occured whilst creating table", e);
+ }
+ finally
+ {
+ try
+ {
+ if (conn != null && !conn.isClosed())
+ {
+ try
+ {
+ conn.close();
+ }
+ catch (SQLException e)
+ {
+ logger.log(Level.SEVERE, "[EssentialsProtect] Unexpected error occured whilst closing the connection", e);
+ }
+
+ }
+ }
+ catch (SQLException e)
+ {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ }
+ return false;
+ }
+}
diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectEntityListener.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectEntityListener.java
new file mode 100644
index 000000000..97b5c59e5
--- /dev/null
+++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectEntityListener.java
@@ -0,0 +1,197 @@
+package com.earth2me.essentials.protect;
+
+import com.earth2me.essentials.Essentials;
+import com.earth2me.essentials.User;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import net.minecraft.server.ChunkPosition;
+import net.minecraft.server.Packet60Explosion;
+import org.bukkit.Location;
+import org.bukkit.block.Block;
+import org.bukkit.craftbukkit.CraftServer;
+import org.bukkit.entity.Creeper;
+import org.bukkit.entity.Entity;
+import org.bukkit.entity.LivingEntity;
+import org.bukkit.entity.Monster;
+import org.bukkit.entity.Player;
+import org.bukkit.event.entity.CreatureSpawnEvent;
+import org.bukkit.event.entity.EntityDamageByBlockEvent;
+import org.bukkit.event.entity.EntityDamageByEntityEvent;
+import org.bukkit.event.entity.EntityDamageByProjectileEvent;
+import org.bukkit.event.entity.EntityDamageEvent;
+import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
+import org.bukkit.event.entity.EntityExplodeEvent;
+import org.bukkit.event.entity.EntityListener;
+
+
+public class EssentialsProtectEntityListener extends EntityListener
+{
+ private EssentialsProtect parent;
+
+ public EssentialsProtectEntityListener(EssentialsProtect parent)
+ {
+ Essentials.loadClasses();
+ this.parent = parent;
+ }
+
+ @Override
+ public void onEntityDamage(EntityDamageEvent event)
+ {
+ if (event.isCancelled()) return;
+ if (event instanceof EntityDamageByBlockEvent)
+ {
+ DamageCause cause = event.getCause();
+
+ if (EssentialsProtect.playerSettings.get("protect.disable.contactdmg") && cause == DamageCause.CONTACT)
+ {
+ event.setCancelled(true);
+ return;
+ }
+ if (EssentialsProtect.playerSettings.get("protect.disable.lavadmg") && cause == DamageCause.LAVA)
+ {
+ event.setCancelled(true);
+ return;
+ }
+ if (EssentialsProtect.guardSettings.get("protect.prevent.tnt-explosion") && cause == DamageCause.BLOCK_EXPLOSION)
+ {
+ event.setCancelled(true);
+ return;
+ }
+ }
+
+ if (event instanceof EntityDamageByEntityEvent)
+ {
+ EntityDamageByEntityEvent edEvent = (EntityDamageByEntityEvent)event;
+ Entity eAttack = edEvent.getDamager();
+ Entity eDefend = edEvent.getEntity();
+
+ // PVP Settings
+ if (eDefend instanceof Player && eAttack instanceof Player)
+ {
+ if (EssentialsProtect.playerSettings.get("protect.disable.pvp"))
+ {
+ User defender = User.get(eDefend);
+ User attacker = User.get(eAttack);
+
+ if (!defender.isAuthorized("essentials.protect.pvp") || !attacker.isAuthorized("essentials.protect.pvp"))
+ {
+ event.setCancelled(true);
+ return;
+ }
+ }
+ }
+ //Creeper explode prevention
+ if (eAttack != null && eAttack instanceof Monster)
+ {
+ if (eAttack instanceof Creeper && EssentialsProtect.guardSettings.get("protect.prevent.creeper-explosion"))
+ {
+ event.setCancelled(true);
+ return;
+ }
+
+ if (eAttack instanceof Creeper && EssentialsProtect.guardSettings.get("protect.prevent.creeper-playerdamage"))
+ {
+ event.setCancelled(true);
+ return;
+ }
+ }
+ }
+
+ if (event instanceof EntityDamageByProjectileEvent)
+ {
+ if (event.getEntity() instanceof Player)
+ {
+ event.setCancelled(EssentialsProtect.playerSettings.get("protect.disable.projectiles"));
+ return;
+ }
+ }
+
+ DamageCause cause = event.getCause();
+ Entity casualty = event.getEntity();
+ if (casualty instanceof Player)
+ {
+ if (EssentialsProtect.playerSettings.get("protect.disable.fall") && cause == DamageCause.FALL)
+ {
+ event.setCancelled(true);
+ return;
+ }
+
+ if (EssentialsProtect.playerSettings.get("protect.disable.suffocate") && cause == DamageCause.SUFFOCATION)
+ {
+ event.setCancelled(true);
+ return;
+ }
+ if (EssentialsProtect.playerSettings.get("protect.disable.firedmg") && (cause == DamageCause.FIRE
+ || cause == DamageCause.FIRE_TICK))
+ {
+ event.setCancelled(true);
+ return;
+ }
+ if (EssentialsProtect.playerSettings.get("protect.disable.drown") && cause == DamageCause.DROWNING)
+ {
+ event.setCancelled(true);
+ return;
+ }
+ }
+ }
+
+ @Override
+ public void onEntityExplode(EntityExplodeEvent event)
+ {
+ if (event.isCancelled()) return;
+ if (event.getEntity() instanceof LivingEntity)
+ {
+ //Nicccccccccce plaaacccccccccce..
+ int maxHeight = Essentials.getSettings().getEpCreeperMaxHeight();
+ if ( EssentialsProtect.guardSettings.get("protect.prevent.creeper-explosion") ||
+ EssentialsProtect.guardSettings.get("protect.prevent.creeper-blockdamage") ||
+ (maxHeight >= 0 && event.getLocation().getBlockY() > maxHeight))
+ {
+ HashSet<ChunkPosition> set = new HashSet<ChunkPosition>(event.blockList().size());
+ Player[] players = parent.getServer().getOnlinePlayers();
+ List<ChunkPosition> blocksUnderPlayers = new ArrayList<ChunkPosition>(players.length);
+ Location loc = event.getLocation();
+ for (Player player : players) {
+ if (player.getWorld().equals(loc.getWorld())) {
+ blocksUnderPlayers.add(
+ new ChunkPosition(
+ player.getLocation().getBlockX(),
+ player.getLocation().getBlockY() - 1,
+ player.getLocation().getBlockZ()));
+ }
+ }
+ for (Block block : event.blockList()) {
+ ChunkPosition cp = new ChunkPosition(block.getX(), block.getY(), block.getZ());
+ if (!blocksUnderPlayers.contains(cp)) {
+ set.add(cp);
+ }
+ }
+
+ ((CraftServer)parent.getServer()).getServer().f.a(loc.getX(), loc.getY(), loc.getZ(), 64.0D,
+ new Packet60Explosion(loc.getX(), loc.getY(), loc.getZ(), 3.0f, set));
+ event.setCancelled(true);
+ return;
+ }
+ }
+ else
+ { //OH NOES TNT
+ if (EssentialsProtect.guardSettings.get("protect.prevent.tnt-explosion"))
+ {
+ event.setCancelled(true);
+ return;
+ }
+ }
+ }
+
+ @Override
+ public void onCreatureSpawn(CreatureSpawnEvent event) {
+ String creatureName = event.getCreatureType().toString().toLowerCase();
+ if (creatureName == null || creatureName.isEmpty()) {
+ return;
+ }
+ if (EssentialsProtect.guardSettings.get("protect.prevent.spawn."+creatureName)) {
+ event.setCancelled(true);
+ }
+ }
+}
diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectPlayerListener.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectPlayerListener.java
new file mode 100644
index 000000000..61419f69a
--- /dev/null
+++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectPlayerListener.java
@@ -0,0 +1,73 @@
+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.event.player.PlayerListener;
+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()
+ {
+ if (spData != null) return;
+ spData = new EssentialsProtectData();
+ }
+
+ @Override
+ public void onPlayerItem(PlayerItemEvent event)
+ {
+ if(event.isCancelled()) return;
+ ItemStack item = event.getItem();
+ 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);
+ return;
+ }
+
+ if (EssentialsProtect.onUseAlert.contains(String.valueOf(item.getTypeId())))
+ {
+ 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/EssentialsProtectRegions.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectRegions.java
new file mode 100644
index 000000000..6a640f3e1
--- /dev/null
+++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectRegions.java
@@ -0,0 +1,5 @@
+package com.earth2me.essentials.protect;
+
+public class EssentialsProtectRegions {
+
+}
diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectSqlProperties.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectSqlProperties.java
new file mode 100644
index 000000000..30b2a7032
--- /dev/null
+++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectSqlProperties.java
@@ -0,0 +1,12 @@
+package com.earth2me.essentials.protect;
+
+public class EssentialsProtectSqlProperties {
+
+ public static String EssentialsProtect="CREATE TABLE IF NOT EXISTS `EssentialsProtect` (`id` int(11) NOT NULL AUTO_INCREMENT, `worldName` varchar(150) NOT NULL, `playerName` varchar(150) NOT NULL, `x` int(11) NOT NULL, `y` int(11) NOT NULL, `z` int(11) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=205 DEFAULT CHARSET=latin1";
+ public static String EssentialsProtect_sqlite = "CREATE TABLE IF NOT EXISTS EssentialsProtect (id INTEGER PRIMARY KEY, worldName TEXT ,playerName TEXT, x NUMERIC, y NUMERIC, z NUMERIC)";
+ public static String CountByLocation="SELECT COUNT(*) from EssentialsProtect where worldName = ? and x = ? and y = ? and z = ? limit 10";
+ public static String CountByPLayerLocation="SELECT COUNT(*) from EssentialsProtect where worldName = ? and playerName =? and x = ? and y = ? and z = ? limit 10";
+ public static String DeleteByLocation="DELETE FROM EssentialsProtect WHERE worldName=? and x=? and y=? and z=?";
+ public static String Insert="INSERT INTO EssentialsProtect (worldName, playerName, x, y, z) VALUES (?,?,?,?,?)";
+ public static String PlayerByLocation="SELECT playerName FROM EssentialsProtect WHERE worldname = ? and x = ? and y = ? and z = ? LIMIT 10";
+}