1
0
mirror of https://github.com/essentials/Essentials.git synced 2025-08-19 21:11:25 +02:00

2.1 prerelease, part 2 of 3

git-svn-id: https://svn.java.net/svn/essentials~svn/trunk2.1@1015 e251c2fe-e539-e718-e476-b85c1f46cddb
This commit is contained in:
Zenexer
2011-03-30 04:03:21 +00:00
commit cb21bc4d7a
237 changed files with 28831 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
Manifest-Version: 1.0
Class-Path: ../lib/mysql.jar ../lib/sqlite.jar

View File

@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE project [ <!ENTITY buildinc SYSTEM "../build.inc.xml"> ]>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<!-- By default, only the Clean and Build commands use this build script. -->
<!-- Commands such as Run, Debug, and Test only use this build script if -->
<!-- the Compile on Save feature is turned off for the project. -->
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
<!-- in the project's Project Properties dialog box.-->
<project name="EssentialsProtect" default="default" basedir=".">
<description>Builds, tests, and runs the project EssentialsProtect.</description>
<import file="nbproject/build-impl.xml"/>
<!--
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
(Targets beginning with '-' are not intended to be called on their own.)
Example of inserting an obfuscator after compilation could look like this:
<target name="-post-compile">
<obfuscate>
<fileset dir="${build.classes.dir}"/>
</obfuscate>
</target>
For list of available properties check the imported
nbproject/build-impl.xml file.
Another way to customize the build is by overriding existing main targets.
The targets of interest are:
-init-macrodef-javac: defines macro for javac compilation
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar-with-manifest: JAR building (if you are using a manifest)
-do-jar-without-manifest: JAR building (if you are not using a manifest)
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation
An example of overriding the target for project execution could look like this:
<target name="run" depends="EssentialsProtect-impl.jar">
<exec dir="bin" executable="launcher.exe">
<arg file="${dist.jar}"/>
</exec>
</target>
Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.
-->
<target name="-post-jar">
<jar jarfile="${dist.dir}/EssentialsProtect.jar">
<zipfileset src="${dist.jar}" excludes="META-INF/*" />
<zipfileset src="../lib/mysql.jar" excludes="META-INF/*" />
<zipfileset src="../lib/sqlite.jar" excludes="META-INF/*" />
<manifest>
<attribute name="Classpath" value="Essentials.jar"/>
</manifest>
</jar>
</target>
</project>

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,8 @@
build.xml.data.CRC32=ff207988
build.xml.script.CRC32=1ed11cc3
build.xml.stylesheet.CRC32=28e38971@1.38.3.45
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=40644caa
nbproject/build-impl.xml.script.CRC32=1d86f3b0
nbproject/build-impl.xml.stylesheet.CRC32=19debb58@1.42.1.45

View File

@@ -0,0 +1 @@
user.properties.file=C:\\Users\\Paul\\.netbeans\\7.0beta2\\build.properties

View File

@@ -0,0 +1,91 @@
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
application.title=EssentialsProtect
application.vendor=devhome
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
build.dir=build
build.generated.dir=${build.dir}/generated
build.generated.sources.dir=${build.dir}/generated-sources
# Only compile against the classpath explicitly listed here:
build.sysclasspath=ignore
build.test.classes.dir=${build.dir}/test/classes
build.test.results.dir=${build.dir}/test/results
# Uncomment to specify the preferred debugger connection transport:
#debug.transport=dt_socket
debug.classpath=\
${run.classpath}
debug.test.classpath=\
${run.test.classpath}
# This directory is removed when the project is cleaned:
dist.dir=dist
dist.jar=${dist.dir}/original-EssentialsProtect.jar
dist.javadoc.dir=${dist.dir}/javadoc
endorsed.classpath=
excludes=
file.reference.c3p0-0.9.1.2.jar=..\\lib\\c3p0-0.9.1.2.jar
file.reference.craftbukkit-0.0.1-SNAPSHOT.jar=..\\lib\\craftbukkit-0.0.1-SNAPSHOT.jar
file.reference.mysql.jar=..\\lib\\mysql.jar
file.reference.sqlite.jar=..\\lib\\sqlite.jar
includes=**
jar.archive.disabled=${jnlp.enabled}
jar.compress=false
jar.index=${jnlp.enabled}
javac.classpath=\
${reference.Essentials.jar}:\
${file.reference.mysql.jar}:\
${file.reference.sqlite.jar}:\
${file.reference.craftbukkit-0.0.1-SNAPSHOT.jar}:\
${file.reference.c3p0-0.9.1.2.jar}
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
javac.processorpath=\
${javac.classpath}
javac.source=1.5
javac.target=1.5
javac.test.classpath=\
${javac.classpath}:\
${build.classes.dir}:\
${libs.junit.classpath}:\
${libs.junit_4.classpath}
javac.test.processorpath=\
${javac.test.classpath}
javadoc.additionalparam=
javadoc.author=false
javadoc.encoding=${source.encoding}
javadoc.noindex=false
javadoc.nonavbar=false
javadoc.notree=false
javadoc.private=false
javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
jnlp.codebase.type=no.codebase
jnlp.descriptor=application
jnlp.enabled=false
jnlp.mixed.code=defaut
jnlp.offline-allowed=false
jnlp.signed=false
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
platform.active=default_platform
project.Essentials=../Essentials
reference.Essentials.jar=${project.Essentials}/dist/Essentials.jar
run.classpath=\
${javac.classpath}:\
${build.classes.dir}
# Space-separated list of JVM arguments used when running the project
# (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value
# or test-sys-prop.name=value to set system properties for unit tests):
run.jvmargs=
run.test.classpath=\
${javac.test.classpath}:\
${build.test.classes.dir}
source.encoding=UTF-8
src.dir=src
test.src.dir=test

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.java.j2seproject</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
<name>EssentialsProtect</name>
<source-roots>
<root id="src.dir"/>
</source-roots>
<test-roots>
<root id="test.src.dir"/>
</test-roots>
</data>
<libraries xmlns="http://www.netbeans.org/ns/ant-project-libraries/1">
<definitions>../lib\nblibraries.properties</definitions>
</libraries>
<references xmlns="http://www.netbeans.org/ns/ant-project-references/1">
<reference>
<foreign-project>Essentials</foreign-project>
<artifact-type>jar</artifact-type>
<script>build.xml</script>
<target>jar</target>
<clean-target>clean</clean-target>
<id>jar</id>
</reference>
</references>
</configuration>
</project>

