Compare commits
29 Commits
1.0.2
...
world-gene
Author | SHA1 | Date | |
---|---|---|---|
1a1cbab798 | |||
99b0e4a692 | |||
50d165d3bb | |||
4ec7f0f7ab | |||
c3ca060ef0 | |||
257d1f5110 | |||
550db367c5 | |||
8592f4ada1 | |||
5b75b83c5b | |||
de145b3a59 | |||
3e975c3093 | |||
fa51e091f1 | |||
49e5f61f19 | |||
cc9bd13b2e | |||
2502975dc1 | |||
2e8372e74f | |||
e20a2c9084 | |||
bb32466902 | |||
bbbe256718 | |||
53c29aa9a8 | |||
094b2f6dca | |||
62e7d558e6 | |||
8fd26b513a | |||
a83c0fbab0 | |||
6a218f97f0 | |||
46d19fa35e | |||
785dc10644 | |||
c671f04f65 | |||
0176ec3a3a |
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,3 +2,4 @@ lib/
|
||||
target/
|
||||
*.iml
|
||||
.idea/
|
||||
bukkit-testserver/
|
||||
|
Binary file not shown.
17
LICENSE
17
LICENSE
@ -1,17 +0,0 @@
|
||||
# GPLv3
|
||||
* src/main/java/de/craftinc/borderprotection/Border.java
|
||||
* src/main/java/de/craftinc/borderprotection/BorderManager.java
|
||||
* src/main/java/de/craftinc/borderprotection/Commands.java
|
||||
* src/main/java/de/craftinc/borderprotection/Messages.java
|
||||
* src/main/java/de/craftinc/borderprotection/PlayerMoveListener.java
|
||||
* src/main/java/de/craftinc/borderprotection/PlayerTeleportListener.java
|
||||
* src/main/java/de/craftinc/borderprotection/Plugin.java
|
||||
* src/main/resources/plugin.yml
|
||||
|
||||
full text of GPLv3 can be found in the file "GPLv3"
|
||||
|
||||
|
||||
# LGPLv3
|
||||
* src/main/java/de/craftinc/borderprotection/LocationSerializer.java
|
||||
|
||||
full text of LGPLv3 can be found in the file "LGPLv3"
|
10
LICENSE.md
Normal file
10
LICENSE.md
Normal file
@ -0,0 +1,10 @@
|
||||
# GPLv3
|
||||
* All files except the files listed under LGPLv3 are licensed under GPLv3
|
||||
|
||||
full text of GPLv3 can be found in the file "GPLv3"
|
||||
|
||||
|
||||
# LGPLv3
|
||||
* src/main/java/de/craftinc/borderprotection/LocationSerializer.java
|
||||
|
||||
full text of LGPLv3 can be found in the file "LGPLv3"
|
46
pom.xml
46
pom.xml
@ -4,27 +4,69 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>de.craftinc</groupId>
|
||||
<artifactId>CraftincBorderProtection</artifactId>
|
||||
<name>Craft Inc. BorderProtection</name>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.0.2</version>
|
||||
<version>2.0-beta</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<finalName>${project.name} ${project.version}</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/lib</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<version>1.2.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>Run Test Bukkit Server</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>exec</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<executable>${basedir}/scripts/test-deployment.sh</executable>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.bukkit</groupId>
|
||||
<artifactId>bukkit</artifactId>
|
||||
<version>RELEASE</version>
|
||||
<version>1.5.2-R0.1</version>
|
||||
<type>jar</type>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.bukkit</groupId>
|
||||
<artifactId>craftbukkit</artifactId>
|
||||
<version>1.5.2-R0.1</version>
|
||||
<type>jar</type>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
160
scripts/minecraft.sh
Executable file
160
scripts/minecraft.sh
Executable file
@ -0,0 +1,160 @@
|
||||
#!/bin/bash
|
||||
|
||||
SCRIPT_DIR=$(readlink -f $(dirname "$0"))
|
||||
SERVICE='craftbukkit*.jar'
|
||||
#USERNAME="minecraft"
|
||||
CPU_COUNT=2
|
||||
BUKKIT="$SCRIPT_DIR/../target/lib/$SERVICE"
|
||||
INVOCATION="java -Xmx1000M -Xms300M -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalPacing -XX:ParallelGCThreads=$CPU_COUNT -XX:+AggressiveOpts -jar $BUKKIT nogui"
|
||||
MCPATH="$SCRIPT_DIR/../bukkit-testserver"
|
||||
|
||||
if [ ! -d "$MCPATH" ]; then
|
||||
mkdir -p "$MCPATH"
|
||||
fi
|
||||
|
||||
ME=$(whoami)
|
||||
as_user() {
|
||||
#if [ $ME == $USERNAME ] ; then
|
||||
bash -c "$1"
|
||||
#else
|
||||
#su - $USERNAME -c "$1"
|
||||
#fi
|
||||
}
|
||||
|
||||
mc_start() {
|
||||
if ps ax | grep -v grep | grep -v -i SCREEN | grep "craftbukkit" > /dev/null
|
||||
then
|
||||
echo "Tried to start but $SERVICE was already running!"
|
||||
else
|
||||
echo "$SERVICE was not running... starting."
|
||||
cd "$MCPATH"
|
||||
as_user "cd "$MCPATH" && screen -dmS minecraft $INVOCATION"
|
||||
sleep 7
|
||||
if ps ax | grep -v grep | grep -v -i SCREEN | grep "craftbukkit" > /dev/null
|
||||
then
|
||||
echo "$SERVICE is now running."
|
||||
else
|
||||
echo "Could not start $SERVICE."
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
mc_stop() {
|
||||
if ps ax | grep -v grep | grep -v -i SCREEN | grep "craftbukkit" > /dev/null
|
||||
then
|
||||
echo "$SERVICE is running... stopping."
|
||||
as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-all\"\015'"
|
||||
sleep 2
|
||||
as_user "screen -p 0 -S minecraft -X eval 'stuff \"stop\"\015'"
|
||||
sleep 6
|
||||
else
|
||||
echo "$SERVICE was not running."
|
||||
fi
|
||||
if ps ax | grep -v grep | grep -v -i SCREEN | grep "craftbukkit" > /dev/null
|
||||
then
|
||||
echo "$SERVICE could not be shut down... still running."
|
||||
else
|
||||
echo "$SERVICE is shut down."
|
||||
fi
|
||||
}
|
||||
|
||||
mc_save() {
|
||||
if ps ax | grep -v grep | grep -v -i SCREEN | grep "craftbukkit" > /dev/null
|
||||
then
|
||||
echo "$SERVICE is running... saving."
|
||||
as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-all\"\015'"
|
||||
else
|
||||
echo "$SERVICE was not running."
|
||||
fi
|
||||
}
|
||||
|
||||
mc_reload() {
|
||||
if ps ax | grep -v grep | grep -v -i SCREEN | grep "craftbukkit" > /dev/null
|
||||
then
|
||||
echo "$SERVICE is running... reloading."
|
||||
as_user "screen -p 0 -S minecraft -X eval 'stuff \"reload\"\015'"
|
||||
else
|
||||
echo "$SERVICE was not running."
|
||||
fi
|
||||
}
|
||||
|
||||
mc_reload_or_start() {
|
||||
if ps ax | grep -v grep | grep -v -i SCREEN | grep "craftbukkit" > /dev/null
|
||||
then
|
||||
echo "$SERVICE was already running! Doing a reload now!"
|
||||
mc_reload
|
||||
else
|
||||
echo "$SERVICE was not running... starting."
|
||||
cd "$MCPATH"
|
||||
as_user "cd \"$MCPATH\" && screen -dmS minecraft $INVOCATION"
|
||||
sleep 7
|
||||
if ps ax | grep -v grep | grep -v -i SCREEN | grep "craftbukkit" > /dev/null
|
||||
then
|
||||
echo "$SERVICE is now running."
|
||||
else
|
||||
echo "Could not start $SERVICE."
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
mc_ddidderr_admin() {
|
||||
if ps ax | grep -v grep | grep -v -i SCREEN | grep "craftbukkit" > /dev/null
|
||||
then
|
||||
echo "$SERVICE is running... making ddidder to admin and reloading permissions."
|
||||
as_user "screen -p 0 -S minecraft -X eval 'stuff \"pex user ddidderr group set admin\"\015'"
|
||||
as_user "screen -p 0 -S minecraft -X eval 'stuff \"pex reload\"\015'"
|
||||
else
|
||||
echo "$SERVICE was not running."
|
||||
fi
|
||||
}
|
||||
|
||||
case "$1" in
|
||||
start)
|
||||
echo "Starting Minecraft..."
|
||||
mc_start
|
||||
echo "DONE"
|
||||
;;
|
||||
stop)
|
||||
echo "Stopping Minecraft..."
|
||||
as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SERVER SHUTTING DOWN!\"\015'"
|
||||
mc_stop
|
||||
echo "DONE"
|
||||
;;
|
||||
restart)
|
||||
as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SERVER REBOOT IN 10 SECONDS.\"\015'"
|
||||
$0 stop
|
||||
sleep 1
|
||||
$0 start
|
||||
;;
|
||||
reload)
|
||||
mc_reload
|
||||
;;
|
||||
reload_or_start)
|
||||
echo "Starting or reloading Minecraft..."
|
||||
mc_reload_or_start
|
||||
echo "DONE"
|
||||
;;
|
||||
ddidderr_admin)
|
||||
mc_ddidderr_admin
|
||||
;;
|
||||
connected)
|
||||
as_user "screen -p 0 -S minecraft -X eval 'stuff \"who\"\015'"
|
||||
sleep 2s
|
||||
tac "$MCPATH"/server.log | grep -m 1 "Connected"
|
||||
;;
|
||||
status)
|
||||
if ps ax | grep -v grep | grep -v -i SCREEN | grep "craftbukkit" > /dev/null
|
||||
then
|
||||
echo "$SERVICE is running."
|
||||
else
|
||||
echo "$SERVICE is not running."
|
||||
fi
|
||||
;;
|
||||
save)
|
||||
mc_save
|
||||
;;
|
||||
*)
|
||||
echo "Usage: /etc/init.d/minecraft {start|stop|restart|connected|status}"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
29
scripts/test-deployment.sh
Executable file
29
scripts/test-deployment.sh
Executable file
@ -0,0 +1,29 @@
|
||||
#!/bin/bash
|
||||
|
||||
SCRIPT_DIR="$(dirname "$0")"
|
||||
|
||||
DEVELOPER=$(whoami)
|
||||
|
||||
if [ $DEVELOPER = "tobi" ]; then
|
||||
BUKKIT_DIR="$HOME/minecraft/testbuk"
|
||||
PLUGIN_DIR="$HOME/minecraft/testbuk/plugins"
|
||||
START_STOP_SCRIPT="$BUKKIT_DIR/../minecraft.sh"
|
||||
else
|
||||
BUKKIT_DIR="$SCRIPT_DIR/../bukkit-testserver"
|
||||
PLUGIN_DIR="$SCRIPT_DIR/../bukkit-testserver/plugins"
|
||||
START_STOP_SCRIPT="$SCRIPT_DIR/minecraft.sh"
|
||||
fi
|
||||
|
||||
# TODO: This is a bad solution! Maven should write necessary information into an extra file.
|
||||
ARTIFACT_ID="$(grep -C5 '<groupId>de.craftinc' "$SCRIPT_DIR/../pom.xml" | grep '<name>' | sed 's/[ \t]*<name>//g' | sed 's/<\/name>[ \t]*//g')"
|
||||
|
||||
# TODO: This is a bad solution! Maven should write necessary information into an extra file.
|
||||
VERSION="$(grep -C5 '<groupId>de.craftinc' "$SCRIPT_DIR/../pom.xml" | grep '<version>' | sed 's/[ \t]*<version>//g' | sed 's/<\/version>[ \t]*//g')"
|
||||
|
||||
mkdir -p "$PLUGIN_DIR"
|
||||
|
||||
cp "$SCRIPT_DIR/../target/$ARTIFACT_ID $VERSION".jar "$PLUGIN_DIR/"
|
||||
|
||||
echo -e "ddidderr\nmice_on_drugs\nMochaccino" > "$BUKKIT_DIR/ops.txt"
|
||||
|
||||
$START_STOP_SCRIPT reload_or_start
|
@ -1,155 +0,0 @@
|
||||
/* Craft Inc. BorderProtection
|
||||
Copyright (C) 2013 Paul Schulze, Tobias Ottenweller
|
||||
|
||||
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 de.craftinc.borderprotection;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Border implements ConfigurationSerializable
|
||||
{
|
||||
private static final String dataFileName = "borders.yml";
|
||||
|
||||
|
||||
private Boolean isActive;
|
||||
private static String isActiveKey = "enabled";
|
||||
|
||||
private Location rectPoint1;
|
||||
private static String rectPoint1Name = "p1";
|
||||
|
||||
private Location rectPoint2;
|
||||
private static String rectPoint2Name = "p2";
|
||||
|
||||
|
||||
private static String rectBordersKey = "rectBorders";
|
||||
|
||||
private static final HashMap<World, Border> borders = new HashMap<World, Border>();
|
||||
|
||||
private static File bordersFile = new File(Plugin.getPlugin().getDataFolder(), dataFileName);
|
||||
private static FileConfiguration bordersFileConf = YamlConfiguration.loadConfiguration(bordersFile);
|
||||
|
||||
public static HashMap<World, Border> getBorders()
|
||||
{
|
||||
return borders;
|
||||
}
|
||||
|
||||
public Location getRectPoint1()
|
||||
{
|
||||
return rectPoint1;
|
||||
}
|
||||
|
||||
public Location getRectPoint2()
|
||||
{
|
||||
return rectPoint2;
|
||||
}
|
||||
|
||||
public Boolean isActive()
|
||||
{
|
||||
return isActive;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked unused")
|
||||
public Border( Map<String, Object> map )
|
||||
{
|
||||
try
|
||||
{
|
||||
rectPoint1 = LocationSerializer.deserializeLocation((Map<String, Object>) map.get(rectPoint1Name));
|
||||
rectPoint2 = LocationSerializer.deserializeLocation((Map<String, Object>) map.get(rectPoint2Name));
|
||||
|
||||
isActive = (Boolean) map.get(isActiveKey);
|
||||
|
||||
if ( rectPoint1.getWorld().equals(rectPoint2.getWorld()) )
|
||||
{
|
||||
borders.put(rectPoint1.getWorld(), this);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Border points are at different worlds.");
|
||||
}
|
||||
}
|
||||
catch ( Exception e )
|
||||
{
|
||||
Plugin.getPlugin().getLogger().severe(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public Border( Location p1, Location p2 ) throws Exception
|
||||
{
|
||||
rectPoint1 = p1;
|
||||
rectPoint2 = p2;
|
||||
|
||||
// new border is active by default
|
||||
isActive = true;
|
||||
|
||||
if ( rectPoint1.getWorld().equals(rectPoint2.getWorld()) )
|
||||
{
|
||||
borders.put(rectPoint1.getWorld(), this);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Border points are at different worlds.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public Map<String, Object> serialize()
|
||||
{
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put(rectPoint1Name, LocationSerializer.serializeLocation(rectPoint1));
|
||||
map.put(rectPoint2Name, LocationSerializer.serializeLocation(rectPoint2));
|
||||
map.put(isActiveKey, isActive);
|
||||
|
||||
System.out.println(map);
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public static void loadBorders()
|
||||
{
|
||||
bordersFileConf.getList(rectBordersKey);
|
||||
}
|
||||
|
||||
public static void saveBorders() throws IOException
|
||||
{
|
||||
bordersFileConf.set(rectBordersKey, new ArrayList<Object>(borders.values()));
|
||||
bordersFileConf.save(bordersFile);
|
||||
}
|
||||
|
||||
public String toString()
|
||||
{
|
||||
return rectPoint1.getX() + "," + rectPoint1.getZ() + " " + rectPoint2.getX() + "," + rectPoint2.getZ();
|
||||
}
|
||||
|
||||
public void enable()
|
||||
{
|
||||
isActive = true;
|
||||
}
|
||||
|
||||
public void disable()
|
||||
{
|
||||
isActive = false;
|
||||
}
|
||||
}
|
@ -1,172 +0,0 @@
|
||||
/* Craft Inc. BorderProtection
|
||||
Copyright (C) 2013 Paul Schulze, Tobias Ottenweller
|
||||
|
||||
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 de.craftinc.borderprotection;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class BorderManager
|
||||
{
|
||||
/**
|
||||
* *********************************************************
|
||||
* GLOBAL VARIABLES
|
||||
* **********************************************************
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* For every player save the time when he got the last borderMessage
|
||||
*/
|
||||
public HashMap<String, Long> lastBorderMessage = new HashMap<String, Long>();
|
||||
|
||||
/**
|
||||
* The buffer in blocks which applies when a player is teleported inside the border. 0 means the player
|
||||
* will be teleported directly to the border.
|
||||
*/
|
||||
public static final double buffer = 0.5;
|
||||
|
||||
/**
|
||||
* A timeout for the border message. When a player tries to cross the border and sees the border message,
|
||||
* the earliest possible time the message will show up again is after <code>timeout</code> milliseconds.
|
||||
*/
|
||||
public static final Long timeout = 10000L;
|
||||
|
||||
|
||||
/**
|
||||
* *********************************************************
|
||||
* CONSTRUCTOR
|
||||
* **********************************************************
|
||||
*/
|
||||
public BorderManager()
|
||||
{
|
||||
// load borders
|
||||
Border.loadBorders();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* *********************************************************
|
||||
* GETTERS AND SETTERS
|
||||
* **********************************************************
|
||||
*/
|
||||
|
||||
public void setBorder( World world, double border ) throws Exception
|
||||
{
|
||||
new Border(new Location(world, border, 0, border), new Location(world, -border, 0, -border));
|
||||
}
|
||||
|
||||
public void setBorder( World world, String p1, String p2 ) throws Exception
|
||||
{
|
||||
|
||||
String[] coordinatesP1 = p1.split(",");
|
||||
Location l1 = new Location(world, Double.parseDouble(coordinatesP1[0]), 0, Double.parseDouble(coordinatesP1[1]));
|
||||
|
||||
String[] coordinatesP2 = p2.split(",");
|
||||
Location l2 = new Location(world, Double.parseDouble(coordinatesP2[0]), 0, Double.parseDouble(coordinatesP2[1]));
|
||||
|
||||
new Border(l1, l2);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if the given location is inside the border rectangle. Returns null if yes, otherwise new coordinates.
|
||||
*
|
||||
* @param location location to check
|
||||
* @param border Border object which defines the border
|
||||
* @param buffer if the player will be teleported back, then he will be <code>buffer</code> far away
|
||||
* from the border he tried to cross
|
||||
* @return null if the player is inside, otherwise a new player location
|
||||
*/
|
||||
public Double[] checkBorder( Location location, Border border, double buffer )
|
||||
{
|
||||
// New x and z: null by default
|
||||
Double[] newXZ = { null, null };
|
||||
|
||||
// check if player is withing the X borders
|
||||
newXZ[0] = _checkBorder(location.getX(), border.getRectPoint1().getX(), border.getRectPoint2().getX(), buffer);
|
||||
// check if player is withing the Z borders
|
||||
newXZ[1] = _checkBorder(location.getZ(), border.getRectPoint1().getZ(), border.getRectPoint2().getZ(), buffer);
|
||||
|
||||
// Do nothing, if no new coordinates have been calculated.
|
||||
if ( newXZ[0] == null && newXZ[1] == null )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return newXZ;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if the given location is between one specific border pair.
|
||||
*
|
||||
* @param location part of the location coordinates
|
||||
* @param border1 one side of the rectangle
|
||||
* @param border2 opposite side of the rectangle
|
||||
* @return null if the location is inside, otherwise a new location
|
||||
*/
|
||||
public Double _checkBorder( double location, double border1, double border2, double buffer )
|
||||
{
|
||||
double bigBorder = Math.max(border1, border2);
|
||||
double smallBorder = Math.min(border1, border2);
|
||||
|
||||
// if location is between borders do nothing
|
||||
if ( location >= smallBorder && location <= bigBorder )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( location > bigBorder )
|
||||
{
|
||||
// if location is outside of the bigBorder, teleport to the bigBorder
|
||||
return bigBorder - buffer;
|
||||
}
|
||||
else
|
||||
{
|
||||
// if location is outside of the smallBorder, teleport to the smallBorder
|
||||
return smallBorder + buffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Show the border message to a player and respect the timeout.
|
||||
*
|
||||
* @param player Player who will see the border message.
|
||||
*/
|
||||
public void showMessageWithTimeout( Player player, String message )
|
||||
{
|
||||
// get the current time
|
||||
Long now = Calendar.getInstance().getTimeInMillis();
|
||||
|
||||
if ( ( lastBorderMessage.get(player.getName()) != null &&
|
||||
now - timeout > lastBorderMessage.get(player.getName()) ) ||
|
||||
lastBorderMessage.get(player.getName()) == null )
|
||||
{
|
||||
// show message
|
||||
player.sendMessage(message);
|
||||
|
||||
// set last sent message for this player to now
|
||||
lastBorderMessage.put(player.getName(), now);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,155 +0,0 @@
|
||||
/* Craft Inc. BorderProtection
|
||||
Copyright (C) 2013 Paul Schulze, Tobias Ottenweller
|
||||
|
||||
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 de.craftinc.borderprotection;
|
||||
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class Commands implements CommandExecutor
|
||||
{
|
||||
private BorderManager borderManager;
|
||||
|
||||
public Commands( BorderManager borderManager )
|
||||
{
|
||||
this.borderManager = borderManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand( CommandSender sender, Command command, String label, String[] args )
|
||||
{
|
||||
// Check if command comes from a player.
|
||||
if ( !( sender instanceof Player ) )
|
||||
{
|
||||
sender.sendMessage(Messages.commandIssuedByNonPlayer);
|
||||
return true;
|
||||
}
|
||||
|
||||
// command for all actions
|
||||
if ( command.getName().equalsIgnoreCase("cibp") )
|
||||
{
|
||||
// help
|
||||
if ( args.length == 0 || ( args.length > 0 && args[0].equalsIgnoreCase("help") ) )
|
||||
{
|
||||
sender.sendMessage(Messages.helpGeneral);
|
||||
return true;
|
||||
}
|
||||
|
||||
// set
|
||||
if ( ( args.length == 2 || args.length == 3 ) && args[0].equalsIgnoreCase("set") )
|
||||
{
|
||||
if ( !sender.hasPermission("craftinc.borderprotection.set") )
|
||||
{
|
||||
sender.sendMessage(Messages.noPermissionSet);
|
||||
return false;
|
||||
}
|
||||
World world = ( (Player) sender ).getWorld();
|
||||
|
||||
// set <distance>
|
||||
if ( args.length == 2 )
|
||||
{
|
||||
try
|
||||
{
|
||||
borderManager.setBorder(( (Player) sender ).getWorld(), Double.parseDouble(args[1]));
|
||||
Border border = Border.getBorders().get(world);
|
||||
sender.sendMessage(Messages.borderCreationSuccessful);
|
||||
sender.sendMessage(Messages.borderInfo(world.getName(), border.toString()));
|
||||
}
|
||||
catch ( Exception e )
|
||||
{
|
||||
sender.sendMessage(e.getMessage());
|
||||
}
|
||||
}
|
||||
// set <point1> <point2>
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
borderManager.setBorder(( (Player) sender ).getWorld(), args[1], args[2]);
|
||||
Border border = Border.getBorders().get(world);
|
||||
sender.sendMessage(Messages.borderCreationSuccessful);
|
||||
sender.sendMessage(Messages.borderInfo(world.getName(), border.toString()));
|
||||
}
|
||||
catch ( Exception e )
|
||||
{
|
||||
sender.sendMessage(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// save the new border
|
||||
try
|
||||
{
|
||||
Border.saveBorders();
|
||||
}
|
||||
catch ( IOException e )
|
||||
{
|
||||
sender.sendMessage(Messages.borderSaveException);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// get
|
||||
if ( args.length == 1 && ( args[0].equalsIgnoreCase("get") || args[0].equalsIgnoreCase("info") ) )
|
||||
{
|
||||
World world = ( (Player) sender ).getWorld();
|
||||
|
||||
// exit and send the player a message if no border is set
|
||||
if ( !Border.getBorders().containsKey(world) )
|
||||
{
|
||||
sender.sendMessage(Messages.borderInfoNoBorderSet);
|
||||
return true;
|
||||
}
|
||||
|
||||
Border border = Border.getBorders().get(world);
|
||||
|
||||
sender.sendMessage(Messages.borderInfo(world.getName(), border.toString()));
|
||||
return true;
|
||||
}
|
||||
|
||||
// on
|
||||
if ( args.length == 1 && ( args[0].equalsIgnoreCase("on") || args[0].equalsIgnoreCase("off") ) )
|
||||
{
|
||||
World world = ( (Player) sender ).getWorld();
|
||||
if (args[0].equalsIgnoreCase("on")) {
|
||||
Border.getBorders().get(world).enable();
|
||||
sender.sendMessage(Messages.borderEnabled);
|
||||
} else {
|
||||
Border.getBorders().get(world).disable();
|
||||
sender.sendMessage(Messages.borderDisabled);
|
||||
}
|
||||
|
||||
// save the new border
|
||||
try
|
||||
{
|
||||
Border.saveBorders();
|
||||
}
|
||||
catch ( IOException e )
|
||||
{
|
||||
sender.sendMessage(Messages.borderEnableDisableException);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
sender.sendMessage(Messages.helpGeneral);
|
||||
return false;
|
||||
}
|
||||
}
|
@ -16,12 +16,24 @@
|
||||
*/
|
||||
package de.craftinc.borderprotection;
|
||||
|
||||
import de.craftinc.borderprotection.borders.Border;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Messages
|
||||
{
|
||||
private static final String NEWLINE = "\n";
|
||||
|
||||
private static final String pluginName = Plugin.instance.getDescription().getName();
|
||||
|
||||
/**
|
||||
* For every player and every message of that player save the time when he got the last one.
|
||||
*/
|
||||
private static final HashMap<String, HashMap<String, Long>> lastMessage = new HashMap<String, HashMap<String, Long>>();
|
||||
|
||||
private static String makeCmd( String command, String explanation, String... args )
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
@ -58,21 +70,28 @@ public class Messages
|
||||
"strange new worlds, to seek out new life and new civilizations, to boldly go where no one has gone before.";
|
||||
|
||||
public static String borderMessage =
|
||||
"Sorry Dude! This is the border... the final frontier! " + borderExplanation + NEWLINE +
|
||||
ChatColor.YELLOW + "Sorry Dude! " +
|
||||
ChatColor.WHITE + "This is the border... the final frontier! " + borderExplanation + NEWLINE +
|
||||
makeCmd("/cibp get", "shows the borders of the current world");
|
||||
|
||||
public static String borderTeleportMessage =
|
||||
"Sorry Dude! You cannot teleport outside the border. " + borderExplanation + NEWLINE +
|
||||
ChatColor.YELLOW + "Sorry Dude! " +
|
||||
ChatColor.WHITE + "You cannot teleport outside the border. " + borderExplanation + NEWLINE +
|
||||
makeCmd("/cibp get", "shows the borders of the current world");
|
||||
|
||||
public static String helpGeneral =
|
||||
ChatColor.GREEN + "CraftInc BorderProtection - Usage:" + NEWLINE +
|
||||
ChatColor.GREEN + pluginName + " - Usage:" + NEWLINE +
|
||||
ChatColor.WHITE + "Commands are always related to the current world." + NEWLINE +
|
||||
makeCmd("help", "shows this help") +
|
||||
makeCmd("get | info", "shows the border of the current world") +
|
||||
makeCmd("on | off", "enables/disables the border of the current world") +
|
||||
makeCmd("set", "Border rectangle edges will be this far away from point of origin.", "<integer>") +
|
||||
makeCmd("set", "Border rectangle is defined by the two points. A point is specified as: x,z",
|
||||
"<point1>", "<point2>");
|
||||
makeCmd("get | info", "Shows information about the border.") +
|
||||
makeCmd("generate", "Generate all not existing chunks inside the border.") +
|
||||
makeCmd("cancelgenerate", "Cancels the generation of chunks.") +
|
||||
makeCmd("on | off", "Enables/disables the border.") +
|
||||
makeCmd("set", "Square border with distance (d) from 0,0.", "r", "<d>") +
|
||||
makeCmd("set", "Rectangle defined by two points. Point=x,z.", "r", "<p1>", "<p2>") +
|
||||
makeCmd("set", "Circle border with radius from 0,0.", "c", "<radius>") +
|
||||
makeCmd("set", "Circle defined by center and radius. Center=x,z.", "c", "<c>", "<r>") +
|
||||
makeCmd("checkversion", "Checks for a newer version.");
|
||||
|
||||
public static String borderCreationSuccessful
|
||||
= ChatColor.YELLOW + "New border was set " +
|
||||
@ -80,22 +99,30 @@ public class Messages
|
||||
ChatColor.YELLOW + "!";
|
||||
|
||||
public static String commandIssuedByNonPlayer
|
||||
= ChatColor.RED + "Only a player can use CraftInc BorderProtection commands!";
|
||||
= ChatColor.RED + "Only a player can use " + pluginName + " commands!";
|
||||
|
||||
public static String borderInfo( String worldName, String borderDef )
|
||||
public static String borderInfo( String worldName, Border border )
|
||||
{
|
||||
String borderEnabled;
|
||||
if ( border.isActive() )
|
||||
borderEnabled = ChatColor.GREEN + "enabled";
|
||||
else
|
||||
borderEnabled = ChatColor.RED + "disabled";
|
||||
|
||||
return ChatColor.WHITE + "Borders of world " +
|
||||
ChatColor.YELLOW + worldName +
|
||||
ChatColor.WHITE + ": " +
|
||||
ChatColor.YELLOW + borderDef;
|
||||
return ChatColor.WHITE + "Border of world " + ChatColor.YELLOW + worldName + ChatColor.WHITE + ": " + NEWLINE +
|
||||
ChatColor.YELLOW + "Type: " + ChatColor.WHITE + border.getBorderTypeString() + NEWLINE +
|
||||
border.getBorderInfoString() + NEWLINE +
|
||||
ChatColor.WHITE + "Border is " + borderEnabled + ChatColor.WHITE + ".";
|
||||
}
|
||||
|
||||
public static String borderInfoNoBorderSet =
|
||||
ChatColor.YELLOW + "No border here.";
|
||||
ChatColor.YELLOW + "No border in this world.";
|
||||
|
||||
public static String noPermissionSet =
|
||||
ChatColor.RED + "Sorry, you don't have permission to set the border.";
|
||||
ChatColor.RED + "Sorry, you don't have permission to change the border.";
|
||||
|
||||
public static String noPermissionCheckversion =
|
||||
ChatColor.RED + "Sorry, you don't have permission to check for new versions.";
|
||||
|
||||
public static String borderEnabled =
|
||||
ChatColor.YELLOW + "Border enabled.";
|
||||
@ -107,5 +134,54 @@ public class Messages
|
||||
ChatColor.RED + "Error: Could not save border on server. After the next reload this border will be lost!";
|
||||
|
||||
public static String borderEnableDisableException =
|
||||
ChatColor.RED + "Error: Could not save border state on server. After the next reload this border state will be lost!";
|
||||
ChatColor.RED +
|
||||
"Error: Could not save border state on server. After the next reload this border state will be lost!";
|
||||
|
||||
public static String updateMessage( String newVersion, String curVersion )
|
||||
{
|
||||
return ChatColor.RED + pluginName + ": New version available!" + NEWLINE +
|
||||
ChatColor.YELLOW + "Current version: " + ChatColor.WHITE + curVersion + NEWLINE +
|
||||
ChatColor.YELLOW + "New version: " + ChatColor.WHITE + newVersion + NEWLINE +
|
||||
ChatColor.YELLOW + "Please visit:" + NEWLINE +
|
||||
ChatColor.AQUA + "http://dev.bukkit.org/bukkit-mods/craftinc-borderprotection" + NEWLINE +
|
||||
ChatColor.YELLOW + "to get the latest version!";
|
||||
}
|
||||
|
||||
public static String noUpdateAvailable =
|
||||
ChatColor.YELLOW + "No updates available.";
|
||||
|
||||
/**
|
||||
* Display a message to a player and then wait for timeout seconds before displaying it again.
|
||||
*
|
||||
* @param player Player who will see the message.
|
||||
* @param message The message String.
|
||||
* @param timeout Timeout in seconds until the message will be displayed earliest.
|
||||
*/
|
||||
public static void showMessageWithTimeout( final Player player, final String message, final Integer timeout )
|
||||
{
|
||||
// get the current time
|
||||
final Long now = Calendar.getInstance().getTimeInMillis();
|
||||
|
||||
if ( ( lastMessage.get(player.getName()) != null && lastMessage.get(player.getName()).get(message) != null &&
|
||||
now - timeout * 1000 > lastMessage.get(player.getName()).get(message) ) ||
|
||||
lastMessage.get(player.getName()) != null && lastMessage.get(player.getName()).get(message) == null ||
|
||||
lastMessage.get(player.getName()) == null )
|
||||
{
|
||||
// show message
|
||||
player.sendMessage(message);
|
||||
|
||||
// set last sent message for this player to now
|
||||
if ( lastMessage.get(player.getName()) == null )
|
||||
{
|
||||
lastMessage.put(player.getName(), new HashMap<String, Long>()
|
||||
{{
|
||||
put(message, now);
|
||||
}});
|
||||
}
|
||||
else
|
||||
{
|
||||
lastMessage.get(player.getName()).put(message, now);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,122 +0,0 @@
|
||||
/* Craft Inc. BorderProtection
|
||||
Copyright (C) 2013 Paul Schulze, Tobias Ottenweller
|
||||
|
||||
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 de.craftinc.borderprotection;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerMoveEvent;
|
||||
|
||||
public class PlayerMoveListener implements Listener
|
||||
{
|
||||
|
||||
private BorderManager borderManager;
|
||||
|
||||
public PlayerMoveListener( BorderManager borderManager )
|
||||
{
|
||||
this.borderManager = borderManager;
|
||||
}
|
||||
|
||||
private Double goUpUntilFreeSpot( Location newLocation )
|
||||
{
|
||||
// go up in height until the player can stand in AIR
|
||||
Block footBlock = newLocation.getBlock();
|
||||
Block headBlock = newLocation.getBlock().getRelative(BlockFace.UP);
|
||||
while ( footBlock.getType() != Material.AIR || headBlock.getType() != Material.AIR )
|
||||
{
|
||||
byte offset = 1;
|
||||
if ( headBlock.getType() != Material.AIR )
|
||||
{
|
||||
offset = 2;
|
||||
}
|
||||
footBlock = footBlock.getRelative(0, offset, 0);
|
||||
headBlock = headBlock.getRelative(0, offset, 0);
|
||||
}
|
||||
// set the y value to a spot where the player can stand free
|
||||
return (double) footBlock.getY();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@EventHandler(priority = EventPriority.NORMAL)
|
||||
public void onPlayerMove( PlayerMoveEvent e )
|
||||
{
|
||||
// do nothing if player has the ignoreborders permission
|
||||
if ( e.getPlayer().hasPermission("craftinc.borderprotection.ignoreborders") )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// do nothing if there are no border definitions at all
|
||||
if ( Border.getBorders().isEmpty() )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// player location
|
||||
Location playerLocation = e.getPlayer().getLocation();
|
||||
|
||||
// world where the player is in
|
||||
World world = e.getPlayer().getWorld();
|
||||
|
||||
// border of this world
|
||||
Border border = Border.getBorders().get(world);
|
||||
|
||||
// do nothing if there are no borders for this specific world
|
||||
if ( border == null )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// do nothing if border is disabled
|
||||
if ( !border.isActive() )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// change x or z. default: do not change
|
||||
Double[] newXZ;
|
||||
|
||||
// check if player is inside the borders. null if yes, otherwise a tuple which defines the new player position
|
||||
newXZ = borderManager.checkBorder(playerLocation, border, BorderManager.buffer);
|
||||
|
||||
// Do nothing, if no new coordinates have been calculated.
|
||||
if ( newXZ == null )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// if one of the coordinates is null, set it to the player's value
|
||||
newXZ[0] = newXZ[0] == null ? playerLocation.getX() : newXZ[0];
|
||||
newXZ[1] = newXZ[1] == null ? playerLocation.getZ() : newXZ[1];
|
||||
|
||||
// change Y if necessary (when there is no free spot)
|
||||
Double newY = goUpUntilFreeSpot(new Location(world, newXZ[0], e.getPlayer().getLocation().getY(), newXZ[1]));
|
||||
|
||||
// teleport the player to the new X and Z coordinates
|
||||
e.getPlayer().teleport(
|
||||
new Location(e.getPlayer().getWorld(), newXZ[0], newY, newXZ[1], playerLocation.getYaw(),
|
||||
playerLocation.getPitch()));
|
||||
|
||||
// send a message to the player
|
||||
borderManager.showMessageWithTimeout(e.getPlayer(), Messages.borderMessage);
|
||||
}
|
||||
}
|
@ -16,23 +16,23 @@
|
||||
*/
|
||||
package de.craftinc.borderprotection;
|
||||
|
||||
import de.craftinc.borderprotection.borders.CircBorder;
|
||||
import de.craftinc.borderprotection.borders.RectBorder;
|
||||
import de.craftinc.borderprotection.commands.CommandSwitch;
|
||||
import de.craftinc.borderprotection.events.*;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerialization;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class Plugin extends JavaPlugin
|
||||
{
|
||||
private static JavaPlugin cibpPlugin;
|
||||
|
||||
public static JavaPlugin getPlugin()
|
||||
{
|
||||
return cibpPlugin;
|
||||
}
|
||||
public static Plugin instance;
|
||||
|
||||
@Override
|
||||
public void onLoad()
|
||||
{
|
||||
ConfigurationSerialization.registerClass(Border.class);
|
||||
ConfigurationSerialization.registerClass(RectBorder.class);
|
||||
ConfigurationSerialization.registerClass(CircBorder.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -43,19 +43,23 @@ public class Plugin extends JavaPlugin
|
||||
@Override
|
||||
public void onEnable()
|
||||
{
|
||||
Plugin.cibpPlugin = this;
|
||||
Plugin.instance = this;
|
||||
|
||||
BorderManager borderManager = new BorderManager();
|
||||
PlayerMoveListener playerMoveListener = new PlayerMoveListener(borderManager);
|
||||
PlayerTeleportListener playerTeleportListener = new PlayerTeleportListener(borderManager);
|
||||
// create listeners
|
||||
PlayerMoveListener playerMoveListener = new PlayerMoveListener();
|
||||
PlayerTeleportListener playerTeleportListener = new PlayerTeleportListener();
|
||||
PlayerLoginListener playerLoginListener = new PlayerLoginListener();
|
||||
PlayerQuitListener playerQuitListener = new PlayerQuitListener();
|
||||
|
||||
// commands
|
||||
Commands commandExecutor = new Commands(borderManager);
|
||||
CommandSwitch commandExecutor = new CommandSwitch();
|
||||
getCommand("cibp").setExecutor(commandExecutor);
|
||||
|
||||
// listeners
|
||||
// register listeners
|
||||
PluginManager pm = this.getServer().getPluginManager();
|
||||
pm.registerEvents(playerMoveListener, this);
|
||||
pm.registerEvents(playerTeleportListener, this);
|
||||
pm.registerEvents(playerLoginListener, this);
|
||||
pm.registerEvents(playerQuitListener, this);
|
||||
}
|
||||
}
|
||||
|
144
src/main/java/de/craftinc/borderprotection/borders/Border.java
Normal file
144
src/main/java/de/craftinc/borderprotection/borders/Border.java
Normal file
@ -0,0 +1,144 @@
|
||||
/* Craft Inc. BorderProtection
|
||||
Copyright (C) 2013 Paul Schulze, Tobias Ottenweller
|
||||
|
||||
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 de.craftinc.borderprotection.borders;
|
||||
|
||||
import de.craftinc.borderprotection.Plugin;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class Border
|
||||
{
|
||||
private static final String dataFileName = "borders.yml";
|
||||
|
||||
protected Boolean isActive;
|
||||
|
||||
protected static final String isActiveKey = "enabled";
|
||||
protected static final String bordersKey = "borders";
|
||||
|
||||
protected static final HashMap<World, Border> borders = new HashMap<World, Border>();
|
||||
|
||||
private static final File bordersFile = new File(Plugin.instance.getDataFolder(), dataFileName);
|
||||
private static final FileConfiguration bordersFileConf = YamlConfiguration.loadConfiguration(bordersFile);
|
||||
|
||||
/**
|
||||
* The buffer in blocks which applies when a player is teleported inside the border. 0 means the player
|
||||
* will be teleported directly to the border.
|
||||
*/
|
||||
public static final double buffer = 0.5;
|
||||
|
||||
public static HashMap<World, Border> getBorders()
|
||||
{
|
||||
return borders;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a String which identifies the type of the border. Example: Rectangle
|
||||
*/
|
||||
public abstract String getBorderTypeString();
|
||||
|
||||
/**
|
||||
* Returns a formatted String (colors and newlines) which fits into the borderInfo message and describes
|
||||
* the border properties properly.
|
||||
*/
|
||||
public abstract String getBorderInfoString();
|
||||
|
||||
/**
|
||||
* Checks if the given location is inside or outside the border. If it is outside a new location (inside the border)
|
||||
* is returned, otherwise null.
|
||||
*
|
||||
* @param l Location to check if inside the border
|
||||
* @return null if l is inside the border otherwise a new Location which is inside
|
||||
*/
|
||||
public Location checkBorder( Location l )
|
||||
{
|
||||
return checkBorder(l, 0.0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given location is inside or outside the border. If it is outside a new location (inside the border)
|
||||
* is returned, otherwise null. Applies padding to the border. (Simulates a larger border using padding.)
|
||||
* @param l Location to check if inside the border
|
||||
* @param padding number of Blocks of padding applied to the border.
|
||||
* @return null if l is inside the border otherwise a new Location which is inside
|
||||
*/
|
||||
public abstract Location checkBorder (Location l, double padding);
|
||||
|
||||
/**
|
||||
* Returns an array of two Location objects defining a rectangle bigger or at size of the border. There are no
|
||||
* locations outside the rectangle which are inside the border.
|
||||
*/
|
||||
public abstract Location[] getSurroundingRect();
|
||||
|
||||
public Boolean isActive()
|
||||
{
|
||||
return isActive;
|
||||
}
|
||||
|
||||
public Border( Map<String, Object> map )
|
||||
{
|
||||
try
|
||||
{
|
||||
isActive = (Boolean) map.get(isActiveKey);
|
||||
}
|
||||
catch ( Exception e )
|
||||
{
|
||||
// FIXME
|
||||
Plugin.instance.getLogger().severe(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public Border()
|
||||
{
|
||||
// new borders are enabled by default
|
||||
isActive = true;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public static void loadBorders()
|
||||
{
|
||||
bordersFileConf.getList(bordersKey);
|
||||
}
|
||||
|
||||
public static void saveBorders() throws IOException
|
||||
{
|
||||
bordersFileConf.set(bordersKey, new ArrayList<Object>(borders.values()));
|
||||
bordersFileConf.save(bordersFile);
|
||||
}
|
||||
|
||||
public void enable()
|
||||
{
|
||||
isActive = true;
|
||||
}
|
||||
|
||||
public void disable()
|
||||
{
|
||||
isActive = false;
|
||||
}
|
||||
|
||||
protected void serialize( Map<String, Object> map )
|
||||
{
|
||||
map.put(isActiveKey, isActive);
|
||||
}
|
||||
}
|
@ -0,0 +1,134 @@
|
||||
/* Craft Inc. BorderProtection
|
||||
Copyright (C) 2013 Paul Schulze, Tobias Ottenweller
|
||||
|
||||
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 de.craftinc.borderprotection.borders;
|
||||
|
||||
import de.craftinc.borderprotection.util.LocationSerializer2D;
|
||||
import de.craftinc.borderprotection.Plugin;
|
||||
import de.craftinc.borderprotection.util.PlayerMovementUtil;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class CircBorder extends Border implements ConfigurationSerializable
|
||||
{
|
||||
private Double radius;
|
||||
private Location center;
|
||||
|
||||
private static String centerKey = "center";
|
||||
private static String radiusKey = "radius";
|
||||
|
||||
@SuppressWarnings("unchecked unused")
|
||||
public CircBorder( Map<String, Object> map )
|
||||
{
|
||||
super(map);
|
||||
try
|
||||
{
|
||||
center = LocationSerializer2D.deserializeLocation((Map<String, Object>) map.get(centerKey));
|
||||
radius = (Double) map.get(radiusKey);
|
||||
|
||||
borders.put(center.getWorld(), this);
|
||||
}
|
||||
catch ( Exception e )
|
||||
{
|
||||
// FIXME
|
||||
Plugin.instance.getLogger().severe(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public CircBorder( Location center, Double radius )
|
||||
{
|
||||
super();
|
||||
|
||||
this.center = center;
|
||||
this.radius = radius;
|
||||
|
||||
borders.put(center.getWorld(), this);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public Map<String, Object> serialize()
|
||||
{
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
super.serialize(map);
|
||||
map.put(centerKey, LocationSerializer2D.serializeLocation(center));
|
||||
map.put(radiusKey, radius);
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public String toString()
|
||||
{
|
||||
return "CircBorder(" + "center: " + center.getX() + "," + center.getZ() + ", radius: " + radius + ")";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBorderTypeString()
|
||||
{
|
||||
return "Circle";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBorderInfoString()
|
||||
{
|
||||
return ChatColor.YELLOW + "Center: " + ChatColor.WHITE + center.getX() + "," + center.getZ() + "\n" +
|
||||
ChatColor.YELLOW + "Raduis: " + ChatColor.WHITE + radius;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location checkBorder( Location l, double padding )
|
||||
{
|
||||
double paddedRadius = radius + padding;
|
||||
|
||||
double distX = l.getX() - center.getX();
|
||||
double distZ = l.getZ() - center.getZ();
|
||||
|
||||
double distanceFromCenterSquared = distX * distX + distZ * distZ;
|
||||
double radiusSquared = paddedRadius * paddedRadius;
|
||||
|
||||
// inside the border
|
||||
if ( distanceFromCenterSquared <= radiusSquared )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// outside the border: it's ok to use square-root function here, because this only happens very few times
|
||||
double ratio = paddedRadius / Math.sqrt(distanceFromCenterSquared);
|
||||
double newX = center.getX() + ( ratio * distX );
|
||||
double newZ = center.getZ() + ( ratio * distZ );
|
||||
|
||||
Location newLocation = new Location(l.getWorld(), newX, l.getY(), newZ, l.getYaw(), l.getPitch());
|
||||
|
||||
// ensure that the player will not appear in a block
|
||||
// TODO: Should hook into another Plugin maybe or implement something better
|
||||
newLocation.setY(PlayerMovementUtil.goUpUntilFreeSpot(newLocation));
|
||||
|
||||
return newLocation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location[] getSurroundingRect()
|
||||
{
|
||||
Location l1 = new Location(center.getWorld(), center.getX()+radius, center.getY(), center.getX()+radius);
|
||||
Location l2 = new Location(center.getWorld(), center.getX()-radius, center.getY(), center.getX()-radius);
|
||||
|
||||
return new Location[]{ l1, l2 };
|
||||
}
|
||||
}
|
@ -0,0 +1,184 @@
|
||||
/* Craft Inc. BorderProtection
|
||||
Copyright (C) 2013 Paul Schulze, Tobias Ottenweller
|
||||
|
||||
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 de.craftinc.borderprotection.borders;
|
||||
|
||||
import de.craftinc.borderprotection.util.LocationSerializer2D;
|
||||
import de.craftinc.borderprotection.Plugin;
|
||||
import de.craftinc.borderprotection.util.PlayerMovementUtil;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class RectBorder extends Border implements ConfigurationSerializable
|
||||
{
|
||||
private Location rectPoint1;
|
||||
private Location rectPoint2;
|
||||
|
||||
private static String rectPoint1Name = "p1";
|
||||
private static String rectPoint2Name = "p2";
|
||||
|
||||
@SuppressWarnings("unchecked unused")
|
||||
public RectBorder( Map<String, Object> map )
|
||||
{
|
||||
super(map);
|
||||
|
||||
try
|
||||
{
|
||||
rectPoint1 = LocationSerializer2D.deserializeLocation((Map<String, Object>) map.get(rectPoint1Name));
|
||||
rectPoint2 = LocationSerializer2D.deserializeLocation((Map<String, Object>) map.get(rectPoint2Name));
|
||||
|
||||
if ( rectPoint1.getWorld().equals(rectPoint2.getWorld()) )
|
||||
{
|
||||
borders.put(rectPoint1.getWorld(), this);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("RectBorder points are at different worlds.");
|
||||
}
|
||||
}
|
||||
catch ( Exception e )
|
||||
{
|
||||
Plugin.instance.getLogger().severe(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public RectBorder( Location p1, Location p2 ) throws Exception
|
||||
{
|
||||
super();
|
||||
|
||||
rectPoint1 = p1;
|
||||
rectPoint2 = p2;
|
||||
|
||||
if ( rectPoint1.getWorld().equals(rectPoint2.getWorld()) )
|
||||
{
|
||||
borders.put(rectPoint1.getWorld(), this);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("RectBorder points are at different worlds.");
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public Map<String, Object> serialize()
|
||||
{
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
super.serialize(map);
|
||||
map.put(rectPoint1Name, LocationSerializer2D.serializeLocation(rectPoint1));
|
||||
map.put(rectPoint2Name, LocationSerializer2D.serializeLocation(rectPoint2));
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
|
||||
public String toString()
|
||||
{
|
||||
return "RectBorder(" + rectPoint1.getX() + "," + rectPoint1.getZ() + ";" + rectPoint2.getX() + "," +
|
||||
rectPoint2.getZ() + ")";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBorderTypeString()
|
||||
{
|
||||
return "Rectangle";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBorderInfoString()
|
||||
{
|
||||
return ChatColor.YELLOW + "Point 1: " + ChatColor.WHITE + rectPoint1.getX() + "," + rectPoint1.getZ() + "\n" +
|
||||
ChatColor.YELLOW + "Point 2: " + ChatColor.WHITE + rectPoint2.getX() + "," + rectPoint2.getZ();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location checkBorder( Location l, double padding )
|
||||
{
|
||||
// New x and z: null by default
|
||||
Double[] newXZ = { null, null };
|
||||
|
||||
// check if player is withing the X borders
|
||||
newXZ[0] = _checkBorder(l.getX(), this.rectPoint1.getX(), this.rectPoint2.getX(), padding);
|
||||
// check if player is withing the Z borders
|
||||
newXZ[1] = _checkBorder(l.getZ(), this.rectPoint1.getZ(), this.rectPoint2.getZ(), padding);
|
||||
|
||||
// Do nothing, if no new coordinates have been calculated.
|
||||
if ( newXZ[0] == null && newXZ[1] == null )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// if one of the coordinates is null, set it to the player's value
|
||||
newXZ[0] = newXZ[0] == null ? l.getX() : newXZ[0];
|
||||
newXZ[1] = newXZ[1] == null ? l.getZ() : newXZ[1];
|
||||
|
||||
// new location
|
||||
Location newLocation = new Location(l.getWorld(), newXZ[0], l.getY(), newXZ[1], l.getYaw(), l.getPitch());
|
||||
|
||||
// change Y if necessary (when there is no free spot)
|
||||
newLocation.setY(PlayerMovementUtil.goUpUntilFreeSpot(newLocation));
|
||||
|
||||
return newLocation;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if the given location is between one specific border pair.
|
||||
*
|
||||
* @param location part of the location coordinates
|
||||
* @param border1 one side of the rectangle
|
||||
* @param border2 opposite side of the rectangle
|
||||
* @param padding a padding (number of block) used to enlarge the border
|
||||
* @return null if the location is inside, otherwise a new location
|
||||
*/
|
||||
private static Double _checkBorder( double location, double border1, double border2, double padding )
|
||||
{
|
||||
double bigBorder = Math.max(border1, border2) + padding;
|
||||
double smallBorder = Math.min(border1, border2) - padding;
|
||||
|
||||
// if location is between borders do nothing
|
||||
if ( location >= smallBorder && location <= bigBorder )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( location > bigBorder )
|
||||
{
|
||||
// if location is outside of the bigBorder, teleport to the bigBorder
|
||||
if ( buffer > bigBorder )
|
||||
return bigBorder;
|
||||
return bigBorder - buffer;
|
||||
}
|
||||
else
|
||||
{
|
||||
// if location is outside of the smallBorder, teleport to the smallBorder
|
||||
if ( buffer > Math.abs(smallBorder) )
|
||||
return smallBorder;
|
||||
return smallBorder + buffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location[] getSurroundingRect()
|
||||
{
|
||||
return new Location[]{ rectPoint1, rectPoint2 };
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
/* Craft Inc. BorderProtection
|
||||
Copyright (C) 2013 Paul Schulze
|
||||
|
||||
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 de.craftinc.borderprotection.commands;
|
||||
|
||||
|
||||
import de.craftinc.borderprotection.Messages;
|
||||
import de.craftinc.borderprotection.util.ChunkGenerator;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CancelGenerateCommand implements SubCommand
|
||||
{
|
||||
@Override
|
||||
public boolean execute(CommandSender sender, String[] parameters)
|
||||
{
|
||||
if ( !sender.hasPermission("craftinc.borderprotection.set") ) // TODO: a new/different permission?
|
||||
{
|
||||
sender.sendMessage(Messages.noPermissionSet);
|
||||
return false;
|
||||
}
|
||||
|
||||
World world = ( (Player) sender ).getWorld();
|
||||
|
||||
if (!ChunkGenerator.isGenerating(world))
|
||||
{
|
||||
sender.sendMessage("nothing to cancel"); // TODO: put better message into Message class
|
||||
}
|
||||
else
|
||||
{
|
||||
ChunkGenerator.cancelRender(world);
|
||||
sender.sendMessage("generation canceled"); // TODO: put better message into Message class
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> commandNames()
|
||||
{
|
||||
ArrayList<String> names = new ArrayList<String>();
|
||||
names.add("cancelgenerate");
|
||||
|
||||
return names;
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
/* Craft Inc. BorderProtection
|
||||
Copyright (C) 2013 Paul Schulze
|
||||
|
||||
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 de.craftinc.borderprotection.commands;
|
||||
|
||||
import de.craftinc.borderprotection.Messages;
|
||||
import de.craftinc.borderprotection.util.UpdateHelper;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CheckVersionCommand implements SubCommand
|
||||
{
|
||||
@Override
|
||||
public boolean execute(CommandSender sender, String[] parameters)
|
||||
{
|
||||
if ( !sender.hasPermission("craftinc.borderprotection.update") )
|
||||
{
|
||||
sender.sendMessage(Messages.noPermissionCheckversion);
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( UpdateHelper.newVersionAvailable() )
|
||||
{
|
||||
sender.sendMessage(
|
||||
Messages.updateMessage(UpdateHelper.cachedLatestVersion, UpdateHelper.getCurrentVersion()));
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
sender.sendMessage(Messages.noUpdateAvailable);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> commandNames()
|
||||
{
|
||||
ArrayList<String> names = new ArrayList<String>();
|
||||
names.add("checkversion");
|
||||
|
||||
return names;
|
||||
}
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
/* Craft Inc. BorderProtection
|
||||
Copyright (C) 2013 Paul Schulze, Tobias Ottenweller
|
||||
|
||||
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 de.craftinc.borderprotection.commands;
|
||||
|
||||
import de.craftinc.borderprotection.Messages;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class CommandSwitch implements CommandExecutor
|
||||
{
|
||||
protected Map<String, SubCommand> subCommandsMap = new HashMap<String, SubCommand>();
|
||||
|
||||
public CommandSwitch()
|
||||
{
|
||||
registerCommand(new CancelGenerateCommand());
|
||||
registerCommand(new CheckVersionCommand());
|
||||
registerCommand(new GenerateCommand());
|
||||
registerCommand(new GetCommand());
|
||||
registerCommand(new HelpCommand());
|
||||
registerCommand(new OnOffCommand());
|
||||
registerCommand(new SetCommand());
|
||||
}
|
||||
|
||||
protected void registerCommand(SubCommand command)
|
||||
{
|
||||
for (String commandName : command.commandNames())
|
||||
{
|
||||
subCommandsMap.put(commandName, command);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand( CommandSender sender, Command command, String label, String[] args )
|
||||
{
|
||||
// Check if command comes from a player.
|
||||
if ( !( sender instanceof Player ) )
|
||||
{
|
||||
sender.sendMessage(Messages.commandIssuedByNonPlayer);
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean success = false;
|
||||
|
||||
// command for all actions
|
||||
if ( command.getName().equalsIgnoreCase("cibp") )
|
||||
{
|
||||
if (args.length > 0)
|
||||
{
|
||||
String lowerCaseSubCommandName = args[0].toLowerCase();
|
||||
SubCommand subCommand = subCommandsMap.get(lowerCaseSubCommandName);
|
||||
|
||||
if (subCommand != null)
|
||||
{
|
||||
success = subCommand.execute(sender, args);
|
||||
}
|
||||
}
|
||||
|
||||
if (!success)
|
||||
{
|
||||
subCommandsMap.get("help").execute(sender, args);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
/* Craft Inc. BorderProtection
|
||||
Copyright (C) 2013 Paul Schulze
|
||||
|
||||
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 de.craftinc.borderprotection.commands;
|
||||
|
||||
import de.craftinc.borderprotection.Messages;
|
||||
import de.craftinc.borderprotection.util.ChunkGenerator;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class GenerateCommand implements SubCommand
|
||||
{
|
||||
@Override
|
||||
public boolean execute(CommandSender sender, String[] parameters)
|
||||
{
|
||||
if ( !sender.hasPermission("craftinc.borderprotection.set") ) // TODO: a new/different permission?
|
||||
{
|
||||
sender.sendMessage(Messages.noPermissionSet);
|
||||
return false;
|
||||
}
|
||||
|
||||
World world = ( (Player) sender ).getWorld();
|
||||
|
||||
if (ChunkGenerator.isGenerating(world))
|
||||
{
|
||||
sender.sendMessage("already generating! will resume on logout"); // TODO: put better message into Message class
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ChunkGenerator.generate(world))
|
||||
{
|
||||
sender.sendMessage("world marked for generation! will start on logout"); // TODO: put better message into Message class
|
||||
}
|
||||
else
|
||||
{
|
||||
sender.sendMessage("could not start generation. is there a border?"); // TODO: put better message into Message class
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> commandNames()
|
||||
{
|
||||
ArrayList<String> names = new ArrayList<String>();
|
||||
names.add("generate");
|
||||
|
||||
return names;
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
/* Craft Inc. BorderProtection
|
||||
Copyright (C) 2013 Paul Schulze
|
||||
|
||||
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 de.craftinc.borderprotection.commands;
|
||||
|
||||
import de.craftinc.borderprotection.Messages;
|
||||
import de.craftinc.borderprotection.borders.Border;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class GetCommand implements SubCommand
|
||||
{
|
||||
@Override
|
||||
public boolean execute(CommandSender sender, String[] parameters)
|
||||
{
|
||||
World world = ( (Player) sender ).getWorld();
|
||||
|
||||
// exit and send the player a message if no border is set
|
||||
if ( !Border.getBorders().containsKey(world) )
|
||||
{
|
||||
sender.sendMessage(Messages.borderInfoNoBorderSet);
|
||||
return true;
|
||||
}
|
||||
|
||||
Border border = Border.getBorders().get(world);
|
||||
|
||||
sender.sendMessage(Messages.borderInfo(world.getName(), border));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> commandNames()
|
||||
{
|
||||
ArrayList<String> names = new ArrayList<String>();
|
||||
names.add("get");
|
||||
names.add("info");
|
||||
|
||||
return names;
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
/* Craft Inc. BorderProtection
|
||||
Copyright (C) 2013 Paul Schulze
|
||||
|
||||
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 de.craftinc.borderprotection.commands;
|
||||
|
||||
import de.craftinc.borderprotection.Messages;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class HelpCommand implements SubCommand
|
||||
{
|
||||
@Override
|
||||
public boolean execute(CommandSender sender, String[] parameters)
|
||||
{
|
||||
sender.sendMessage(Messages.helpGeneral);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> commandNames()
|
||||
{
|
||||
ArrayList<String> names = new ArrayList<String>();
|
||||
names.add("help");
|
||||
|
||||
return names;
|
||||
}
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
/* Craft Inc. BorderProtection
|
||||
Copyright (C) 2013 Paul Schulze
|
||||
|
||||
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 de.craftinc.borderprotection.commands;
|
||||
|
||||
|
||||
import de.craftinc.borderprotection.Messages;
|
||||
import de.craftinc.borderprotection.borders.Border;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class OnOffCommand implements SubCommand
|
||||
{
|
||||
@Override
|
||||
public boolean execute(CommandSender sender, String[] parameters)
|
||||
{
|
||||
if ( !sender.hasPermission("craftinc.borderprotection.set") )
|
||||
{
|
||||
sender.sendMessage(Messages.noPermissionSet);
|
||||
return false;
|
||||
}
|
||||
|
||||
World world = ( (Player) sender ).getWorld();
|
||||
Border border = Border.getBorders().get(world);
|
||||
|
||||
if ( border != null )
|
||||
{
|
||||
if ( parameters[0].equalsIgnoreCase("on") )
|
||||
{
|
||||
border.enable();
|
||||
sender.sendMessage(Messages.borderEnabled);
|
||||
}
|
||||
else
|
||||
{
|
||||
border.disable();
|
||||
sender.sendMessage(Messages.borderDisabled);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sender.sendMessage(Messages.borderInfoNoBorderSet);
|
||||
}
|
||||
|
||||
// save the changed border
|
||||
try
|
||||
{
|
||||
Border.saveBorders();
|
||||
}
|
||||
catch ( IOException e )
|
||||
{
|
||||
sender.sendMessage(Messages.borderEnableDisableException);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> commandNames()
|
||||
{
|
||||
ArrayList<String> names = new ArrayList<String>();
|
||||
names.add("on");
|
||||
names.add("off");
|
||||
|
||||
return names;
|
||||
}
|
||||
}
|
@ -0,0 +1,144 @@
|
||||
/* Craft Inc. BorderProtection
|
||||
Copyright (C) 2013 Paul Schulze
|
||||
|
||||
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 de.craftinc.borderprotection.commands;
|
||||
|
||||
|
||||
import de.craftinc.borderprotection.Messages;
|
||||
import de.craftinc.borderprotection.borders.Border;
|
||||
import de.craftinc.borderprotection.borders.CircBorder;
|
||||
import de.craftinc.borderprotection.borders.RectBorder;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SetCommand implements SubCommand
|
||||
{
|
||||
@Override
|
||||
public boolean execute(CommandSender sender, String[] parameters)
|
||||
{
|
||||
if ( !( parameters.length == 3 || parameters.length == 4 ) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( !sender.hasPermission("craftinc.borderprotection.set") )
|
||||
{
|
||||
sender.sendMessage(Messages.noPermissionSet);
|
||||
return false;
|
||||
}
|
||||
|
||||
World world = ( (Player) sender ).getWorld();
|
||||
|
||||
// set [r|c] <distance>
|
||||
if ( parameters.length == 3 )
|
||||
{
|
||||
try
|
||||
{
|
||||
Double distance = Double.parseDouble(parameters[2]);
|
||||
Border newBorder = null;
|
||||
|
||||
// rect border
|
||||
if ( parameters[1].equalsIgnoreCase("r") )
|
||||
{
|
||||
newBorder = new RectBorder(new Location(world, distance, 0, distance),
|
||||
new Location(world, -distance, 0, -distance));
|
||||
}
|
||||
// circ border
|
||||
else if ( parameters[1].equalsIgnoreCase("c") )
|
||||
{
|
||||
newBorder = new CircBorder(new Location(world, 0, 0, 0), distance);
|
||||
}
|
||||
|
||||
if ( newBorder != null )
|
||||
{
|
||||
sender.sendMessage(Messages.borderCreationSuccessful);
|
||||
sender.sendMessage(
|
||||
Messages.borderInfo(world.getName(), newBorder));
|
||||
}
|
||||
}
|
||||
catch ( Exception e )
|
||||
{
|
||||
sender.sendMessage(e.getMessage());
|
||||
}
|
||||
}
|
||||
// set r <point1> <point2> | set c <center> <radius>
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
Border newBorder = null;
|
||||
|
||||
// rect border
|
||||
if ( parameters[1].equalsIgnoreCase("r") )
|
||||
{
|
||||
Double p1X = Double.parseDouble(parameters[2].split(",")[0]);
|
||||
Double p1Z = Double.parseDouble(parameters[2].split(",")[1]);
|
||||
Double p2X = Double.parseDouble(parameters[3].split(",")[0]);
|
||||
Double p2Z = Double.parseDouble(parameters[3].split(",")[1]);
|
||||
|
||||
newBorder = new RectBorder(new Location(world, p1X, 0, p1Z),
|
||||
new Location(world, p2X, 0, p2Z));
|
||||
}
|
||||
// circ border
|
||||
else if ( parameters[1].equalsIgnoreCase("c") )
|
||||
{
|
||||
Double centerX = Double.parseDouble(parameters[2].split(",")[0]);
|
||||
Double centerZ = Double.parseDouble(parameters[2].split(",")[1]);
|
||||
Double radius = Double.parseDouble(parameters[3]);
|
||||
|
||||
newBorder = new CircBorder(new Location(world, centerX, 0, centerZ), radius);
|
||||
}
|
||||
|
||||
if ( newBorder != null )
|
||||
{
|
||||
sender.sendMessage(Messages.borderCreationSuccessful);
|
||||
sender.sendMessage(
|
||||
Messages.borderInfo(world.getName(), newBorder));
|
||||
}
|
||||
}
|
||||
catch ( Exception e )
|
||||
{
|
||||
sender.sendMessage(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// save the new border
|
||||
try
|
||||
{
|
||||
Border.saveBorders();
|
||||
}
|
||||
catch ( IOException e )
|
||||
{
|
||||
sender.sendMessage(Messages.borderSaveException);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> commandNames()
|
||||
{
|
||||
ArrayList<String> names = new ArrayList<String>();
|
||||
names.add("set");
|
||||
|
||||
return names;
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
/* Craft Inc. BorderProtection
|
||||
Copyright (C) 2013 Paul Schulze
|
||||
|
||||
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 de.craftinc.borderprotection.commands;
|
||||
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public interface SubCommand
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @param sender The sender (player) who executed the command.
|
||||
* @param parameters All parameters for executing this subcommand. Includes the subcommand name at index 0.
|
||||
* @return Returns an boolean indicating if the subcommand got could be executed.
|
||||
*/
|
||||
public boolean execute(CommandSender sender, String[] parameters);
|
||||
|
||||
/**
|
||||
* Returns a list of names of the command. All strings should be lowercase!
|
||||
*/
|
||||
public List<String> commandNames();
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
/* Craft Inc. BorderProtection
|
||||
Copyright (C) 2013 Paul Schulze
|
||||
|
||||
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 de.craftinc.borderprotection.events;
|
||||
|
||||
import de.craftinc.borderprotection.Messages;
|
||||
import de.craftinc.borderprotection.Plugin;
|
||||
import de.craftinc.borderprotection.util.ChunkGenerator;
|
||||
import de.craftinc.borderprotection.util.UpdateHelper;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerLoginEvent;
|
||||
|
||||
public class PlayerLoginListener implements Listener
|
||||
{
|
||||
@SuppressWarnings("unused")
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onPlayerLogin( PlayerLoginEvent e )
|
||||
{
|
||||
final Player player = e.getPlayer();
|
||||
|
||||
if ( e.getPlayer().hasPermission("craftinc.borderprotection.update") )
|
||||
{
|
||||
if ( UpdateHelper.newVersionAvailable() )
|
||||
{
|
||||
// Schedule a task which delays 20 ticks (1 second) and then sends a message to the player
|
||||
Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.instance, new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
player.sendMessage(Messages.updateMessage(UpdateHelper.cachedLatestVersion,
|
||||
UpdateHelper.getCurrentVersion()));
|
||||
}
|
||||
}, 20L);
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("pausing generation");
|
||||
ChunkGenerator.pause();
|
||||
}
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
/* Craft Inc. BorderProtection
|
||||
Copyright (C) 2013 Paul Schulze, Tobias Ottenweller
|
||||
|
||||
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 de.craftinc.borderprotection.events;
|
||||
|
||||
import de.craftinc.borderprotection.Messages;
|
||||
import de.craftinc.borderprotection.borders.Border;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerMoveEvent;
|
||||
|
||||
public class PlayerMoveListener implements Listener
|
||||
{
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onPlayerMove( PlayerMoveEvent e )
|
||||
{
|
||||
// do nothing if the event is already cancelled
|
||||
if ( e.isCancelled() )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// do nothing if player has the ignoreborders permission
|
||||
if ( e.getPlayer().hasPermission("craftinc.borderprotection.ignoreborders") )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// do nothing if there are no border definitions at all
|
||||
if ( Border.getBorders().isEmpty() )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// player location
|
||||
Location playerLocation = e.getPlayer().getLocation();
|
||||
|
||||
// world where the player is in
|
||||
World world = e.getPlayer().getWorld();
|
||||
|
||||
// Border of this world
|
||||
Border border = Border.getBorders().get(world);
|
||||
|
||||
// do nothing if there are no borders for this specific world
|
||||
if ( border == null )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// do nothing if border is disabled
|
||||
if ( !border.isActive() )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// check if player is inside the borders and get a new location if not
|
||||
Location destination = border.checkBorder(playerLocation);
|
||||
|
||||
// Do nothing, if no new location has been calculated.
|
||||
if ( destination == null )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// teleport the player to the new location within the borders
|
||||
e.getPlayer().teleport(destination);
|
||||
|
||||
// send a message to the player
|
||||
Messages.showMessageWithTimeout(e.getPlayer(), Messages.borderMessage, 10);
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
/* Craft Inc. BorderProtection
|
||||
Copyright (C) 2013 Paul Schulze, Tobias Ottenweller
|
||||
|
||||
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 de.craftinc.borderprotection.events;
|
||||
|
||||
import de.craftinc.borderprotection.Plugin;
|
||||
import de.craftinc.borderprotection.util.ChunkGenerator;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
public class PlayerQuitListener implements Listener
|
||||
{
|
||||
@SuppressWarnings("unused")
|
||||
@EventHandler(priority = EventPriority.NORMAL)
|
||||
public void onPlayerQuit( PlayerQuitEvent e )
|
||||
{
|
||||
if (Plugin.instance.getServer().getOnlinePlayers().length == 1)
|
||||
{
|
||||
ChunkGenerator.resume();
|
||||
}
|
||||
}
|
||||
}
|
@ -14,8 +14,10 @@
|
||||
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 de.craftinc.borderprotection;
|
||||
package de.craftinc.borderprotection.events;
|
||||
|
||||
import de.craftinc.borderprotection.Messages;
|
||||
import de.craftinc.borderprotection.borders.Border;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.event.EventHandler;
|
||||
@ -25,17 +27,16 @@ import org.bukkit.event.player.PlayerTeleportEvent;
|
||||
|
||||
public class PlayerTeleportListener implements Listener
|
||||
{
|
||||
private BorderManager borderManager;
|
||||
|
||||
public PlayerTeleportListener( BorderManager borderManager )
|
||||
@SuppressWarnings("unused")
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onPlayerTeleport( PlayerTeleportEvent e )
|
||||
{
|
||||
this.borderManager = borderManager;
|
||||
// do nothing if the event is already cancelled
|
||||
if ( e.isCancelled() )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onPlayerMove( PlayerTeleportEvent e )
|
||||
{
|
||||
// do nothing if player has the ignoreborders permission
|
||||
if ( e.getPlayer().hasPermission("craftinc.borderprotection.ignoreborders") )
|
||||
{
|
||||
@ -63,24 +64,22 @@ public class PlayerTeleportListener implements Listener
|
||||
return;
|
||||
}
|
||||
|
||||
// do nothing if border is disabled
|
||||
// do nothing if rectBorder is disabled
|
||||
if ( !border.isActive() )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// change x or z. default: do not change
|
||||
Double[] newXZ;
|
||||
// check if player is inside the borders and get a new location if not
|
||||
Location destination = border.checkBorder(targetLocation);
|
||||
|
||||
// check if target is inside the borders. null if yes, otherwise a tuple which defines the new position
|
||||
newXZ = borderManager.checkBorder(targetLocation, border, BorderManager.buffer);
|
||||
|
||||
|
||||
// Cancel event, if new coordinates have been calculated.
|
||||
if ( newXZ != null )
|
||||
// Do nothing, if no new location has been calculated.
|
||||
if ( destination == null )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
e.setCancelled(true);
|
||||
borderManager.showMessageWithTimeout(e.getPlayer(), Messages.borderTeleportMessage);
|
||||
}
|
||||
Messages.showMessageWithTimeout(e.getPlayer(), Messages.borderTeleportMessage, 10);
|
||||
}
|
||||
}
|
@ -0,0 +1,238 @@
|
||||
/* Craft Inc. BorderProtection
|
||||
Copyright (C) 2013 Paul Schulze, Tobias Ottenweller
|
||||
|
||||
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 de.craftinc.borderprotection.util;
|
||||
|
||||
import de.craftinc.borderprotection.Plugin;
|
||||
import de.craftinc.borderprotection.borders.Border;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class ChunkGenerator
|
||||
{
|
||||
protected static HashMap<World, Integer[]> chunkGenerationStatus = new HashMap<World, Integer[]>();
|
||||
protected static boolean isPaused = true;
|
||||
|
||||
public static long waitTicks = 5; // TODO: make adjustable via config file
|
||||
public static int batchGenerationSize = 5; //TODO: make adjustable via config file
|
||||
public static int paddingChunksAroundBorder = 15; // TODO: make adjustable via config file
|
||||
|
||||
public static void pause()
|
||||
{
|
||||
isPaused = true;
|
||||
}
|
||||
|
||||
public static void resume()
|
||||
{
|
||||
if (isPaused)
|
||||
{
|
||||
isPaused = false;
|
||||
|
||||
for (World w : chunkGenerationStatus.keySet())
|
||||
{
|
||||
slowLoadNextChunk(w);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void cancelRender(World w)
|
||||
{
|
||||
if (w == null)
|
||||
{
|
||||
throw new IllegalArgumentException("World 'w' must not be null!");
|
||||
}
|
||||
|
||||
chunkGenerationStatus.remove(w);
|
||||
}
|
||||
|
||||
public static boolean isGenerating(World w)
|
||||
{
|
||||
if (w == null)
|
||||
{
|
||||
throw new IllegalArgumentException("World 'w' must not be null!");
|
||||
}
|
||||
|
||||
return chunkGenerationStatus.containsKey(w);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the generation of all chunks inside a border.
|
||||
* @param w The world in which chunks will be generated. Must not be 'null'. An exception will be thrown otherwise!
|
||||
* @return A boolean indicating if the generation was successfully started. Will return false if no border exists
|
||||
* for a given world. Will return true if the generation was already running but will not restart the generation.
|
||||
*/
|
||||
public static boolean generate(World w)
|
||||
{
|
||||
if (w == null)
|
||||
{
|
||||
throw new IllegalArgumentException("World 'w' must not be null!");
|
||||
}
|
||||
|
||||
Border border = Border.getBorders().get(w);
|
||||
|
||||
if (border == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Location[] borderRect = border.getSurroundingRect();
|
||||
|
||||
int firstChunkX = (Math.min(borderRect[0].getBlockX(), borderRect[1].getBlockX()) >> 4) - paddingChunksAroundBorder;
|
||||
int firstChunkZ = (Math.min(borderRect[0].getBlockZ(), borderRect[1].getBlockZ()) >> 4) - paddingChunksAroundBorder;
|
||||
|
||||
firstChunkX--;
|
||||
|
||||
chunkGenerationStatus.put(w, new Integer[]{firstChunkX, firstChunkZ});
|
||||
// the actual generation will start when resume is called!
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected static void slowLoadNextChunk(World w)
|
||||
{
|
||||
if (w == null)
|
||||
{
|
||||
throw new IllegalArgumentException("World 'w' must not be null!");
|
||||
}
|
||||
|
||||
if (isPaused)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
DelayedCall delayedCall = new DelayedCall();
|
||||
delayedCall.w = w;
|
||||
delayedCall.batchGenerationSize = batchGenerationSize;
|
||||
|
||||
Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.instance, delayedCall, waitTicks);
|
||||
}
|
||||
|
||||
/**
|
||||
* Will only load/generate the next chunks inside the border of the given world. Will stop if no border exists.
|
||||
*/
|
||||
protected static void loadNextChunk(World w)
|
||||
{
|
||||
if (w == null)
|
||||
{
|
||||
throw new IllegalArgumentException("World 'w' must not be null!");
|
||||
}
|
||||
|
||||
Border border = Border.getBorders().get(w);
|
||||
|
||||
if (border == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Integer[] lastGeneratedChunk = chunkGenerationStatus.get(w);
|
||||
|
||||
if (lastGeneratedChunk == null)
|
||||
{
|
||||
return; // the generation got most likely canceled
|
||||
}
|
||||
|
||||
int chunkX = lastGeneratedChunk[0];
|
||||
int chunkZ = lastGeneratedChunk[1];
|
||||
|
||||
Location[] borderRect = border.getSurroundingRect();
|
||||
int minChunkX = (Math.min(borderRect[0].getBlockX(), borderRect[1].getBlockX()) >> 4) - paddingChunksAroundBorder;
|
||||
int maxChunkX = (Math.max(borderRect[0].getBlockX(), borderRect[1].getBlockX()) >> 4) + paddingChunksAroundBorder;
|
||||
int maxChunkZ = (Math.max(borderRect[0].getBlockZ(), borderRect[1].getBlockZ()) >> 4) + paddingChunksAroundBorder;
|
||||
|
||||
chunkX++;
|
||||
|
||||
while (!chunkIsInsideBorder(chunkX, chunkZ, w, border)
|
||||
&& chunkZ <= maxChunkZ)
|
||||
{
|
||||
chunkX++;
|
||||
|
||||
if (chunkX > maxChunkX)
|
||||
{
|
||||
chunkZ++;
|
||||
chunkX = minChunkX;
|
||||
}
|
||||
}
|
||||
|
||||
if (chunkZ <= maxChunkZ)
|
||||
{
|
||||
chunkGenerationStatus.put(w, new Integer[]{chunkX, chunkZ});
|
||||
|
||||
// TODO: only display a message every 10s or so with a fake percentage number.
|
||||
Plugin.instance.getLogger().info("Loading/Generating Chunk ( x=" + chunkX + " z=" + chunkZ + " world=" + w.getName() + " )");
|
||||
|
||||
Chunk chunk = w.getChunkAt(chunkX, chunkZ);
|
||||
chunk.load(true);
|
||||
loadSurroundingChunks(chunkX, chunkZ, w); // this will get the server to generate trees, … inside the new chunk
|
||||
}
|
||||
else
|
||||
{
|
||||
Plugin.instance.getLogger().info("Finished generating Chunks for world " + w.getName());
|
||||
chunkGenerationStatus.remove(w);
|
||||
}
|
||||
}
|
||||
|
||||
protected static boolean chunkIsInsideBorder(int x, int z, World w, Border b)
|
||||
{
|
||||
double xLoc = (double)(x << 4) + 8.0;
|
||||
double yLoc = 0.0;
|
||||
double zLoc = (double)(z << 4) + 8.0;
|
||||
|
||||
double padding = (double)(paddingChunksAroundBorder << 4);
|
||||
Location l = new Location(w, xLoc, yLoc, zLoc);
|
||||
|
||||
return b.checkBorder(l, padding) == null;
|
||||
}
|
||||
|
||||
protected static void loadSurroundingChunks(int x, int z, World w)
|
||||
{
|
||||
int radius = 1;
|
||||
|
||||
for (int i=-radius; i<radius; i++)
|
||||
{
|
||||
for (int j=-radius; j<radius; j++)
|
||||
{
|
||||
if (j == 0 && i == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
w.loadChunk(i+x, j+z, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class DelayedCall implements Runnable
|
||||
{
|
||||
public World w;
|
||||
public int batchGenerationSize;
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
for (int i=0; i<batchGenerationSize;i++)
|
||||
{
|
||||
ChunkGenerator.loadNextChunk(w);
|
||||
}
|
||||
|
||||
ChunkGenerator.slowLoadNextChunk(w);
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/* Craft Inc. BorderProtection
|
||||
Copyright (C) 2013 Tobias Ottenweller
|
||||
Copyright (C) 2013 Tobias Ottenweller, Paul Schulze
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published
|
||||
@ -14,8 +14,9 @@
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package de.craftinc.borderprotection;
|
||||
package de.craftinc.borderprotection.util;
|
||||
|
||||
import de.craftinc.borderprotection.Plugin;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
|
||||
@ -26,17 +27,16 @@ import java.util.Map;
|
||||
/**
|
||||
* NOTE: We do not care about yaw and pitch for gate locations. So we won't serialize them.
|
||||
*/
|
||||
public class LocationSerializer
|
||||
public class LocationSerializer2D
|
||||
{
|
||||
protected static String worldKey = "world";
|
||||
protected static String xKey = "x";
|
||||
protected static String yKey = "y";
|
||||
protected static String zKey = "z";
|
||||
|
||||
|
||||
protected static World getWorld( String name ) throws Exception
|
||||
{
|
||||
World world = Plugin.getPlugin().getServer().getWorld(name);
|
||||
World world = Plugin.instance.getServer().getWorld(name);
|
||||
|
||||
if ( world == null )
|
||||
{
|
||||
@ -58,7 +58,6 @@ public class LocationSerializer
|
||||
|
||||
serializedLocation.put(worldKey, l.getWorld().getName());
|
||||
serializedLocation.put(xKey, l.getX());
|
||||
serializedLocation.put(yKey, l.getY());
|
||||
serializedLocation.put(zKey, l.getZ());
|
||||
|
||||
return serializedLocation;
|
||||
@ -77,27 +76,21 @@ public class LocationSerializer
|
||||
|
||||
// verbose loading of coordinates (they might be Double or Integer)
|
||||
Object objX = map.get(xKey);
|
||||
Object objY = map.get(yKey);
|
||||
Object objZ = map.get(zKey);
|
||||
|
||||
double x, y, z;
|
||||
double x, z;
|
||||
|
||||
if ( objX instanceof Integer )
|
||||
x = (double) (Integer) objX;
|
||||
else
|
||||
x = (Double) objX;
|
||||
|
||||
if ( objY instanceof Integer )
|
||||
y = (double) (Integer) objY;
|
||||
else
|
||||
y = (Double) objY;
|
||||
|
||||
if ( objZ instanceof Integer )
|
||||
z = (double) (Integer) objZ;
|
||||
else
|
||||
z = (Double) objZ;
|
||||
|
||||
|
||||
return new Location(w, x, y, z);
|
||||
return new Location(w, x, 0d, z);
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
/* Craft Inc. BorderProtection
|
||||
Copyright (C) 2013 Paul Schulze, Tobias Ottenweller
|
||||
|
||||
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 de.craftinc.borderprotection.util;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockFace;
|
||||
|
||||
public class PlayerMovementUtil
|
||||
{
|
||||
public static Double goUpUntilFreeSpot( Location l )
|
||||
{
|
||||
// FIXME: Player should not be placed above lava or other harmful Materials
|
||||
// go up in height until the player can stand in AIR
|
||||
Block footBlock = l.getBlock();
|
||||
Block headBlock = l.getBlock().getRelative(BlockFace.UP);
|
||||
while ( footBlock.getType() != Material.AIR || headBlock.getType() != Material.AIR )
|
||||
{
|
||||
byte offset = 1;
|
||||
if ( headBlock.getType() != Material.AIR )
|
||||
{
|
||||
offset = 2;
|
||||
}
|
||||
footBlock = footBlock.getRelative(0, offset, 0);
|
||||
headBlock = headBlock.getRelative(0, offset, 0);
|
||||
}
|
||||
// return the y value to a spot where the player can stand free
|
||||
return (double) footBlock.getY();
|
||||
}
|
||||
}
|
@ -0,0 +1,102 @@
|
||||
/* Craft Inc. BorderProtection
|
||||
Copyright (C) 2013 Paul Schulze
|
||||
|
||||
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 de.craftinc.borderprotection.util;
|
||||
|
||||
import de.craftinc.borderprotection.Plugin;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
|
||||
public class UpdateHelper
|
||||
{
|
||||
/**
|
||||
* The URL from which the Plugin tries to get the latest version.
|
||||
*/
|
||||
@SuppressWarnings("FieldCanBeLocal")
|
||||
private static final String updateUrl = "http://www.craftinc.de/plugins/update/craftinc-borderprotection";
|
||||
|
||||
/**
|
||||
* The latest version which was seen on last check.
|
||||
*/
|
||||
public static String cachedLatestVersion = null;
|
||||
|
||||
/**
|
||||
* Gets the latest version from the updateURL and returns it as <code>String</code>.
|
||||
*
|
||||
* @return latest version as <code>String</code>.
|
||||
*/
|
||||
@SuppressWarnings("StringBufferMayBeStringBuilder")
|
||||
public static String getLatestVersion()
|
||||
{
|
||||
// StringBuffer is thread-safe. Don't know if this is really important here, but safe is safe :).
|
||||
StringBuffer s = new StringBuffer();
|
||||
try
|
||||
{
|
||||
URLConnection c = new URL(updateUrl).openConnection();
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
|
||||
String inputLine;
|
||||
while ( ( inputLine = br.readLine() ) != null )
|
||||
{
|
||||
s.append(inputLine);
|
||||
}
|
||||
br.close();
|
||||
}
|
||||
catch ( MalformedURLException e )
|
||||
{
|
||||
Plugin.instance.getLogger().warning("Could not check for latest version. Update URL is malformed.");
|
||||
}
|
||||
catch ( IOException e )
|
||||
{
|
||||
Plugin.instance.getLogger().warning("Could not check for latest version. Update URL was not readable.");
|
||||
}
|
||||
|
||||
// update cached latest version
|
||||
cachedLatestVersion = s.toString();
|
||||
|
||||
return s.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current version of this plugin directly from the plugin.yml version entry.
|
||||
*
|
||||
* @return current version as <code>String</code>.
|
||||
*/
|
||||
public static String getCurrentVersion()
|
||||
{
|
||||
return Plugin.instance.getDescription().getVersion();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a newer version is available.
|
||||
*
|
||||
* @return Boolean
|
||||
*/
|
||||
public static Boolean newVersionAvailable()
|
||||
{
|
||||
final String version = getLatestVersion();
|
||||
|
||||
// do not show beta or dev versions
|
||||
if (version.contains("beta") || version.contains("dev"))
|
||||
return false;
|
||||
|
||||
return !getCurrentVersion().equals(getLatestVersion());
|
||||
}
|
||||
}
|
@ -14,9 +14,10 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
name: Craft Inc. BorderProtection
|
||||
name: ${project.name}
|
||||
main: de.craftinc.borderprotection.Plugin
|
||||
version: ${project.version}
|
||||
softdepend: [Multiverse-Core]
|
||||
authors: [ddidderr, mice_on_drugs]
|
||||
website: http://www.craftinc.de/plugins/borderprotection
|
||||
|
||||
@ -30,5 +31,8 @@ permissions:
|
||||
default: op
|
||||
description: Allows to set the border for a world.
|
||||
craftinc.borderprotection.ignoreborders:
|
||||
default: op
|
||||
default: false
|
||||
description: Allows to be everywhere on the map (ignoring the borders).
|
||||
craftinc.borderprotection.update:
|
||||
default: op
|
||||
description: Allows to get notified on login, when a new update is available.
|
Reference in New Issue
Block a user