1
0
mirror of https://github.com/essentials/Essentials.git synced 2025-08-21 05:51:56 +02:00

Fairly expansive cleanup of ess protect.

This commit is contained in:
md_5
2012-06-27 20:23:06 +10:00
parent 7bef2d3817
commit 4561a7f010
14 changed files with 216 additions and 238 deletions

View File

@@ -8,23 +8,27 @@ import java.util.Map;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.permissions.PermissionDefault; import org.bukkit.permissions.PermissionDefault;
public class BlockBreakPermissions extends AbstractSuperpermsPermission{
public final class BlockBreakPermissions extends AbstractSuperpermsPermission
{
private static Map<Material, IPermission> permissions = new EnumMap<Material, IPermission>(Material.class); private static Map<Material, IPermission> permissions = new EnumMap<Material, IPermission>(Material.class);
private static final String base = "essentials.protect.blockbreak."; private static final String base = "essentials.protect.blockbreak.";
private final String permission; private final String permission;
public static IPermission getPermission(Material mat) public static IPermission getPermission(final Material mat)
{ {
IPermission perm = permissions.get(mat); IPermission perm = permissions.get(mat);
if (perm == null) { if (perm == null)
{
perm = new BlockBreakPermissions(mat.toString().toLowerCase(Locale.ENGLISH)); perm = new BlockBreakPermissions(mat.toString().toLowerCase(Locale.ENGLISH));
permissions.put(mat, perm); permissions.put(mat, perm);
} }
return perm; return perm;
} }
private BlockBreakPermissions(String matName) private BlockBreakPermissions(final String matName)
{ {
super();
this.permission = base + matName; this.permission = base + matName;
} }
@@ -40,4 +44,3 @@ public class BlockBreakPermissions extends AbstractSuperpermsPermission{
return PermissionDefault.TRUE; return PermissionDefault.TRUE;
} }
} }

View File