View File

@@ -0,0 +1,30 @@
EssentialsProtect:
REQUIRED : Essentials.jar. Also sqlite.jar, mysql.jar in the bukkit lib folder.
Config Settings in plugins/Essentials/config.yml
protect:
datatype: 'sqlite' type of db, options sqlite or mysql
username: 'root' mysql username
pasword: 'root' mysql password
mysqlDb: 'jdbc:mysql://localhost:3306/minecraft' mysql database location
protectSigns: true
protectRails: true
protectBlockBelow: true
preventBlockOnRail: false prevents block placement on protected rails
On startup creates a sqlite database under /plugins/essentials called EssentialsProtect.db
Permissions:
"essentials.protect" - allows protection
"essentials.protect.admin" can delete protected blocks / query protection by right clicking a protected item.
Usage:
Place a sign or rail and they (plus optionally the block below) will be protected. Only the owners and those with admin permissions can destroy.

View File

@@ -0,0 +1,154 @@
package com.earth2me.essentials.protect;
import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.IConf;
import com.earth2me.essentials.User;
import com.earth2me.essentials.protect.data.IProtectedBlock;
import com.earth2me.essentials.protect.data.ProtectedBlockMemory;
import com.earth2me.essentials.protect.data.ProtectedBlockMySQL;
import com.earth2me.essentials.protect.data.ProtectedBlockSQLite;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.Event.Type;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
public class EssentialsProtect extends JavaPlugin implements IConf
{
private EssentialsProtectBlockListener blockListener = null;
private EssentialsProtectPlayerListener playerListener = null;
private EssentialsProtectEntityListener entityListener = null;
public static final String AUTHORS = Essentials.AUTHORS;
private static final Logger logger = Logger.getLogger("Minecraft");
public static HashMap<String, Boolean> genSettings = null;
public static HashMap<String, String> dataSettings = null;
public static HashMap<String, Boolean> guardSettings = null;
public static HashMap<String, Boolean> playerSettings = null;
public static ArrayList usageList = null;
public static ArrayList blackListPlace = null;
public static ArrayList breakBlackList = null;
public static ArrayList onPlaceAlert = null;
public static ArrayList onUseAlert = null;
public static ArrayList onBreakAlert = null;
private IProtectedBlock storage = null;
private static EssentialsProtect instance = null;
public EssentialsProtect()
{
}
public void onEnable()
{
PluginManager pm = this.getServer().getPluginManager();
Essentials ess = (Essentials)pm.getPlugin("Essentials");
if (!ess.isEnabled()) {
pm.enablePlugin(ess);
}
instance = this;
reloadConfig();
playerListener = new EssentialsProtectPlayerListener(this);
blockListener = new EssentialsProtectBlockListener(this);
entityListener = new EssentialsProtectEntityListener(this);
pm.registerEvent(Type.PLAYER_PICKUP_ITEM, playerListener, Priority.Low, this);
pm.registerEvent(Type.PLAYER_INTERACT, playerListener, Priority.Low, this);
pm.registerEvent(Type.BLOCK_PLACE, blockListener, Priority.Highest, this);
pm.registerEvent(Type.BLOCK_FROMTO, blockListener, Priority.Highest, this);
pm.registerEvent(Type.BLOCK_IGNITE, blockListener, Priority.Highest, this);
pm.registerEvent(Type.BLOCK_BURN, blockListener, Priority.Highest, this);
pm.registerEvent(Type.ENTITY_EXPLODE, entityListener, Priority.Highest, this);
pm.registerEvent(Type.ENTITY_DAMAGE, entityListener, Priority.Highest, this);
pm.registerEvent(Type.BLOCK_BREAK, blockListener, Priority.Highest, this);
pm.registerEvent(Type.CREATURE_SPAWN, entityListener, Priority.Highest, this);
if (!this.getDescription().getVersion().equals(Essentials.getStatic().getDescription().getVersion())) {
logger.log(Level.WARNING, "Version mismatch! Please update all Essentials jars to the same version.");
}
logger.info("Loaded " + this.getDescription().getName() + " build " + this.getDescription().getVersion() + " maintained by " + AUTHORS);
}
public static boolean checkProtectionItems(ArrayList itemList, int id)
{
return !itemList.isEmpty() && itemList.contains(String.valueOf(id));
}
@Override
public void onDisable()
{
genSettings.clear();
dataSettings.clear();
blockListener = null;
playerListener = null;
entityListener = null;
genSettings = null;
dataSettings = null;
guardSettings = null;
playerSettings = null;
usageList = null;
blackListPlace = null;
onPlaceAlert = null;
onUseAlert = null;
onBreakAlert = null;
}
public void alert(User user, String item, String type)
{
Location loc = user.getLocation();
for (Player p : this.getServer().getOnlinePlayers())
{
User alertUser = User.get(p);
if (alertUser.isAuthorized("essentials.protect.alerts"))
alertUser.sendMessage(ChatColor.DARK_AQUA + "[" + user.getName() + "] " + ChatColor.WHITE + type + ChatColor.GOLD + item + " at: " + formatCoords(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()));
}
}
public static String formatCoords(int x, int y, int z)
{
return x + "," + y + "," + z;
}
public void reloadConfig() {
dataSettings = Essentials.getSettings().getEpDBSettings();
genSettings = Essentials.getSettings().getEpSettings();
guardSettings = Essentials.getSettings().getEpGuardSettings();
usageList = Essentials.getSettings().epBlackListUsage();
blackListPlace = Essentials.getSettings().epBlackListPlacement();
breakBlackList = Essentials.getSettings().epBlockBreakingBlacklist();
onPlaceAlert = Essentials.getSettings().getEpAlertOnPlacement();
onUseAlert = Essentials.getSettings().getEpAlertOnUse();
onBreakAlert = Essentials.getSettings().getEpAlertOnBreak();
playerSettings = Essentials.getSettings().getEpPlayerSettings();
if (dataSettings.get("protect.datatype").equals("mysql")) {
try {
storage = new ProtectedBlockMySQL(dataSettings.get("protect.mysqlDb"), dataSettings.get("protect.username"), dataSettings.get("protect.password"));
} catch (PropertyVetoException ex) {
logger.log(Level.SEVERE, null, ex);
}
} else {
try {
storage = new ProtectedBlockSQLite("jdbc:sqlite:plugins/Essentials/EssentialsProtect.db");
} catch (PropertyVetoException ex) {
logger.log(Level.SEVERE, null, ex);
}
}
if (genSettings.get("protect.memstore")) {
storage = new ProtectedBlockMemory(storage);
}
}
public static IProtectedBlock getStorage() {
return EssentialsProtect.instance.storage;
}
}

