Major cleanup of the Protect code

This commit is contained in:
snowleo 2011-06-06 22:29:08 +02:00
parent d877880171
commit a45e620946
14 changed files with 1046 additions and 854 deletions

View File

@ -198,167 +198,7 @@ public class Settings implements IConf
return config.getBoolean("non-ess-in-help", true); return config.getBoolean("non-ess-in-help", true);
} }
public Map<String, Boolean> getEpSettings() public int getProtectCreeperMaxHeight()
{
Map<String, Boolean> epSettings = new HashMap<String, Boolean>();
epSettings.put("protect.protect.signs", config.getBoolean("protect.protect.signs", true));
epSettings.put("protect.protect.rails", config.getBoolean("protect.protect.rails", true));
epSettings.put("protect.protect.block-below", config.getBoolean("protect.protect.block-below", true));
epSettings.put("protect.protect.prevent-block-on-rails", config.getBoolean("protect.protect.prevent-block-on-rails", false));
epSettings.put("protect.memstore", config.getBoolean("protect.memstore", false));
return epSettings;
}
public Map<String, String> getEpDBSettings()
{
Map<String, String> epSettings = new HashMap<String, String>();
epSettings.put("protect.datatype", config.getString("protect.datatype", "sqlite"));
epSettings.put("protect.username", config.getString("protect.username", "root"));
epSettings.put("protect.password", config.getString("protect.password", "root"));
epSettings.put("protect.mysqlDb", config.getString("protect.mysqlDb", "jdbc:mysql://localhost:3306/minecraft"));
return epSettings;
}
public List<Integer> getEpAlertOnPlacement()
{
final List<Integer> epAlertPlace = new ArrayList<Integer>();
for (String itemName : config.getString("protect.alert.on-placement", "").split(",")) {
itemName = itemName.trim();
if (itemName.isEmpty()) {
continue;
}
ItemStack is;
try {
is = ItemDb.get(itemName);
epAlertPlace.add(is.getTypeId());
} catch (Exception ex) {
logger.log(Level.SEVERE, Util.format("unknownItemInList", itemName, "alert.on-placement"));
}
}
return epAlertPlace;
}
public List<Integer> getEpAlertOnUse()
{
final List<Integer> epAlertUse = new ArrayList<Integer>();
for (String itemName : config.getString("protect.alert.on-use", "").split(",")) {
itemName = itemName.trim();
if (itemName.isEmpty()) {
continue;
}
ItemStack is;
try {
is = ItemDb.get(itemName);
epAlertUse.add(is.getTypeId());
} catch (Exception ex) {
logger.log(Level.SEVERE, Util.format("unknownItemInList", itemName, "alert.on-use"));
}
}
return epAlertUse;
}
public List<Integer> getEpAlertOnBreak()
{
final List<Integer> epAlertPlace = new ArrayList<Integer>();
for (String itemName : config.getString("protect.alert.on-break", "").split(",")) {
itemName = itemName.trim();
if (itemName.isEmpty()) {
continue;
}
ItemStack is;
try {
is = ItemDb.get(itemName);
epAlertPlace.add(is.getTypeId());
} catch (Exception ex) {
logger.log(Level.SEVERE, Util.format("unknownItemInList", itemName, "alert.on-break"));
}
}
return epAlertPlace;
}
public List<Integer> epBlackListPlacement()
{
final List<Integer> epBlacklistPlacement = new ArrayList<Integer>();
for (String itemName : config.getString("protect.blacklist.placement", "").split(",")) {
itemName = itemName.trim();
if (itemName.isEmpty()) {
continue;
}
ItemStack is;
try {
is = ItemDb.get(itemName);
epBlacklistPlacement.add(is.getTypeId());
} catch (Exception ex) {
logger.log(Level.SEVERE, Util.format("unknownItemInList", itemName, "blacklist.placement"));
}
}
return epBlacklistPlacement;
}
public List<Integer> epBlackListUsage()
{
final List<Integer> epBlackListUsage = new ArrayList<Integer>();
for (String itemName : config.getString("protect.blacklist.usage", "").split(",")) {
itemName = itemName.trim();
if (itemName.isEmpty()) {
continue;
}
ItemStack is;
try {
is = ItemDb.get(itemName);
epBlackListUsage.add(is.getTypeId());
} catch (Exception ex) {
logger.log(Level.SEVERE, Util.format("unknownItemInList", itemName, "blacklist.usage"));
}
}
return epBlackListUsage;
}
public Map<String, Boolean> getEpGuardSettings()
{
final Map<String, Boolean> epSettings = new HashMap<String, Boolean>();
epSettings.put("protect.prevent.lava-flow", config.getBoolean("protect.prevent.lava-flow", false));
epSettings.put("protect.prevent.water-flow", config.getBoolean("protect.prevent.water-flow", false));
epSettings.put("protect.prevent.water-bucket-flow", config.getBoolean("protect.prevent.water-bucket-flow", false));
epSettings.put("protect.prevent.fire-spread", config.getBoolean("protect.prevent.fire-spread", true));
epSettings.put("protect.prevent.flint-fire", config.getBoolean("protect.prevent.flint-fire", false));
epSettings.put("protect.prevent.portal-creation", config.getBoolean("protect.prevent.portal-creation", false));
epSettings.put("protect.prevent.lava-fire-spread", config.getBoolean("protect.prevent.lava-fire-spread", true));
epSettings.put("protect.prevent.tnt-explosion", config.getBoolean("protect.prevent.tnt-explosion", false));
epSettings.put("protect.prevent.creeper-explosion", config.getBoolean("protect.prevent.creeper-explosion", false));
epSettings.put("protect.prevent.creeper-playerdamage", config.getBoolean("protect.prevent.creeper-playerdamage", false));
epSettings.put("protect.prevent.creeper-blockdamage", config.getBoolean("protect.prevent.creeper-blockdamage", false));
epSettings.put("protect.prevent.entitytarget", config.getBoolean("protect.prevent.entitytarget", false));
for (CreatureType ct : CreatureType.values()) {
final String name = ct.toString().toLowerCase();
epSettings.put("protect.prevent.spawn."+name, config.getBoolean("protect.prevent.spawn."+name, false));
}
epSettings.put("protect.prevent.lightning-fire-spread", config.getBoolean("protect.prevent.lightning-fire-spread", true));
return epSettings;
}
public Map<String, Boolean> getEpPlayerSettings()
{
final Map<String, Boolean> epPlayerSettings = new HashMap<String, Boolean>();
epPlayerSettings.put("protect.disable.fall", config.getBoolean("protect.disable.fall", false));
epPlayerSettings.put("protect.disable.pvp", config.getBoolean("protect.disable.pvp", false));
epPlayerSettings.put("protect.disable.drown", config.getBoolean("protect.disable.drown", false));
epPlayerSettings.put("protect.disable.suffocate", config.getBoolean("protect.disable.suffocate", false));
epPlayerSettings.put("protect.disable.lavadmg", config.getBoolean("protect.disable.lavadmg", false));
epPlayerSettings.put("protect.disable.projectiles", config.getBoolean("protect.disable.projectiles", false));
epPlayerSettings.put("protect.disable.contactdmg", config.getBoolean("protect.disable.contactdmg", false));
epPlayerSettings.put("protect.disable.firedmg", config.getBoolean("protect.disable.firedmg", false));
epPlayerSettings.put("protect.disable.build", config.getBoolean("protect.disable.build", false));
epPlayerSettings.put("protect.disable.lightning", config.getBoolean("protect.disable.lightning", false));
epPlayerSettings.put("protect.disable.weather.lightning", config.getBoolean("protect.disable.weather.lightning", false));
epPlayerSettings.put("protect.disable.weather.storm", config.getBoolean("protect.disable.weather.storm", false));
epPlayerSettings.put("protect.disable.weather.thunder", config.getBoolean("protect.disable.weather.thunder", false));
return epPlayerSettings;
}
public int getEpCreeperMaxHeight()
{ {
return config.getInt("protect.creeper.max-height", -1); return config.getInt("protect.creeper.max-height", -1);
} }
@ -441,25 +281,6 @@ public class Settings implements IConf
return epItemSpwn; return epItemSpwn;
} }
public List<Integer> epBlockBreakingBlacklist()
{
final List<Integer> epBreakList = new ArrayList<Integer>();
for (String itemName : config.getString("protect.blacklist.break", "").split(",")) {
itemName = itemName.trim();
if (itemName.isEmpty()) {
continue;
}
ItemStack is;
try {
is = ItemDb.get(itemName);
epBreakList.add(is.getTypeId());
} catch (Exception ex) {
logger.log(Level.SEVERE, Util.format("unknownItemInList", itemName, "blacklist.break"));
}
}
return epBreakList;
}
public boolean spawnIfNoHome() public boolean spawnIfNoHome()
{ {
return config.getBoolean("spawn-if-no-home", false); return config.getBoolean("spawn-if-no-home", false);
@ -517,4 +338,38 @@ public class Settings implements IConf
{ {
return config.getBoolean("disable-eco", false); return config.getBoolean("disable-eco", false);
} }
public boolean getProtectPreventSpawn(final String creatureName)
{
return config.getBoolean("protect.prevent.spawn."+creatureName, false);
}
public List<Integer> getProtectList(final String configName)
{
final List<Integer> list = new ArrayList<Integer>();
for (String itemName : config.getString(configName, "").split(",")) {
itemName = itemName.trim();
if (itemName.isEmpty()) {
continue;
}
ItemStack itemStack;
try {
itemStack = ItemDb.get(itemName);
list.add(itemStack.getTypeId());
} catch (Exception ex) {
logger.log(Level.SEVERE, Util.format("unknownItemInList", itemName, configName));
}
}
return list;
}
public String getProtectString(final String configName)
{
return config.getString(configName, null);
}
public boolean getProtectBoolean(final String configName, boolean def)
{
return config.getBoolean(configName, def);
}
} }

View File