@@ -8,23 +8,27 @@ import java.util.Map;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.permissions.PermissionDefault; import org.bukkit.permissions.PermissionDefault;
public class BlockPlacePermissions extends AbstractSuperpermsPermission{
public class BlockPlacePermissions extends AbstractSuperpermsPermission
{
private static Map<Material, IPermission> permissions = new EnumMap<Material, IPermission>(Material.class); private static Map<Material, IPermission> permissions = new EnumMap<Material, IPermission>(Material.class);
private static final String base = "essentials.protect.blockplace."; private static final String base = "essentials.protect.blockplace.";
private final String permission; private final String permission;
public static IPermission getPermission(Material mat) public static IPermission getPermission(final Material mat)
{ {
IPermission perm = permissions.get(mat); IPermission perm = permissions.get(mat);
if (perm == null) { if (perm == null)
{
perm = new BlockPlacePermissions(mat.toString().toLowerCase(Locale.ENGLISH)); perm = new BlockPlacePermissions(mat.toString().toLowerCase(Locale.ENGLISH));
permissions.put(mat, perm); permissions.put(mat, perm);
} }
return perm; return perm;
} }
private BlockPlacePermissions(String matName) private BlockPlacePermissions(final String matName)
{ {
super();
this.permission = base + matName; this.permission = base + matName;
} }

View File

@@ -20,7 +20,7 @@ public class EssentialsConnect
private final transient IEssentials ess; private final transient IEssentials ess;
private final transient IProtect protect; private final transient IProtect protect;
public EssentialsConnect(Plugin essPlugin, Plugin essProtect) public EssentialsConnect(final Plugin essPlugin, final Plugin essProtect)
{ {
if (!essProtect.getDescription().getVersion().equals(essPlugin.getDescription().getVersion())) if (!essProtect.getDescription().getVersion().equals(essPlugin.getDescription().getVersion()))
{ {
@@ -29,15 +29,11 @@ public class EssentialsConnect
ess = (IEssentials)essPlugin; ess = (IEssentials)essPlugin;
protect = (IProtect)essProtect; protect = (IProtect)essProtect;
protect.setSettings(new ProtectHolder(ess)); protect.setSettings(new ProtectHolder(ess));
ProtectReloader pr = new ProtectReloader(); final ProtectReloader pr = new ProtectReloader();
pr.onReload(); pr.onReload();
ess.addReloadListener(pr); ess.addReloadListener(pr);
} }
public void onDisable()
{
}
public IEssentials getEssentials() public IEssentials getEssentials()
{ {
return ess; return ess;
@@ -84,7 +80,7 @@ public class EssentialsConnect
* } * }
*/ */
ProtectHolder settings = protect.getSettings(); final ProtectHolder settings = protect.getSettings();
settings.acquireReadLock(); settings.acquireReadLock();
try try
{ {

View File

@@ -29,13 +29,14 @@ public class EssentialsProtect extends JavaPlugin implements IProtect
C3P0logger = com.mchange.v2.log.MLog.getLogger(com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.class); C3P0logger = com.mchange.v2.log.MLog.getLogger(com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.class);
C3P0logger.setFilter(new Filter() C3P0logger.setFilter(new Filter()
{ {
public boolean isLoggable(LogRecord lr) public boolean isLoggable(final LogRecord lr)
{ {
return lr.getLevel() != Level.INFO; return lr.getLevel() != Level.INFO;
} }
}); });
} }
@Override
public void onEnable() public void onEnable()
{ {
final PluginManager pm = this.getServer().getPluginManager(); final PluginManager pm = this.getServer().getPluginManager();
@@ -78,7 +79,6 @@ public class EssentialsProtect extends JavaPlugin implements IProtect
final List<Integer> itemList = settingsList.get(list); final List<Integer> itemList = settingsList.get(list);
return itemList != null && !itemList.isEmpty() && itemList.contains(id); return itemList != null && !itemList.isEmpty() && itemList.contains(id);
}*/ }*/
@Override @Override
public IProtectedBlock getStorage() public IProtectedBlock getStorage()
{ {
@@ -86,11 +86,12 @@ public class EssentialsProtect extends JavaPlugin implements IProtect
} }
@Override @Override
public void setStorage(IProtectedBlock pb) public void setStorage(final IProtectedBlock pb)
{ {
storage = pb; storage = pb;
} }
@Override
public EssentialsConnect getEssentialsConnect() public EssentialsConnect getEssentialsConnect()
{ {
return ess; return ess;
@@ -124,7 +125,7 @@ public class EssentialsProtect extends JavaPlugin implements IProtect
final String str = settingsString.get(protectConfig); final String str = settingsString.get(protectConfig);
return str == null ? protectConfig.getDefaultValueString() : str; return str == null ? protectConfig.getDefaultValueString() : str;
}*/ }*/
@Override
public void onDisable() public void onDisable()
{ {
if (storage != null) if (storage != null)
@@ -141,11 +142,13 @@ public class EssentialsProtect extends JavaPlugin implements IProtect
} }
} }
@Override
public ProtectHolder getSettings() public ProtectHolder getSettings()
{ {
return settings; return settings;
} }
@Override
public void setSettings(final ProtectHolder settings) public void setSettings(final ProtectHolder settings)
{ {
this.settings = settings; this.settings = settings;

View File

@@ -1,7 +1,6 @@
package com.earth2me.essentials.protect; package com.earth2me.essentials.protect;
import static com.earth2me.essentials.I18n._; import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.api.IEssentials;
import com.earth2me.essentials.protect.data.IProtectedBlock; import com.earth2me.essentials.protect.data.IProtectedBlock;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -18,22 +17,15 @@ import org.bukkit.event.block.*;
public class EssentialsProtectBlockListener implements Listener public class EssentialsProtectBlockListener implements Listener
{ {
final private transient IProtect prot; final private transient IProtect prot;
final private transient IEssentials ess;
public EssentialsProtectBlockListener(final IProtect parent) public EssentialsProtectBlockListener(final IProtect parent)
{ {
this.prot = parent; this.prot = parent;
this.ess = prot.getEssentialsConnect().getEssentials();
} }
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockPlace(final BlockPlaceEvent event) public void onBlockPlace(final BlockPlaceEvent event)
{ {
if (event.isCancelled())
{
return;
}
final Player user = event.getPlayer(); final Player user = event.getPlayer();
final ProtectHolder settings = prot.getSettings(); final ProtectHolder settings = prot.getSettings();
settings.acquireReadLock(); settings.acquireReadLock();
@@ -46,7 +38,6 @@ public class EssentialsProtectBlockListener implements Listener
} }
final Block blockPlaced = event.getBlockPlaced(); final Block blockPlaced = event.getBlockPlaced();
final int id = blockPlaced.getTypeId();
if (!BlockPlacePermissions.getPermission(blockPlaced.getType()).isAuthorized(user)) if (!BlockPlacePermissions.getPermission(blockPlaced.getType()).isAuthorized(user))
{ {
@@ -54,8 +45,8 @@ public class EssentialsProtectBlockListener implements Listener
return; return;
} }
if (!Permissions.ALERTS_NOTRIGGER.isAuthorized(user) && if (!Permissions.ALERTS_NOTRIGGER.isAuthorized(user)
settings.getData().getAlertOnPlacement().contains(blockPlaced.getType())) && settings.getData().getAlertOnPlacement().contains(blockPlaced.getType()))
{ {
prot.getEssentialsConnect().alert(user, blockPlaced.getType().toString(), _("alertPlaced")); prot.getEssentialsConnect().alert(user, blockPlaced.getType().toString(), _("alertPlaced"));
} }
@@ -105,13 +96,9 @@ public class EssentialsProtectBlockListener implements Listener
} }
} }
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockIgnite(BlockIgniteEvent event) public void onBlockIgnite(BlockIgniteEvent event)
{ {
if (event.isCancelled())
{
return;
}
final ProtectHolder settings = prot.getSettings(); final ProtectHolder settings = prot.getSettings();
settings.acquireReadLock(); settings.acquireReadLock();
try try
@@ -165,13 +152,9 @@ public class EssentialsProtectBlockListener implements Listener
} }
} }
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockFromTo(final BlockFromToEvent event) public void onBlockFromTo(final BlockFromToEvent event)
{ {
if (event.isCancelled())
{
return;
}
final ProtectHolder settings = prot.getSettings(); final ProtectHolder settings = prot.getSettings();
settings.acquireReadLock(); settings.acquireReadLock();
try try
@@ -214,13 +197,9 @@ public class EssentialsProtectBlockListener implements Listener
} }
} }
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockBurn(final BlockBurnEvent event) public void onBlockBurn(final BlockBurnEvent event)
{ {
if (event.isCancelled())
{
return;
}
final ProtectHolder settings = prot.getSettings(); final ProtectHolder settings = prot.getSettings();
settings.acquireReadLock(); settings.acquireReadLock();
try try
@@ -260,13 +239,9 @@ public class EssentialsProtectBlockListener implements Listener
BlockFace.SELF BlockFace.SELF
}; };
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockBreak(final BlockBreakEvent event) public void onBlockBreak(final BlockBreakEvent event)
{ {
if (event.isCancelled())
{
return;
}
final Player user = event.getPlayer(); final Player user = event.getPlayer();
if (!Permissions.BUILD.isAuthorized(user)) if (!Permissions.BUILD.isAuthorized(user))
@@ -275,7 +250,6 @@ public class EssentialsProtectBlockListener implements Listener
return; return;
} }
final Block block = event.getBlock(); final Block block = event.getBlock();
final int typeId = block.getTypeId();
if (!BlockBreakPermissions.getPermission(block.getType()).isAuthorized(user)) if (!BlockBreakPermissions.getPermission(block.getType()).isAuthorized(user))
{ {
@@ -366,13 +340,9 @@ public class EssentialsProtectBlockListener implements Listener
} }
} }
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockPistonExtend(final BlockPistonExtendEvent event) public void onBlockPistonExtend(final BlockPistonExtendEvent event)
{ {
if (event.isCancelled())
{
return;
}
final ProtectHolder settings = prot.getSettings(); final ProtectHolder settings = prot.getSettings();
settings.acquireReadLock(); settings.acquireReadLock();
try try
@@ -428,10 +398,10 @@ public class EssentialsProtectBlockListener implements Listener
} }
} }
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockPistonRetract(final BlockPistonRetractEvent event) public void onBlockPistonRetract(final BlockPistonRetractEvent event)
{ {
if (event.isCancelled() || !event.isSticky()) if (!event.isSticky())
{ {
return; return;
} }
@@ -482,7 +452,6 @@ public class EssentialsProtectBlockListener implements Listener
} }
} }
} }
finally finally
{ {
settings.unlock(); settings.unlock();

View File

@@ -1,6 +1,5 @@
package com.earth2me.essentials.protect; package com.earth2me.essentials.protect;
import com.earth2me.essentials.api.IEssentials;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
@@ -16,22 +15,16 @@ import org.bukkit.event.entity.EntityTargetEvent.TargetReason;
public class EssentialsProtectEntityListener implements Listener public class EssentialsProtectEntityListener implements Listener
{ {
private final transient IProtect prot; private final transient IProtect prot;
private final transient IEssentials ess;
public EssentialsProtectEntityListener(final IProtect prot) public EssentialsProtectEntityListener(final IProtect prot)
{ {
super(); super();
this.prot = prot; this.prot = prot;
this.ess = prot.getEssentialsConnect().getEssentials();
} }
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onEntityDamage(final EntityDamageEvent event) public void onEntityDamage(final EntityDamageEvent event)
{ {
if (event.isCancelled())
{
return;
}
final ProtectHolder settings = prot.getSettings(); final ProtectHolder settings = prot.getSettings();
settings.acquireReadLock(); settings.acquireReadLock();
try try
@@ -171,13 +164,9 @@ public class EssentialsProtectEntityListener implements Listener
} }
} }
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onEntityExplode(final EntityExplodeEvent event) public void onEntityExplode(final EntityExplodeEvent event)
{ {
if (event.isCancelled())
{
return;
}
final ProtectHolder settings = prot.getSettings(); final ProtectHolder settings = prot.getSettings();
settings.acquireReadLock(); settings.acquireReadLock();
try try
@@ -247,14 +236,10 @@ public class EssentialsProtectEntityListener implements Listener
} }
} }
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onCreatureSpawn(final CreatureSpawnEvent event) public void onCreatureSpawn(final CreatureSpawnEvent event)
{ {
if (event.getEntity() instanceof Player) if (event.getEntity().getType() == EntityType.PLAYER)
{
return;
}
if (event.isCancelled())
{ {
return; return;
} }
@@ -279,13 +264,11 @@ public class EssentialsProtectEntityListener implements Listener
} }
} }
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onEntityTarget(final EntityTargetEvent event) public void onEntityTarget(final EntityTargetEvent event)
{ {
if (event.isCancelled() || !(event.getTarget() instanceof Player)) if (event.getTarget().getType() == EntityType.PLAYER)
{ {
return;
}
final Player user = (Player)event.getTarget(); final Player user = (Player)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
@@ -296,7 +279,7 @@ public class EssentialsProtectEntityListener implements Listener
&& Permissions.ENTITYTARGET.isAuthorized(user)) && Permissions.ENTITYTARGET.isAuthorized(user))
{ {
event.setCancelled(true); event.setCancelled(true);
return; }
} }
} }
@@ -319,13 +302,9 @@ public class EssentialsProtectEntityListener implements Listener
} }
} }
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onEntityChangeBlock(final EntityChangeBlockEvent event) public void onEntityChangeBlock(final EntityChangeBlockEvent event)
{ {
if (event.isCancelled())
{
return;
}
final ProtectHolder settings = prot.getSettings(); final ProtectHolder settings = prot.getSettings();
settings.acquireReadLock(); settings.acquireReadLock();
try try

View File

@@ -1,7 +1,6 @@
package com.earth2me.essentials.protect; package com.earth2me.essentials.protect;
import static com.earth2me.essentials.I18n._; import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.api.IEssentials;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@@ -16,12 +15,10 @@ import org.bukkit.inventory.ItemStack;
public class EssentialsProtectPlayerListener implements Listener public class EssentialsProtectPlayerListener implements Listener
{ {
private final transient IProtect prot; private final transient IProtect prot;
private final transient IEssentials ess;
public EssentialsProtectPlayerListener(final IProtect prot) public EssentialsProtectPlayerListener(final IProtect prot)
{ {
this.prot = prot; this.prot = prot;
this.ess = prot.getEssentialsConnect().getEssentials();
} }
@EventHandler(priority = EventPriority.LOW) @EventHandler(priority = EventPriority.LOW)

View File

@@ -8,22 +8,25 @@ import java.util.Map;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.permissions.PermissionDefault; import org.bukkit.permissions.PermissionDefault;
public class ItemUsePermissions extends AbstractSuperpermsPermission{
public class ItemUsePermissions extends AbstractSuperpermsPermission
{
private static Map<Material, IPermission> permissions = new EnumMap<Material, IPermission>(Material.class); private static Map<Material, IPermission> permissions = new EnumMap<Material, IPermission>(Material.class);
private static final String base = "essentials.protect.itemuse."; private static final String base = "essentials.protect.itemuse.";
private final String permission; private final String permission;
public static IPermission getPermission(Material mat) public static IPermission getPermission(final Material mat)
{ {
IPermission perm = permissions.get(mat); IPermission perm = permissions.get(mat);
if (perm == null) { if (perm == null)
{
perm = new ItemUsePermissions(mat.toString().toLowerCase(Locale.ENGLISH)); perm = new ItemUsePermissions(mat.toString().toLowerCase(Locale.ENGLISH));
permissions.put(mat, perm); permissions.put(mat, perm);
} }
return perm; return perm;
} }
private ItemUsePermissions(String matName) private ItemUsePermissions(final String matName)
{ {
this.permission = base + matName; this.permission = base + matName;
} }

View File

@@ -1,7 +1,7 @@
package com.earth2me.essentials.protect; package com.earth2me.essentials.protect;
import com.earth2me.essentials.utils.Util;
import com.earth2me.essentials.api.IPermission; import com.earth2me.essentials.api.IPermission;
import com.earth2me.essentials.utils.Util;
import java.util.Locale; import java.util.Locale;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.permissions.Permission; import org.bukkit.permissions.Permission;
@@ -74,7 +74,7 @@ public enum Permissions implements IPermission
} }
@Override @Override
public boolean isAuthorized(CommandSender sender) public boolean isAuthorized(final CommandSender sender)
{ {
return sender.hasPermission(getBukkitPermission()); return sender.hasPermission(getBukkitPermission());
} }

View File

@@ -9,7 +9,7 @@ import java.io.IOException;
public class ProtectHolder extends AsyncStorageObjectHolder<Protect> public class ProtectHolder extends AsyncStorageObjectHolder<Protect>
{ {
public ProtectHolder(IEssentials ess) public ProtectHolder(final IEssentials ess)
{ {
super(ess, Protect.class); super(ess, Protect.class);
} }
@@ -23,12 +23,10 @@ public class ProtectHolder extends AsyncStorageObjectHolder<Protect>
@Override @Override
public void finishRead() public void finishRead()
{ {
} }
@Override @Override
public void finishWrite() public void finishWrite()
{ {
} }
} }

View File

@@ -34,12 +34,12 @@ public abstract class ProtectedBlockJDBC implements IProtectedBlock
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(final String driver, final 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(final String driver, final String url, final String username, final String password) throws PropertyVetoException
{ {
cpds = new ComboPooledDataSource(); cpds = new ComboPooledDataSource();
cpds.setDriverClass(driver); cpds.setDriverClass(driver);
@@ -97,6 +97,7 @@ public abstract class ProtectedBlockJDBC implements IProtectedBlock
} }
} }
@Override
public void clearProtections() public void clearProtections()
{ {
Connection conn = null; Connection conn = null;
@@ -138,7 +139,8 @@ public abstract class ProtectedBlockJDBC implements IProtectedBlock
} }
} }
public void importProtections(List<OwnedBlock> blocks) @Override
public void importProtections(final List<OwnedBlock> blocks)
{ {
for (OwnedBlock ownedBlock : blocks) for (OwnedBlock ownedBlock : blocks)
{ {
@@ -150,6 +152,7 @@ public abstract class ProtectedBlockJDBC implements IProtectedBlock
} }
} }
@Override
public List<OwnedBlock> exportProtections() public List<OwnedBlock> exportProtections()
{ {
Connection conn = null; Connection conn = null;
@@ -216,12 +219,13 @@ public abstract class ProtectedBlockJDBC implements IProtectedBlock
} }
} }
public void protectBlock(Block block, String playerName) @Override
public void protectBlock(final Block block, final 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(final String world, final int x, final int y, final int z, final String playerName)
{ {
Connection conn = null; Connection conn = null;
PreparedStatement ps = null; PreparedStatement ps = null;
@@ -262,7 +266,8 @@ public abstract class ProtectedBlockJDBC implements IProtectedBlock
} }
} }
public boolean isProtected(Block block, String playerName) @Override
public boolean isProtected(final Block block, final String playerName)
{ {
Connection conn = null; Connection conn = null;
PreparedStatement ps = null; PreparedStatement ps = null;
@@ -317,7 +322,8 @@ public abstract class ProtectedBlockJDBC implements IProtectedBlock
} }
} }
public List<String> getOwners(Block block) @Override
public List<String> getOwners(final Block block)
{ {
Connection conn = null; Connection conn = null;
PreparedStatement ps = null; PreparedStatement ps = null;
@@ -377,7 +383,8 @@ public abstract class ProtectedBlockJDBC implements IProtectedBlock
} }
} }
public int unprotectBlock(Block block) @Override
public int unprotectBlock(final Block block)
{ {
Connection conn = null; Connection conn = null;
PreparedStatement ps = null; PreparedStatement ps = null;
@@ -419,6 +426,7 @@ public abstract class ProtectedBlockJDBC implements IProtectedBlock
} }
} }
@Override
public void onPluginDeactivation() public void onPluginDeactivation()
{ {
cpds.close(); cpds.close();

View File

@@ -1,13 +1,13 @@
package com.earth2me.essentials.protect.data; package com.earth2me.essentials.protect.data;
import java.util.Map.Entry;
import java.util.*; import java.util.*;
import java.util.Map.Entry;
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 public final class ProtectedBlockMemory implements IProtectedBlock
{ {
private final transient List<String> worlds = new ArrayList<String>(); private final transient List<String> worlds = new ArrayList<String>();
private final transient List<String> playerNames = new ArrayList<String>(); private final transient List<String> playerNames = new ArrayList<String>();
@@ -123,12 +123,14 @@ public class ProtectedBlockMemory implements IProtectedBlock
importProtections(storage.exportProtections()); importProtections(storage.exportProtections());
} }
@Override
public void clearProtections() public void clearProtections()
{ {
blocks.clear(); blocks.clear();
} }
public final void importProtections(final List<OwnedBlock> blocks) @Override
public void importProtections(final List<OwnedBlock> blocks)
{ {
for (OwnedBlock ownedBlock : blocks) for (OwnedBlock ownedBlock : blocks)
{ {
@@ -141,6 +143,7 @@ public class ProtectedBlockMemory implements IProtectedBlock
} }
} }
@Override
public List<OwnedBlock> exportProtections() public List<OwnedBlock> exportProtections()
{ {
final List<OwnedBlock> blockList = new ArrayList<OwnedBlock>(blocks.size()); final List<OwnedBlock> blockList = new ArrayList<OwnedBlock>(blocks.size());
@@ -160,12 +163,14 @@ public class ProtectedBlockMemory implements IProtectedBlock
return blockList; return blockList;
} }
@Override
public void protectBlock(final Block block, final String playerName) public void protectBlock(final Block block, final String playerName)
{ {
final 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()
{ {
@Override
public void run() public void run()
{ {
storage.protectBlock(block, playerName); storage.protectBlock(block, playerName);
@@ -173,9 +178,9 @@ public class ProtectedBlockMemory implements IProtectedBlock
}); });
} }
private final void protectBlock(ProtectedLocation pl, String playerName) private void protectBlock(final ProtectedLocation pl, final String playerName)
{ {
int playerId = getPlayerId(playerName); final int playerId = getPlayerId(playerName);
ProtectedBy pb = blocks.get(pl); ProtectedBy pb = blocks.get(pl);
if (pb == null) if (pb == null)
{ {
@@ -185,27 +190,31 @@ public class ProtectedBlockMemory implements IProtectedBlock
pb.add(playerId); pb.add(playerId);
} }
public boolean isProtected(Block block, String playerName) @Override
public boolean isProtected(final Block block, final String playerName)
{ {
int playerId = getPlayerId(playerName); final int playerId = getPlayerId(playerName);
ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld())); final ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld()));
ProtectedBy pb = blocks.get(pl); final ProtectedBy pb = blocks.get(pl);
return !pb.contains(playerId); return !pb.contains(playerId);
} }
public List<String> getOwners(Block block) @Override
public List<String> getOwners(final 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);
} }
@Override
public int unprotectBlock(final Block block) public int unprotectBlock(final Block block)
{ {
ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld())); final ProtectedLocation pl = new ProtectedLocation(block, getWorldId(block.getWorld()));
ProtectedBy pb = blocks.remove(pl); final ProtectedBy pb = blocks.remove(pl);
plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable()
{ {
@Override
public void run() public void run()
{ {
storage.unprotectBlock(block); storage.unprotectBlock(block);
@@ -214,7 +223,7 @@ public class ProtectedBlockMemory implements IProtectedBlock
return pb.size(); return pb.size();
} }
private int getPlayerId(String playername) private int getPlayerId(final String playername)
{ {
int id = playerNames.indexOf(playername); int id = playerNames.indexOf(playername);
if (id < 0) if (id < 0)
@@ -225,12 +234,12 @@ public class ProtectedBlockMemory implements IProtectedBlock
return id; return id;
} }
private int getWorldId(World world) private int getWorldId(final World world)
{ {
return getWorldId(world.getName()); return getWorldId(world.getName());
} }
private int getWorldId(String name) private int getWorldId(final String name)
{ {
int id = worlds.indexOf(name); int id = worlds.indexOf(name);
if (id < 0) if (id < 0)
@@ -241,6 +250,7 @@ public class ProtectedBlockMemory implements IProtectedBlock
return id; return id;
} }
@Override
public void onPluginDeactivation() public void onPluginDeactivation()
{ {
storage.onPluginDeactivation(); storage.onPluginDeactivation();

View File

@@ -11,7 +11,7 @@ import java.util.logging.Logger;
public class ProtectedBlockMySQL extends ProtectedBlockJDBC public class ProtectedBlockMySQL extends ProtectedBlockJDBC
{ {
public ProtectedBlockMySQL(String url, String username, String password) throws PropertyVetoException public ProtectedBlockMySQL(final String url, final String username, final String password) throws PropertyVetoException
{ {
super("com.mysql.jdbc.Driver", url, username, password); super("com.mysql.jdbc.Driver", url, username, password);
} }
@@ -24,7 +24,7 @@ public class ProtectedBlockMySQL extends ProtectedBlockJDBC
+ ") ENGINE=MyISAM DEFAULT CHARSET=utf8"; + ") ENGINE=MyISAM DEFAULT CHARSET=utf8";
@Override @Override
protected PreparedStatement getStatementCreateTable(Connection conn) throws SQLException protected PreparedStatement getStatementCreateTable(final Connection conn) throws SQLException
{ {
return conn.prepareStatement(QueryCreateTable); return conn.prepareStatement(QueryCreateTable);
} }
@@ -40,7 +40,7 @@ public class ProtectedBlockMySQL extends ProtectedBlockJDBC
+ "DROP PRIMARY KEY ;"; + "DROP PRIMARY KEY ;";
@Override @Override
protected PreparedStatement getStatementUpdateFrom2_0Table(Connection conn) throws SQLException protected PreparedStatement getStatementUpdateFrom2_0Table(final Connection conn) throws SQLException
{ {
PreparedStatement testPS = null; PreparedStatement testPS = null;
ResultSet testRS = null; ResultSet testRS = null;
@@ -86,7 +86,7 @@ public class ProtectedBlockMySQL extends ProtectedBlockJDBC
private static final String QueryDeleteAll = "DELETE FROM EssentialsProtect;"; private static final String QueryDeleteAll = "DELETE FROM EssentialsProtect;";
@Override @Override
protected PreparedStatement getStatementDeleteAll(Connection conn) throws SQLException protected PreparedStatement getStatementDeleteAll(final Connection conn) throws SQLException
{ {
return conn.prepareStatement(QueryDeleteAll); return conn.prepareStatement(QueryDeleteAll);
} }
@@ -94,9 +94,10 @@ public class ProtectedBlockMySQL extends ProtectedBlockJDBC
"INSERT INTO EssentialsProtect (worldName, x, y, z, playerName) VALUES (?, ?, ?, ?, ?);"; "INSERT INTO EssentialsProtect (worldName, x, y, z, playerName) VALUES (?, ?, ?, ?, ?);";
@Override @Override
protected PreparedStatement getStatementInsert(Connection conn, String world, int x, int y, int z, String playerName) throws SQLException protected PreparedStatement getStatementInsert(final Connection conn, final String world, final int x, final int y, final int z,
final String playerName) throws SQLException
{ {
PreparedStatement ps = conn.prepareStatement(QueryInsert); final PreparedStatement ps = conn.prepareStatement(QueryInsert);
ps.setString(1, world); ps.setString(1, world);
ps.setInt(2, x); ps.setInt(2, x);
ps.setInt(3, y); ps.setInt(3, y);
@@ -109,9 +110,10 @@ public class ProtectedBlockMySQL extends ProtectedBlockJDBC
+ "WHERE worldName = ? AND x = ? AND y = ? AND z = ? GROUP BY x;"; + "WHERE worldName = ? AND x = ? AND y = ? AND z = ? GROUP BY x;";
@Override @Override
protected PreparedStatement getStatementPlayerCountByLocation(Connection conn, String world, int x, int y, int z, String playerName) throws SQLException protected PreparedStatement getStatementPlayerCountByLocation(final Connection conn, final String world, final int x, final int y, final int z,
final String playerName) throws SQLException
{ {
PreparedStatement ps = conn.prepareStatement(QueryCountByPlayer); final PreparedStatement ps = conn.prepareStatement(QueryCountByPlayer);
ps.setString(1, playerName); ps.setString(1, playerName);
ps.setString(2, world); ps.setString(2, world);
ps.setInt(3, x); ps.setInt(3, x);
@@ -123,9 +125,10 @@ public class ProtectedBlockMySQL extends ProtectedBlockJDBC
"SELECT playerName FROM EssentialsProtect WHERE worldname = ? AND x = ? AND y = ? AND z = ?;"; "SELECT playerName FROM EssentialsProtect WHERE worldname = ? AND x = ? AND y = ? AND z = ?;";
@Override @Override
protected PreparedStatement getStatementPlayersByLocation(Connection conn, String world, int x, int y, int z) throws SQLException protected PreparedStatement getStatementPlayersByLocation(final Connection conn, final String world,
final int x, final int y, final int z) throws SQLException
{ {
PreparedStatement ps = conn.prepareStatement(QueryPlayersByLocation); final PreparedStatement ps = conn.prepareStatement(QueryPlayersByLocation);
ps.setString(1, world); ps.setString(1, world);
ps.setInt(2, x); ps.setInt(2, x);
ps.setInt(3, y); ps.setInt(3, y);
@@ -136,9 +139,10 @@ public class ProtectedBlockMySQL extends ProtectedBlockJDBC
"DELETE FROM EssentialsProtect WHERE worldName = ? AND x = ? AND y = ? AND z = ?;"; "DELETE FROM EssentialsProtect WHERE worldName = ? AND x = ? AND y = ? AND z = ?;";
@Override @Override
protected PreparedStatement getStatementDeleteByLocation(Connection conn, String world, int x, int y, int z) throws SQLException protected PreparedStatement getStatementDeleteByLocation(final Connection conn, final String world,
final int x, final int y, final int z) throws SQLException
{ {
PreparedStatement ps = conn.prepareStatement(QueryDeleteByLocation); final PreparedStatement ps = conn.prepareStatement(QueryDeleteByLocation);
ps.setString(1, world); ps.setString(1, world);
ps.setInt(2, x); ps.setInt(2, x);
ps.setInt(3, y); ps.setInt(3, y);
@@ -149,7 +153,7 @@ public class ProtectedBlockMySQL extends ProtectedBlockJDBC
"SELECT worldName, x, y, z, playerName FROM EssentialsProtect;"; "SELECT worldName, x, y, z, playerName FROM EssentialsProtect;";
@Override @Override
protected PreparedStatement getStatementAllBlocks(Connection conn) throws SQLException protected PreparedStatement getStatementAllBlocks(final Connection conn) throws SQLException
{ {
return conn.prepareStatement(QueryAllBlocks); return conn.prepareStatement(QueryAllBlocks);
} }

View File

@@ -8,7 +8,7 @@ import java.sql.SQLException;
public class ProtectedBlockSQLite extends ProtectedBlockJDBC public class ProtectedBlockSQLite extends ProtectedBlockJDBC
{ {
public ProtectedBlockSQLite(String url) throws PropertyVetoException public ProtectedBlockSQLite(final String url) throws PropertyVetoException
{ {
super("org.sqlite.JDBC", url); super("org.sqlite.JDBC", url);
} }
@@ -18,7 +18,7 @@ public class ProtectedBlockSQLite extends ProtectedBlockJDBC
+ "x NUMERIC, y NUMERIC, z NUMERIC)"; + "x NUMERIC, y NUMERIC, z NUMERIC)";
@Override @Override
protected PreparedStatement getStatementCreateTable(Connection conn) throws SQLException protected PreparedStatement getStatementCreateTable(final Connection conn) throws SQLException
{ {
return conn.prepareStatement(QueryCreateTable); return conn.prepareStatement(QueryCreateTable);
} }
@@ -27,14 +27,14 @@ public class ProtectedBlockSQLite extends ProtectedBlockJDBC
+ "worldName, x, z, y)"; + "worldName, x, z, y)";
@Override @Override
protected PreparedStatement getStatementUpdateFrom2_0Table(Connection conn) throws SQLException protected PreparedStatement getStatementUpdateFrom2_0Table(final Connection conn) throws SQLException
{ {
return conn.prepareStatement(QueryUpdateFrom2_0Table); return conn.prepareStatement(QueryUpdateFrom2_0Table);
} }
private static final String QueryDeleteAll = "DELETE FROM EssentialsProtect;"; private static final String QueryDeleteAll = "DELETE FROM EssentialsProtect;";
@Override @Override
protected PreparedStatement getStatementDeleteAll(Connection conn) throws SQLException protected PreparedStatement getStatementDeleteAll(final Connection conn) throws SQLException
{ {
return conn.prepareStatement(QueryDeleteAll); return conn.prepareStatement(QueryDeleteAll);
} }
@@ -42,9 +42,10 @@ public class ProtectedBlockSQLite extends ProtectedBlockJDBC
"INSERT INTO EssentialsProtect (worldName, x, y, z, playerName) VALUES (?, ?, ?, ?, ?);"; "INSERT INTO EssentialsProtect (worldName, x, y, z, playerName) VALUES (?, ?, ?, ?, ?);";
@Override @Override
protected PreparedStatement getStatementInsert(Connection conn, String world, int x, int y, int z, String playerName) throws SQLException protected PreparedStatement getStatementInsert(final Connection conn, final String world,
final int x, final int y, final int z, final String playerName) throws SQLException
{ {
PreparedStatement ps = conn.prepareStatement(QueryInsert); final PreparedStatement ps = conn.prepareStatement(QueryInsert);
ps.setString(1, world); ps.setString(1, world);
ps.setInt(2, x); ps.setInt(2, x);
ps.setInt(3, y); ps.setInt(3, y);
@@ -57,9 +58,10 @@ public class ProtectedBlockSQLite extends ProtectedBlockJDBC
+ "WHERE worldName = ? AND x = ? AND y = ? AND z = ? GROUP BY x;"; + "WHERE worldName = ? AND x = ? AND y = ? AND z = ? GROUP BY x;";
@Override @Override
protected PreparedStatement getStatementPlayerCountByLocation(Connection conn, String world, int x, int y, int z, String playerName) throws SQLException protected PreparedStatement getStatementPlayerCountByLocation(final Connection conn, final String world,
final int x, final int y, final int z, final String playerName) throws SQLException
{ {
PreparedStatement ps = conn.prepareStatement(QueryPlayerCountByLocation); final PreparedStatement ps = conn.prepareStatement(QueryPlayerCountByLocation);
ps.setString(1, playerName); ps.setString(1, playerName);
ps.setString(2, world); ps.setString(2, world);
ps.setInt(3, x); ps.setInt(3, x);
@@ -71,9 +73,10 @@ public class ProtectedBlockSQLite extends ProtectedBlockJDBC
"SELECT playerName FROM EssentialsProtect WHERE worldname = ? AND x = ? AND y = ? AND z = ?;"; "SELECT playerName FROM EssentialsProtect WHERE worldname = ? AND x = ? AND y = ? AND z = ?;";
@Override @Override
protected PreparedStatement getStatementPlayersByLocation(Connection conn, String world, int x, int y, int z) throws SQLException protected PreparedStatement getStatementPlayersByLocation(final Connection conn, final String world,
final int x, final int y, final int z) throws SQLException
{ {
PreparedStatement ps = conn.prepareStatement(QueryPlayersByLocation); final PreparedStatement ps = conn.prepareStatement(QueryPlayersByLocation);
ps.setString(1, world); ps.setString(1, world);
ps.setInt(2, x); ps.setInt(2, x);
ps.setInt(3, y); ps.setInt(3, y);
@@ -84,9 +87,10 @@ public class ProtectedBlockSQLite extends ProtectedBlockJDBC
"DELETE FROM EssentialsProtect WHERE worldName = ? AND x = ? AND y = ? AND z = ?;"; "DELETE FROM EssentialsProtect WHERE worldName = ? AND x = ? AND y = ? AND z = ?;";
@Override @Override
protected PreparedStatement getStatementDeleteByLocation(Connection conn, String world, int x, int y, int z) throws SQLException protected PreparedStatement getStatementDeleteByLocation(final Connection conn, final String world,
final int x, final int y, final int z) throws SQLException
{ {
PreparedStatement ps = conn.prepareStatement(QueryDeleteByLocation); final PreparedStatement ps = conn.prepareStatement(QueryDeleteByLocation);
ps.setString(1, world); ps.setString(1, world);
ps.setInt(2, x); ps.setInt(2, x);
ps.setInt(3, y); ps.setInt(3, y);
@@ -97,7 +101,7 @@ public class ProtectedBlockSQLite extends ProtectedBlockJDBC
"SELECT worldName, x, y, z, playerName FROM EssentialsProtect;"; "SELECT worldName, x, y, z, playerName FROM EssentialsProtect;";
@Override @Override
protected PreparedStatement getStatementAllBlocks(Connection conn) throws SQLException protected PreparedStatement getStatementAllBlocks(final Connection conn) throws SQLException
{ {
return conn.prepareStatement(QueryAllBlocks); return conn.prepareStatement(QueryAllBlocks);
} }