View File

@@ -0,0 +1,272 @@
package com.earth2me.essentials.protect;
import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.User;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockBurnEvent;
import org.bukkit.event.block.BlockFromToEvent;
import org.bukkit.event.block.BlockIgniteEvent;
import org.bukkit.event.block.BlockListener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack;
public class EssentialsProtectBlockListener extends BlockListener
{
private EssentialsProtect parent;
public EssentialsProtectBlockListener(EssentialsProtect parent)
{
Essentials.loadClasses();
this.parent = parent;
}
@Override
public void onBlockPlace(BlockPlaceEvent event)
{
if (event.isCancelled()) return;
ItemStack item = event.getItemInHand();
User user = User.get(event.getPlayer());
if (EssentialsProtect.playerSettings.get("protect.disable.build") && !user.canBuild())
{
event.setCancelled(true);
return;
}
int id = event.getBlockPlaced().getTypeId();
if (EssentialsProtect.checkProtectionItems(EssentialsProtect.blackListPlace, id) && !user.isAuthorized("essentials.protect.exemptplacement"))
{
event.setCancelled(true);
return;
}
if (!EssentialsProtect.onPlaceAlert.isEmpty() && EssentialsProtect.onPlaceAlert.contains(String.valueOf(item.getTypeId())))
{
parent.alert(user, item.getType().toString(), "placed: ");
}
Block blockPlaced = event.getBlockPlaced();
Block below = blockPlaced.getFace(BlockFace.DOWN);
if (below.getType() == Material.RAILS) {
if (EssentialsProtect.genSettings.get("protect.protect.prevent.block-on-rail"))
{
if (EssentialsProtect.getStorage().isProtected(below, user.getName())) {
event.setCancelled(true);
return;
}
}
}
List<Block> protect = new ArrayList<Block>();
if (blockPlaced.getType() == Material.RAILS) {
if (EssentialsProtect.genSettings.get("protect.protect.rails"))
{
if (user.isAuthorized("essentials.protect"))
{
protect.add(blockPlaced);
if (EssentialsProtect.genSettings.get("protect.protect.block-below"))
{
protect.add(blockPlaced.getFace(BlockFace.DOWN));
}
}
}
}
if (blockPlaced.getType() == Material.SIGN_POST || blockPlaced.getType() == Material.WALL_SIGN) {
if (EssentialsProtect.genSettings.get("protect.protect.signs"))
{
if (user.isAuthorized("essentials.protect"))
{
protect.add(blockPlaced);
if (EssentialsProtect.genSettings.get("protect.protect.block-below"))
{
protect.add(event.getBlockAgainst());
}
}
}
}
for (Block block : protect) {
EssentialsProtect.getStorage().protectBlock(block, user.getName());
}
}
@Override
public void onBlockIgnite(BlockIgniteEvent event)
{
Block block = event.getBlock();
if (block.getType() == Material.RAILS && EssentialsProtect.genSettings.get("protect.protect.rails"))
{
event.setCancelled(true);
return;
}
if ((block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST) && EssentialsProtect.genSettings.get("protect.protect.signs"))
{
event.setCancelled(true);
return;
}
if ((event.getCause().equals(BlockIgniteEvent.IgniteCause.SPREAD)))
{
event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.fire-spread"));
return;
}
if (event.getCause().equals(BlockIgniteEvent.IgniteCause.FLINT_AND_STEEL))
{
event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.flint-fire"));
return;
}
if (event.getCause().equals(BlockIgniteEvent.IgniteCause.LAVA))
{
event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.lava-fire-spread"));
return;
}
}
@Override
public void onBlockFromTo(BlockFromToEvent event)
{
if (event.isCancelled()) return;
Block block = event.getBlock();
Block toBlock = event.getToBlock();
if (toBlock.getType() == Material.RAILS && EssentialsProtect.genSettings.get("protect.protect.rails"))
{
event.setCancelled(true);
return;
}
if ((toBlock.getType() == Material.WALL_SIGN || toBlock.getType() == Material.SIGN_POST) && EssentialsProtect.genSettings.get("protect.protect.signs"))
{
event.setCancelled(true);
return;
}
if (block.getType() == Material.WATER || block.getType() == Material.STATIONARY_WATER)
{
event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.water-flow"));
return;
}
if (block.getType() == Material.LAVA || block.getType() == Material.STATIONARY_LAVA)
{
event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.lava-flow"));
return;
}
if (block.getType() == Material.AIR)
{
event.setCancelled(EssentialsProtect.guardSettings.get("protect.prevent.water-bucket-flow"));
return;
}
}
@Override
public void onBlockBurn(BlockBurnEvent event)
{
Block block = event.getBlock();
if (block.getType() == Material.RAILS && EssentialsProtect.genSettings.get("protect.protect.rails"))
{
event.setCancelled(true);
return;
}
if ((block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST) && EssentialsProtect.genSettings.get("protect.protect.signs"))
{
event.setCancelled(true);
return;
}
if (EssentialsProtect.guardSettings.get("protect.prevent.fire-spread"))
{
event.setCancelled(true);
return;
}
}
@Override
public void onBlockBreak(BlockBreakEvent event)
{
if (event.isCancelled()) return;
User user = User.get(event.getPlayer());
Block block = event.getBlock();
if (EssentialsProtect.playerSettings.get("protect.disable.build") && !user.canBuild())
{
event.setCancelled(true);
return;
}
if(EssentialsProtect.breakBlackList.contains(String.valueOf(block.getTypeId())) && !user.isAuthorized("essentials.protect.exemptbreak"))
{
event.setCancelled(true);
return;
}
if (!EssentialsProtect.onBreakAlert.isEmpty() && EssentialsProtect.onBreakAlert.contains(String.valueOf(block.getTypeId())))
{
parent.alert(user, block.getType().toString(), "broke: ");
}
if (user.isAuthorized("essentials.protect.admin"))
{
if (block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST || block.getType() == Material.RAILS)
{
EssentialsProtect.getStorage().unprotectBlock(block);
if (block.getType() == Material.RAILS || block.getType() == Material.SIGN_POST) {
Block below = block.getFace(BlockFace.DOWN);
EssentialsProtect.getStorage().unprotectBlock(below);
} else {
BlockFace[] faces = new BlockFace[] {
BlockFace.NORTH,
BlockFace.EAST,
BlockFace.SOUTH,
BlockFace.WEST
};
for (BlockFace blockFace : faces) {
Block against = block.getFace(blockFace);
EssentialsProtect.getStorage().unprotectBlock(against);
}
}
}
else
{
EssentialsProtect.getStorage().unprotectBlock(block);
}
return;
}
else
{
boolean isProtected = EssentialsProtect.getStorage().isProtected(block, user.getName());
if (!isProtected) {
if (block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST || block.getType() == Material.RAILS)
{
EssentialsProtect.getStorage().unprotectBlock(block);
if (block.getType() == Material.RAILS || block.getType() == Material.SIGN_POST) {
Block below = block.getFace(BlockFace.DOWN);
EssentialsProtect.getStorage().unprotectBlock(below);
} else {
BlockFace[] faces = new BlockFace[] {
BlockFace.NORTH,
BlockFace.EAST,
BlockFace.SOUTH,
BlockFace.WEST
};
for (BlockFace blockFace : faces) {
Block against = block.getFace(blockFace);
EssentialsProtect.getStorage().unprotectBlock(against);
}
}
}
else
{
EssentialsProtect.getStorage().unprotectBlock(block);
}
}
event.setCancelled(true);
return;
}
}
}