@ -10,6 +10,7 @@ import com.earth2me.essentials.protect.data.ProtectedBlockMemory;
import com.earth2me.essentials.protect.data.ProtectedBlockMySQL; import com.earth2me.essentials.protect.data.ProtectedBlockMySQL;
import com.earth2me.essentials.protect.data.ProtectedBlockSQLite; import com.earth2me.essentials.protect.data.ProtectedBlockSQLite;
import java.beans.PropertyVetoException; import java.beans.PropertyVetoException;
import java.util.EnumMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
@ -22,125 +23,104 @@ import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
public class EssentialsProtect extends JavaPlugin implements IConf public class EssentialsProtect extends JavaPlugin implements IConf, IProtect
{ {
private EssentialsProtectBlockListener blockListener = null; private static final Logger LOGGER = Logger.getLogger("Minecraft");
private EssentialsProtectPlayerListener playerListener = null;
private EssentialsProtectEntityListener entityListener = null;
private EssentialsProtectWeatherListener weatherListener = null;
public static final String AUTHORS = Essentials.AUTHORS;
private static final Logger logger = Logger.getLogger("Minecraft");
public static Map<String, Boolean> genSettings = null;
public static Map<String, String> dataSettings = null;
public static Map<String, Boolean> guardSettings = null;
public static Map<String, Boolean> playerSettings = null;
public static List<Integer> usageList = null;
public static List<Integer> blackListPlace = null;
public static List<Integer> breakBlackList = null;
public static List<Integer> onPlaceAlert = null;
public static List<Integer> onUseAlert = null;
public static List<Integer> onBreakAlert = null;
private IProtectedBlock storage = null;
IEssentials ess = null;
private static EssentialsProtect instance = null;
public EssentialsProtect() private final transient Map<ProtectConfig, Boolean> settingsBoolean = new EnumMap<ProtectConfig, Boolean>(ProtectConfig.class);
{ private final transient Map<ProtectConfig, String> settingsString = new EnumMap<ProtectConfig, String>(ProtectConfig.class);
} private final transient Map<ProtectConfig, List<Integer>> settingsList = new EnumMap<ProtectConfig, List<Integer>>(ProtectConfig.class);
private transient IProtectedBlock storage = null;
public transient IEssentials ess = null;
public void onEnable() public void onEnable()
{ {
ess = Essentials.getStatic(); ess = Essentials.getStatic();
ess.getDependancyChecker().checkProtectDependancies(); ess.getDependancyChecker().checkProtectDependancies();
PluginManager pm = this.getServer().getPluginManager(); final PluginManager pm = this.getServer().getPluginManager();
playerListener = new EssentialsProtectPlayerListener(this);
blockListener = new EssentialsProtectBlockListener(this); final EssentialsProtectPlayerListener playerListener = new EssentialsProtectPlayerListener(this);
entityListener = new EssentialsProtectEntityListener(this);
weatherListener = new EssentialsProtectWeatherListener(this);
pm.registerEvent(Type.PLAYER_INTERACT, playerListener, Priority.Low, this); pm.registerEvent(Type.PLAYER_INTERACT, playerListener, Priority.Low, this);
final EssentialsProtectBlockListener blockListener = new EssentialsProtectBlockListener(this);
pm.registerEvent(Type.BLOCK_PLACE, blockListener, Priority.Highest, this); pm.registerEvent(Type.BLOCK_PLACE, blockListener, Priority.Highest, this);
pm.registerEvent(Type.BLOCK_FROMTO, 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_IGNITE, blockListener, Priority.Highest, this);
pm.registerEvent(Type.BLOCK_BURN, blockListener, Priority.Highest, this); pm.registerEvent(Type.BLOCK_BURN, blockListener, Priority.Highest, this);
pm.registerEvent(Type.BLOCK_BREAK, blockListener, Priority.Highest, this);
final EssentialsProtectEntityListener entityListener = new EssentialsProtectEntityListener(this);
pm.registerEvent(Type.ENTITY_EXPLODE, entityListener, Priority.Highest, this); pm.registerEvent(Type.ENTITY_EXPLODE, entityListener, Priority.Highest, this);
pm.registerEvent(Type.ENTITY_DAMAGE, 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); pm.registerEvent(Type.CREATURE_SPAWN, entityListener, Priority.Highest, this);
pm.registerEvent(Type.ENTITY_TARGET, entityListener, Priority.Highest, this);
final EssentialsProtectWeatherListener weatherListener = new EssentialsProtectWeatherListener(this);
pm.registerEvent(Type.LIGHTNING_STRIKE, weatherListener, Priority.Highest, this); pm.registerEvent(Type.LIGHTNING_STRIKE, weatherListener, Priority.Highest, this);
pm.registerEvent(Type.THUNDER_CHANGE, weatherListener, Priority.Highest, this); pm.registerEvent(Type.THUNDER_CHANGE, weatherListener, Priority.Highest, this);
pm.registerEvent(Type.WEATHER_CHANGE, weatherListener, Priority.Highest, this); pm.registerEvent(Type.WEATHER_CHANGE, weatherListener, Priority.Highest, this);
pm.registerEvent(Type.ENTITY_TARGET, entityListener, Priority.Highest, this);
reloadConfig(); reloadConfig();
ess.addReloadListener(this); ess.addReloadListener(this);
if (!this.getDescription().getVersion().equals(Essentials.getStatic().getDescription().getVersion())) if (!this.getDescription().getVersion().equals(ess.getDescription().getVersion()))
{ {
logger.log(Level.WARNING, Util.i18n("versionMismatchAll")); LOGGER.log(Level.WARNING, Util.i18n("versionMismatchAll"));
} }
logger.info(Util.format("loadinfo", this.getDescription().getName(), this.getDescription().getVersion(), Essentials.AUTHORS)); LOGGER.info(Util.format("loadinfo", this.getDescription().getName(), this.getDescription().getVersion(), Essentials.AUTHORS));
}
public static boolean checkProtectionItems(List<Integer> itemList, int id)
{
return !itemList.isEmpty() && itemList.contains(id);
} }
@Override @Override
public void onDisable() public boolean checkProtectionItems(final ProtectConfig list, final int id)
{ {
genSettings.clear(); final List<Integer> itemList = settingsList.get(list);
dataSettings.clear(); return itemList != null && !itemList.isEmpty() && itemList.contains(id);
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 void alert(User user, String item, String type) @Override
public void alert(final User user, final String item, final String type)
{ {
Location loc = user.getLocation(); final Location loc = user.getLocation();
for (Player p : this.getServer().getOnlinePlayers()) for (Player p : this.getServer().getOnlinePlayers())
{ {
User alertUser = ess.getUser(p); final User alertUser = ess.getUser(p);
if (alertUser.isAuthorized("essentials.protect.alerts")) if (alertUser.isAuthorized("essentials.protect.alerts"))
{
alertUser.sendMessage(Util.format("alertFormat", user.getName(), type, item, formatCoords(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()))); alertUser.sendMessage(Util.format("alertFormat", user.getName(), type, item, formatCoords(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())));
} }
} }
}
public static String formatCoords(int x, int y, int z) public static String formatCoords(final int x, final int y, final int z)
{ {
return x + "," + y + "," + z; return x + "," + y + "," + z;
} }
public void reloadConfig() public void reloadConfig()
{ {
dataSettings = ess.getSettings().getEpDBSettings(); for (ProtectConfig protectConfig : ProtectConfig.values())
genSettings = ess.getSettings().getEpSettings(); {
guardSettings = ess.getSettings().getEpGuardSettings(); if (protectConfig.isList()) {
usageList = ess.getSettings().epBlackListUsage(); settingsList.put(protectConfig, ess.getSettings().getProtectList(protectConfig.getConfigName()));
blackListPlace = ess.getSettings().epBlackListPlacement(); } else if (protectConfig.isString()) {
breakBlackList = ess.getSettings().epBlockBreakingBlacklist(); settingsString.put(protectConfig, ess.getSettings().getProtectString(protectConfig.getConfigName()));
onPlaceAlert = ess.getSettings().getEpAlertOnPlacement(); } else {
onUseAlert = ess.getSettings().getEpAlertOnUse(); settingsBoolean.put(protectConfig, ess.getSettings().getProtectBoolean(protectConfig.getConfigName(), protectConfig.getDefaultValueBoolean()));
onBreakAlert = ess.getSettings().getEpAlertOnBreak(); }
playerSettings = ess.getSettings().getEpPlayerSettings();
if (dataSettings.get("protect.datatype").equals("mysql")) }
if (getSettingString(ProtectConfig.datatype).equalsIgnoreCase("mysql"))
{ {
try try
{ {
storage = new ProtectedBlockMySQL(dataSettings.get("protect.mysqlDb"), dataSettings.get("protect.username"), dataSettings.get("protect.password")); storage = new ProtectedBlockMySQL(
getSettingString(ProtectConfig.mysqlDB),
getSettingString(ProtectConfig.dbUsername),
getSettingString(ProtectConfig.dbPassword));
} }
catch (PropertyVetoException ex) catch (PropertyVetoException ex)
{ {
logger.log(Level.SEVERE, null, ex); LOGGER.log(Level.SEVERE, null, ex);
} }
} }
else else
@ -151,17 +131,41 @@ public class EssentialsProtect extends JavaPlugin implements IConf
} }
catch (PropertyVetoException ex) catch (PropertyVetoException ex)
{ {
logger.log(Level.SEVERE, null, ex); LOGGER.log(Level.SEVERE, null, ex);
} }
} }
if (genSettings.get("protect.memstore")) if (getSettingBool(ProtectConfig.memstore))
{ {
storage = new ProtectedBlockMemory(storage); storage = new ProtectedBlockMemory(storage, this);
} }
} }
public static IProtectedBlock getStorage() @Override
public IProtectedBlock getStorage()
{ {
return EssentialsProtect.instance.storage; return storage;
}
@Override
public boolean getSettingBool(final ProtectConfig protectConfig)
{
final Boolean bool = settingsBoolean.get(protectConfig);
return bool == null ? protectConfig.getDefaultValueBoolean() : bool;
}
@Override
public String getSettingString(final ProtectConfig protectConfig)
{
final String str = settingsString.get(protectConfig);
return str == null ? protectConfig.getDefaultValueString() : str;
}
public void onDisable()
{
}
public IEssentials getEssentials()
{
return ess;
} }
} }

View File

