Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d1cb628473 | ||
|
|
44fc802e8b | ||
|
|
bfa1734d23 | ||
|
|
66139dacac | ||
|
|
714c24445d | ||
|
|
79f55a02cb | ||
|
|
484fe22e9e | ||
|
|
119d8b1a81 | ||
|
|
953d1f0aed | ||
|
|
2e225a9452 | ||
|
|
960dc2cd48 | ||
|
|
345ac35649 | ||
|
|
802114c605 | ||
|
|
16bbd30209 | ||
|
|
0021e789bc | ||
|
|
41daebb629 | ||
|
|
b496e88557 | ||
|
|
7c0e10d058 | ||
|
|
1d72447096 | ||
|
|
01e93a642e | ||
|
|
696ae7c967 | ||
|
|
b28202f1cb | ||
|
|
601e15db5a | ||
|
|
f30b413b46 | ||
|
|
a9c4a81abe | ||
|
|
d619ed5798 | ||
|
|
e94a9bf68f | ||
|
|
257c12e727 | ||
|
|
8b3eaa70fb | ||
|
|
c96bb890b4 |
No files matched your search
@@ -1,3 +1,24 @@
|
||||
2.0beta2:
|
||||
- Fixed a NullPointerException in the damage hook.
|
||||
|
||||
2.0beta1:
|
||||
- Added area protection. Use /region or /rg to define regions.
|
||||
|
||||
1.8.1
|
||||
- Fixed NullPointerException.
|
||||
|
||||
1.8
|
||||
- Updated for Minecraft beta.
|
||||
- Blacklist hooks have CHANGED. The new hooks are work better. on-create has
|
||||
been replaced with on-place (for block placement) and on-use
|
||||
(for item usage, light buckets and lighters). The old on-use is now
|
||||
on-right-click although it currently does nothing.
|
||||
- Added coordinates to blacklist file logger.
|
||||
- Added suffocation damage prevention.
|
||||
|
||||
1.7.1
|
||||
- Updated for v0.2.8.
|
||||
|
||||
1.7
|
||||
- Updated for v0.2.6_02 and hMod b129.
|
||||
- Added option to prevent water from damaging particular blocks
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
<project name="WorldGuard" default="dist" basedir=".">
|
||||
<description>WorldGuard fixes bugs with Minecraft SMP and provides for
|
||||
protection.</description>
|
||||
|
||||
<property name="src.dir" location="src"/>
|
||||
<property name="build.dir" location="build"/>
|
||||
<property name="dist.dir" location="dist"/>
|
||||
<property name="release.dir" location="release"/>
|
||||
<property name="lib.dir" location="lib"/>
|
||||
|
||||
<target name="init">
|
||||
<mkdir dir="${build.dir}"/>
|
||||
</target>
|
||||
|
||||
<!-- Compile the source files -->
|
||||
<target name="compile" depends="init">
|
||||
<javac srcdir="${src.dir}" deprecation="true" includeantruntime="true" destdir="${build.dir}" debug="true">
|
||||
<classpath>
|
||||
<fileset dir="${lib.dir}">
|
||||
<include name="*.jar"/>
|
||||
</fileset>
|
||||
</classpath>
|
||||
</javac>
|
||||
</target>
|
||||
|
||||
<!-- Build the .jar -->
|
||||
<target name="jar" depends="compile">
|
||||
<mkdir dir="${dist.dir}"/>
|
||||
<manifest file="manifest.mf" mode="replace">
|
||||
<attribute name="Implementation-Title" value="WorldGuard"/>
|
||||
<attribute name="Implementation-Version" value="${version}"/>
|
||||
<attribute name="WorldGuard-Version" value="${version}"/>
|
||||
<attribute name="Class-Path" value="WorldEdit.jar"/>
|
||||
</manifest>
|
||||
<jar jarfile="${dist.dir}/WorldGuard.jar" basedir="${build.dir}" manifest="manifest.mf"/>
|
||||
</target>
|
||||
|
||||
<!-- Create the .jar -->
|
||||
<target name="dist">
|
||||
<property name="version" value="nightly"/>
|
||||
<antcall target="jar"/>
|
||||
</target>
|
||||
|
||||
<!-- Build a release -->
|
||||
<target name="release">
|
||||
<input message="Enter version:" addproperty="version"/>
|
||||
<antcall target="jar"/>
|
||||
<delete dir="${release.dir}"/>
|
||||
<mkdir dir="${release.dir}"/>
|
||||
<copy todir="${release.dir}">
|
||||
<fileset dir="."/>
|
||||
<globmapper from="*.txt" to="*.txt"/>
|
||||
</copy>
|
||||
<copy file="worldguard.properties" todir="${release.dir}"/>
|
||||
<copy tofile="${release.dir}/WorldGuard.jar" file="${dist.dir}/WorldGuard.jar"/>
|
||||
<zip destfile="${release.dir}/worldguard-${version}.zip" basedir="${release.dir}" excludes="*.zip"/>
|
||||
<mkdir dir="${release.dir}/src"/>
|
||||
<copy todir="${release.dir}/src">
|
||||
<fileset dir="${src.dir}"/>
|
||||
</copy>
|
||||
<zip destfile="${release.dir}/worldguard-${version}-source.zip" basedir="${release.dir}" excludes="*.zip"/>
|
||||
</target>
|
||||
|
||||
<!-- Clean the output -->
|
||||
<target name="clean">
|
||||
<delete dir="${build.dir}"/>
|
||||
<delete dir="${dist.dir}"/>
|
||||
<delete dir="${release.dir}"/>
|
||||
</target>
|
||||
|
||||
<!--<target name="-get-version-git" if="git-revision">
|
||||
<sequential>
|
||||
<exec executable="git" failifexecutionfails="false" outputproperty="git-revision">
|
||||
<arg value="rev-parse"/>
|
||||
<arg value="HEAD"/>
|
||||
</exec>
|
||||
<exec executable="git" outputproperty="git-revision-dirty">
|
||||
<arg value="diff"/>
|
||||
<arg value="- -shortstat"/>
|
||||
</exec>
|
||||
<condition else="dev-${git-revision}-modified" property="version" value="dev-${git-revision}">
|
||||
<equals arg1="" arg2="${git-revision-dirty}"/>
|
||||
</condition>
|
||||
</sequential>
|
||||
</target>-->
|
||||
</project>
|
||||
@@ -1,2 +0,0 @@
|
||||
Manifest-Version: 1.0
|
||||
WorldGuard-Version: 1.7
|
||||
+32
-11
@@ -141,20 +141,20 @@ public class Blacklist {
|
||||
}
|
||||
|
||||
/**
|
||||
* Called on right click. Returns true to let the action pass through.
|
||||
* Called on place. Returns true to let the action pass through.
|
||||
*
|
||||
* @param item
|
||||
* @param player
|
||||
* @return
|
||||
*/
|
||||
public boolean onCreate(int item, Player player) {
|
||||
public boolean onPlace(int item, Player player) {
|
||||
List<BlacklistEntry> entries = getEntries(item);
|
||||
if (entries == null) {
|
||||
return true;
|
||||
}
|
||||
boolean ret = true;
|
||||
for (BlacklistEntry entry : entries) {
|
||||
if (!entry.onCreate(item, player)) {
|
||||
if (!entry.onPlace(item, player)) {
|
||||
ret = false;
|
||||
}
|
||||
}
|
||||
@@ -164,18 +164,39 @@ public class Blacklist {
|
||||
/**
|
||||
* Called on right click upon. Returns true to let the action pass through.
|
||||
*
|
||||
* @param item
|
||||
* @param block
|
||||
* @param player
|
||||
* @return
|
||||
*/
|
||||
public boolean onUse(Block block, Player player) {
|
||||
public boolean onRightClick(Block block, Player player) {
|
||||
List<BlacklistEntry> entries = getEntries(block.getType());
|
||||
if (entries == null) {
|
||||
return true;
|
||||
}
|
||||
boolean ret = true;
|
||||
for (BlacklistEntry entry : entries) {
|
||||
if (!entry.onUse(block, player)) {
|
||||
if (!entry.onRightClick(block, player)) {
|
||||
ret = false;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called on item use. Returns true to let the action pass through.
|
||||
*
|
||||
* @param item
|
||||
* @param player
|
||||
* @return
|
||||
*/
|
||||
public boolean onUse(int item, Player player) {
|
||||
List<BlacklistEntry> entries = getEntries(item);
|
||||
if (entries == null) {
|
||||
return true;
|
||||
}
|
||||
boolean ret = true;
|
||||
for (BlacklistEntry entry : entries) {
|
||||
if (!entry.onUse(item, player)) {
|
||||
ret = false;
|
||||
}
|
||||
}
|
||||
@@ -345,12 +366,12 @@ public class Blacklist {
|
||||
} else if (parts[0].equalsIgnoreCase("on-left")
|
||||
|| parts[0].equalsIgnoreCase("on-destroy-with")) {
|
||||
entry.setDestroyWithActions(parts[1].split(","));
|
||||
} else if (parts[0].equalsIgnoreCase("on-right")
|
||||
|| parts[0].equalsIgnoreCase("on-create")) {
|
||||
entry.setCreateActions(parts[1].split(","));
|
||||
} else if (parts[0].equalsIgnoreCase("on-right-on")
|
||||
|| parts[0].equalsIgnoreCase("on-use")) {
|
||||
} else if (parts[0].equalsIgnoreCase("on-place")) {
|
||||
entry.setPlaceActions(parts[1].split(","));
|
||||
} else if (parts[0].equalsIgnoreCase("on-use")) {
|
||||
entry.setUseActions(parts[1].split(","));
|
||||
} else if (parts[0].equalsIgnoreCase("on-right-click")) {
|
||||
entry.setRightClickActions(parts[1].split(","));
|
||||
} else if (parts[0].equalsIgnoreCase("on-drop")) {
|
||||
entry.setDropActions(parts[1].split(","));
|
||||
} else if (parts[0].equalsIgnoreCase("on-acquire")) {
|
||||
|
||||
+69
-17
@@ -17,8 +17,6 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import java.util.logging.Logger;
|
||||
import java.util.logging.Level;
|
||||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
@@ -55,13 +53,17 @@ public class BlacklistEntry {
|
||||
*/
|
||||
private String[] destroyWithActions;
|
||||
/**
|
||||
* List of actions to perform on right click.
|
||||
* List of actions to perform on block placement.
|
||||
*/
|
||||
private String[] createActions;
|
||||
private String[] placeActions;
|
||||
/**
|
||||
* List of actions to perform on item use.
|
||||
*/
|
||||
private String[] useActions;
|
||||
/**
|
||||
* List of actions to perform on right click upon.
|
||||
*/
|
||||
private String[] useActions;
|
||||
private String[] rightClickActions;
|
||||
/**
|
||||
* List of actions to perform on drop.
|
||||
*/
|
||||
@@ -160,17 +162,17 @@ public class BlacklistEntry {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the rightClickActions
|
||||
* @return
|
||||
*/
|
||||
public String[] getCreateActions() {
|
||||
return createActions;
|
||||
public String[] getPlaceActions() {
|
||||
return placeActions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param actions
|
||||
*/
|
||||
public void setCreateActions(String[] actions) {
|
||||
this.createActions = actions;
|
||||
public void setPlaceActions(String[] actions) {
|
||||
this.placeActions = actions;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -187,6 +189,20 @@ public class BlacklistEntry {
|
||||
this.useActions = actions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
public String[] getRightClickActions() {
|
||||
return rightClickActions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param actions
|
||||
*/
|
||||
public void setRightClickActions(String[] actions) {
|
||||
this.rightClickActions = actions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
@@ -401,14 +417,14 @@ public class BlacklistEntry {
|
||||
}
|
||||
|
||||
/**
|
||||
* Called on right click. Returns true to let the action pass through.
|
||||
* Called on block placement. Returns true to let the action pass through.
|
||||
*
|
||||
* @param item
|
||||
* @param player
|
||||
* @return
|
||||
*/
|
||||
public boolean onCreate(final int item, final Player player) {
|
||||
if (createActions == null) {
|
||||
public boolean onPlace(final int item, final Player player) {
|
||||
if (placeActions == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -416,7 +432,7 @@ public class BlacklistEntry {
|
||||
|
||||
ActionHandler handler = new ActionHandler() {
|
||||
public void log(String itemName) {
|
||||
blacklist.getLogger().logCreateAttempt(player, item, comment);
|
||||
blacklist.getLogger().logPlaceAttempt(player, item, comment);
|
||||
}
|
||||
public void kick(String itemName) {
|
||||
player.kick("You can't place " + itemName);
|
||||
@@ -433,7 +449,43 @@ public class BlacklistEntry {
|
||||
}
|
||||
};
|
||||
|
||||
return process(item, player, createActions, handler, true, false);
|
||||
return process(item, player, placeActions, handler, true, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called on use. Returns true to let the action pass through.
|
||||
*
|
||||
* @param item
|
||||
* @param player
|
||||
* @return
|
||||
*/
|
||||
public boolean onUse(final int item, final Player player) {
|
||||
if (useActions == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
final BlacklistEntry entry = this;
|
||||
|
||||
ActionHandler handler = new ActionHandler() {
|
||||
public void log(String itemName) {
|
||||
blacklist.getLogger().logUseAttempt(player, item, comment);
|
||||
}
|
||||
public void kick(String itemName) {
|
||||
player.kick("You can't use " + itemName);
|
||||
}
|
||||
public void ban(String itemName) {
|
||||
entry.banPlayer(player, "Banned: You can't use " + itemName);
|
||||
}
|
||||
public void notifyAdmins(String itemName) {
|
||||
entry.notifyAdmins(player.getName() + " (use) " + itemName
|
||||
+ (comment != null ? " (" + comment + ")" : "") + ".");
|
||||
}
|
||||
public void tell(String itemName) {
|
||||
player.sendMessage(Colors.Yellow + "You're not allowed to use " + itemName + ".");
|
||||
}
|
||||
};
|
||||
|
||||
return process(item, player, useActions, handler, false, false);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -443,7 +495,7 @@ public class BlacklistEntry {
|
||||
* @param player
|
||||
* @return
|
||||
*/
|
||||
public boolean onUse(final Block block, final Player player) {
|
||||
public boolean onRightClick(final Block block, final Player player) {
|
||||
if (useActions == null) {
|
||||
return true;
|
||||
}
|
||||
@@ -452,7 +504,7 @@ public class BlacklistEntry {
|
||||
|
||||
ActionHandler handler = new ActionHandler() {
|
||||
public void log(String itemName) {
|
||||
blacklist.getLogger().logUseAttempt(player, block, comment);
|
||||
blacklist.getLogger().logRightClickAttempt(player, block, comment);
|
||||
}
|
||||
public void kick(String itemName) {
|
||||
player.kick("You can't use " + itemName);
|
||||
|
||||
@@ -88,9 +88,21 @@ public class BlacklistLogger implements BlacklistLoggerHandler {
|
||||
* @param player
|
||||
* @param block
|
||||
*/
|
||||
public void logUseAttempt(Player player, Block block, String comment) {
|
||||
public void logUseAttempt(Player player, int item, String comment) {
|
||||
for (BlacklistLoggerHandler handler : handlers) {
|
||||
handler.logUseAttempt(player, block, comment);
|
||||
handler.logUseAttempt(player, item, comment);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a right click on attempt.
|
||||
*
|
||||
* @param player
|
||||
* @param block
|
||||
*/
|
||||
public void logRightClickAttempt(Player player, Block block, String comment) {
|
||||
for (BlacklistLoggerHandler handler : handlers) {
|
||||
handler.logRightClickAttempt(player, block, comment);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,14 +119,14 @@ public class BlacklistLogger implements BlacklistLoggerHandler {
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a right click attempt.
|
||||
* Log a place attempt.
|
||||
*
|
||||
* @param player
|
||||
* @param item
|
||||
*/
|
||||
public void logCreateAttempt(Player player, int item, String comment) {
|
||||
public void logPlaceAttempt(Player player, int item, String comment) {
|
||||
for (BlacklistLoggerHandler handler : handlers) {
|
||||
handler.logCreateAttempt(player, item, comment);
|
||||
handler.logPlaceAttempt(player, item, comment);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,12 +38,12 @@ public interface BlacklistLoggerHandler {
|
||||
*/
|
||||
public void logBreakAttempt(Player player, Block block, String comment);
|
||||
/**
|
||||
* Log a right click on attempt.
|
||||
* Log a use attempt.
|
||||
*
|
||||
* @param player
|
||||
* @param block
|
||||
*/
|
||||
public void logUseAttempt(Player player, Block block, String comment);
|
||||
public void logUseAttempt(Player player, int item, String comment);
|
||||
/**
|
||||
* Right a left click attempt.
|
||||
*
|
||||
@@ -52,12 +52,19 @@ public interface BlacklistLoggerHandler {
|
||||
*/
|
||||
public void logDestroyWithAttempt(Player player, int item, String comment);
|
||||
/**
|
||||
* Log a right click attempt.
|
||||
* Log a place attempt.
|
||||
*
|
||||
* @param player
|
||||
* @param item
|
||||
*/
|
||||
public void logCreateAttempt(Player player, int item, String comment);
|
||||
public void logPlaceAttempt(Player player, int item, String comment);
|
||||
/**
|
||||
* Log a right click on attempt.
|
||||
*
|
||||
* @param player
|
||||
* @param item
|
||||
*/
|
||||
public void logRightClickAttempt(Player player, Block block, String comment);
|
||||
/**
|
||||
* Log a drop attempt.
|
||||
*
|
||||
|
||||
@@ -60,9 +60,9 @@ public class ConsoleLoggerHandler implements BlacklistLoggerHandler {
|
||||
* @param player
|
||||
* @param block
|
||||
*/
|
||||
public void logUseAttempt(Player player, Block block, String comment) {
|
||||
public void logRightClickAttempt(Player player, Block block, String comment) {
|
||||
logger.log(Level.INFO, "WorldGuard: " + player.getName()
|
||||
+ " tried to use " + getFriendlyItemName(block.getType())
|
||||
+ " tried to right click " + getFriendlyItemName(block.getType())
|
||||
+ (comment != null ? " (" + comment + ")" : ""));
|
||||
}
|
||||
|
||||
@@ -84,9 +84,21 @@ public class ConsoleLoggerHandler implements BlacklistLoggerHandler {
|
||||
* @param player
|
||||
* @param item
|
||||
*/
|
||||
public void logCreateAttempt(Player player, int item, String comment) {
|
||||
public void logPlaceAttempt(Player player, int item, String comment) {
|
||||
logger.log(Level.INFO, "WorldGuard: " + player.getName()
|
||||
+ " tried to create " + getFriendlyItemName(item)
|
||||
+ " tried to place " + getFriendlyItemName(item)
|
||||
+ (comment != null ? " (" + comment + ")" : ""));
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a use attempt.
|
||||
*
|
||||
* @param player
|
||||
* @param item
|
||||
*/
|
||||
public void logUseAttempt(Player player, int item, String comment) {
|
||||
logger.log(Level.INFO, "WorldGuard: " + player.getName()
|
||||
+ " tried to use " + getFriendlyItemName(item)
|
||||
+ (comment != null ? " (" + comment + ")" : ""));
|
||||
}
|
||||
|
||||
|
||||
@@ -146,8 +146,8 @@ public class DatabaseLoggerHandler implements BlacklistLoggerHandler {
|
||||
* @param player
|
||||
* @param block
|
||||
*/
|
||||
public void logUseAttempt(Player player, Block block, String comment) {
|
||||
logEvent("USE", player.getName(),
|
||||
public void logRightClickAttempt(Player player, Block block, String comment) {
|
||||
logEvent("RIGHT_CLICK", player.getName(),
|
||||
block.getX(), block.getY(), block.getZ(), block.getType(),
|
||||
comment);
|
||||
}
|
||||
@@ -165,13 +165,25 @@ public class DatabaseLoggerHandler implements BlacklistLoggerHandler {
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a right click attempt.
|
||||
* Log a place attempt.
|
||||
*
|
||||
* @param player
|
||||
* @param item
|
||||
*/
|
||||
public void logCreateAttempt(Player player, int item, String comment) {
|
||||
logEvent("CREATE", player.getName(),
|
||||
public void logPlaceAttempt(Player player, int item, String comment) {
|
||||
logEvent("PLACE", player.getName(),
|
||||
(int)Math.floor(player.getX()), (int)Math.floor(player.getY()),
|
||||
(int)Math.floor(player.getZ()), item, comment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a use attempt.
|
||||
*
|
||||
* @param player
|
||||
* @param item
|
||||
*/
|
||||
public void logUseAttempt(Player player, int item, String comment) {
|
||||
logEvent("USE", player.getName(),
|
||||
(int)Math.floor(player.getX()), (int)Math.floor(player.getY()),
|
||||
(int)Math.floor(player.getZ()), item, comment);
|
||||
}
|
||||
|
||||
@@ -93,8 +93,7 @@ public class FileLoggerHandler implements BlacklistLoggerHandler {
|
||||
private String buildPath(String playerName) {
|
||||
GregorianCalendar calendar = new GregorianCalendar();
|
||||
|
||||
Pattern p = Pattern.compile("%.");
|
||||
Matcher m = p.matcher(pathPattern);
|
||||
Matcher m = pattern.matcher(pathPattern);
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
|
||||
// Pattern replacements
|
||||
@@ -206,6 +205,16 @@ public class FileLoggerHandler implements BlacklistLoggerHandler {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the coordinates of a block in text form for the log.
|
||||
*
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
private String getCoordinates(Block block) {
|
||||
return "@" + block.getX() + "," + block.getY() + "," + block.getZ();
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a block destroy attempt.
|
||||
*
|
||||
@@ -213,7 +222,8 @@ public class FileLoggerHandler implements BlacklistLoggerHandler {
|
||||
* @param block
|
||||
*/
|
||||
public void logDestroyAttempt(Player player, Block block, String comment) {
|
||||
log(player, "Tried to destroy " + getFriendlyItemName(block.getType()),
|
||||
log(player, "Tried to destroy " + getFriendlyItemName(block.getType())
|
||||
+ " " + getCoordinates(block),
|
||||
comment);
|
||||
}
|
||||
|
||||
@@ -224,7 +234,8 @@ public class FileLoggerHandler implements BlacklistLoggerHandler {
|
||||
* @param block
|
||||
*/
|
||||
public void logBreakAttempt(Player player, Block block, String comment) {
|
||||
log(player, "Tried to break " + getFriendlyItemName(block.getType()),
|
||||
log(player, "Tried to break " + getFriendlyItemName(block.getType())
|
||||
+ " " + getCoordinates(block),
|
||||
comment);
|
||||
}
|
||||
|
||||
@@ -234,8 +245,9 @@ public class FileLoggerHandler implements BlacklistLoggerHandler {
|
||||
* @param player
|
||||
* @param block
|
||||
*/
|
||||
public void logUseAttempt(Player player, Block block, String comment) {
|
||||
log(player, "Tried to use " + getFriendlyItemName(block.getType()),
|
||||
public void logRightClickAttempt(Player player, Block block, String comment) {
|
||||
log(player, "Tried to right click " + getFriendlyItemName(block.getType())
|
||||
+ " " + getCoordinates(block),
|
||||
comment);
|
||||
}
|
||||
|
||||
@@ -255,8 +267,18 @@ public class FileLoggerHandler implements BlacklistLoggerHandler {
|
||||
* @param player
|
||||
* @param item
|
||||
*/
|
||||
public void logCreateAttempt(Player player, int item, String comment) {
|
||||
log(player, "Tried to create " + getFriendlyItemName(item), comment);
|
||||
public void logPlaceAttempt(Player player, int item, String comment) {
|
||||
log(player, "Tried to place " + getFriendlyItemName(item), comment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a block use attempt.
|
||||
*
|
||||
* @param player
|
||||
* @param item
|
||||
*/
|
||||
public void logUseAttempt(Player player, int item, String comment) {
|
||||
log(player, "Tried to use " + getFriendlyItemName(item), comment);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldGuard
|
||||
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import com.sk89q.worldguard.LocalPlayer;
|
||||
|
||||
public class HMPlayer extends LocalPlayer {
|
||||
private Player player;
|
||||
|
||||
public HMPlayer(Player player) {
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return player.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasGroup(String group) {
|
||||
return player.isInGroup(group);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldGuard
|
||||
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Inventory related functions.
|
||||
*
|
||||
* @author sk89q
|
||||
*/
|
||||
public class ItemArrayUtil {
|
||||
/**
|
||||
* Set the contents of an ItemArray.
|
||||
*
|
||||
* @param itemArray
|
||||
* @param contents
|
||||
*/
|
||||
public static void setContents(ItemArray<?> itemArray, Item[] contents) {
|
||||
int size = itemArray.getContentsSize();
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (contents[i] == null) {
|
||||
itemArray.removeItem(i);
|
||||
} else {
|
||||
itemArray.setSlot(contents[i], i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,8 +17,6 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import java.util.logging.Logger;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.LogRecord;
|
||||
|
||||
|
||||
+29
-47
@@ -25,7 +25,6 @@ import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.net.URL;
|
||||
import java.io.*;
|
||||
import java.lang.reflect.*;
|
||||
|
||||
/**
|
||||
* Entry point for the plugin for hey0's mod.
|
||||
@@ -46,7 +45,14 @@ public class WorldGuard extends Plugin {
|
||||
* Initialize the plugin.
|
||||
*/
|
||||
public WorldGuard() {
|
||||
listener = new WorldGuardListener(this);
|
||||
try {
|
||||
listener = new WorldGuardListener(this);
|
||||
} catch (NoClassDefFoundError e) {
|
||||
logger.severe("*** WORLDGUARD FAILED TO LOAD. ALL PROTECTION IS DISABLED!");
|
||||
logger.severe("*** YOUR SERVER WILL BE SAVED AND STOPPED TO PREVENT DAMAGE TO YOUR WORLD. DISABLE WORLDGUARD OR CORRECT THE PROBLEM.");
|
||||
logger.severe("*** WorldEdit must be placed into the plugins/ directory");
|
||||
etc.getServer().useConsoleCommand("stop");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,22 +60,17 @@ public class WorldGuard extends Plugin {
|
||||
*/
|
||||
@Override
|
||||
public void initialize() {
|
||||
PluginLoader loader = etc.getLoader();
|
||||
|
||||
if (listener == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<String> missingFeatures = new ArrayList<String>();
|
||||
|
||||
registerHook("COMMAND", PluginListener.Priority.MEDIUM);
|
||||
registerHook("SERVERCOMMAND", PluginListener.Priority.MEDIUM);
|
||||
if (!registerHook("EXPLODE", PluginListener.Priority.HIGH)) {
|
||||
missingFeatures.add("disabling TNT or creeper explosions");
|
||||
}
|
||||
if (!registerHook("IGNITE", PluginListener.Priority.HIGH)) {
|
||||
missingFeatures.add("disabling fire or lava fire");
|
||||
}
|
||||
if (!registerHook("FLOW", PluginListener.Priority.HIGH)) {
|
||||
missingFeatures.add("controlling lava flow");
|
||||
missingFeatures.add("sponge simulation");
|
||||
}
|
||||
registerHook("EXPLODE", PluginListener.Priority.HIGH);
|
||||
registerHook("IGNITE", PluginListener.Priority.HIGH);
|
||||
registerHook("FLOW", PluginListener.Priority.HIGH);
|
||||
registerHook("LOGINCHECK", PluginListener.Priority.HIGH);
|
||||
registerHook("LOGIN", PluginListener.Priority.MEDIUM);
|
||||
registerHook("BLOCK_CREATED", PluginListener.Priority.HIGH);
|
||||
@@ -78,38 +79,15 @@ public class WorldGuard extends Plugin {
|
||||
registerHook("BLOCK_PLACE", PluginListener.Priority.HIGH);
|
||||
registerHook("DISCONNECT", PluginListener.Priority.HIGH);
|
||||
registerHook("ITEM_DROP", PluginListener.Priority.HIGH);
|
||||
if (!registerHook("ITEM_USE", PluginListener.Priority.HIGH)) {
|
||||
missingFeatures.add("denying use of the lava or water buckets");
|
||||
}
|
||||
if (!registerHook("ITEM_PICK_UP", PluginListener.Priority.HIGH)) {
|
||||
missingFeatures.add("denying item pickups");
|
||||
missingFeatures.add("the item durability fix");
|
||||
} else {
|
||||
try {
|
||||
Method method =
|
||||
Item.class.getDeclaredMethod("setDamage", new Class[]{ Integer.class });
|
||||
if (method == null) {
|
||||
missingFeatures.add("the item durability fix");
|
||||
}
|
||||
} catch (NoSuchMethodException e) {
|
||||
missingFeatures.add("the item durability fix");
|
||||
}
|
||||
}
|
||||
registerHook("COMPLEX_BLOCK_CHANGE", PluginListener.Priority.HIGH);
|
||||
registerHook("COMPLEX_BLOCK_SEND", PluginListener.Priority.HIGH);
|
||||
registerHook("INVENTORY_CHANGE", PluginListener.Priority.HIGH);
|
||||
if (!registerHook("BLOCK_PHYSICS", PluginListener.Priority.MEDIUM)) {
|
||||
missingFeatures.add("controlling the physics of gravel, sand, or portal blocks");
|
||||
}
|
||||
if (!registerHook("HEALTH_CHANGE", PluginListener.Priority.MEDIUM)) {
|
||||
missingFeatures.add("god mode");
|
||||
}
|
||||
if (!registerHook("DAMAGE", PluginListener.Priority.MEDIUM)) {
|
||||
missingFeatures.add("disabling fall, water, lava, and fire damage");
|
||||
}
|
||||
if (!registerHook("LIQUID_DESTROY", PluginListener.Priority.MEDIUM)) {
|
||||
missingFeatures.add("disabling water damage");
|
||||
}
|
||||
registerHook("ITEM_USE", PluginListener.Priority.HIGH);
|
||||
registerHook("ITEM_PICK_UP", PluginListener.Priority.HIGH);
|
||||
registerHook("SIGN_CHANGE", PluginListener.Priority.HIGH);
|
||||
registerHook("OPEN_INVENTORY", PluginListener.Priority.HIGH);
|
||||
registerHook("BLOCK_PHYSICS", PluginListener.Priority.MEDIUM);
|
||||
registerHook("HEALTH_CHANGE", PluginListener.Priority.MEDIUM);
|
||||
registerHook("DAMAGE", PluginListener.Priority.MEDIUM);
|
||||
registerHook("LIQUID_DESTROY", PluginListener.Priority.MEDIUM);
|
||||
registerHook("BLOCK_RIGHTCLICKED", PluginListener.Priority.MEDIUM);
|
||||
|
||||
if (missingFeatures.size() > 0) {
|
||||
logger.log(Level.WARNING, "WorldGuard: Your version of hMod does not support "
|
||||
@@ -133,7 +111,7 @@ public class WorldGuard extends Plugin {
|
||||
etc.getLoader().addListener(hook, listener, this, priority);
|
||||
return true;
|
||||
} catch (IllegalArgumentException e) {
|
||||
//logger.log(Level.WARNING, "WorldGuard: Missing hook " + name + "!");
|
||||
logger.log(Level.WARNING, "WorldGuard: Missing hook " + name + "!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -143,6 +121,10 @@ public class WorldGuard extends Plugin {
|
||||
*/
|
||||
@Override
|
||||
public void enable() {
|
||||
if (listener == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
logger.log(Level.INFO, "WorldGuard version " + getVersion() + " loaded");
|
||||
listener.loadConfiguration();
|
||||
|
||||
|
||||
+650
-136
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,307 @@
|
||||
package au.com.bytecode.opencsv;
|
||||
|
||||
/**
|
||||
Copyright 2005 Bytecode Pty Ltd.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A very simple CSV parser released under a commercial-friendly license.
|
||||
* This just implements splitting a single line into fields.
|
||||
*
|
||||
* @author Glen Smith
|
||||
* @author Rainer Pruy
|
||||
*
|
||||
*/
|
||||
public class CSVParser {
|
||||
|
||||
private final char separator;
|
||||
|
||||
private final char quotechar;
|
||||
|
||||
private final char escape;
|
||||
|
||||
private final boolean strictQuotes;
|
||||
|
||||
private String pending;
|
||||
private boolean inField = false;
|
||||
|
||||
private final boolean ignoreLeadingWhiteSpace;
|
||||
|
||||
/** The default separator to use if none is supplied to the constructor. */
|
||||
public static final char DEFAULT_SEPARATOR = ',';
|
||||
|
||||
public static final int INITIAL_READ_SIZE = 128;
|
||||
|
||||
/**
|
||||
* The default quote character to use if none is supplied to the
|
||||
* constructor.
|
||||
*/
|
||||
public static final char DEFAULT_QUOTE_CHARACTER = '"';
|
||||
|
||||
|
||||
/**
|
||||
* The default escape character to use if none is supplied to the
|
||||
* constructor.
|
||||
*/
|
||||
public static final char DEFAULT_ESCAPE_CHARACTER = '\\';
|
||||
|
||||
/**
|
||||
* The default strict quote behavior to use if none is supplied to the
|
||||
* constructor
|
||||
*/
|
||||
public static final boolean DEFAULT_STRICT_QUOTES = false;
|
||||
|
||||
/**
|
||||
* The default leading whitespace behavior to use if none is supplied to the
|
||||
* constructor
|
||||
*/
|
||||
public static final boolean DEFAULT_IGNORE_LEADING_WHITESPACE = true;
|
||||
|
||||
/**
|
||||
* Constructs CSVParser using a comma for the separator.
|
||||
*/
|
||||
public CSVParser() {
|
||||
this(DEFAULT_SEPARATOR, DEFAULT_QUOTE_CHARACTER, DEFAULT_ESCAPE_CHARACTER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs CSVParser with supplied separator.
|
||||
* @param separator
|
||||
* the delimiter to use for separating entries.
|
||||
*/
|
||||
public CSVParser(char separator) {
|
||||
this(separator, DEFAULT_QUOTE_CHARACTER, DEFAULT_ESCAPE_CHARACTER);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Constructs CSVParser with supplied separator and quote char.
|
||||
* @param separator
|
||||
* the delimiter to use for separating entries
|
||||
* @param quotechar
|
||||
* the character to use for quoted elements
|
||||
*/
|
||||
public CSVParser(char separator, char quotechar) {
|
||||
this(separator, quotechar, DEFAULT_ESCAPE_CHARACTER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs CSVReader with supplied separator and quote char.
|
||||
* @param separator
|
||||
* the delimiter to use for separating entries
|
||||
* @param quotechar
|
||||
* the character to use for quoted elements
|
||||
* @param escape
|
||||
* the character to use for escaping a separator or quote
|
||||
*/
|
||||
public CSVParser(char separator, char quotechar, char escape) {
|
||||
this(separator, quotechar, escape, DEFAULT_STRICT_QUOTES);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs CSVReader with supplied separator and quote char.
|
||||
* Allows setting the "strict quotes" flag
|
||||
* @param separator
|
||||
* the delimiter to use for separating entries
|
||||
* @param quotechar
|
||||
* the character to use for quoted elements
|
||||
* @param escape
|
||||
* the character to use for escaping a separator or quote
|
||||
* @param strictQuotes
|
||||
* if true, characters outside the quotes are ignored
|
||||
*/
|
||||
public CSVParser(char separator, char quotechar, char escape, boolean strictQuotes) {
|
||||
this(separator, quotechar, escape, strictQuotes, DEFAULT_IGNORE_LEADING_WHITESPACE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs CSVReader with supplied separator and quote char.
|
||||
* Allows setting the "strict quotes" and "ignore leading whitespace" flags
|
||||
* @param separator
|
||||
* the delimiter to use for separating entries
|
||||
* @param quotechar
|
||||
* the character to use for quoted elements
|
||||
* @param escape
|
||||
* the character to use for escaping a separator or quote
|
||||
* @param strictQuotes
|
||||
* if true, characters outside the quotes are ignored
|
||||
* @param ignoreLeadingWhiteSpace
|
||||
* if true, white space in front of a quote in a field is ignored
|
||||
*/
|
||||
public CSVParser(char separator, char quotechar, char escape, boolean strictQuotes, boolean ignoreLeadingWhiteSpace) {
|
||||
this.separator = separator;
|
||||
this.quotechar = quotechar;
|
||||
this.escape = escape;
|
||||
this.strictQuotes = strictQuotes;
|
||||
this.ignoreLeadingWhiteSpace = ignoreLeadingWhiteSpace;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @return true if something was left over from last call(s)
|
||||
*/
|
||||
public boolean isPending() {
|
||||
return pending != null;
|
||||
}
|
||||
|
||||
public String[] parseLineMulti(String nextLine) throws IOException {
|
||||
return parseLine(nextLine, true);
|
||||
}
|
||||
|
||||
public String[] parseLine(String nextLine) throws IOException {
|
||||
return parseLine(nextLine, false);
|
||||
}
|
||||
/**
|
||||
* Parses an incoming String and returns an array of elements.
|
||||
*
|
||||
* @param nextLine
|
||||
* the string to parse
|
||||
* @param multi
|
||||
* @return the comma-tokenized list of elements, or null if nextLine is null
|
||||
* @throws IOException if bad things happen during the read
|
||||
*/
|
||||
private String[] parseLine(String nextLine, boolean multi) throws IOException {
|
||||
|
||||
if (!multi && pending != null) {
|
||||
pending = null;
|
||||
}
|
||||
|
||||
if (nextLine == null) {
|
||||
if (pending != null) {
|
||||
String s = pending;
|
||||
pending = null;
|
||||
return new String[] {s};
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
List<String>tokensOnThisLine = new ArrayList<String>();
|
||||
StringBuilder sb = new StringBuilder(INITIAL_READ_SIZE);
|
||||
boolean inQuotes = false;
|
||||
if (pending != null) {
|
||||
sb.append(pending);
|
||||
pending = null;
|
||||
inQuotes = true;
|
||||
}
|
||||
for (int i = 0; i < nextLine.length(); i++) {
|
||||
|
||||
char c = nextLine.charAt(i);
|
||||
if (c == this.escape) {
|
||||
if( isNextCharacterEscapable(nextLine, inQuotes || inField, i) ){
|
||||
sb.append(nextLine.charAt(i+1));
|
||||
i++;
|
||||
}
|
||||
} else if (c == quotechar) {
|
||||
if( isNextCharacterEscapedQuote(nextLine, inQuotes || inField, i) ){
|
||||
sb.append(nextLine.charAt(i+1));
|
||||
i++;
|
||||
}else{
|
||||
inQuotes = !inQuotes;
|
||||
|
||||
// the tricky case of an embedded quote in the middle: a,bc"d"ef,g
|
||||
if (!strictQuotes) {
|
||||
if(i>2 //not on the beginning of the line
|
||||
&& nextLine.charAt(i-1) != this.separator //not at the beginning of an escape sequence
|
||||
&& nextLine.length()>(i+1) &&
|
||||
nextLine.charAt(i+1) != this.separator //not at the end of an escape sequence
|
||||
){
|
||||
|
||||
if (ignoreLeadingWhiteSpace && sb.length() > 0 && isAllWhiteSpace(sb)) {
|
||||
sb = new StringBuilder(INITIAL_READ_SIZE); //discard white space leading up to quote
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
inField = !inField;
|
||||
} else if (c == separator && !inQuotes) {
|
||||
tokensOnThisLine.add(sb.toString());
|
||||
sb = new StringBuilder(INITIAL_READ_SIZE); // start work on next token
|
||||
inField = false;
|
||||
} else {
|
||||
if (!strictQuotes || inQuotes){
|
||||
sb.append(c);
|
||||
inField = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
// line is done - check status
|
||||
if (inQuotes) {
|
||||
if (multi) {
|
||||
// continuing a quoted section, re-append newline
|
||||
sb.append("\n");
|
||||
pending = sb.toString();
|
||||
sb = null; // this partial content is not to be added to field list yet
|
||||
} else {
|
||||
throw new IOException("Un-terminated quoted field at end of CSV line");
|
||||
}
|
||||
}
|
||||
if (sb != null) {
|
||||
tokensOnThisLine.add(sb.toString());
|
||||
}
|
||||
return tokensOnThisLine.toArray(new String[tokensOnThisLine.size()]);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* precondition: the current character is a quote or an escape
|
||||
* @param nextLine the current line
|
||||
* @param inQuotes true if the current context is quoted
|
||||
* @param i current index in line
|
||||
* @return true if the following character is a quote
|
||||
*/
|
||||
private boolean isNextCharacterEscapedQuote(String nextLine, boolean inQuotes, int i) {
|
||||
return inQuotes // we are in quotes, therefore there can be escaped quotes in here.
|
||||
&& nextLine.length() > (i+1) // there is indeed another character to check.
|
||||
&& nextLine.charAt(i+1) == quotechar;
|
||||
}
|
||||
|
||||
/**
|
||||
* precondition: the current character is an escape
|
||||
* @param nextLine the current line
|
||||
* @param inQuotes true if the current context is quoted
|
||||
* @param i current index in line
|
||||
* @return true if the following character is a quote
|
||||
*/
|
||||
protected boolean isNextCharacterEscapable(String nextLine, boolean inQuotes, int i) {
|
||||
return inQuotes // we are in quotes, therefore there can be escaped quotes in here.
|
||||
&& nextLine.length() > (i+1) // there is indeed another character to check.
|
||||
&& ( nextLine.charAt(i+1) == quotechar || nextLine.charAt(i+1) == this.escape);
|
||||
}
|
||||
|
||||
/**
|
||||
* precondition: sb.length() > 0
|
||||
* @param sb A sequence of characters to examine
|
||||
* @return true if every character in the sequence is whitespace
|
||||
*/
|
||||
protected boolean isAllWhiteSpace(CharSequence sb) {
|
||||
boolean result = true;
|
||||
for (int i = 0; i < sb.length(); i++) {
|
||||
char c = sb.charAt(i);
|
||||
|
||||
if ( !Character.isWhitespace(c) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,282 @@
|
||||
package au.com.bytecode.opencsv;
|
||||
|
||||
/**
|
||||
Copyright 2005 Bytecode Pty Ltd.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A very simple CSV reader released under a commercial-friendly license.
|
||||
*
|
||||
* @author Glen Smith
|
||||
*
|
||||
*/
|
||||
public class CSVReader implements Closeable {
|
||||
|
||||
private BufferedReader br;
|
||||
|
||||
private boolean hasNext = true;
|
||||
|
||||
private CSVParser parser;
|
||||
|
||||
private int skipLines;
|
||||
|
||||
private boolean linesSkiped;
|
||||
|
||||
/**
|
||||
* The default line to start reading.
|
||||
*/
|
||||
public static final int DEFAULT_SKIP_LINES = 0;
|
||||
|
||||
/**
|
||||
* Constructs CSVReader using a comma for the separator.
|
||||
*
|
||||
* @param reader
|
||||
* the reader to an underlying CSV source.
|
||||
*/
|
||||
public CSVReader(Reader reader) {
|
||||
this(reader, CSVParser.DEFAULT_SEPARATOR, CSVParser.DEFAULT_QUOTE_CHARACTER, CSVParser.DEFAULT_ESCAPE_CHARACTER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs CSVReader with supplied separator.
|
||||
*
|
||||
* @param reader
|
||||
* the reader to an underlying CSV source.
|
||||
* @param separator
|
||||
* the delimiter to use for separating entries.
|
||||
*/
|
||||
public CSVReader(Reader reader, char separator) {
|
||||
this(reader, separator, CSVParser.DEFAULT_QUOTE_CHARACTER, CSVParser.DEFAULT_ESCAPE_CHARACTER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs CSVReader with supplied separator and quote char.
|
||||
*
|
||||
* @param reader
|
||||
* the reader to an underlying CSV source.
|
||||
* @param separator
|
||||
* the delimiter to use for separating entries
|
||||
* @param quotechar
|
||||
* the character to use for quoted elements
|
||||
*/
|
||||
public CSVReader(Reader reader, char separator, char quotechar) {
|
||||
this(reader, separator, quotechar, CSVParser.DEFAULT_ESCAPE_CHARACTER, DEFAULT_SKIP_LINES, CSVParser.DEFAULT_STRICT_QUOTES);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs CSVReader with supplied separator, quote char and quote handling
|
||||
* behavior.
|
||||
*
|
||||
* @param reader
|
||||
* the reader to an underlying CSV source.
|
||||
* @param separator
|
||||
* the delimiter to use for separating entries
|
||||
* @param quotechar
|
||||
* the character to use for quoted elements
|
||||
* @param strictQuotes
|
||||
* sets if characters outside the quotes are ignored
|
||||
*/
|
||||
public CSVReader(Reader reader, char separator, char quotechar, boolean strictQuotes) {
|
||||
this(reader, separator, quotechar, CSVParser.DEFAULT_ESCAPE_CHARACTER, DEFAULT_SKIP_LINES, strictQuotes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs CSVReader with supplied separator and quote char.
|
||||
*
|
||||
* @param reader
|
||||
* the reader to an underlying CSV source.
|
||||
* @param separator
|
||||
* the delimiter to use for separating entries
|
||||
* @param quotechar
|
||||
* the character to use for quoted elements
|
||||
* @param escape
|
||||
* the character to use for escaping a separator or quote
|
||||
*/
|
||||
|
||||
public CSVReader(Reader reader, char separator,
|
||||
char quotechar, char escape) {
|
||||
this(reader, separator, quotechar, escape, DEFAULT_SKIP_LINES, CSVParser.DEFAULT_STRICT_QUOTES);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs CSVReader with supplied separator and quote char.
|
||||
*
|
||||
* @param reader
|
||||
* the reader to an underlying CSV source.
|
||||
* @param separator
|
||||
* the delimiter to use for separating entries
|
||||
* @param quotechar
|
||||
* the character to use for quoted elements
|
||||
* @param line
|
||||
* the line number to skip for start reading
|
||||
*/
|
||||
public CSVReader(Reader reader, char separator, char quotechar, int line) {
|
||||
this(reader, separator, quotechar, CSVParser.DEFAULT_ESCAPE_CHARACTER, line, CSVParser.DEFAULT_STRICT_QUOTES);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs CSVReader with supplied separator and quote char.
|
||||
*
|
||||
* @param reader
|
||||
* the reader to an underlying CSV source.
|
||||
* @param separator
|
||||
* the delimiter to use for separating entries
|
||||
* @param quotechar
|
||||
* the character to use for quoted elements
|
||||
* @param escape
|
||||
* the character to use for escaping a separator or quote
|
||||
* @param line
|
||||
* the line number to skip for start reading
|
||||
*/
|
||||
public CSVReader(Reader reader, char separator, char quotechar, char escape, int line) {
|
||||
this(reader, separator, quotechar, escape, line, CSVParser.DEFAULT_STRICT_QUOTES);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs CSVReader with supplied separator and quote char.
|
||||
*
|
||||
* @param reader
|
||||
* the reader to an underlying CSV source.
|
||||
* @param separator
|
||||
* the delimiter to use for separating entries
|
||||
* @param quotechar
|
||||
* the character to use for quoted elements
|
||||
* @param escape
|
||||
* the character to use for escaping a separator or quote
|
||||
* @param line
|
||||
* the line number to skip for start reading
|
||||
* @param strictQuotes
|
||||
* sets if characters outside the quotes are ignored
|
||||
*/
|
||||
public CSVReader(Reader reader, char separator, char quotechar, char escape, int line, boolean strictQuotes) {
|
||||
this(reader, separator, quotechar, escape, line, strictQuotes, CSVParser.DEFAULT_IGNORE_LEADING_WHITESPACE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs CSVReader with supplied separator and quote char.
|
||||
*
|
||||
* @param reader
|
||||
* the reader to an underlying CSV source.
|
||||
* @param separator
|
||||
* the delimiter to use for separating entries
|
||||
* @param quotechar
|
||||
* the character to use for quoted elements
|
||||
* @param escape
|
||||
* the character to use for escaping a separator or quote
|
||||
* @param line
|
||||
* the line number to skip for start reading
|
||||
* @param strictQuotes
|
||||
* sets if characters outside the quotes are ignored
|
||||
* @param ignoreLeadingWhiteSpace
|
||||
* it true, parser should ignore white space before a quote in a field
|
||||
*/
|
||||
public CSVReader(Reader reader, char separator, char quotechar, char escape, int line, boolean strictQuotes, boolean ignoreLeadingWhiteSpace) {
|
||||
this.br = new BufferedReader(reader);
|
||||
this.parser = new CSVParser(separator, quotechar, escape, strictQuotes, ignoreLeadingWhiteSpace);
|
||||
this.skipLines = line;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the entire file into a List with each element being a String[] of
|
||||
* tokens.
|
||||
*
|
||||
* @return a List of String[], with each String[] representing a line of the
|
||||
* file.
|
||||
*
|
||||
* @throws IOException
|
||||
* if bad things happen during the read
|
||||
*/
|
||||
public List<String[]> readAll() throws IOException {
|
||||
|
||||
List<String[]> allElements = new ArrayList<String[]>();
|
||||
while (hasNext) {
|
||||
String[] nextLineAsTokens = readNext();
|
||||
if (nextLineAsTokens != null)
|
||||
allElements.add(nextLineAsTokens);
|
||||
}
|
||||
return allElements;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the next line from the buffer and converts to a string array.
|
||||
*
|
||||
* @return a string array with each comma-separated element as a separate
|
||||
* entry.
|
||||
*
|
||||
* @throws IOException
|
||||
* if bad things happen during the read
|
||||
*/
|
||||
public String[] readNext() throws IOException {
|
||||
|
||||
String[] result = null;
|
||||
do {
|
||||
String nextLine = getNextLine();
|
||||
if (!hasNext) {
|
||||
return result; // should throw if still pending?
|
||||
}
|
||||
String[] r = parser.parseLineMulti(nextLine);
|
||||
if (r.length > 0) {
|
||||
if (result == null) {
|
||||
result = r;
|
||||
} else {
|
||||
String[] t = new String[result.length+r.length];
|
||||
System.arraycopy(result, 0, t, 0, result.length);
|
||||
System.arraycopy(r, 0, t, result.length, r.length);
|
||||
result = t;
|
||||
}
|
||||
}
|
||||
} while (parser.isPending());
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the next line from the file.
|
||||
*
|
||||
* @return the next line from the file without trailing newline
|
||||
* @throws IOException
|
||||
* if bad things happen during the read
|
||||
*/
|
||||
private String getNextLine() throws IOException {
|
||||
if (!this.linesSkiped) {
|
||||
for (int i = 0; i < skipLines; i++) {
|
||||
br.readLine();
|
||||
}
|
||||
this.linesSkiped = true;
|
||||
}
|
||||
String nextLine = br.readLine();
|
||||
if (nextLine == null) {
|
||||
hasNext = false;
|
||||
}
|
||||
return hasNext ? nextLine : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the underlying reader.
|
||||
*
|
||||
* @throws IOException if the close fails
|
||||
*/
|
||||
public void close() throws IOException{
|
||||
br.close();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,302 @@
|
||||
package au.com.bytecode.opencsv;
|
||||
|
||||
/**
|
||||
Copyright 2005 Bytecode Pty Ltd.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.Writer;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A very simple CSV writer released under a commercial-friendly license.
|
||||
*
|
||||
* @author Glen Smith
|
||||
*
|
||||
*/
|
||||
public class CSVWriter implements Closeable {
|
||||
|
||||
public static final int INITIAL_STRING_SIZE = 128;
|
||||
|
||||
private Writer rawWriter;
|
||||
|
||||
private PrintWriter pw;
|
||||
|
||||
private char separator;
|
||||
|
||||
private char quotechar;
|
||||
|
||||
private char escapechar;
|
||||
|
||||
private String lineEnd;
|
||||
|
||||
/** The character used for escaping quotes. */
|
||||
public static final char DEFAULT_ESCAPE_CHARACTER = '"';
|
||||
|
||||
/** The default separator to use if none is supplied to the constructor. */
|
||||
public static final char DEFAULT_SEPARATOR = ',';
|
||||
|
||||
/**
|
||||
* The default quote character to use if none is supplied to the
|
||||
* constructor.
|
||||
*/
|
||||
public static final char DEFAULT_QUOTE_CHARACTER = '"';
|
||||
|
||||
/** The quote constant to use when you wish to suppress all quoting. */
|
||||
public static final char NO_QUOTE_CHARACTER = '\u0000';
|
||||
|
||||
/** The escape constant to use when you wish to suppress all escaping. */
|
||||
public static final char NO_ESCAPE_CHARACTER = '\u0000';
|
||||
|
||||
/** Default line terminator uses platform encoding. */
|
||||
public static final String DEFAULT_LINE_END = "\n";
|
||||
|
||||
private ResultSetHelper resultService = new ResultSetHelperService();
|
||||
|
||||
/**
|
||||
* Constructs CSVWriter using a comma for the separator.
|
||||
*
|
||||
* @param writer
|
||||
* the writer to an underlying CSV source.
|
||||
*/
|
||||
public CSVWriter(Writer writer) {
|
||||
this(writer, DEFAULT_SEPARATOR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs CSVWriter with supplied separator.
|
||||
*
|
||||
* @param writer
|
||||
* the writer to an underlying CSV source.
|
||||
* @param separator
|
||||
* the delimiter to use for separating entries.
|
||||
*/
|
||||
public CSVWriter(Writer writer, char separator) {
|
||||
this(writer, separator, DEFAULT_QUOTE_CHARACTER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs CSVWriter with supplied separator and quote char.
|
||||
*
|
||||
* @param writer
|
||||
* the writer to an underlying CSV source.
|
||||
* @param separator
|
||||
* the delimiter to use for separating entries
|
||||
* @param quotechar
|
||||
* the character to use for quoted elements
|
||||
*/
|
||||
public CSVWriter(Writer writer, char separator, char quotechar) {
|
||||
this(writer, separator, quotechar, DEFAULT_ESCAPE_CHARACTER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs CSVWriter with supplied separator and quote char.
|
||||
*
|
||||
* @param writer
|
||||
* the writer to an underlying CSV source.
|
||||
* @param separator
|
||||
* the delimiter to use for separating entries
|
||||
* @param quotechar
|
||||
* the character to use for quoted elements
|
||||
* @param escapechar
|
||||
* the character to use for escaping quotechars or escapechars
|
||||
*/
|
||||
public CSVWriter(Writer writer, char separator, char quotechar, char escapechar) {
|
||||
this(writer, separator, quotechar, escapechar, DEFAULT_LINE_END);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Constructs CSVWriter with supplied separator and quote char.
|
||||
*
|
||||
* @param writer
|
||||
* the writer to an underlying CSV source.
|
||||
* @param separator
|
||||
* the delimiter to use for separating entries
|
||||
* @param quotechar
|
||||
* the character to use for quoted elements
|
||||
* @param lineEnd
|
||||
* the line feed terminator to use
|
||||
*/
|
||||
public CSVWriter(Writer writer, char separator, char quotechar, String lineEnd) {
|
||||
this(writer, separator, quotechar, DEFAULT_ESCAPE_CHARACTER, lineEnd);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Constructs CSVWriter with supplied separator, quote char, escape char and line ending.
|
||||
*
|
||||
* @param writer
|
||||
* the writer to an underlying CSV source.
|
||||
* @param separator
|
||||
* the delimiter to use for separating entries
|
||||
* @param quotechar
|
||||
* the character to use for quoted elements
|
||||
* @param escapechar
|
||||
* the character to use for escaping quotechars or escapechars
|
||||
* @param lineEnd
|
||||
* the line feed terminator to use
|
||||
*/
|
||||
public CSVWriter(Writer writer, char separator, char quotechar, char escapechar, String lineEnd) {
|
||||
this.rawWriter = writer;
|
||||
this.pw = new PrintWriter(writer);
|
||||
this.separator = separator;
|
||||
this.quotechar = quotechar;
|
||||
this.escapechar = escapechar;
|
||||
this.lineEnd = lineEnd;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the entire list to a CSV file. The list is assumed to be a
|
||||
* String[]
|
||||
*
|
||||
* @param allLines
|
||||
* a List of String[], with each String[] representing a line of
|
||||
* the file.
|
||||
*/
|
||||
public void writeAll(List<String[]> allLines) {
|
||||
for (String[] line : allLines) {
|
||||
writeNext(line);
|
||||
}
|
||||
}
|
||||
|
||||
protected void writeColumnNames(ResultSet rs)
|
||||
throws SQLException {
|
||||
|
||||
writeNext(resultService.getColumnNames(rs));
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the entire ResultSet to a CSV file.
|
||||
*
|
||||
* The caller is responsible for closing the ResultSet.
|
||||
*
|
||||
* @param rs the recordset to write
|
||||
* @param includeColumnNames true if you want column names in the output, false otherwise
|
||||
*
|
||||
* @throws java.io.IOException thrown by getColumnValue
|
||||
* @throws java.sql.SQLException thrown by getColumnValue
|
||||
*/
|
||||
public void writeAll(java.sql.ResultSet rs, boolean includeColumnNames) throws SQLException, IOException {
|
||||
|
||||
|
||||
if (includeColumnNames) {
|
||||
writeColumnNames(rs);
|
||||
}
|
||||
|
||||
while (rs.next())
|
||||
{
|
||||
writeNext(resultService.getColumnValues(rs));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Writes the next line to the file.
|
||||
*
|
||||
* @param nextLine
|
||||
* a string array with each comma-separated element as a separate
|
||||
* entry.
|
||||
*/
|
||||
public void writeNext(String[] nextLine) {
|
||||
|
||||
if (nextLine == null)
|
||||
return;
|
||||
|
||||
StringBuilder sb = new StringBuilder(INITIAL_STRING_SIZE);
|
||||
for (int i = 0; i < nextLine.length; i++) {
|
||||
|
||||
if (i != 0) {
|
||||
sb.append(separator);
|
||||
}
|
||||
|
||||
String nextElement = nextLine[i];
|
||||
if (nextElement == null)
|
||||
continue;
|
||||
if (quotechar != NO_QUOTE_CHARACTER)
|
||||
sb.append(quotechar);
|
||||
|
||||
sb.append(stringContainsSpecialCharacters(nextElement) ? processLine(nextElement) : nextElement);
|
||||
|
||||
if (quotechar != NO_QUOTE_CHARACTER)
|
||||
sb.append(quotechar);
|
||||
}
|
||||
|
||||
sb.append(lineEnd);
|
||||
pw.write(sb.toString());
|
||||
|
||||
}
|
||||
|
||||
private boolean stringContainsSpecialCharacters(String line) {
|
||||
return line.indexOf(quotechar) != -1 || line.indexOf(escapechar) != -1;
|
||||
}
|
||||
|
||||
protected StringBuilder processLine(String nextElement)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder(INITIAL_STRING_SIZE);
|
||||
for (int j = 0; j < nextElement.length(); j++) {
|
||||
char nextChar = nextElement.charAt(j);
|
||||
if (escapechar != NO_ESCAPE_CHARACTER && nextChar == quotechar) {
|
||||
sb.append(escapechar).append(nextChar);
|
||||
} else if (escapechar != NO_ESCAPE_CHARACTER && nextChar == escapechar) {
|
||||
sb.append(escapechar).append(nextChar);
|
||||
} else {
|
||||
sb.append(nextChar);
|
||||
}
|
||||
}
|
||||
|
||||
return sb;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush underlying stream to writer.
|
||||
*
|
||||
* @throws IOException if bad things happen
|
||||
*/
|
||||
public void flush() throws IOException {
|
||||
|
||||
pw.flush();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the underlying stream writer flushing any buffered content.
|
||||
*
|
||||
* @throws IOException if bad things happen
|
||||
*
|
||||
*/
|
||||
public void close() throws IOException {
|
||||
flush();
|
||||
pw.close();
|
||||
rawWriter.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if the there has been an error in the printstream.
|
||||
*/
|
||||
public boolean checkError() {
|
||||
return pw.checkError();
|
||||
}
|
||||
|
||||
public void setResultService(ResultSetHelper resultService) {
|
||||
this.resultService = resultService;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package au.com.bytecode.opencsv;
|
||||
/**
|
||||
Copyright 2005 Bytecode Pty Ltd.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
public interface ResultSetHelper {
|
||||
public String[] getColumnNames(ResultSet rs) throws SQLException;
|
||||
|
||||
public String[] getColumnValues(ResultSet rs) throws SQLException, IOException;
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
package au.com.bytecode.opencsv;
|
||||
/**
|
||||
Copyright 2005 Bytecode Pty Ltd.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.*;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* helper class for processing JDBC ResultSet objects
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class ResultSetHelperService implements ResultSetHelper {
|
||||
public static final int CLOBBUFFERSIZE = 2048;
|
||||
|
||||
// note: we want to maintain compatibility with Java 5 VM's
|
||||
// These types don't exist in Java 5
|
||||
private static final int NVARCHAR = -9;
|
||||
private static final int NCHAR = -15;
|
||||
private static final int LONGNVARCHAR = -16;
|
||||
private static final int NCLOB = 2011;
|
||||
|
||||
public String[] getColumnNames(ResultSet rs) throws SQLException {
|
||||
List<String> names = new ArrayList<String>();
|
||||
ResultSetMetaData metadata = rs.getMetaData();
|
||||
|
||||
for (int i = 0; i < metadata.getColumnCount(); i++) {
|
||||
names.add(metadata.getColumnName(i+1));
|
||||
}
|
||||
|
||||
String[] nameArray = new String[names.size()];
|
||||
return names.toArray(nameArray);
|
||||
}
|
||||
|
||||
public String[] getColumnValues(ResultSet rs) throws SQLException, IOException {
|
||||
|
||||
List<String> values = new ArrayList<String>();
|
||||
ResultSetMetaData metadata = rs.getMetaData();
|
||||
|
||||
for (int i = 0; i < metadata.getColumnCount(); i++) {
|
||||
values.add(getColumnValue(rs, metadata.getColumnType(i + 1), i + 1));
|
||||
}
|
||||
|
||||
String[] valueArray = new String[values.size()];
|
||||
return values.toArray(valueArray);
|
||||
}
|
||||
|
||||
private String handleObject(Object obj){
|
||||
return obj == null ? "" : String.valueOf(obj);
|
||||
}
|
||||
|
||||
private String handleBigDecimal(BigDecimal decimal) {
|
||||
return decimal == null ? "" : decimal.toString();
|
||||
}
|
||||
|
||||
private String handleLong(ResultSet rs, int columnIndex) throws SQLException {
|
||||
long lv = rs.getLong(columnIndex);
|
||||
return rs.wasNull() ? "" : Long.toString(lv);
|
||||
}
|
||||
|
||||
private String handleInteger(ResultSet rs, int columnIndex) throws SQLException {
|
||||
int i = rs.getInt(columnIndex);
|
||||
return rs.wasNull() ? "" : Integer.toString(i);
|
||||
}
|
||||
|
||||
private String handleDate(ResultSet rs, int columnIndex) throws SQLException {
|
||||
java.sql.Date date = rs.getDate(columnIndex);
|
||||
String value = null;
|
||||
if (date != null) {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
|
||||
value = dateFormat.format(date);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private String handleTime(Time time) {
|
||||
return time == null ? null : time.toString();
|
||||
}
|
||||
|
||||
private String handleTimestamp(Timestamp timestamp) {
|
||||
SimpleDateFormat timeFormat = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
|
||||
return timestamp == null ? null : timeFormat.format(timestamp);
|
||||
}
|
||||
|
||||
private String getColumnValue(ResultSet rs, int colType, int colIndex)
|
||||
throws SQLException, IOException {
|
||||
|
||||
String value = "";
|
||||
|
||||
switch (colType)
|
||||
{
|
||||
case Types.BIT:
|
||||
case Types.JAVA_OBJECT:
|
||||
value = handleObject(rs.getObject(colIndex));
|
||||
break;
|
||||
case Types.BOOLEAN:
|
||||
boolean b = rs.getBoolean(colIndex);
|
||||
value = Boolean.valueOf(b).toString();
|
||||
break;
|
||||
case NCLOB: // todo : use rs.getNClob
|
||||
case Types.CLOB:
|
||||
Clob c = rs.getClob(colIndex);
|
||||
if (c != null) {
|
||||
value = read(c);
|
||||
}
|
||||
break;
|
||||
case Types.BIGINT:
|
||||
value = handleLong(rs, colIndex);
|
||||
break;
|
||||
case Types.DECIMAL:
|
||||
case Types.DOUBLE:
|
||||
case Types.FLOAT:
|
||||
case Types.REAL:
|
||||
case Types.NUMERIC:
|
||||
value = handleBigDecimal(rs.getBigDecimal(colIndex));
|
||||
break;
|
||||
case Types.INTEGER:
|
||||
case Types.TINYINT:
|
||||
case Types.SMALLINT:
|
||||
value = handleInteger(rs, colIndex);
|
||||
break;
|
||||
case Types.DATE:
|
||||
value = handleDate(rs, colIndex);
|
||||
break;
|
||||
case Types.TIME:
|
||||
value = handleTime(rs.getTime(colIndex));
|
||||
break;
|
||||
case Types.TIMESTAMP:
|
||||
value = handleTimestamp(rs.getTimestamp(colIndex));
|
||||
break;
|
||||
case NVARCHAR: // todo : use rs.getNString
|
||||
case NCHAR: // todo : use rs.getNString
|
||||
case LONGNVARCHAR: // todo : use rs.getNString
|
||||
case Types.LONGVARCHAR:
|
||||
case Types.VARCHAR:
|
||||
case Types.CHAR:
|
||||
value = rs.getString(colIndex);
|
||||
break;
|
||||
default:
|
||||
value = "";
|
||||
}
|
||||
|
||||
|
||||
if (value == null)
|
||||
{
|
||||
value = "";
|
||||
}
|
||||
|
||||
return value;
|
||||
|
||||
}
|
||||
|
||||
private static String read(Clob c) throws SQLException, IOException
|
||||
{
|
||||
StringBuilder sb = new StringBuilder( (int) c.length());
|
||||
Reader r = c.getCharacterStream();
|
||||
char[] cbuf = new char[CLOBBUFFERSIZE];
|
||||
int n;
|
||||
while ((n = r.read(cbuf, 0, cbuf.length)) != -1) {
|
||||
sb.append(cbuf, 0, n);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldGuard
|
||||
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldguard;
|
||||
|
||||
public abstract class LocalPlayer {
|
||||
/**
|
||||
* Get a player's name.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public abstract String getName();
|
||||
|
||||
/**
|
||||
* Returns true if the player is inside a group.
|
||||
*
|
||||
* @param group
|
||||
* @return
|
||||
*/
|
||||
public abstract boolean hasGroup(String group);
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (!(obj instanceof LocalPlayer)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ((LocalPlayer)obj).getName().equals(getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return getName().hashCode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldGuard
|
||||
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldguard.domains;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
import com.sk89q.worldguard.LocalPlayer;
|
||||
|
||||
public class DefaultDomain implements Domain {
|
||||
private Set<String> groups;
|
||||
private Set<String> players;
|
||||
|
||||
public DefaultDomain() {
|
||||
this.groups = new LinkedHashSet<String>();
|
||||
this.players = new HashSet<String>();
|
||||
}
|
||||
|
||||
public void addPlayer(String name) {
|
||||
players.add(name.toLowerCase());
|
||||
}
|
||||
|
||||
public void addPlayer(LocalPlayer player) {
|
||||
players.add(player.getName().toLowerCase());
|
||||
}
|
||||
|
||||
public void removePlayer(String name) {
|
||||
players.remove(name.toLowerCase());
|
||||
}
|
||||
|
||||
public void removePlayer(LocalPlayer player) {
|
||||
players.remove(player.getName().toLowerCase());
|
||||
}
|
||||
|
||||
public void addGroup(String name) {
|
||||
groups.add(name.toLowerCase());
|
||||
}
|
||||
|
||||
public void removeGroup(String name) {
|
||||
groups.remove(name.toLowerCase());
|
||||
}
|
||||
|
||||
public Set<String> getGroups() {
|
||||
return groups;
|
||||
}
|
||||
|
||||
public Set<String> getPlayers() {
|
||||
return players;
|
||||
}
|
||||
|
||||
public boolean contains(LocalPlayer player) {
|
||||
if (players.contains(player.getName().toLowerCase())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (String group : groups) {
|
||||
if (player.hasGroup(group)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return groups.size();
|
||||
}
|
||||
|
||||
public String toPlayersString() {
|
||||
StringBuilder str = new StringBuilder();
|
||||
for (Iterator<String> it = players.iterator(); it.hasNext(); ) {
|
||||
str.append(it.next());
|
||||
if (it.hasNext()) {
|
||||
str.append(", ");
|
||||
}
|
||||
}
|
||||
return str.toString();
|
||||
}
|
||||
|
||||
public String toGroupsString() {
|
||||
StringBuilder str = new StringBuilder();
|
||||
for (Iterator<String> it = groups.iterator(); it.hasNext(); ) {
|
||||
str.append(it.next());
|
||||
if (it.hasNext()) {
|
||||
str.append(", ");
|
||||
}
|
||||
}
|
||||
return str.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.sk89q.worldguard.domains;
|
||||
|
||||
import com.sk89q.worldguard.LocalPlayer;
|
||||
|
||||
public interface Domain {
|
||||
/**
|
||||
* Returns true if a domain contains a player.
|
||||
*
|
||||
* @param player
|
||||
* @return
|
||||
*/
|
||||
public boolean contains(LocalPlayer player);
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldGuard
|
||||
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldguard.domains;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.LinkedHashSet;
|
||||
import com.sk89q.worldguard.LocalPlayer;
|
||||
|
||||
public class DomainCollection implements Domain {
|
||||
private Set<Domain> domains;
|
||||
|
||||
public DomainCollection() {
|
||||
domains = new LinkedHashSet<Domain>();
|
||||
}
|
||||
|
||||
public void add(Domain domain) {
|
||||
domains.add(domain);
|
||||
}
|
||||
|
||||
public void remove(Domain domain) {
|
||||
domains.remove(domain);
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return domains.size();
|
||||
}
|
||||
|
||||
public boolean contains(LocalPlayer player) {
|
||||
for (Domain domain : domains) {
|
||||
if (domain.contains(player)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldGuard
|
||||
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldguard.domains;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Set;
|
||||
import java.util.LinkedHashSet;
|
||||
import com.sk89q.worldguard.LocalPlayer;
|
||||
|
||||
public class GroupDomain implements Domain {
|
||||
private Set<String> groups;
|
||||
|
||||
public GroupDomain() {
|
||||
this.groups = new LinkedHashSet<String>();
|
||||
}
|
||||
|
||||
public GroupDomain(String[] groups) {
|
||||
this.groups = new LinkedHashSet<String>(Arrays.asList(groups));
|
||||
}
|
||||
|
||||
public void addGroup(String name) {
|
||||
groups.add(name);
|
||||
}
|
||||
|
||||
public boolean contains(LocalPlayer player) {
|
||||
for (String group : groups) {
|
||||
if (player.hasGroup(group)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return groups.size();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldGuard
|
||||
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldguard.domains;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
import com.sk89q.worldguard.LocalPlayer;
|
||||
|
||||
public class PlayerDomain implements Domain {
|
||||
private Set<String> players;
|
||||
|
||||
public PlayerDomain() {
|
||||
this.players = new HashSet<String>();
|
||||
}
|
||||
|
||||
public PlayerDomain(String[] players) {
|
||||
this.players = new HashSet<String>();
|
||||
|
||||
for (String name : players) {
|
||||
this.players.add(name.toLowerCase());
|
||||
}
|
||||
}
|
||||
|
||||
public void addPlayer(String name) {
|
||||
players.add(name.toLowerCase());
|
||||
}
|
||||
|
||||
public boolean contains(LocalPlayer player) {
|
||||
return players.contains(player.getName().toLowerCase());
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return players.size();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldGuard
|
||||
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldguard.protection;
|
||||
|
||||
import java.util.Map;
|
||||
import com.sk89q.worldedit.Vector;
|
||||
import com.sk89q.worldguard.LocalPlayer;
|
||||
import com.sk89q.worldguard.protection.AreaFlags.State;
|
||||
|
||||
/**
|
||||
* Represents a set of regions and their rules as applied to one point.
|
||||
*
|
||||
* @author sk89q
|
||||
*/
|
||||
public class ApplicableRegionSet {
|
||||
private Vector pt;
|
||||
private Map<String,ProtectedRegion> regions;
|
||||
|
||||
public ApplicableRegionSet(Vector pt, Map<String,ProtectedRegion> regions) {
|
||||
this.pt = pt;
|
||||
this.regions = regions;
|
||||
}
|
||||
|
||||
public boolean canBuild(LocalPlayer player) {
|
||||
boolean allowed = false;
|
||||
boolean found = false;
|
||||
|
||||
for (ProtectedRegion region : regions.values()) {
|
||||
if (!region.contains(pt)) continue;
|
||||
if (region.getFlags().allowBuild == State.DENY) return false;
|
||||
|
||||
found = true;
|
||||
|
||||
if (!allowed && region.getFlags().allowBuild == State.ALLOW) {
|
||||
allowed = true;
|
||||
}
|
||||
|
||||
if (!allowed && region.getOwners().contains(player)) {
|
||||
allowed = true;
|
||||
}
|
||||
}
|
||||
|
||||
return found ? allowed : true;
|
||||
}
|
||||
|
||||
public boolean allowsPvP() {
|
||||
for (ProtectedRegion region : regions.values()) {
|
||||
if (!region.contains(pt)) continue;
|
||||
if (region.getFlags().allowBuild == State.DENY) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean allowsMobDamage() {
|
||||
for (ProtectedRegion region : regions.values()) {
|
||||
if (!region.contains(pt)) continue;
|
||||
if (region.getFlags().allowMobDamage == State.DENY) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean allowsCreeperExplosions() {
|
||||
for (ProtectedRegion region : regions.values()) {
|
||||
if (!region.contains(pt)) continue;
|
||||
if (region.getFlags().allowCreeperExplosions == State.DENY) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean allowsTNT() {
|
||||
for (ProtectedRegion region : regions.values()) {
|
||||
if (!region.contains(pt)) continue;
|
||||
if (region.getFlags().allowTNT == State.DENY) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean allowsLighter() {
|
||||
for (ProtectedRegion region : regions.values()) {
|
||||
if (!region.contains(pt)) continue;
|
||||
if (region.getFlags().allowLighter == State.DENY) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean allowsFireSpread() {
|
||||
for (ProtectedRegion region : regions.values()) {
|
||||
if (!region.contains(pt)) continue;
|
||||
if (region.getFlags().allowFireSpread == State.DENY) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean allowsLavaFire() {
|
||||
for (ProtectedRegion region : regions.values()) {
|
||||
if (!region.contains(pt)) continue;
|
||||
if (region.getFlags().allowLavaFire == State.DENY) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldGuard
|
||||
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldguard.protection;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author sk89q
|
||||
*/
|
||||
public class AreaFlags {
|
||||
public enum State {
|
||||
NONE,
|
||||
ALLOW,
|
||||
DENY,
|
||||
};
|
||||
|
||||
public State allowBuild = State.NONE;
|
||||
public State allowPvP = State.NONE;
|
||||
public State allowMobDamage = State.NONE;
|
||||
public State allowCreeperExplosions = State.NONE;
|
||||
public State allowTNT = State.NONE;
|
||||
public State allowLighter = State.NONE;
|
||||
public State allowFireSpread = State.NONE;
|
||||
public State allowLavaFire = State.NONE;
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (!(obj instanceof AreaFlags)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
AreaFlags other = (AreaFlags)obj;
|
||||
|
||||
return other.allowBuild == allowBuild
|
||||
&& other.allowPvP == allowPvP
|
||||
&& other.allowMobDamage == allowMobDamage
|
||||
&& other.allowCreeperExplosions == allowCreeperExplosions
|
||||
&& other.allowTNT == allowTNT
|
||||
&& other.allowLighter == allowLighter
|
||||
&& other.allowFireSpread == allowFireSpread
|
||||
&& other.allowLavaFire == allowLavaFire;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,333 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldGuard
|
||||
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldguard.protection;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.io.*;
|
||||
|
||||
import au.com.bytecode.opencsv.CSVReader;
|
||||
import au.com.bytecode.opencsv.CSVWriter;
|
||||
import com.sk89q.worldedit.BlockVector;
|
||||
import com.sk89q.worldguard.domains.DefaultDomain;
|
||||
import com.sk89q.worldguard.protection.AreaFlags.State;
|
||||
|
||||
/**
|
||||
* Represents a protected area database that uses CSV files.
|
||||
*
|
||||
* @author sk89q
|
||||
*/
|
||||
public class CSVDatabase implements ProtectionDatabase {
|
||||
private static Logger logger = Logger.getLogger("Minecraft.WorldGuard");
|
||||
|
||||
/**
|
||||
* References the CSV file.
|
||||
*/
|
||||
private File file;
|
||||
/**
|
||||
* Holds the list of regions.
|
||||
*/
|
||||
private Map<String,ProtectedRegion> regions;
|
||||
|
||||
/**
|
||||
* Construct the database with a path to a file. No file is read or
|
||||
* written at this time.
|
||||
*
|
||||
* @param file
|
||||
*/
|
||||
public CSVDatabase(File file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the database.
|
||||
*/
|
||||
@Override
|
||||
public void save() throws IOException {
|
||||
CSVWriter writer = new CSVWriter(new FileWriter(file));
|
||||
|
||||
try {
|
||||
for (Map.Entry<String,ProtectedRegion> entry : regions.entrySet()) {
|
||||
String id = entry.getKey();
|
||||
ProtectedRegion region = entry.getValue();
|
||||
|
||||
if (!(region instanceof ProtectedCuboidRegion)) {
|
||||
logger.warning("The CSV database only supports cuboid regions.");
|
||||
continue;
|
||||
}
|
||||
|
||||
ProtectedCuboidRegion cuboid = (ProtectedCuboidRegion)region;
|
||||
BlockVector min = cuboid.getMinimumPoint();
|
||||
BlockVector max = cuboid.getMaximumPoint();
|
||||
|
||||
writer.writeNext(new String[] {
|
||||
id,
|
||||
"cuboid",
|
||||
String.valueOf(min.getBlockX()),
|
||||
String.valueOf(min.getBlockY()),
|
||||
String.valueOf(min.getBlockZ()),
|
||||
String.valueOf(max.getBlockX()),
|
||||
String.valueOf(max.getBlockY()),
|
||||
String.valueOf(max.getBlockZ()),
|
||||
String.valueOf(cuboid.getPriority()),
|
||||
writeDomains(cuboid.getOwners()),
|
||||
writeFlags(cuboid.getFlags()),
|
||||
cuboid.getEnterMessage(),
|
||||
});
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the database from file.
|
||||
*/
|
||||
@Override
|
||||
public void load() throws IOException {
|
||||
Map<String,ProtectedRegion> regions =
|
||||
new HashMap<String,ProtectedRegion>();
|
||||
|
||||
CSVReader reader = new CSVReader(new FileReader(file));
|
||||
|
||||
try {
|
||||
String[] line;
|
||||
|
||||
while ((line = reader.readNext()) != null) {
|
||||
if (line.length >= 12) {
|
||||
String type = line[1];
|
||||
if (!type.equalsIgnoreCase("cuboid")) {
|
||||
logger.warning("Only cuboid region types are supported: "
|
||||
+ line);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
String id = line[0];
|
||||
BlockVector min = new BlockVector(
|
||||
Integer.parseInt(line[2]),
|
||||
Integer.parseInt(line[3]),
|
||||
Integer.parseInt(line[4]));
|
||||
BlockVector max = new BlockVector(
|
||||
Integer.parseInt(line[5]),
|
||||
Integer.parseInt(line[6]),
|
||||
Integer.parseInt(line[7]));
|
||||
int priority = Integer.parseInt(line[8]);
|
||||
String ownersData = line[9];
|
||||
String flagsData = line[10];
|
||||
String enterMessage = line[11];
|
||||
|
||||
ProtectedRegion region = new ProtectedCuboidRegion(min, max);
|
||||
region.setPriority(priority);
|
||||
region.setOwners(this.parseDomains(ownersData));
|
||||
region.setEnterMessage(enterMessage);
|
||||
region.setFlags(parseFlags(flagsData));
|
||||
regions.put(id, region);
|
||||
} else {
|
||||
logger.warning("Line has invalid: " + line);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
reader.close();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
|
||||
this.regions = regions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the list of regions into a region manager.
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public void load(RegionManager manager) throws IOException {
|
||||
load();
|
||||
manager.setRegions(regions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the list of regions from a region manager.
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public void save(RegionManager manager) throws IOException {
|
||||
regions = manager.getRegions();
|
||||
save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to parse the specified domain in the CSV file.
|
||||
*
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
private DefaultDomain parseDomains(String data) {
|
||||
DefaultDomain domain = new DefaultDomain();
|
||||
Pattern pattern = Pattern.compile("^([A-Za-z]):(.*)$");
|
||||
|
||||
String[] parts = data.split(",");
|
||||
|
||||
for (String part : parts) {
|
||||
Matcher matcher = pattern.matcher(part);
|
||||
|
||||
if (!matcher.matches()) {
|
||||
logger.warning("Invalid owner specification: " + part);
|
||||
continue;
|
||||
}
|
||||
|
||||
String type = matcher.group(1);
|
||||
String id = matcher.group(2);
|
||||
|
||||
if (type.equals("u")) {
|
||||
domain.addPlayer(id);
|
||||
} else if (type.equals("g")) {
|
||||
domain.addGroup(id);
|
||||
} else {
|
||||
logger.warning("Unknown owner specification: " + type);
|
||||
}
|
||||
}
|
||||
|
||||
return domain;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to parse the list of flags.
|
||||
*
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
private AreaFlags parseFlags(String data) {
|
||||
AreaFlags flags = new AreaFlags();
|
||||
State curState = State.ALLOW;
|
||||
|
||||
for (int i = 0; i < data.length(); i++) {
|
||||
char flag = data.charAt(i);
|
||||
if (flag == '+') {
|
||||
curState = State.ALLOW;
|
||||
} else if (flag == '-') {
|
||||
curState = State.DENY;
|
||||
} else if (flag == 'b') {
|
||||
flags.allowBuild = curState;
|
||||
} else if (flag == 'p') {
|
||||
flags.allowPvP = curState;
|
||||
} else if (flag == 'm') {
|
||||
flags.allowMobDamage = curState;
|
||||
} else if (flag == 'c') {
|
||||
flags.allowCreeperExplosions = curState;
|
||||
} else if (flag == 't') {
|
||||
flags.allowTNT = curState;
|
||||
} else if (flag == 'l') {
|
||||
flags.allowLighter = curState;
|
||||
} else if (flag == 'f') {
|
||||
flags.allowFireSpread = curState;
|
||||
} else if (flag == 'F') {
|
||||
flags.allowLavaFire = curState;
|
||||
} else {
|
||||
logger.warning("Unknown area flag/flag modifier: " + flag);
|
||||
}
|
||||
}
|
||||
|
||||
return flags;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to write the list of domains.
|
||||
*
|
||||
* @param domain
|
||||
* @return
|
||||
*/
|
||||
private String writeDomains(DefaultDomain domain) {
|
||||
StringBuilder str = new StringBuilder();
|
||||
|
||||
for (String player : domain.getPlayers()) {
|
||||
str.append("u:" + player + ",");
|
||||
}
|
||||
|
||||
for (String group : domain.getGroups()) {
|
||||
str.append("g:" + group + ",");
|
||||
}
|
||||
|
||||
return str.length() > 0 ?
|
||||
str.toString().substring(0, str.length() - 1) : "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to prepend '+' or '-' in front of a flag according
|
||||
* to the flag's state.
|
||||
*
|
||||
* @param state
|
||||
* @param flag
|
||||
* @return
|
||||
*/
|
||||
private String writeFlag(State state, String flag) {
|
||||
if (state == State.ALLOW) {
|
||||
return "+" + flag;
|
||||
} else if (state == State.DENY) {
|
||||
return "-" + flag;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to write the list of flags.
|
||||
*
|
||||
* @param flags
|
||||
* @return
|
||||
*/
|
||||
private String writeFlags(AreaFlags flags) {
|
||||
StringBuilder str = new StringBuilder();
|
||||
str.append(writeFlag(flags.allowBuild, "b"));
|
||||
str.append(writeFlag(flags.allowPvP, "p"));
|
||||
str.append(writeFlag(flags.allowMobDamage, "m"));
|
||||
str.append(writeFlag(flags.allowCreeperExplosions, "c"));
|
||||
str.append(writeFlag(flags.allowTNT, "t"));
|
||||
str.append(writeFlag(flags.allowLighter, "l"));
|
||||
str.append(writeFlag(flags.allowFireSpread, "f"));
|
||||
str.append(writeFlag(flags.allowLavaFire, "F"));
|
||||
return str.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of protected regions.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Map<String,ProtectedRegion> getRegions() {
|
||||
return regions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of protected regions.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public void setRegions(Map<String,ProtectedRegion> regions) {
|
||||
this.regions = regions;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldGuard
|
||||
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldguard.protection;
|
||||
|
||||
/**
|
||||
* Caches the results of the protection to prevent necessary lookups.
|
||||
*
|
||||
* @author sk89q
|
||||
*/
|
||||
public class CacheManager {
|
||||
/**
|
||||
* Region manager.
|
||||
*/
|
||||
private RegionManager manager;
|
||||
|
||||
/**
|
||||
* Construct the object.
|
||||
*
|
||||
* @param manager
|
||||
*/
|
||||
public CacheManager(RegionManager manager) {
|
||||
this.manager = manager;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldGuard
|
||||
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldguard.protection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.LinkedHashMap;
|
||||
import com.sk89q.worldedit.Vector;
|
||||
|
||||
/**
|
||||
* A very simple implementation of the region manager that uses a flat list
|
||||
* and iterates through the list to identify applicable regions. This method
|
||||
* is not very efficient.
|
||||
*
|
||||
* @author sk89q
|
||||
*/
|
||||
public class FlatRegionManager implements RegionManager {
|
||||
/**
|
||||
* List of protected regions.
|
||||
*/
|
||||
private Map<String,ProtectedRegion> regions;
|
||||
|
||||
/**
|
||||
* Construct the manager.
|
||||
*/
|
||||
public FlatRegionManager() {
|
||||
regions = new LinkedHashMap<String,ProtectedRegion>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of protected regions.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Map<String,ProtectedRegion> getRegions() {
|
||||
return regions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a region.
|
||||
*
|
||||
* @param id
|
||||
* @param region
|
||||
*/
|
||||
public void addRegion(String id, ProtectedRegion region) {
|
||||
regions.put(id, region);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a region.
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
public void removeRegion(String id) {
|
||||
regions.remove(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether a region exists by an ID.
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
public boolean hasRegion(String id) {
|
||||
return regions.containsKey(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a region by its ID.
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
public ProtectedRegion getRegion(String id) {
|
||||
return regions.get(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a list of protected regions.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public void setRegions(Map<String,ProtectedRegion> regions) {
|
||||
this.regions = regions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an object for a point for rules to be applied with.
|
||||
*
|
||||
* @param pt
|
||||
* @return
|
||||
*/
|
||||
public ApplicableRegionSet getApplicableRegions(Vector pt) {
|
||||
return new ApplicableRegionSet(pt, regions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of region IDs that contain a point.
|
||||
*
|
||||
* @param pt
|
||||
* @return
|
||||
*/
|
||||
public List<String> getApplicableRegionsIDs(Vector pt) {
|
||||
List<String> applicable = new ArrayList<String>();
|
||||
|
||||
for (Map.Entry<String,ProtectedRegion> entry : regions.entrySet()) {
|
||||
if (entry.getValue().contains(pt)) {
|
||||
applicable.add(entry.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
return applicable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of regions.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int size() {
|
||||
return regions.size();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldGuard
|
||||
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldguard.protection;
|
||||
|
||||
import com.sk89q.worldedit.*;
|
||||
|
||||
/**
|
||||
* Represents a cuboid region that can be protected.
|
||||
*
|
||||
* @author sk89q
|
||||
*/
|
||||
public class ProtectedCuboidRegion extends ProtectedRegion {
|
||||
/**
|
||||
* Store the first point.
|
||||
*/
|
||||
private BlockVector min;
|
||||
/**
|
||||
* Store the second point.
|
||||
*/
|
||||
private BlockVector max;
|
||||
|
||||
/**
|
||||
* Construct a new instance of this cuboid region.
|
||||
*
|
||||
* @param pos1
|
||||
* @param pos2
|
||||
* @param priority
|
||||
*/
|
||||
public ProtectedCuboidRegion(BlockVector min, BlockVector max) {
|
||||
super();
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the lower point of the cuboid.
|
||||
*
|
||||
* @return min point
|
||||
*/
|
||||
public BlockVector getMinimumPoint() {
|
||||
return min;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the lower point of the cuboid.
|
||||
*
|
||||
* @param pt
|
||||
*/
|
||||
public void setMinimumPoint(BlockVector pt) {
|
||||
min = pt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the upper point of the cuboid.
|
||||
*
|
||||
* @return max point
|
||||
*/
|
||||
public BlockVector getMaximumPoint() {
|
||||
return max;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the upper point of the cuboid.
|
||||
*
|
||||
* @param pt
|
||||
*/
|
||||
public void setMaximumPoint(BlockVector pt) {
|
||||
max = pt;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Vector pt) {
|
||||
int x = pt.getBlockX();
|
||||
int y = pt.getBlockY();
|
||||
int z = pt.getBlockZ();
|
||||
return x >= min.getBlockX() && x <= max.getBlockX()
|
||||
&& y >= min.getBlockY() && y <= max.getBlockY()
|
||||
&& z >= min.getBlockZ() && z <= max.getBlockZ();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldGuard
|
||||
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldguard.protection;
|
||||
|
||||
import com.sk89q.worldedit.Vector;
|
||||
import com.sk89q.worldguard.domains.DefaultDomain;
|
||||
|
||||
/**
|
||||
* Represents a region of any shape and size that can be protected.
|
||||
*
|
||||
* @author sk89q
|
||||
*/
|
||||
public abstract class ProtectedRegion {
|
||||
/**
|
||||
* Area message.
|
||||
*/
|
||||
private String enterMessage;
|
||||
/**
|
||||
* List of owners.
|
||||
*/
|
||||
private DefaultDomain owners = new DefaultDomain();
|
||||
/**
|
||||
* Priority.
|
||||
*/
|
||||
private int priority = 0;
|
||||
/**
|
||||
* Area flags.
|
||||
*/
|
||||
private AreaFlags flags = new AreaFlags();
|
||||
|
||||
/**
|
||||
* Construct a new instance of this cuboid region.
|
||||
*
|
||||
* @param id
|
||||
* @param priority
|
||||
* @param owners
|
||||
* @param enterMessage
|
||||
*/
|
||||
public ProtectedRegion() {
|
||||
this.priority = 0;
|
||||
this.owners = new DefaultDomain();
|
||||
this.enterMessage = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the priority
|
||||
*/
|
||||
public int getPriority() {
|
||||
return priority;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param priority the priority to set
|
||||
*/
|
||||
public void setPriority(int priority) {
|
||||
this.priority = priority;
|
||||
}
|
||||
|
||||
/**
|
||||
* Se flags.
|
||||
* @param flags
|
||||
*/
|
||||
public void setFlags(AreaFlags flags) {
|
||||
this.flags = flags;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the enterMessage
|
||||
*/
|
||||
public String getEnterMessage() {
|
||||
return enterMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param enterMessage the enterMessage to set
|
||||
*/
|
||||
public void setEnterMessage(String enterMessage) {
|
||||
this.enterMessage = enterMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the owners
|
||||
*/
|
||||
public DefaultDomain getOwners() {
|
||||
return owners;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param owners the owners to set
|
||||
*/
|
||||
public void setOwners(DefaultDomain owners) {
|
||||
this.owners = owners;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get flags.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public AreaFlags getFlags() {
|
||||
return flags;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check to see if a point is inside this region.
|
||||
*
|
||||
* @param pt
|
||||
* @return
|
||||
*/
|
||||
public abstract boolean contains(Vector pt);
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldGuard
|
||||
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldguard.protection;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Represents a database to read and write lists of regions from and to.
|
||||
*
|
||||
* @author sk89q
|
||||
*/
|
||||
public interface ProtectionDatabase {
|
||||
/**
|
||||
* Load the list of regions. The method should not modify the list returned
|
||||
* by getRegions() unless the load finishes successfully.
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public void load() throws IOException;
|
||||
/**
|
||||
* Save the list of regions.
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public void save() throws IOException;
|
||||
/**
|
||||
* Load the list of regions into a region manager.
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public void load(RegionManager manager) throws IOException;
|
||||
/**
|
||||
* Save the list of regions from a region manager.
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public void save(RegionManager manager) throws IOException;
|
||||
/**
|
||||
* Get a list of regions.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Map<String,ProtectedRegion> getRegions();
|
||||
/**
|
||||
* Set the list of regions.
|
||||
*
|
||||
* @param regions
|
||||
*/
|
||||
public void setRegions(Map<String,ProtectedRegion> regions);
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldGuard
|
||||
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldguard.protection;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import com.sk89q.worldedit.Vector;
|
||||
|
||||
/**
|
||||
* An interface for getting, setting, and looking up regions. The most
|
||||
* simple implementation uses a flat list and iterates through the entire
|
||||
* list to look for applicable regions, but a more complicated (and more
|
||||
* efficient) implementation may use space partitioning techniques.
|
||||
*
|
||||
* @author sk89q
|
||||
*/
|
||||
public interface RegionManager {
|
||||
/**
|
||||
* Get a list of protected regions.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Map<String,ProtectedRegion> getRegions();
|
||||
|
||||
/**
|
||||
* Set a list of protected regions.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public void setRegions(Map<String,ProtectedRegion> regions);
|
||||
|
||||
/**
|
||||
* Adds a region.
|
||||
*
|
||||
* @param id
|
||||
* @param region
|
||||
*/
|
||||
public void addRegion(String id, ProtectedRegion region);
|
||||
|
||||
/**
|
||||
* Return whether a region exists by an ID.
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
public boolean hasRegion(String id);
|
||||
|
||||
/**
|
||||
* Get a region by its ID.
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
public ProtectedRegion getRegion(String id);
|
||||
|
||||
/**
|
||||
* Removes a region.
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
public void removeRegion(String id);
|
||||
|
||||
/**
|
||||
* Get an object for a point for rules to be applied with.
|
||||
*
|
||||
* @param pt
|
||||
* @return
|
||||
*/
|
||||
public ApplicableRegionSet getApplicableRegions(Vector pt);
|
||||
|
||||
/**
|
||||
* Get a list of region IDs that contain a point.
|
||||
*
|
||||
* @param pt
|
||||
* @return
|
||||
*/
|
||||
public List<String> getApplicableRegionsIDs(Vector pt);
|
||||
|
||||
/**
|
||||
* Get the number of regions.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int size();
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldGuard
|
||||
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldguard;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
import com.sk89q.worldguard.LocalPlayer;
|
||||
|
||||
public class TestPlayer extends LocalPlayer {
|
||||
private String name;
|
||||
private Set<String> groups = new HashSet<String>();
|
||||
|
||||
public TestPlayer(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void addGroup(String group) {
|
||||
groups.add(group.toLowerCase());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasGroup(String group) {
|
||||
return groups.contains(group.toLowerCase());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldGuard
|
||||
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldguard.protection;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
import com.sk89q.worldedit.BlockVector;
|
||||
import com.sk89q.worldedit.Vector;
|
||||
import com.sk89q.worldguard.TestPlayer;
|
||||
import com.sk89q.worldguard.domains.DefaultDomain;
|
||||
|
||||
public class ApplicableRegionSetTest {
|
||||
static String COURTYARD_ID = "courtyard";
|
||||
static String FOUNTAIN_ID = "fountain";
|
||||
static String MEMBER_GROUP = "member";
|
||||
static String COURTYARD_GROUP = "courtyard";
|
||||
|
||||
Vector inFountain = new Vector(2, 2, 2);
|
||||
Vector inCourtyard = new Vector(7, 7, 7);
|
||||
Vector outside = new Vector(15, 15, 15);
|
||||
RegionManager manager;
|
||||
TestPlayer player1;
|
||||
TestPlayer player2;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
manager = new FlatRegionManager();
|
||||
|
||||
setUpPlayers();
|
||||
setUpCourtyardRegion();
|
||||
setUpFountainRegion();
|
||||
}
|
||||
|
||||
void setUpPlayers() {
|
||||
player1 = new TestPlayer("tetsu");
|
||||
player1.addGroup(MEMBER_GROUP);
|
||||
player1.addGroup(COURTYARD_GROUP);
|
||||
|
||||
player2 = new TestPlayer("alex");
|
||||
player2.addGroup(MEMBER_GROUP);
|
||||
}
|
||||
|
||||
void setUpCourtyardRegion() {
|
||||
DefaultDomain domain = new DefaultDomain();
|
||||
domain.addGroup(COURTYARD_GROUP);
|
||||
|
||||
ProtectedRegion region = new ProtectedCuboidRegion(
|
||||
new BlockVector(0, 0, 0), new BlockVector(10, 10, 10));
|
||||
AreaFlags flags = new AreaFlags();
|
||||
flags.allowBuild = AreaFlags.State.NONE;
|
||||
flags.allowFireSpread = AreaFlags.State.ALLOW;
|
||||
region.setFlags(flags);
|
||||
region.setOwners(domain);
|
||||
manager.addRegion(COURTYARD_ID, region);
|
||||
}
|
||||
|
||||
void setUpFountainRegion() {
|
||||
ProtectedRegion region = new ProtectedCuboidRegion(
|
||||
new BlockVector(0, 0, 0), new BlockVector(5, 5, 5));
|
||||
AreaFlags flags = new AreaFlags();
|
||||
flags.allowBuild = AreaFlags.State.ALLOW;
|
||||
flags.allowFireSpread = AreaFlags.State.DENY;
|
||||
region.setFlags(flags);
|
||||
manager.addRegion(FOUNTAIN_ID, region);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonBuildFlag() {
|
||||
ApplicableRegionSet appl;
|
||||
|
||||
// Outside
|
||||
appl = manager.getApplicableRegions(outside);
|
||||
assertTrue(appl.allowsFireSpread());
|
||||
// Inside courtyard
|
||||
appl = manager.getApplicableRegions(inCourtyard);
|
||||
assertTrue(appl.allowsFireSpread());
|
||||
// Inside fountain
|
||||
appl = manager.getApplicableRegions(inFountain);
|
||||
assertFalse(appl.allowsFireSpread());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPlayer1BuildAccess() {
|
||||
ApplicableRegionSet appl;
|
||||
|
||||
// Outside
|
||||
appl = manager.getApplicableRegions(outside);
|
||||
assertTrue(appl.canBuild(player1));
|
||||
// Inside courtyard
|
||||
appl = manager.getApplicableRegions(inCourtyard);
|
||||
assertTrue(appl.canBuild(player1));
|
||||
// Inside fountain
|
||||
appl = manager.getApplicableRegions(inFountain);
|
||||
assertTrue(appl.canBuild(player1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPlayer2BuildAccess() {
|
||||
ApplicableRegionSet appl;
|
||||
|
||||
// Outside
|
||||
appl = manager.getApplicableRegions(outside);
|
||||
assertTrue(appl.canBuild(player2));
|
||||
// Inside courtyard
|
||||
appl = manager.getApplicableRegions(inCourtyard);
|
||||
assertFalse(appl.canBuild(player2));
|
||||
// Inside fountain
|
||||
appl = manager.getApplicableRegions(inFountain);
|
||||
assertTrue(appl.canBuild(player2));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldGuard
|
||||
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldguard.protection;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
import com.sk89q.worldedit.BlockVector;
|
||||
import com.sk89q.worldguard.domains.DefaultDomain;
|
||||
|
||||
public class CSVDatabaseTest {
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadSave() throws Exception {
|
||||
File temp = File.createTempFile("worldguard_csv_test", ".tmp");
|
||||
temp.deleteOnExit();
|
||||
|
||||
Map<String,ProtectedRegion> regions =
|
||||
new HashMap<String,ProtectedRegion>();
|
||||
regions.put("test1", getTestRegion1());
|
||||
regions.put("test2", getTestRegion2());
|
||||
|
||||
CSVDatabase writeDB = new CSVDatabase(temp);
|
||||
writeDB.setRegions(regions);
|
||||
writeDB.save();
|
||||
|
||||
CSVDatabase readDB = new CSVDatabase(temp);
|
||||
readDB.load();
|
||||
|
||||
Map<String,ProtectedRegion> loaded = readDB.getRegions();
|
||||
|
||||
ProtectedRegion region1 = loaded.get("test1");
|
||||
checkTestRegion1(region1);
|
||||
}
|
||||
|
||||
private void checkTestRegion1(ProtectedRegion region) {
|
||||
AreaFlags flags = new AreaFlags();
|
||||
flags.allowFireSpread = AreaFlags.State.ALLOW;
|
||||
flags.allowPvP = AreaFlags.State.DENY;
|
||||
flags.allowLighter = AreaFlags.State.DENY;
|
||||
region.setFlags(flags);
|
||||
|
||||
assertEquals(region.getFlags(), flags);
|
||||
}
|
||||
|
||||
private ProtectedRegion getTestRegion1() {
|
||||
BlockVector min = new BlockVector(1, 2, 3);
|
||||
BlockVector max = new BlockVector(4, 5, 6);
|
||||
|
||||
ProtectedRegion region = new ProtectedCuboidRegion(min, max);
|
||||
|
||||
AreaFlags flags = new AreaFlags();
|
||||
flags.allowFireSpread = AreaFlags.State.ALLOW;
|
||||
flags.allowPvP = AreaFlags.State.DENY;
|
||||
flags.allowLighter = AreaFlags.State.DENY;
|
||||
region.setFlags(flags);
|
||||
|
||||
DefaultDomain domain = new DefaultDomain();
|
||||
domain.addGroup("members");
|
||||
domain.addGroup("sturmehs");
|
||||
domain.addPlayer("hollie");
|
||||
domain.addPlayer("chad");
|
||||
domain.addPlayer("tetsu");
|
||||
region.setOwners(domain);
|
||||
|
||||
region.setEnterMessage("hello there!");
|
||||
region.setPriority(444);
|
||||
|
||||
return region;
|
||||
}
|
||||
|
||||
private ProtectedRegion getTestRegion2() {
|
||||
BlockVector min = new BlockVector(7, 8, 9);
|
||||
BlockVector max = new BlockVector(10, 11, 12);
|
||||
|
||||
ProtectedRegion region = new ProtectedCuboidRegion(min, max);
|
||||
|
||||
AreaFlags flags = new AreaFlags();
|
||||
flags.allowFireSpread = AreaFlags.State.DENY;
|
||||
flags.allowPvP = AreaFlags.State.ALLOW;
|
||||
flags.allowMobDamage = AreaFlags.State.DENY;
|
||||
region.setFlags(flags);
|
||||
|
||||
DefaultDomain domain = new DefaultDomain();
|
||||
domain.addGroup("admins");
|
||||
domain.addPlayer("jon");
|
||||
domain.addPlayer("ester");
|
||||
domain.addPlayer("amy");
|
||||
region.setOwners(domain);
|
||||
|
||||
region.setEnterMessage("Testing");
|
||||
region.setPriority(555);
|
||||
|
||||
return region;
|
||||
}
|
||||
}
|
||||
+14
-15
@@ -11,7 +11,7 @@
|
||||
# Example to block ore and catch some events:
|
||||
# [coalore,goldore,ironore]
|
||||
# on-destroy-with=deny,log,kick
|
||||
# on-create=deny,tell
|
||||
# on-place=deny,tell
|
||||
#
|
||||
# Options:
|
||||
# - ignore-groups (comma-delimited list of groups to not affect)
|
||||
@@ -22,8 +22,9 @@
|
||||
# - on-destroy (when a block of this type is being destroyed)
|
||||
# - on-break (when a block of this type is about to be broken)
|
||||
# - on-destroy-with (the item/block held by the user while destroying)
|
||||
# - on-create (the item/block in the user's inventory is being created)
|
||||
# - on-use (the block is right clicked)
|
||||
# - on-place (a block is being placed)
|
||||
# - on-use (an item like a lighter is being used)
|
||||
# - on-right-click (the block is right clicked -- curently INEFFECTIVE)
|
||||
# - on-drop (the item is being dropped from the player's inventory)
|
||||
# - on-acquire (the item enters a player's inventory via some method)
|
||||
#
|
||||
@@ -68,24 +69,22 @@
|
||||
# Some examples follow. Remember to remove # in front if you want them
|
||||
# to work for you!
|
||||
|
||||
# Deny lava buckets
|
||||
#[lavabucket]
|
||||
#ignore-groups=admins,mods
|
||||
#on-destroy-with=deny,log,kick
|
||||
#on-create=deny,tell
|
||||
#on-use=deny,tell
|
||||
|
||||
# Deny lighter usage
|
||||
#[259]
|
||||
#on-use=deny,tell
|
||||
|
||||
# Deny some ore
|
||||
#[coalore,goldore,ironore]
|
||||
#ignore-groups=admins,mods,vip
|
||||
#ignore-groups=admins,mods
|
||||
#on-destroy=notify,deny,log
|
||||
|
||||
#[cobblestone]
|
||||
#on-create=deny,tell,log
|
||||
#on-place=deny,tell,log
|
||||
|
||||
#[lever]
|
||||
#on-break=deny
|
||||
|
||||
# This is an example of the 'durability cheat' workaround that destroys your
|
||||
# tool when you drop it
|
||||
#[256,257,258,259,267,268,269,270,271,272,273,274,275,276,277,278,279,283,284,285,286,290,291,292,293,294,346]
|
||||
#on-drop=deny,tell
|
||||
#comment=Durability Hacks
|
||||
#message=Your tool was destroyed
|
||||
#on-break=deny,tell
|
||||
@@ -65,6 +65,11 @@ disable-fall-damage=false
|
||||
disable-lava-damage=false
|
||||
disable-fire-damage=false
|
||||
disable-water-damage=false
|
||||
disable-suffocation-damage=false
|
||||
|
||||
# Teleports player when s/he is suffocating. This can be abused to get
|
||||
# through ceilings.
|
||||
teleport-on-suffocation=false
|
||||
|
||||
# Disable fire spreading between blocks. Note that this does not prevent
|
||||
# fire damage -- it only prevents fire spread.
|
||||
@@ -95,3 +100,9 @@ log-database=false
|
||||
log-file-open-files=10
|
||||
log-file-path=worldguard/logs/%Y-%m-%d.log
|
||||
log-file=false
|
||||
|
||||
# On death options.
|
||||
teleport-to-home-on-death=false
|
||||
exact-respawn=false
|
||||
|
||||
#EOF
|
||||
Reference in new issue
Block a user