View File

@@ -0,0 +1,533 @@
package com.earth2me.essentials.protect;
import com.earth2me.essentials.Essentials;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.block.Block;
public class EssentialsProtectData
{
private static final Logger logger = Logger.getLogger("Minecraft");
private static final String mysqlDriver = "com.mysql.jdbc.Driver";
private static String mysqlUsername;
private static String mysqlPassword;
private static String mysqlDatabase;
private static String dataType;
private static final String sqlite = "jdbc:sqlite:plugins/Essentials/EssentialsProtect.db";
private static final String mysqlTable;
private static final String sqliteTable;
private static final String insertQuery;
private static final String countByLocationQuery;
private static final String countByPlayerLocationQuery;
private static final String playerByLocationQuery;
private static final String deleteByLocationQuery;
static
{
mysqlTable = EssentialsProtectSqlProperties.EssentialsProtect;
sqliteTable = EssentialsProtectSqlProperties.EssentialsProtect_sqlite;
insertQuery = EssentialsProtectSqlProperties.Insert;
countByLocationQuery = EssentialsProtectSqlProperties.CountByLocation;
countByPlayerLocationQuery = EssentialsProtectSqlProperties.CountByPLayerLocation;
playerByLocationQuery = EssentialsProtectSqlProperties.PlayerByLocation;
deleteByLocationQuery = EssentialsProtectSqlProperties.DeleteByLocation;
mysqlUsername = EssentialsProtect.dataSettings.get("protect.username");
mysqlPassword = EssentialsProtect.dataSettings.get("protect.password");
mysqlDatabase = EssentialsProtect.dataSettings.get("protect.mysqlDb");
dataType = EssentialsProtect.dataSettings.get("protect.datatype");
}
public EssentialsProtectData()
{
}
public static String formatCoords(int x, int y, int z)
{
return x + "," + y + "," + z;
}
public void insertProtectionIntoDb(String worldname, String playerName, int x, int y, int z)
{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try
{
if (dataType.contentEquals("mysql"))
{
Class.forName(mysqlDriver);
conn = DriverManager.getConnection(mysqlDatabase, mysqlUsername, mysqlPassword);
}
else
{
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection(sqlite);
}
ps = conn.prepareStatement(insertQuery);
ps.setString(1, worldname);
ps.setString(2, playerName);
ps.setInt(3, x);
ps.setInt(4, y);
ps.setInt(5, z);
ps.executeUpdate();
}
catch (SQLException ex)
{
logger.log(Level.SEVERE, "[EssentialsProtect] Unable to add protection into SQL", ex);
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
logger.log(Level.SEVERE, "[EssentialsProtect] Class not found", e);
}
finally
{
try
{
if (ps != null)
{
ps.close();
}
if (rs != null)
{
rs.close();
}
if (conn != null)
{
conn.close();
}
}
catch (SQLException ex)
{
logger.log(Level.SEVERE, "[EssentialsProtect] Could not close connection to SQL", ex);
}
}
}
public boolean canDestroy(String worldName, String playerName, Block block)
{
int x = block.getX();
int y = block.getY();
int z = block.getZ();
int rowCount = 0;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try
{
if (dataType.contentEquals("mysql"))
{
Class.forName(mysqlDriver);
conn = DriverManager.getConnection(mysqlDatabase, mysqlUsername, mysqlPassword);
}
else
{
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection(sqlite);
}
conn.setAutoCommit(false);
ps = conn.prepareStatement(countByLocationQuery);
ps.setString(1, worldName);
ps.setInt(2, x);
ps.setInt(3, y);
ps.setInt(4, z);
rs = ps.executeQuery();
rs.next();
rowCount = rs.getInt(1);
rs.close();
ps.close();
if (rowCount == 0)
{
return true;
}
else
{
ps = conn.prepareStatement(countByPlayerLocationQuery);
ps.setString(1, worldName);
ps.setString(2, playerName);
ps.setInt(3, x);
ps.setInt(4, y);
ps.setInt(5, z);
rs = ps.executeQuery();
rs.next();
rowCount = rs.getInt(1);
if (rowCount == 0)
{
return false;
}
}
}
catch (SQLException ex)
{
logger.log(Level.SEVERE, "[EssentialsProtect] Unable to query Protection", ex);
}
catch (Throwable e)
{
logger.log(Level.SEVERE, "[EssentialsProtect] Unable to query Protection", e);
}
finally
{
try
{
if (ps != null)
{
ps.close();
}
if (rs != null)
{
rs.close();
}
if (conn != null)
{
conn.close();
}
}
catch (SQLException ex)
{
logger.log(Level.SEVERE, "[EssentialsProtection] Could not close connection to SQL", ex);
}
}
return true;
}
@SuppressWarnings("CallToThreadDumpStack")
public static void createSqlTable()
{
Connection conn = null;
Statement st = null;
try
{
if (dataType.contentEquals("mysql"))
{
Class.forName(mysqlDriver);
conn = DriverManager.getConnection(mysqlDatabase, mysqlUsername, mysqlPassword);
}
else
{
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection(sqlite);
}
st = conn.createStatement();
st.executeUpdate(dataType.contentEquals("mysql") ? mysqlTable : sqliteTable);
}
catch (SQLException s)
{
logger.log(Level.SEVERE, "[EssentialsProtect] Could not create table for " + dataType, s);
}
catch (ClassNotFoundException ex)
{
logger.log(Level.SEVERE, "[EssentialsProtect] Could not find driver for " + dataType, ex);
}
catch (Throwable e)
{
logger.log(Level.SEVERE, "[EssentialsProtect] Unexpected error occured whilst creating table ", e);
}
finally
{
try
{
if (conn != null && !conn.isClosed())
{
try
{
conn.close();
}
catch (SQLException e)
{
logger.log(Level.SEVERE, "[EssentialsProtect] Unexpected error occured whilst closing the connection", e);
}
}
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public String getBlockOwner(String worldName, String playerName, Block block)
{
String returnPlayerName = null;
int x = block.getX();
int y = block.getY();
int z = block.getZ();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try
{
if (dataType.contentEquals("mysql"))
{
Class.forName(mysqlDriver);
conn = DriverManager.getConnection(mysqlDatabase, mysqlUsername, mysqlPassword);
}
else
{
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection(sqlite);
}
conn.setAutoCommit(false);
ps = conn.prepareStatement(playerByLocationQuery);
ps.setString(1, worldName);
ps.setInt(2, x);
ps.setInt(3, y);
ps.setInt(4, z);
rs = ps.executeQuery();
while (rs.next())
{
returnPlayerName = rs.getString("playerName");
}
rs.close();
ps.close();
}
catch (SQLException ex)
{
logger.log(Level.SEVERE, "[EssentialsProtect] Unable to query EssentialsProtection", ex);
}
catch (Throwable e)
{
logger.log(Level.SEVERE, "[EssentialsProtect] Unable to query EssentialsProtection", e);
}
finally
{
try
{
if (ps != null)
{
ps.close();
}
if (rs != null)
{
rs.close();
}
if (conn != null)
{
conn.close();
}
}
catch (SQLException ex)
{
logger.log(Level.SEVERE, "[EssentialsProtection] Could not close connection to SQL", ex);
}
}
return returnPlayerName;
}
public void removeProtectionFromDB(Block block)
{
try
{
Connection conn = null;
if (dataType.contentEquals("mysql"))
{
Class.forName(mysqlDriver);
conn = DriverManager.getConnection(mysqlDatabase, mysqlUsername,
mysqlPassword);
}
else
{
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection(sqlite);
}
PreparedStatement ps = null;
try
{
ps = conn.prepareStatement(deleteByLocationQuery);
ps.setString(1, block.getWorld().getName());
ps.setInt(2, block.getX());
ps.setInt(3, block.getY());
ps.setInt(4, block.getZ());
ps.executeUpdate();
}
catch (SQLException ex)
{
logger.log(Level.WARNING,
"[EssentialsProtect] Could not delete block data from database",
ex);
}
finally
{
if (conn != null && !conn.isClosed())
{
conn.close();
}
if (ps != null)
{
ps.close();
}
}
}
catch (Throwable e)
{
logger.log(Level.SEVERE, " [EssentialsProtect] Exception occured whilst trying to delete data from sql", e);
}
}
public void removeProtectionFromDB(Block block, boolean removeBelow)
{
try
{
Connection conn = null;
if (dataType.contentEquals("mysql"))
{
Class.forName(mysqlDriver);
conn = DriverManager.getConnection(mysqlDatabase, mysqlUsername, mysqlPassword);
}
else
{
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection(sqlite);
}
PreparedStatement ps = null;
try
{
ps = conn.prepareStatement(deleteByLocationQuery);
ps.setString(1, block.getWorld().getName());
ps.setInt(2, block.getX());
ps.setInt(3, block.getY());
ps.setInt(4, block.getZ());
ps.executeUpdate();
if (removeBelow)
{
ps = conn.prepareStatement(deleteByLocationQuery);
ps.setString(1, block.getWorld().getName());
ps.setInt(2, block.getX());
ps.setInt(3, block.getY() - 1);
ps.setInt(4, block.getZ());
ps.executeUpdate();
}
}
catch (SQLException ex)
{
logger.log(Level.WARNING, "[EssentialsProtect] Could not delete block data from database", ex);
}
finally
{
if (conn != null && !conn.isClosed())
{
conn.close();
}
if (ps != null)
{
ps.close();
}
}
}
catch (Throwable e)
{
logger.log(Level.SEVERE, "[EssentialsProtect] Exception occured whilst trying to delete data from sql", e);
}
}
public boolean isBlockAboveProtectedRail(Block block)
{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
if (block.getTypeId() == 66)
{
try
{
if (dataType.contentEquals("mysql"))
{
Class.forName(mysqlDriver);
conn = DriverManager.getConnection(mysqlDatabase, mysqlUsername, mysqlPassword);
}
else
{
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection(sqlite);
}
int rowCount = 0;
conn.setAutoCommit(false);
ps = conn.prepareStatement(countByLocationQuery);
ps.setString(1, block.getWorld().getName());
ps.setInt(2, block.getX());
ps.setInt(3, block.getY());
ps.setInt(4, block.getZ());
rs = ps.executeQuery();
rs.next();
rowCount = rs.getInt(1);
rs.close();
ps.close();
if (rowCount == 0)
{
return false;
}
else
{
return true;
}
}
catch (SQLException s)
{
logger.log(Level.SEVERE, "[EssentialsProtect] Could not query protection", s);
}
catch (ClassNotFoundException ex)
{
logger.log(Level.SEVERE, "[EssentialsProtect] Could not find driver for " + dataType, ex);
}
catch (Throwable e)
{
logger.log(Level.SEVERE, "[EssentialsProtect] Unexpected error occured whilst creating table", e);
}
finally
{
try
{
if (conn != null && !conn.isClosed())
{
try
{
conn.close();
}
catch (SQLException e)
{
logger.log(Level.SEVERE, "[EssentialsProtect] Unexpected error occured whilst closing the connection", e);
}
}
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return false;
}
}

View File

@@ -0,0 +1,197 @@
package com.earth2me.essentials.protect;
import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.User;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import net.minecraft.server.ChunkPosition;
import net.minecraft.server.Packet60Explosion;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.entity.Creeper;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Monster;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.EntityDamageByBlockEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageByProjectileEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.EntityListener;
public class EssentialsProtectEntityListener extends EntityListener
{
private EssentialsProtect parent;
public EssentialsProtectEntityListener(EssentialsProtect parent)
{
Essentials.loadClasses();
this.parent = parent;
}
@Override
public void onEntityDamage(EntityDamageEvent event)
{
if (event.isCancelled()) return;
if (event instanceof EntityDamageByBlockEvent)
{
DamageCause cause = event.getCause();
if (EssentialsProtect.playerSettings.get("protect.disable.contactdmg") && cause == DamageCause.CONTACT)
{
event.setCancelled(true);
return;
}
if (EssentialsProtect.playerSettings.get("protect.disable.lavadmg") && cause == DamageCause.LAVA)
{
event.setCancelled(true);
return;
}
if (EssentialsProtect.guardSettings.get("protect.prevent.tnt-explosion") && cause == DamageCause.BLOCK_EXPLOSION)
{
event.setCancelled(true);
return;
}
}
if (event instanceof EntityDamageByEntityEvent)
{
EntityDamageByEntityEvent edEvent = (EntityDamageByEntityEvent)event;
Entity eAttack = edEvent.getDamager();
Entity eDefend = edEvent.getEntity();
// PVP Settings
if (eDefend instanceof Player && eAttack instanceof Player)
{
if (EssentialsProtect.playerSettings.get("protect.disable.pvp"))
{
User defender = User.get(eDefend);
User attacker = User.get(eAttack);
if (!defender.isAuthorized("essentials.protect.pvp") || !attacker.isAuthorized("essentials.protect.pvp"))
{
event.setCancelled(true);
return;
}
}
}
//Creeper explode prevention
if (eAttack != null && eAttack instanceof Monster)
{
if (eAttack instanceof Creeper && EssentialsProtect.guardSettings.get("protect.prevent.creeper-explosion"))
{
event.setCancelled(true);
return;
}
if (eAttack instanceof Creeper && EssentialsProtect.guardSettings.get("protect.prevent.creeper-playerdamage"))
{
event.setCancelled(true);
return;
}
}
}
if (event instanceof EntityDamageByProjectileEvent)
{
if (event.getEntity() instanceof Player)
{
event.setCancelled(EssentialsProtect.playerSettings.get("protect.disable.projectiles"));
return;
}
}
DamageCause cause = event.getCause();
Entity casualty = event.getEntity();
if (casualty instanceof Player)
{
if (EssentialsProtect.playerSettings.get("protect.disable.fall") && cause == DamageCause.FALL)
{
event.setCancelled(true);
return;
}
if (EssentialsProtect.playerSettings.get("protect.disable.suffocate") && cause == DamageCause.SUFFOCATION)
{
event.setCancelled(true);
return;
}
if (EssentialsProtect.playerSettings.get("protect.disable.firedmg") && (cause == DamageCause.FIRE
|| cause == DamageCause.FIRE_TICK))
{
event.setCancelled(true);
return;
}
if (EssentialsProtect.playerSettings.get("protect.disable.drown") && cause == DamageCause.DROWNING)
{
event.setCancelled(true);
return;
}
}
}
@Override
public void onEntityExplode(EntityExplodeEvent event)
{
if (event.isCancelled()) return;
if (event.getEntity() instanceof LivingEntity)
{
//Nicccccccccce plaaacccccccccce..
int maxHeight = Essentials.getSettings().getEpCreeperMaxHeight();
if ( EssentialsProtect.guardSettings.get("protect.prevent.creeper-explosion") ||
EssentialsProtect.guardSettings.get("protect.prevent.creeper-blockdamage") ||
(maxHeight >= 0 && event.getLocation().getBlockY() > maxHeight))
{
HashSet<ChunkPosition> set = new HashSet<ChunkPosition>(event.blockList().size());
Player[] players = parent.getServer().getOnlinePlayers();
List<ChunkPosition> blocksUnderPlayers = new ArrayList<ChunkPosition>(players.length);
Location loc = event.getLocation();
for (Player player : players) {
if (player.getWorld().equals(loc.getWorld())) {
blocksUnderPlayers.add(
new ChunkPosition(
player.getLocation().getBlockX(),
player.getLocation().getBlockY() - 1,
player.getLocation().getBlockZ()));
}
}
for (Block block : event.blockList()) {
ChunkPosition cp = new ChunkPosition(block.getX(), block.getY(), block.getZ());
if (!blocksUnderPlayers.contains(cp)) {
set.add(cp);
}
}
((CraftServer)parent.getServer()).getServer().f.a(loc.getX(), loc.getY(), loc.getZ(), 64.0D,
new Packet60Explosion(loc.getX(), loc.getY(), loc.getZ(), 3.0f, set));
event.setCancelled(true);
return;
}
}
else
{ //OH NOES TNT
if (EssentialsProtect.guardSettings.get("protect.prevent.tnt-explosion"))
{
event.setCancelled(true);
return;
}
}
}
@Override
public void onCreatureSpawn(CreatureSpawnEvent event) {
String creatureName = event.getCreatureType().toString().toLowerCase();
if (creatureName == null || creatureName.isEmpty()) {
return;
}
if (EssentialsProtect.guardSettings.get("protect.prevent.spawn."+creatureName)) {
event.setCancelled(true);
}
}
}

View File

@@ -0,0 +1,70 @@
package com.earth2me.essentials.protect;
import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.User;
import org.bukkit.ChatColor;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.inventory.ItemStack;
public class EssentialsProtectPlayerListener extends PlayerListener
{
private EssentialsProtect parent;
public EssentialsProtectPlayerListener(EssentialsProtect parent)
{
Essentials.loadClasses();
this.parent = parent;
}
@Override
public void onPlayerInteract(PlayerInteractEvent event)
{
if (event.isCancelled()) return;
User user = User.get(event.getPlayer());
if (EssentialsProtect.playerSettings.get("protect.disable.build") && !user.canBuild())
{
event.setCancelled(true);
return;
}
if (user.isAuthorized("essentials.protect.admin"))
{
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String owner : EssentialsProtect.getStorage().getOwners(event.getClickedBlock())) {
if (!first) {
sb.append(", ");
}
first = false;
sb.append(owner);
}
String ownerNames = sb.toString();
if (ownerNames != null)
{
user.sendMessage(ChatColor.GOLD + "[EssentialsProtect] Protection owners: " + ownerNames);
}
}
}
@Override
public void onPlayerPickupItem(PlayerPickupItemEvent event)
{
if(event.isCancelled()) return;
ItemStack item = event.getItem().getItemStack();
User user = User.get(event.getPlayer());
if (EssentialsProtect.checkProtectionItems(EssentialsProtect.usageList, item.getTypeId()) && !user.isAuthorized("essentials.protect.exemptusage"))
{
event.setCancelled(true);
return;
}
if (EssentialsProtect.onUseAlert.contains(String.valueOf(item.getTypeId())))
{
parent.alert(user, item.getType().toString(), "used: ");
}
}
}

View File

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

View File

@@ -0,0 +1,12 @@
package com.earth2me.essentials.protect;
public class EssentialsProtectSqlProperties {
public static String EssentialsProtect="CREATE TABLE IF NOT EXISTS `EssentialsProtect` (`id` int(11) NOT NULL AUTO_INCREMENT, `worldName` varchar(150) NOT NULL, `playerName` varchar(150) NOT NULL, `x` int(11) NOT NULL, `y` int(11) NOT NULL, `z` int(11) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=205 DEFAULT CHARSET=latin1";
public static String EssentialsProtect_sqlite = "CREATE TABLE IF NOT EXISTS EssentialsProtect (id INTEGER PRIMARY KEY, worldName TEXT ,playerName TEXT, x NUMERIC, y NUMERIC, z NUMERIC)";
public static String CountByLocation="SELECT COUNT(*) from EssentialsProtect where worldName = ? and x = ? and y = ? and z = ? limit 10";
public static String CountByPLayerLocation="SELECT COUNT(*) from EssentialsProtect where worldName = ? and playerName =? and x = ? and y = ? and z = ? limit 10";
public static String DeleteByLocation="DELETE FROM EssentialsProtect WHERE worldName=? and x=? and y=? and z=?";
public static String Insert="INSERT INTO EssentialsProtect (worldName, playerName, x, y, z) VALUES (?,?,?,?,?)";
public static String PlayerByLocation="SELECT playerName FROM EssentialsProtect WHERE worldname = ? and x = ? and y = ? and z = ? LIMIT 10";
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,8 @@
# This determines the command prefix when there are conflicts (/name:home, /name:help, etc.)
name: EssentialsProtect
main: com.earth2me.essentials.protect.EssentialsProtect
# Note to developers: This next line cannot change, or the automatic versioning system will break.
version: TeamCity
website: http://www.earth2me.net:8001/
description: Provides protection for various parts of the world.
authors: [Zenexer, ementalo, Aelux, Brettflan, KimKandor, snowleo]