@ -1,8 +1,9 @@
package com.earth2me.essentials.protect; package com.earth2me.essentials.protect;
import com.earth2me.essentials.Essentials; import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import com.earth2me.essentials.Util; import com.earth2me.essentials.Util;
import com.earth2me.essentials.protect.data.IProtectedBlock;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.bukkit.Material; import org.bukkit.Material;
@ -14,85 +15,82 @@ import org.bukkit.event.block.BlockFromToEvent;
import org.bukkit.event.block.BlockIgniteEvent; import org.bukkit.event.block.BlockIgniteEvent;
import org.bukkit.event.block.BlockListener; import org.bukkit.event.block.BlockListener;
import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack;
public class EssentialsProtectBlockListener extends BlockListener public class EssentialsProtectBlockListener extends BlockListener
{ {
private EssentialsProtect parent; final private transient IProtect prot;
final private transient IEssentials ess;
public EssentialsProtectBlockListener(EssentialsProtect parent) public EssentialsProtectBlockListener(final IProtect parent)
{ {
this.parent = parent; this.prot = parent;
this.ess = prot.getEssentials();
} }
@Override @Override
public void onBlockPlace(BlockPlaceEvent event) public void onBlockPlace(final BlockPlaceEvent event)
{ {
if (event.isCancelled()) return; if (event.isCancelled())
final ItemStack item = event.getItemInHand(); {
final User user = parent.ess.getUser(event.getPlayer()); return;
}
if (EssentialsProtect.playerSettings.get("protect.disable.build") && !user.canBuild()) final User user = ess.getUser(event.getPlayer());
if (prot.getSettingBool(ProtectConfig.disable_build) && !user.canBuild())
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
int id = event.getBlockPlaced().getTypeId(); final Block blockPlaced = event.getBlockPlaced();
final int id = blockPlaced.getTypeId();
if (EssentialsProtect.checkProtectionItems(EssentialsProtect.blackListPlace, id) && !user.isAuthorized("essentials.protect.exemptplacement")) if (prot.checkProtectionItems(ProtectConfig.blacklist_placement, id) && !user.isAuthorized("essentials.protect.exemptplacement"))
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
if (!EssentialsProtect.onPlaceAlert.isEmpty() && EssentialsProtect.onPlaceAlert.contains(String.valueOf(item.getTypeId()))) if (prot.checkProtectionItems(ProtectConfig.alert_on_placement, id))
{ {
parent.alert(user, item.getType().toString(), Util.i18n("alertPlaced")); prot.alert(user, blockPlaced.getType().toString(), Util.i18n("alertPlaced"));
} }
Block blockPlaced = event.getBlockPlaced(); final Block below = blockPlaced.getFace(BlockFace.DOWN);
Block below = blockPlaced.getFace(BlockFace.DOWN); if (below.getType() == Material.RAILS
if (below.getType() == Material.RAILS) { && prot.getSettingBool(ProtectConfig.prevent_block_on_rail)
if (EssentialsProtect.genSettings.get("protect.protect.prevent.block-on-rail")) && prot.getStorage().isProtected(below, user.getName()))
{ {
if (EssentialsProtect.getStorage().isProtected(below, user.getName())) {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
}
}
List<Block> protect = new ArrayList<Block>(); final List<Block> protect = new ArrayList<Block>();
if (blockPlaced.getType() == Material.RAILS) { if (blockPlaced.getType() == Material.RAILS
if (EssentialsProtect.genSettings.get("protect.protect.rails")) && prot.getSettingBool(ProtectConfig.protect_rails)
{ && user.isAuthorized("essentials.protect"))
if (user.isAuthorized("essentials.protect"))
{ {
protect.add(blockPlaced); protect.add(blockPlaced);
if (EssentialsProtect.genSettings.get("protect.protect.block-below")) if (prot.getSettingBool(ProtectConfig.protect_below_rails))
{ {
protect.add(blockPlaced.getFace(BlockFace.DOWN)); protect.add(blockPlaced.getFace(BlockFace.DOWN));
} }
} }
} if ((blockPlaced.getType() == Material.SIGN_POST || blockPlaced.getType() == Material.WALL_SIGN)
} && prot.getSettingBool(ProtectConfig.protect_signs)
if (blockPlaced.getType() == Material.SIGN_POST || blockPlaced.getType() == Material.WALL_SIGN) { && user.isAuthorized("essentials.protect"))
if (EssentialsProtect.genSettings.get("protect.protect.signs"))
{
if (user.isAuthorized("essentials.protect"))
{ {
protect.add(blockPlaced); protect.add(blockPlaced);
if (EssentialsProtect.genSettings.get("protect.protect.block-below")) if (prot.getSettingBool(ProtectConfig.protect_against_signs))
{ {
protect.add(event.getBlockAgainst()); protect.add(event.getBlockAgainst());
} }
} }
} for (Block block : protect)
} {
for (Block block : protect) { prot.getStorage().protectBlock(block, user.getName());
EssentialsProtect.getStorage().protectBlock(block, user.getName());
} }
} }
@ -100,180 +98,201 @@ public class EssentialsProtectBlockListener extends BlockListener
public void onBlockIgnite(BlockIgniteEvent event) public void onBlockIgnite(BlockIgniteEvent event)
{ {
Block block = event.getBlock(); Block block = event.getBlock();
if (block.getType() == Material.RAILS && EssentialsProtect.genSettings.get("protect.protect.rails")) if (block.getType() == Material.RAILS
&& prot.getSettingBool(ProtectConfig.protect_rails))
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
if ((block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST) && EssentialsProtect.genSettings.get("protect.protect.signs")) if ((block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST)
&& prot.getSettingBool(ProtectConfig.protect_signs))
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
if (event.getBlock().getType() == Material.OBSIDIAN || if (event.getBlock().getType() == Material.OBSIDIAN
event.getBlock().getFace(BlockFace.DOWN).getType() == Material.OBSIDIAN) || event.getBlock().getFace(BlockFace.DOWN).getType() == Material.OBSIDIAN)
{ {
event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.portal-creation")); event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_portal_creation));
return; return;
} }
if ((event.getCause().equals(BlockIgniteEvent.IgniteCause.SPREAD))) if (event.getCause().equals(BlockIgniteEvent.IgniteCause.SPREAD))
{ {
event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.fire-spread")); event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_fire_spread));
return; return;
} }
if (event.getCause().equals(BlockIgniteEvent.IgniteCause.FLINT_AND_STEEL)) if (event.getCause().equals(BlockIgniteEvent.IgniteCause.FLINT_AND_STEEL))
{ {
event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.flint-fire")); event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_flint_fire));
return; return;
} }
if (event.getCause().equals(BlockIgniteEvent.IgniteCause.LAVA)) if (event.getCause().equals(BlockIgniteEvent.IgniteCause.LAVA))
{ {
event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.lava-fire-spread")); event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_lava_fire_spread));
return; return;
} }
if (event.getCause().equals(BlockIgniteEvent.IgniteCause.LIGHTNING)) if (event.getCause().equals(BlockIgniteEvent.IgniteCause.LIGHTNING))
{ {
event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.lightning-fire-spread")); event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_lightning_fire_spread));
return; return;
} }
} }
@Override @Override
public void onBlockFromTo(BlockFromToEvent event) public void onBlockFromTo(final BlockFromToEvent event)
{ {
if (event.isCancelled()) return; if (event.isCancelled())
Block block = event.getBlock(); {
Block toBlock = event.getToBlock(); return;
if (toBlock.getType() == Material.RAILS && EssentialsProtect.genSettings.get("protect.protect.rails")) }
final Block toBlock = event.getToBlock();
if (toBlock.getType() == Material.RAILS
&& prot.getSettingBool(ProtectConfig.protect_rails))
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
if ((toBlock.getType() == Material.WALL_SIGN || toBlock.getType() == Material.SIGN_POST) && EssentialsProtect.genSettings.get("protect.protect.signs")) if ((toBlock.getType() == Material.WALL_SIGN || toBlock.getType() == Material.SIGN_POST)
&& prot.getSettingBool(ProtectConfig.protect_signs))
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
final Block block = event.getBlock();
if (block.getType() == Material.WATER || block.getType() == Material.STATIONARY_WATER) if (block.getType() == Material.WATER || block.getType() == Material.STATIONARY_WATER)
{ {
event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.water-flow")); event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_water_flow));
return; return;
} }
if (block.getType() == Material.LAVA || block.getType() == Material.STATIONARY_LAVA) if (block.getType() == Material.LAVA || block.getType() == Material.STATIONARY_LAVA)
{ {
event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.lava-flow")); event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_lava_flow));
return; return;
} }
if (block.getType() == Material.AIR) if (block.getType() == Material.AIR)
{ {
event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.water-bucket-flow")); event.setCancelled(prot.getSettingBool(ProtectConfig.prevent_water_bucket_flow));
return; return;
} }
} }
@Override @Override
public void onBlockBurn(BlockBurnEvent event) public void onBlockBurn(final BlockBurnEvent event)
{ {
Block block = event.getBlock(); final Block block = event.getBlock();
if (block.getType() == Material.RAILS && EssentialsProtect.genSettings.get("protect.protect.rails")) if (block.getType() == Material.RAILS && prot.getSettingBool(ProtectConfig.protect_rails))
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
if ((block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST) && EssentialsProtect.genSettings.get("protect.protect.signs")) if ((block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST)
&& prot.getSettingBool(ProtectConfig.protect_signs))
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
if (EssentialsProtect.guardSettings.get("protect.prevent.fire-spread")) if (prot.getSettingBool(ProtectConfig.prevent_fire_spread))
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
} }
private final static BlockFace[] faces = new BlockFace[]
{
BlockFace.NORTH,
BlockFace.EAST,
BlockFace.SOUTH,
BlockFace.WEST
};
@Override @Override
public void onBlockBreak(BlockBreakEvent event) public void onBlockBreak(final BlockBreakEvent event)
{ {
if (event.isCancelled()) return; if (event.isCancelled())
User user = parent.ess.getUser(event.getPlayer()); {
Block block = event.getBlock(); return;
if (EssentialsProtect.playerSettings.get("protect.disable.build") && !user.canBuild()) }
final User user = ess.getUser(event.getPlayer());
if (prot.getSettingBool(ProtectConfig.disable_build) && !user.canBuild())
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
final Block block = event.getBlock();
final int typeId = block.getTypeId();
if(EssentialsProtect.breakBlackList.contains(String.valueOf(block.getTypeId())) && !user.isAuthorized("essentials.protect.exemptbreak")) if (prot.checkProtectionItems(ProtectConfig.blacklist_break, typeId)
&& !user.isAuthorized("essentials.protect.exemptbreak"))
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
final Material type = block.getType();
if (!EssentialsProtect.onBreakAlert.isEmpty() && EssentialsProtect.onBreakAlert.contains(String.valueOf(block.getTypeId()))) if (prot.checkProtectionItems(ProtectConfig.alert_on_break, typeId))
{ {
parent.alert(user, block.getType().toString(), Util.i18n("alertBroke")); prot.alert(user, type.toString(), Util.i18n("alertBroke"));
} }
final IProtectedBlock storage = prot.getStorage();
if (user.isAuthorized("essentials.protect.admin")) if (user.isAuthorized("essentials.protect.admin"))
{ {
if (block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST || block.getType() == Material.RAILS) if (type == Material.WALL_SIGN || type == Material.SIGN_POST || type == Material.RAILS)
{ {
EssentialsProtect.getStorage().unprotectBlock(block); storage.unprotectBlock(block);
if (block.getType() == Material.RAILS || block.getType() == Material.SIGN_POST) { if (type == Material.RAILS || type == Material.SIGN_POST)
Block below = block.getFace(BlockFace.DOWN); {
EssentialsProtect.getStorage().unprotectBlock(below); final Block below = block.getFace(BlockFace.DOWN);
} else { storage.unprotectBlock(below);
BlockFace[] faces = new BlockFace[] { }
BlockFace.NORTH, else
BlockFace.EAST, {
BlockFace.SOUTH, for (BlockFace blockFace : faces)
BlockFace.WEST {
}; final Block against = block.getFace(blockFace);
for (BlockFace blockFace : faces) { storage.unprotectBlock(against);
Block against = block.getFace(blockFace);
EssentialsProtect.getStorage().unprotectBlock(against);
} }
} }
} }
else else
{ {
EssentialsProtect.getStorage().unprotectBlock(block); storage.unprotectBlock(block);
} }
return; return;
} }
else else
{ {
boolean isProtected = EssentialsProtect.getStorage().isProtected(block, user.getName()); final boolean isProtected = storage.isProtected(block, user.getName());
if (!isProtected) { if (!isProtected)
if (block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST || block.getType() == Material.RAILS)
{ {
EssentialsProtect.getStorage().unprotectBlock(block); if (type == Material.WALL_SIGN || type == Material.SIGN_POST || type == Material.RAILS)
if (block.getType() == Material.RAILS || block.getType() == Material.SIGN_POST) { {
Block below = block.getFace(BlockFace.DOWN); storage.unprotectBlock(block);
EssentialsProtect.getStorage().unprotectBlock(below); if (type == Material.RAILS || type == Material.SIGN_POST)
} else { {
BlockFace[] faces = new BlockFace[] { final Block below = block.getFace(BlockFace.DOWN);
BlockFace.NORTH, storage.unprotectBlock(below);
BlockFace.EAST, }
BlockFace.SOUTH, else
BlockFace.WEST {
}; for (BlockFace blockFace : faces)
for (BlockFace blockFace : faces) { {
Block against = block.getFace(blockFace); final Block against = block.getFace(blockFace);
EssentialsProtect.getStorage().unprotectBlock(against); storage.unprotectBlock(against);
} }
} }
} }
else else
{ {
EssentialsProtect.getStorage().unprotectBlock(block); storage.unprotectBlock(block);
} }
} }
event.setCancelled(true); event.setCancelled(true);

View File

@ -1,11 +1,10 @@
package com.earth2me.essentials.protect; package com.earth2me.essentials.protect;
import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.EssentialsBlockListener; import com.earth2me.essentials.EssentialsBlockListener;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.Set;
import net.minecraft.server.ChunkPosition; import net.minecraft.server.ChunkPosition;
import net.minecraft.server.Packet60Explosion; import net.minecraft.server.Packet60Explosion;
import org.bukkit.Location; import org.bukkit.Location;
@ -18,7 +17,6 @@ import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Creeper; import org.bukkit.entity.Creeper;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Monster;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.entity.CreatureSpawnEvent; import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.EntityDamageByBlockEvent; import org.bukkit.event.entity.EntityDamageByBlockEvent;
@ -34,11 +32,13 @@ import org.bukkit.event.entity.EntityTargetEvent.TargetReason;
public class EssentialsProtectEntityListener extends EntityListener public class EssentialsProtectEntityListener extends EntityListener
{ {
private EssentialsProtect parent; private final transient IProtect prot;
private final transient IEssentials ess;
public EssentialsProtectEntityListener(EssentialsProtect parent) public EssentialsProtectEntityListener(final IProtect prot)
{ {
this.parent = parent; this.prot = prot;
this.ess = prot.getEssentials();
} }
@Override @Override
@ -48,30 +48,35 @@ public class EssentialsProtectEntityListener extends EntityListener
{ {
return; return;
} }
final Entity target = event.getEntity();
final User user = ess.getUser(target);
if (event instanceof EntityDamageByBlockEvent) if (event instanceof EntityDamageByBlockEvent)
{ {
DamageCause cause = event.getCause(); final DamageCause cause = event.getCause();
if (EssentialsProtect.playerSettings.get("protect.disable.contactdmg") && cause == DamageCause.CONTACT if (prot.getSettingBool(ProtectConfig.disable_contactdmg)
&& !(event.getEntity() instanceof Player && cause == DamageCause.CONTACT
&& Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.contact") && !(target instanceof Player
&& !Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.disable"))) && user.isAuthorized("essentials.protect.damage.contact")
&& !user.isAuthorized("essentials.protect.damage.disable")))
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
if (EssentialsProtect.playerSettings.get("protect.disable.lavadmg") && cause == DamageCause.LAVA if (prot.getSettingBool(ProtectConfig.disable_lavadmg)
&& !(event.getEntity() instanceof Player && cause == DamageCause.LAVA
&& Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.lava") && !(target instanceof Player
&& !Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.disable"))) && user.isAuthorized("essentials.protect.damage.lava")
&& !user.isAuthorized("essentials.protect.damage.disable")))
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
if (EssentialsProtect.guardSettings.get("protect.prevent.tnt-explosion") && cause == DamageCause.BLOCK_EXPLOSION if (prot.getSettingBool(ProtectConfig.prevent_tnt_explosion)
&& !(event.getEntity() instanceof Player && cause == DamageCause.BLOCK_EXPLOSION
&& Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.tnt") && !(target instanceof Player
&& !Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.disable"))) && user.isAuthorized("essentials.protect.damage.tnt")
&& !user.isAuthorized("essentials.protect.damage.disable")))
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
@ -80,97 +85,90 @@ public class EssentialsProtectEntityListener extends EntityListener
if (event instanceof EntityDamageByEntityEvent) if (event instanceof EntityDamageByEntityEvent)
{ {
EntityDamageByEntityEvent edEvent = (EntityDamageByEntityEvent)event; final EntityDamageByEntityEvent edEvent = (EntityDamageByEntityEvent)event;
Entity eAttack = edEvent.getDamager(); final Entity eAttack = edEvent.getDamager();
Entity eDefend = edEvent.getEntity(); final User attacker = ess.getUser(eAttack);
// PVP Settings // PVP Settings
if (eDefend instanceof Player && eAttack instanceof Player) if (target instanceof Player && eAttack instanceof Player
{ && prot.getSettingBool(ProtectConfig.disable_pvp)
if (EssentialsProtect.playerSettings.get("protect.disable.pvp")) && (!user.isAuthorized("essentials.protect.pvp") || !attacker.isAuthorized("essentials.protect.pvp")))
{
User defender = parent.ess.getUser(eDefend);
User attacker = parent.ess.getUser(eAttack);
if (!defender.isAuthorized("essentials.protect.pvp") || !attacker.isAuthorized("essentials.protect.pvp"))
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
}
}
//Creeper explode prevention //Creeper explode prevention
if (eAttack != null && eAttack instanceof Monster) if (eAttack instanceof Creeper && prot.getSettingBool(ProtectConfig.prevent_creeper_explosion)
{ && !(target instanceof Player
if (eAttack instanceof Creeper && EssentialsProtect.guardSettings.get("protect.prevent.creeper-explosion") && user.isAuthorized("essentials.protect.damage.creeper")
&& !(event.getEntity() instanceof Player && !user.isAuthorized("essentials.protect.damage.disable")))
&& Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.creeper")
&& !Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.disable")))
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
if (eAttack instanceof Creeper && EssentialsProtect.guardSettings.get("protect.prevent.creeper-playerdamage") if (eAttack instanceof Creeper && prot.getSettingBool(ProtectConfig.prevent_creeper_playerdmg)
&& !(event.getEntity() instanceof Player && !(target instanceof Player
&& Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.creeper") && user.isAuthorized("essentials.protect.damage.creeper")
&& !Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.disable"))) && !user.isAuthorized("essentials.protect.damage.disable")))
{
event.setCancelled(true);
return;
}
}
}
if (event instanceof EntityDamageByProjectileEvent)
{
if (event.getEntity() instanceof Player
&& EssentialsProtect.playerSettings.get("protect.disable.projectiles")
&& !Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.projectiles")
&& !Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.disable"))
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
} }
DamageCause cause = event.getCause(); if (event instanceof EntityDamageByProjectileEvent
Entity casualty = event.getEntity(); && target instanceof Player
if (casualty instanceof Player) && prot.getSettingBool(ProtectConfig.disable_projectiles)
{ && !(user.isAuthorized("essentials.protect.damage.projectiles")
if (EssentialsProtect.playerSettings.get("protect.disable.fall") && cause == DamageCause.FALL && !user.isAuthorized("essentials.protect.damage.disable")))
&& !(Essentials.getStatic().getUser(casualty).isAuthorized("essentials.protect.damage.fall")
&& !Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.disable")))
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
if (EssentialsProtect.playerSettings.get("protect.disable.suffocate") && cause == DamageCause.SUFFOCATION final DamageCause cause = event.getCause();
&& !(Essentials.getStatic().getUser(casualty).isAuthorized("essentials.protect.damage.suffocation") if (target instanceof Player)
&& !Essentials.getStatic().getUser(event.getEntity()).isAuthorized("essentials.protect.damage.disable"))) {
if (cause == DamageCause.FALL
&& prot.getSettingBool(ProtectConfig.disable_fall)
&& !(user.isAuthorized("essentials.protect.damage.fall")
&& !user.isAuthorized("essentials.protect.damage.disable")))
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
if (EssentialsProtect.playerSettings.get("protect.disable.firedmg") && (cause == DamageCause.FIRE
if (cause == DamageCause.SUFFOCATION
&& prot.getSettingBool(ProtectConfig.disable_suffocate)
&& !(user.isAuthorized("essentials.protect.damage.suffocation")
&& !user.isAuthorized("essentials.protect.damage.disable")))
{
event.setCancelled(true);
return;
}
if ((cause == DamageCause.FIRE
|| cause == DamageCause.FIRE_TICK) || cause == DamageCause.FIRE_TICK)
&& !(parent.ess.getUser(casualty).isAuthorized("essentials.protect.damage.fire") && prot.getSettingBool(ProtectConfig.disable_firedmg)
&& !parent.ess.getUser(event.getEntity()).isAuthorized("essentials.protect.damage.disable"))) && !(user.isAuthorized("essentials.protect.damage.fire")
&& !user.isAuthorized("essentials.protect.damage.disable")))
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
if (EssentialsProtect.playerSettings.get("protect.disable.drown") && cause == DamageCause.DROWNING if (cause == DamageCause.DROWNING
&& !(parent.ess.getUser(casualty).isAuthorized("essentials.protect.damage.drowning") && prot.getSettingBool(ProtectConfig.disable_drown)
&& !parent.ess.getUser(event.getEntity()).isAuthorized("essentials.protect.damage.disable"))) && !(user.isAuthorized("essentials.protect.damage.drowning")
&& !user.isAuthorized("essentials.protect.damage.disable")))
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
if (EssentialsProtect.playerSettings.get("protect.disable.lightning") && cause == DamageCause.LIGHTNING if (cause == DamageCause.LIGHTNING
&& !(parent.ess.getUser(casualty).isAuthorized("essentials.protect.damage.lightning") && prot.getSettingBool(ProtectConfig.disable_lightning)
&& !parent.ess.getUser(event.getEntity()).isAuthorized("essentials.protect.damage.disable"))) && !(user.isAuthorized("essentials.protect.damage.lightning")
&& !user.isAuthorized("essentials.protect.damage.disable")))
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
@ -185,17 +183,16 @@ public class EssentialsProtectEntityListener extends EntityListener
{ {
return; return;
} }
if (event.getEntity() instanceof LivingEntity) final int maxHeight = ess.getSettings().getProtectCreeperMaxHeight();
{
//Nicccccccccce plaaacccccccccce.. //Nicccccccccce plaaacccccccccce..
int maxHeight = parent.ess.getSettings().getEpCreeperMaxHeight(); if (event.getEntity() instanceof LivingEntity
if (EssentialsProtect.guardSettings.get("protect.prevent.creeper-explosion") && (prot.getSettingBool(ProtectConfig.prevent_creeper_explosion)
|| EssentialsProtect.guardSettings.get("protect.prevent.creeper-blockdamage") || prot.getSettingBool(ProtectConfig.prevent_creeper_blockdmg)
|| (maxHeight >= 0 && event.getLocation().getBlockY() > maxHeight)) || (maxHeight >= 0 && event.getLocation().getBlockY() > maxHeight)))
{ {
HashSet<ChunkPosition> set = new HashSet<ChunkPosition>(event.blockList().size()); final Set<ChunkPosition> set = new HashSet<ChunkPosition>(event.blockList().size());
Player[] players = parent.getServer().getOnlinePlayers(); final Player[] players = ess.getServer().getOnlinePlayers();
List<ChunkPosition> blocksUnderPlayers = new ArrayList<ChunkPosition>(players.length); final Set<ChunkPosition> blocksUnderPlayers = new HashSet<ChunkPosition>(players.length);
final Location loc = event.getLocation(); final Location loc = event.getLocation();
for (Player player : players) for (Player player : players)
{ {
@ -218,25 +215,24 @@ public class EssentialsProtectEntityListener extends EntityListener
} }
} }
((CraftServer)parent.getServer()).getHandle().a(loc.getX(), loc.getY(), loc.getZ(), 64.0D, ((CraftWorld)loc.getWorld()).getHandle().worldProvider.dimension, ((CraftServer)ess.getServer()).getHandle().a(loc.getX(), loc.getY(), loc.getZ(), 64.0D, ((CraftWorld)loc.getWorld()).getHandle().worldProvider.dimension,
new Packet60Explosion(loc.getX(), loc.getY(), loc.getZ(), 3.0f, set)); new Packet60Explosion(loc.getX(), loc.getY(), loc.getZ(), 3.0f, set));
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
} else if (!(event.getEntity() instanceof LivingEntity)
else && prot.getSettingBool(ProtectConfig.prevent_tnt_explosion))
{ //OH NOES TNT
if (EssentialsProtect.guardSettings.get("protect.prevent.tnt-explosion"))
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
}
// This code will prevent explosions near protected rails, signs or protected chests // This code will prevent explosions near protected rails, signs or protected chests
// TODO: Use protect db instead of this code // TODO: Use protect db instead of this code
for (Block block : event.blockList()) for (Block block : event.blockList())
{ {
if ((block.getType() == Material.RAILS || block.getFace(BlockFace.UP).getType() == Material.RAILS) && EssentialsProtect.genSettings.get("protect.protect.rails")) if ((block.getType() == Material.RAILS || block.getFace(BlockFace.UP).getType() == Material.RAILS)
&& prot.getSettingBool(ProtectConfig.protect_rails))
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
@ -248,7 +244,7 @@ public class EssentialsProtectEntityListener extends EntityListener
|| block.getFace(BlockFace.WEST).getType() == Material.WALL_SIGN || block.getFace(BlockFace.WEST).getType() == Material.WALL_SIGN
|| block.getType() == Material.SIGN_POST || block.getType() == Material.SIGN_POST
|| block.getFace(BlockFace.UP).getType() == Material.SIGN_POST) || block.getFace(BlockFace.UP).getType() == Material.SIGN_POST)
&& EssentialsProtect.genSettings.get("protect.protect.signs")) && prot.getSettingBool(ProtectConfig.protect_signs))
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
@ -263,7 +259,7 @@ public class EssentialsProtectEntityListener extends EntityListener
} }
@Override @Override
public void onCreatureSpawn(CreatureSpawnEvent event) public void onCreatureSpawn(final CreatureSpawnEvent event)
{ {
if (event.getEntity() instanceof CraftPlayer) if (event.getEntity() instanceof CraftPlayer)
{ {
@ -273,29 +269,29 @@ public class EssentialsProtectEntityListener extends EntityListener
{ {
return; return;
} }
String creatureName = event.getCreatureType().toString().toLowerCase(); final String creatureName = event.getCreatureType().toString().toLowerCase();
if (creatureName == null || creatureName.isEmpty()) if (creatureName == null || creatureName.isEmpty())
{ {
return; return;
} }
if (EssentialsProtect.guardSettings.get("protect.prevent.spawn." + creatureName)) if (ess.getSettings().getProtectPreventSpawn(creatureName))
{ {
event.setCancelled(true); event.setCancelled(true);
} }
} }
@Override @Override
public void onEntityTarget(EntityTargetEvent event) public void onEntityTarget(final EntityTargetEvent event)
{ {
if (!(event.getTarget() instanceof Player)) if (!(event.getTarget() instanceof Player))
{ {
return; return;
} }
User user = Essentials.getStatic().getUser(event.getTarget()); final User user = ess.getUser(event.getTarget());
if ((event.getReason() == TargetReason.CLOSEST_PLAYER if ((event.getReason() == TargetReason.CLOSEST_PLAYER
|| event.getReason() == TargetReason.TARGET_ATTACKED_ENTITY || event.getReason() == TargetReason.TARGET_ATTACKED_ENTITY
|| event.getReason() == TargetReason.PIG_ZOMBIE_TARGET) || event.getReason() == TargetReason.PIG_ZOMBIE_TARGET)
&& EssentialsProtect.guardSettings.get("protect.prevent.entitytarget") && prot.getSettingBool(ProtectConfig.prevent_entitytarget)
&& !user.isAuthorized("essentials.protect.entitytarget.bypass")) && !user.isAuthorized("essentials.protect.entitytarget.bypass"))
{ {
event.setCancelled(true); event.setCancelled(true);

View File

@ -1,39 +1,45 @@
package com.earth2me.essentials.protect; package com.earth2me.essentials.protect;
import com.earth2me.essentials.IEssentials;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerListener; import org.bukkit.event.player.PlayerListener;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import com.earth2me.essentials.Util; import com.earth2me.essentials.Util;
public class EssentialsProtectPlayerListener extends PlayerListener public class EssentialsProtectPlayerListener extends PlayerListener
{ {
private EssentialsProtect parent; private final transient IProtect prot;
private final transient IEssentials ess;
public EssentialsProtectPlayerListener(EssentialsProtect parent) public EssentialsProtectPlayerListener(final IProtect prot)
{ {
this.prot = prot;
this.parent = parent; this.ess = prot.getEssentials();
} }
@Override @Override
public void onPlayerInteract(PlayerInteractEvent event) public void onPlayerInteract(final PlayerInteractEvent event)
{ {
if (event.isCancelled()) return; if (event.isCancelled())
ItemStack item = event.getItem(); {
User user = parent.ess.getUser(event.getPlayer()); return;
Block blockClicked = event.getClickedBlock(); }
if (EssentialsProtect.playerSettings.get("protect.disable.build") && !user.canBuild()) final User user = ess.getUser(event.getPlayer());
if (prot.getSettingBool(ProtectConfig.disable_build) && !user.canBuild())
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
final ItemStack item = event.getItem();
if (item != null && EssentialsProtect.checkProtectionItems(EssentialsProtect.usageList, item.getTypeId()) && !user.isAuthorized("essentials.protect.exemptusage")) if (item != null
&& prot.checkProtectionItems(ProtectConfig.blacklist_usage, item.getTypeId())
&& !user.isAuthorized("essentials.protect.exemptusage"))
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
@ -41,26 +47,28 @@ public class EssentialsProtectPlayerListener extends PlayerListener
if (user.isAuthorized("essentials.protect.admin")) if (user.isAuthorized("essentials.protect.admin"))
{ {
StringBuilder sb = new StringBuilder(); final StringBuilder stringBuilder = new StringBuilder();
boolean first = true; boolean first = true;
for (String owner : EssentialsProtect.getStorage().getOwners(blockClicked)) final Block blockClicked = event.getClickedBlock();
for (String owner : prot.getStorage().getOwners(blockClicked))
{ {
if (!first) if (!first)
{ {
sb.append(", "); stringBuilder.append(", ");
} }
first = false; first = false;
sb.append(owner); stringBuilder.append(owner);
} }
String ownerNames = sb.toString(); final String ownerNames = stringBuilder.toString();
if (ownerNames != null && !ownerNames.isEmpty()) if (ownerNames != null && !ownerNames.isEmpty())
{ {
user.sendMessage(Util.format("protectionOwner", ownerNames)); user.sendMessage(Util.format("protectionOwner", ownerNames));
} }
} }
if (item != null && EssentialsProtect.onUseAlert.contains(String.valueOf(item.getTypeId()))) if (item != null
&& prot.checkProtectionItems(ProtectConfig.alert_on_use, item.getTypeId()))
{ {
parent.alert(user, item.getType().toString(), Util.i18n("alertUsed")); prot.alert(user, item.getType().toString(), Util.i18n("alertUsed"));
} }
} }
} }

View File

@ -1,5 +0,0 @@
package com.earth2me.essentials.protect;
public class EssentialsProtectRegions {
}

View File

@ -8,53 +8,43 @@ import org.bukkit.event.weather.WeatherListener;
public class EssentialsProtectWeatherListener extends WeatherListener public class EssentialsProtectWeatherListener extends WeatherListener
{ {
private EssentialsProtect parent; private final transient IProtect prot;
public EssentialsProtectWeatherListener(EssentialsProtect parent) public EssentialsProtectWeatherListener(final IProtect prot)
{ {
this.parent = parent; this.prot = prot;
} }
@Override @Override
public void onWeatherChange(WeatherChangeEvent event) public void onWeatherChange(final WeatherChangeEvent event)
{ {
if (event.isCancelled()) if (!event.isCancelled()
{ && prot.getSettingBool(ProtectConfig.disable_weather_storm)
return; && event.toWeatherState())
}
if (EssentialsProtect.playerSettings.get("protect.disable.weather.storm") && event.toWeatherState())
{ {
event.setCancelled(true); event.setCancelled(true);
return;
} }
} }
@Override @Override
public void onLightningStrike(LightningStrikeEvent event) public void onLightningStrike(final LightningStrikeEvent event)
{ {
if (event.isCancelled()) if (!event.isCancelled()
{ && prot.getSettingBool(ProtectConfig.disable_weather_lightning))
return;
}
if (EssentialsProtect.playerSettings.get("protect.disable.weather.lightning"))
{ {
event.setCancelled(true); event.setCancelled(true);
return;
} }
} }
@Override @Override
public void onThunderChange(ThunderChangeEvent event) public void onThunderChange(final ThunderChangeEvent event)
{ {
if (event.isCancelled()) if (!event.isCancelled()
{ && prot.getSettingBool(ProtectConfig.disable_weather_thunder)
return; && event.toThunderState())
}
if (EssentialsProtect.playerSettings.get("protect.disable.weather.thunder") && event.toThunderState())
{ {
event.setCancelled(true); event.setCancelled(true);
return;
} }
} }
} }

View File

@ -0,0 +1,21 @@
package com.earth2me.essentials.protect;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.User;
import com.earth2me.essentials.protect.data.IProtectedBlock;
public interface IProtect
{
void alert(final User user, final String item, final String type);
boolean checkProtectionItems(final ProtectConfig list, final int id);
boolean getSettingBool(final ProtectConfig protectConfig);
String getSettingString(final ProtectConfig protectConfig);
IProtectedBlock getStorage();
IEssentials getEssentials();
}

View File

@ -0,0 +1,111 @@
package com.earth2me.essentials.protect;
public enum ProtectConfig
{
datatype("protect.datatype", "sqlite"),
mysqlDB("protect.mysqlDb", "jdbc:mysql://localhost:3306/minecraft"),
dbUsername("protect.username", "root"),
dbPassword("protect.password", ""),
memstore("protect.memstore", false),
disable_contactdmg("protect.disable.contactdmg", false),
disable_lavadmg("protect.disable.lavadmg", false),
disable_build("protect.disable.build", false),
disable_pvp("protect.disable.pvp", false),
disable_projectiles("protect.disable.projectiles", false),
disable_fall("protect.disable.fall", false),
disable_suffocate("protect.disable.suffocate", false),
disable_firedmg("protect.disable.firedmg", false),
disable_lightning("protect.disable.lightning", false),
disable_drown("protect.disable.drown", false),
disable_weather_storm("protect.disable.weather.storm", false),
disable_weather_lightning("protect.disable.weather.lightning", false),
disable_weather_thunder("protect.disable.weather.thunder", false),
prevent_fire_spread("protect.prevent.fire-spread", true),
prevent_flint_fire("protect.prevent.flint-fire", false),
prevent_lava_fire_spread("protect.prevent.lava-fire-spread", true),
prevent_lightning_fire_spread("protect.prevent.lightning-fire-spread", true),
prevent_water_flow("protect.prevent.water-flow", false),
prevent_lava_flow("protect.prevent.lava-flow", false),
prevent_water_bucket_flow("protect.prevent.water-bucket-flow", false),
prevent_portal_creation("protect.prevent.portal-creation", false),
prevent_block_on_rail("protect.protect.prevent-block-on-rails", false),
prevent_tnt_explosion("protect.prevent.tnt-explosion", false),
prevent_creeper_explosion("protect.prevent.creeper-explosion", true),
prevent_creeper_playerdmg("protect.prevent.creeper-playerdamage", false),
prevent_creeper_blockdmg("protect.prevent.creeper-blockdamage", false),
prevent_entitytarget("protect.prevent.entitytarget", false),
protect_rails("protect.protect.rails", true),
protect_below_rails("protect.protect.block-below", true),
protect_signs("protect.protect.signs", true),
protect_against_signs("protect.protect.block-below", true),
alert_on_placement("protect.alert.on-placement"),
alert_on_use("protect.alert.on-use"),
alert_on_break("protect.alert.on-break"),
blacklist_placement("protect.blacklist.placement"),
blacklist_usage("protect.blacklist.usage"),
blacklist_break("protect.blacklist.break");
private final String configName;
private final String defValueString;
private final boolean defValueBoolean;
private final boolean isList;
private final boolean isString;
private ProtectConfig(final String configName)
{
this(configName, null, false, true, false);
}
private ProtectConfig(final String configName, final String defValueString)
{
this(configName, defValueString, false, false, true);
}
private ProtectConfig(final String configName, final boolean defValueBoolean)
{
this(configName, null, defValueBoolean, false, false);
}
private ProtectConfig(final String configName, final String defValueString, final boolean defValueBoolean, final boolean isList, final boolean isString)
{
this.configName = configName;
this.defValueString = defValueString;
this.defValueBoolean = defValueBoolean;
this.isList = isList;
this.isString = isString;
}
/**
* @return the configName
*/
public String getConfigName()
{
return configName;
}
/**
* @return the default value String
*/
public String getDefaultValueString()
{
return defValueString;
}
/**
* @return the default value boolean
*/
public boolean getDefaultValueBoolean()
{
return defValueBoolean;
}
public boolean isString()
{
return isString;
}
public boolean isList()
{
return isList;
}
}

View File

@ -1,7 +1,6 @@
package com.earth2me.essentials.protect.data; package com.earth2me.essentials.protect.data;
import java.util.List; import java.util.List;
import java.util.Set;
import org.bukkit.block.Block; import org.bukkit.block.Block;
public interface IProtectedBlock { public interface IProtectedBlock {

View File

@ -1,9 +1,18 @@
package com.earth2me.essentials.protect.data; package com.earth2me.essentials.protect.data;
public class OwnedBlock { public class OwnedBlock {
int x; final int x;
int y; final int y;
int z; final int z;
String world; final String world;
String playerName; final String playerName;
public OwnedBlock(int x, int y, int z, String world, String playerName)
{
this.x = x;
this.y = y;
this.z = z;
this.world = world;
this.playerName = playerName;
}
} }

View File

@ -12,28 +12,40 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.bukkit.block.Block; import org.bukkit.block.Block;
public abstract class ProtectedBlockJDBC implements IProtectedBlock {
protected static final Logger logger = Logger.getLogger("Minecraft"); public abstract class ProtectedBlockJDBC implements IProtectedBlock
protected ComboPooledDataSource cpds; {
protected static final Logger LOGGER = Logger.getLogger("Minecraft");
protected final transient ComboPooledDataSource cpds;
protected abstract PreparedStatement getStatementCreateTable(Connection conn) throws SQLException; protected abstract PreparedStatement getStatementCreateTable(Connection conn) throws SQLException;
protected abstract PreparedStatement getStatementUpdateFrom2_0Table(Connection conn) throws SQLException; protected abstract PreparedStatement getStatementUpdateFrom2_0Table(Connection conn) throws SQLException;
protected abstract PreparedStatement getStatementDeleteAll(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 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 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 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 getStatementDeleteByLocation(Connection conn, String world, int x, int y, int z) throws SQLException;
protected abstract PreparedStatement getStatementAllBlocks(Connection conn) throws SQLException; protected abstract PreparedStatement getStatementAllBlocks(Connection conn) throws SQLException;
public ProtectedBlockJDBC(String driver, String url) throws PropertyVetoException { public ProtectedBlockJDBC(String driver, String url) throws PropertyVetoException
{
this(driver, url, null, null); this(driver, url, null, null);
} }
public ProtectedBlockJDBC(String driver, String url, String username, String password) throws PropertyVetoException { public ProtectedBlockJDBC(String driver, String url, String username, String password) throws PropertyVetoException
{
cpds = new ComboPooledDataSource(); cpds = new ComboPooledDataSource();
cpds.setDriverClass(driver); cpds.setDriverClass(driver);
cpds.setJdbcUrl(url); cpds.setJdbcUrl(url);
if (username != null) { if (username != null)
{
cpds.setUser(username); cpds.setUser(username);
cpds.setPassword(password); cpds.setPassword(password);
} }
@ -41,256 +53,370 @@ public abstract class ProtectedBlockJDBC implements IProtectedBlock {
createAndConvertTable(); createAndConvertTable();
} }
private void createAndConvertTable()
private void createAndConvertTable() { {
Connection conn = null; Connection conn = null;
PreparedStatement ps = null; PreparedStatement ps = null;
try { try
{
conn = cpds.getConnection(); conn = cpds.getConnection();
ps = getStatementCreateTable(conn); ps = getStatementCreateTable(conn);
ps.execute(); ps.execute();
ps.close(); ps.close();
ps = getStatementUpdateFrom2_0Table(conn); ps = getStatementUpdateFrom2_0Table(conn);
ps.execute(); ps.execute();
} catch (SQLException ex) { }
logger.log(Level.SEVERE, null, ex); catch (SQLException ex)
} finally { {
if (ps != null) { LOGGER.log(Level.SEVERE, null, ex);
try { }
finally
{
if (ps != null)
{
try
{
ps.close(); ps.close();
} catch (SQLException ex) { }
logger.log(Level.SEVERE, null, ex); catch (SQLException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
} }
} }
if (conn != null) { if (conn != null)
try { {
try
{
conn.close(); conn.close();
} catch (SQLException ex) { }
logger.log(Level.SEVERE, null, ex); catch (SQLException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
} }
} }
} }
} }
public void clearProtections() { public void clearProtections()
{
Connection conn = null; Connection conn = null;
PreparedStatement ps = null; PreparedStatement ps = null;
try { try
{
conn = cpds.getConnection(); conn = cpds.getConnection();
ps = getStatementDeleteAll(conn); ps = getStatementDeleteAll(conn);
ps.executeUpdate(); ps.executeUpdate();
} catch (SQLException ex) { }
logger.log(Level.SEVERE, null, ex); catch (SQLException ex)
} finally { {
if (ps != null) { LOGGER.log(Level.SEVERE, null, ex);
try { }
finally
{
if (ps != null)
{
try
{
ps.close(); ps.close();
} catch (SQLException ex) { }
logger.log(Level.SEVERE, null, ex); catch (SQLException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
} }
} }
if (conn != null) { if (conn != null)
try { {
try
{
conn.close(); conn.close();
} catch (SQLException ex) { }
logger.log(Level.SEVERE, null, ex); catch (SQLException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
} }
} }
} }
} }
public void importProtections(List<OwnedBlock> blocks) { public void importProtections(List<OwnedBlock> blocks)
for (OwnedBlock ownedBlock : blocks) { {
if (ownedBlock.playerName == null) { for (OwnedBlock ownedBlock : blocks)
{
if (ownedBlock.playerName == null)
{
continue; continue;
} }
protectBlock(ownedBlock.world, ownedBlock.x, ownedBlock.y, ownedBlock.z, ownedBlock.playerName); protectBlock(ownedBlock.world, ownedBlock.x, ownedBlock.y, ownedBlock.z, ownedBlock.playerName);
} }
} }
public List<OwnedBlock> exportProtections() { public List<OwnedBlock> exportProtections()
{
Connection conn = null; Connection conn = null;
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = null; ResultSet rs = null;
List<OwnedBlock> blocks = new ArrayList<OwnedBlock>(); List<OwnedBlock> blocks = new ArrayList<OwnedBlock>();
try { try
{
conn = cpds.getConnection(); conn = cpds.getConnection();
ps = getStatementAllBlocks(conn); ps = getStatementAllBlocks(conn);
rs = ps.executeQuery(); rs = ps.executeQuery();
while (rs.next()) { while (rs.next())
OwnedBlock ob = new OwnedBlock(); {
ob.world = rs.getString(1); OwnedBlock ob = new OwnedBlock(
ob.x = rs.getInt(2); rs.getInt(2),
ob.y = rs.getInt(3); rs.getInt(3),
ob.z = rs.getInt(4); rs.getInt(4),
ob.playerName = rs.getString(5); rs.getString(1),
rs.getString(5));
blocks.add(ob); blocks.add(ob);
} }
return blocks; return blocks;
} catch (SQLException ex) { }
logger.log(Level.SEVERE, null, ex); catch (SQLException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
return blocks; return blocks;
} finally { }
if (rs != null) { finally
try { {
if (rs != null)
{
try
{
rs.close(); rs.close();
} catch (SQLException ex) { }
logger.log(Level.SEVERE, null, ex); catch (SQLException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
} }
} }
if (ps != null) { if (ps != null)
try { {
try
{
ps.close(); ps.close();
} catch (SQLException ex) { }
logger.log(Level.SEVERE, null, ex); catch (SQLException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
} }
} }
if (conn != null) { if (conn != null)
try { {
try
{
conn.close(); conn.close();
} catch (SQLException ex) { }
logger.log(Level.SEVERE, null, ex); catch (SQLException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
} }
} }
} }
} }
public void protectBlock(Block block, String playerName) { public void protectBlock(Block block, String playerName)
{
protectBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), playerName); protectBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), playerName);
} }
private void protectBlock(String world, int x, int y, int z, String playerName) { private void protectBlock(String world, int x, int y, int z, String playerName)
{
Connection conn = null; Connection conn = null;
PreparedStatement ps = null; PreparedStatement ps = null;
try { try
{
conn = cpds.getConnection(); conn = cpds.getConnection();
ps = getStatementInsert(conn, world, x, y, z, playerName); ps = getStatementInsert(conn, world, x, y, z, playerName);
ps.executeUpdate(); ps.executeUpdate();
} catch (SQLException ex) { }
logger.log(Level.SEVERE, null, ex); catch (SQLException ex)
} finally { {
if (ps != null) { LOGGER.log(Level.SEVERE, null, ex);
try { }
finally
{
if (ps != null)
{
try
{
ps.close(); ps.close();
} catch (SQLException ex) { }
logger.log(Level.SEVERE, null, ex); catch (SQLException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
} }
} }
if (conn != null) { if (conn != null)
try { {
try
{
conn.close(); conn.close();
} catch (SQLException ex) { }
logger.log(Level.SEVERE, null, ex); catch (SQLException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
} }
} }
} }
} }
public boolean isProtected(Block block, String playerName) { public boolean isProtected(Block block, String playerName)
{
Connection conn = null; Connection conn = null;
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = null; ResultSet rs = null;
try { try
{
conn = cpds.getConnection(); conn = cpds.getConnection();
ps = getStatementPlayerCountByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), playerName); ps = getStatementPlayerCountByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), playerName);
rs = ps.executeQuery(); rs = ps.executeQuery();
rs.next(); rs.next();
return rs.getInt(1) > 0 && rs.getInt(2) == 0; return rs.getInt(1) > 0 && rs.getInt(2) == 0;
} catch (SQLException ex) { }
logger.log(Level.SEVERE, null, ex); catch (SQLException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
return true; return true;
} finally { }
if (rs != null) { finally
try { {
if (rs != null)
{
try
{
rs.close(); rs.close();
} catch (SQLException ex) { }
logger.log(Level.SEVERE, null, ex); catch (SQLException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
} }
} }
if (ps != null) { if (ps != null)
try { {
try
{
ps.close(); ps.close();
} catch (SQLException ex) { }
logger.log(Level.SEVERE, null, ex); catch (SQLException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
} }
} }
if (conn != null) { if (conn != null)
try { {
try
{
conn.close(); conn.close();
} catch (SQLException ex) { }
logger.log(Level.SEVERE, null, ex); catch (SQLException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
} }
} }
} }
} }
public List<String> getOwners(Block block) { public List<String> getOwners(Block block)
{
Connection conn = null; Connection conn = null;
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = null; ResultSet rs = null;
List<String> owners = new ArrayList<String>(); List<String> owners = new ArrayList<String>();
try { try
{
conn = cpds.getConnection(); conn = cpds.getConnection();
ps = getStatementPlayersByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ()); ps = getStatementPlayersByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ());
rs = ps.executeQuery(); rs = ps.executeQuery();
while (rs.next()) { while (rs.next())
{
owners.add(rs.getString(1)); owners.add(rs.getString(1));
} }
return owners; return owners;
} catch (SQLException ex) { }
logger.log(Level.SEVERE, null, ex); catch (SQLException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
return owners; return owners;
} finally { }
if (rs != null) { finally
try { {
if (rs != null)
{
try
{
rs.close(); rs.close();
} catch (SQLException ex) { }
logger.log(Level.SEVERE, null, ex); catch (SQLException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
} }
} }
if (ps != null) { if (ps != null)
try { {
try
{
ps.close(); ps.close();
} catch (SQLException ex) { }
logger.log(Level.SEVERE, null, ex); catch (SQLException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
} }
} }
if (conn != null) { if (conn != null)
try { {
try
{
conn.close(); conn.close();
} catch (SQLException ex) { }
logger.log(Level.SEVERE, null, ex); catch (SQLException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
} }
} }
} }
} }
public int unprotectBlock(Block block) { public int unprotectBlock(Block block)
{
Connection conn = null; Connection conn = null;
PreparedStatement ps = null; PreparedStatement ps = null;
try { try
{
conn = cpds.getConnection(); conn = cpds.getConnection();
ps = getStatementDeleteByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ()); ps = getStatementDeleteByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ());
return ps.executeUpdate(); return ps.executeUpdate();
} catch (SQLException ex) { }
logger.log(Level.SEVERE, null, ex); catch (SQLException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
return 0; return 0;
} finally { }
if (ps != null) { finally
try { {
if (ps != null)
{
try
{
ps.close(); ps.close();
} catch (SQLException ex) { }
logger.log(Level.SEVERE, null, ex); catch (SQLException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
} }
} }
if (conn != null) { if (conn != null)
try { {
try
{
conn.close(); conn.close();
} catch (SQLException ex) { }
logger.log(Level.SEVERE, null, ex); catch (SQLException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
} }
} }
} }
} }
} }

View File

@ -4,34 +4,39 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
public class ProtectedBlockMemory implements IProtectedBlock {
List<String> worlds = new ArrayList<String>(); public class ProtectedBlockMemory implements IProtectedBlock
List<String> playerNames = new ArrayList<String>(); {
IProtectedBlock storage; private final transient List<String> worlds = new ArrayList<String>();
Plugin plugin; private final transient List<String> playerNames = new ArrayList<String>();
private final transient IProtectedBlock storage;
private final transient Plugin plugin;
class ProtectedLocation {
int x; static class ProtectedLocation
int y; {
int z; private final transient int x;
int w; private final transient int y;
private final transient int z;
private final transient int w;
private ProtectedLocation(Block block, int worldId) { public ProtectedLocation(final Block block, final int worldId)
{
this.x = block.getX(); this.x = block.getX();
this.y = block.getY(); this.y = block.getY();
this.z = block.getZ(); this.z = block.getZ();
this.w = worldId; this.w = worldId;
} }
private ProtectedLocation(OwnedBlock ownedBlock, int worldId) { public ProtectedLocation(final OwnedBlock ownedBlock, final int worldId)
{
this.x = ownedBlock.x; this.x = ownedBlock.x;
this.y = ownedBlock.y; this.y = ownedBlock.y;
this.z = ownedBlock.z; this.z = ownedBlock.z;
@ -39,33 +44,39 @@ public class ProtectedBlockMemory implements IProtectedBlock {
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(final Object object)
if (o instanceof ProtectedLocation) { {
ProtectedLocation pl = (ProtectedLocation) o; if (object instanceof ProtectedLocation)
return x == pl.x && y == pl.y && z == pl.z && w == pl.w; {
final ProtectedLocation pLoc = (ProtectedLocation)object;
return x == pLoc.x && y == pLoc.y && z == pLoc.z && w == pLoc.w;
} }
return false; return false;
} }
@Override @Override
public int hashCode() { public int hashCode()
{
return x ^ y ^ z ^ w; return x ^ y ^ z ^ w;
} }
} }
class ProtectedBy {
private int playerId = -1; static class ProtectedBy
private Set<Integer> playerIds; {
private transient int playerId = -1;
private transient Set<Integer> playerIds;
private ProtectedBy() { public void add(final int playerId)
} {
if (this.playerId == -1 || this.playerId == playerId)
private void add(int playerId) { {
if (this.playerId == -1 || this.playerId == playerId) {
this.playerId = playerId; this.playerId = playerId;
} else { }
if (playerIds == null) { else
{
if (playerIds == null)
{
playerIds = new HashSet<Integer>(4); playerIds = new HashSet<Integer>(4);
playerIds.add(this.playerId); playerIds.add(this.playerId);
} }
@ -73,130 +84,162 @@ public class ProtectedBlockMemory implements IProtectedBlock {
} }
} }
private boolean contains(int playerId) { public boolean contains(final int playerId)
if (playerIds == null) { {
if (playerIds == null)
{
return this.playerId == playerId; return this.playerId == playerId;
} }
return playerIds.contains(playerId); return playerIds.contains(playerId);
} }
private List<String> getPlayers(List<String> playerNames) { public List<String> getPlayers(final List<String> playerNames)
if (playerIds == null) { {
List<String> list = new ArrayList<String>(2); final List<String> list = new ArrayList<String>(2);
if (playerIds == null)
{
list.add(playerNames.get(playerId)); list.add(playerNames.get(playerId));
return list;
} }
List<String> list = new ArrayList<String>(playerIds.size()); else
for (Integer integer : playerIds) { {
for (Integer integer : playerIds)
{
list.add(playerNames.get(integer)); list.add(playerNames.get(integer));
} }
}
return list; return list;
} }
private int size() { public int size()
if (playerIds == null) { {
if (playerIds == null)
{
return 1; return 1;
} }
return playerIds.size(); return playerIds.size();
} }
} }
HashMap<ProtectedLocation, ProtectedBy> blocks = new HashMap<ProtectedLocation, ProtectedBy>(); private final transient Map<ProtectedLocation, ProtectedBy> blocks = new HashMap<ProtectedLocation, ProtectedBy>();
public ProtectedBlockMemory(IProtectedBlock storage) { public ProtectedBlockMemory(final IProtectedBlock storage, final Plugin plugin)
{
this.storage = storage; this.storage = storage;
this.plugin = plugin;
importProtections(storage.exportProtections()); importProtections(storage.exportProtections());
} }
public void clearProtections() { public void clearProtections()
{
blocks.clear(); blocks.clear();
} }
public final void importProtections(List<OwnedBlock> blocks) { public final void importProtections(final List<OwnedBlock> blocks)
for (OwnedBlock ownedBlock : blocks) { {
ProtectedLocation pl = new ProtectedLocation(ownedBlock, getWorldId(ownedBlock.world)); for (OwnedBlock ownedBlock : blocks)
if (ownedBlock.playerName == null) { {
final ProtectedLocation pl = new ProtectedLocation(ownedBlock, getWorldId(ownedBlock.world));
if (ownedBlock.playerName == null)
{
continue; continue;
} }
protectBlock(pl, ownedBlock.playerName); protectBlock(pl, ownedBlock.playerName);
} }
} }
public List<OwnedBlock> exportProtections() { public List<OwnedBlock> exportProtections()
List<OwnedBlock> blockList = new ArrayList<OwnedBlock>(blocks.size()); {
for (Entry<ProtectedLocation, ProtectedBy> entry : blocks.entrySet()) { final List<OwnedBlock> blockList = new ArrayList<OwnedBlock>(blocks.size());
for (String name : entry.getValue().getPlayers(playerNames)) { for (Entry<ProtectedLocation, ProtectedBy> entry : blocks.entrySet())
OwnedBlock ob = new OwnedBlock(); {
ob.x = entry.getKey().x; for (String name : entry.getValue().getPlayers(playerNames))
ob.y = entry.getKey().y; {
ob.z = entry.getKey().z; final OwnedBlock ob = new OwnedBlock(
ob.world = worlds.get(entry.getKey().w); entry.getKey().x,
ob.playerName = name; entry.getKey().y,
entry.getKey().z,
worlds.get(entry.getKey().w),
name);
blockList.add(ob); blockList.add(ob);
} }
} }
return blockList; return blockList;
} }
public void protectBlock(final Block block, final String playerName) { public void protectBlock(final Block block, final String playerName)
ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld())); {
final ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld()));
protectBlock(pl, playerName); protectBlock(pl, playerName);
plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() { plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable()
public void run() { {
public void run()
{
storage.protectBlock(block, playerName); storage.protectBlock(block, playerName);
} }
}); });
} }
private void protectBlock(ProtectedLocation pl, String playerName) { private final void protectBlock(ProtectedLocation pl, String playerName)
{
int playerId = getPlayerId(playerName); int playerId = getPlayerId(playerName);
ProtectedBy pb = blocks.get(pl); ProtectedBy pb = blocks.get(pl);
if (pb == null) { if (pb == null)
{
pb = new ProtectedBy(); pb = new ProtectedBy();
blocks.put(pl, pb); blocks.put(pl, pb);
} }
pb.add(playerId); pb.add(playerId);
} }
public boolean isProtected(Block block, String playerName) { public boolean isProtected(Block block, String playerName)
{
int playerId = getPlayerId(playerName); int playerId = getPlayerId(playerName);
ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld())); ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld()));
ProtectedBy pb = blocks.get(pl); ProtectedBy pb = blocks.get(pl);
return !pb.contains(playerId); return !pb.contains(playerId);
} }
public List<String> getOwners(Block block) { public List<String> getOwners(Block block)
{
ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld())); ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld()));
ProtectedBy pb = blocks.get(pl); ProtectedBy pb = blocks.get(pl);
return pb.getPlayers(playerNames); return pb.getPlayers(playerNames);
} }
public int unprotectBlock(final Block block) { public int unprotectBlock(final Block block)
{
ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld())); ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld()));
ProtectedBy pb = blocks.remove(pl); ProtectedBy pb = blocks.remove(pl);
plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() { plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable()
public void run() { {
public void run()
{
storage.unprotectBlock(block); storage.unprotectBlock(block);
} }
}); });
return pb.size(); return pb.size();
} }
private int getPlayerId(String playername) { private int getPlayerId(String playername)
{
int id = playerNames.indexOf(playername); int id = playerNames.indexOf(playername);
if (id < 0) { if (id < 0)
{
playerNames.add(playername); playerNames.add(playername);
id = playerNames.indexOf(playername); id = playerNames.indexOf(playername);
} }
return id; return id;
} }
private int getWorldId(World world) { private int getWorldId(World world)
{
return getWorldId(world.getName()); return getWorldId(world.getName());
} }
private int getWorldId(String name) { private int getWorldId(String name)
{
int id = worlds.indexOf(name); int id = worlds.indexOf(name);
if (id < 0) { if (id < 0)
{
worlds.add(name); worlds.add(name);
id = worlds.indexOf(name); id = worlds.indexOf(name);
} }

View File

@ -5,6 +5,8 @@ import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ProtectedBlockMySQL extends ProtectedBlockJDBC { public class ProtectedBlockMySQL extends ProtectedBlockJDBC {
@ -50,11 +52,25 @@ public class ProtectedBlockMySQL extends ProtectedBlockJDBC {
} }
} finally { } finally {
if (testRS != null) { if (testRS != null) {
try
{
testRS.close(); testRS.close();
} }
catch (SQLException ex)
{
Logger.getLogger(ProtectedBlockMySQL.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (testPS != null) { if (testPS != null) {
try
{
testPS.close(); testPS.close();
} }
catch (SQLException ex)
{
Logger.getLogger(ProtectedBlockMySQL.class.getName()).log(Level.SEVERE, null, ex);
}
}
} }
} }
private static final String QueryDeleteAll = "DELETE FROM EssentialsProtect;"; private static final String QueryDeleteAll = "DELETE FROM EssentialsProtect;";