on the way to 1.0.0

https://github.com/craftinc/craftinc-borderprotection/issues/6  fixed
(change storage mechanism)
bugfix: now player is always teleported to a place where he can stand
This commit is contained in:
Paul Schulze 2013-02-09 19:50:52 +01:00
parent e03c30fa15
commit d104f79830
10 changed files with 252 additions and 331 deletions

View File

@ -5,7 +5,7 @@
<groupId>de.craftinc</groupId>
<artifactId>CraftincBorderProtection</artifactId>
<packaging>jar</packaging>
<version>0.9.9</version>
<version>1.0.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@ -0,0 +1,104 @@
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 java.io.File;
import java.util.HashMap;
import java.util.Map;
public class Border
{
private static final String dataFileName = "borders.json";
private Location rectPoint1;
private Location rectPoint2;
private static String rectPoint1Name = "p1";
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;
}
@SuppressWarnings("unchecked")
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));
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;
if ( rectPoint1.getWorld().equals(rectPoint2.getWorld()) )
{
borders.put(rectPoint1.getWorld(), this);
}
else
{
throw new Exception("Border points are at different worlds.");
}
}
public Map<String, Object> serialize()
{
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(rectPoint1Name, LocationSerializer.serializeLocation(rectPoint1));
map.put(rectPoint2Name, LocationSerializer.serializeLocation(rectPoint2));
return map;
}
public static void loadBorders()
{
bordersFileConf.getList(rectBordersKey);
}
public static void saveBorders()
{
bordersFileConf.set(rectBordersKey, borders.values());
}
public String toString()
{
return rectPoint1.getX() + "," + rectPoint1.getZ() + " " + rectPoint2.getX() + "," + rectPoint2.getZ();
}
}

View File

@ -20,9 +20,6 @@ import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
@ -34,13 +31,6 @@ public class BorderManager
* **********************************************************
*/
private final String dataFileName = "data.json";
/**
* Borders of all Worlds. String is World.getName(). Location is one point of the border. A border
* consists of two points which create a rectangle.
*/
private HashMap<String, ArrayList<Location>> borders = null;
/**
* For every player save the time when he got the last borderMessage
@ -51,18 +41,14 @@ public class BorderManager
* 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.
*/
private double buffer = 0.5;
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.
*/
private Long timeout = 10000L;
public static final Long timeout = 10000L;
/**
* Serializer, which is used for loading and saving data to harddisk
*/
private Serializer serializer;
/**
* *********************************************************
@ -71,9 +57,8 @@ public class BorderManager
*/
public BorderManager()
{
// initialize Serializer and load data file
serializer = new Serializer(new File(Plugin.getPlugin().getDataFolder(), dataFileName));
borders = serializer.loadDataFile();
// load borders
Border.loadBorders();
}
@ -83,59 +68,21 @@ public class BorderManager
* **********************************************************
*/
public Serializer getSerializer()
public void setBorder( World world, double border ) throws Exception
{
return serializer;
new Border(new Location(world, border, 0, border), new Location(world, -border, 0, -border));
}
public double getBuffer()
public void setBorder( World world, String p1, String p2 ) throws Exception
{
return buffer;
}
public Long getTimeout()
{
return timeout;
}
String[] coordinatesP1 = p1.split(",");
Location l1 = new Location(world, Double.parseDouble(coordinatesP1[0]), 0, Double.parseDouble(coordinatesP1[1]));
public HashMap<String, ArrayList<Location>> getBorders()
{
return borders;
}
String[] coordinatesP2 = p2.split(",");
Location l2 = new Location(world, Double.parseDouble(coordinatesP2[0]), 0, Double.parseDouble(coordinatesP2[1]));
public void setBorder( String worldName, double border )
{
if ( borders == null )
{
borders = new HashMap<String, ArrayList<Location>>();
}
World world = Plugin.getPlugin().getServer().getWorld(worldName);
// set two points which define a square
borders.put(worldName, new ArrayList<Location>(Arrays.asList(
new Location(world, border, 0, border),
new Location(world, -border, 0, -border)
)));
}
public void setBorder( String worldName, String[] borderPoints )
{
if ( borders == null )
{
borders = new HashMap<String, ArrayList<Location>>();
}
ArrayList<Location> locations = new ArrayList<Location>();
World world = Plugin.getPlugin().getServer().getWorld(worldName);
for ( String borderPoint : borderPoints )
{
String[] point = borderPoint.split(",");
locations.add(new Location(world, Double.parseDouble(point[0]), 0, Double.parseDouble(point[1])));
}
borders.put(worldName, locations);
new Border(l1, l2);
}
@ -143,20 +90,20 @@ public class BorderManager
* Checks if the given location is inside the border rectangle. Returns null if yes, otherwise new coordinates.
*
* @param location location to check
* @param borderPoints points which define the border rectangle
* @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, ArrayList<Location> borderPoints, double buffer )
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(), borderPoints.get(0).getX(), borderPoints.get(1).getX(), buffer);
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(), borderPoints.get(0).getZ(), borderPoints.get(1).getZ(), buffer);
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 )
@ -212,7 +159,7 @@ public class BorderManager
Long now = Calendar.getInstance().getTimeInMillis();
if ( ( lastBorderMessage.get(player.getName()) != null &&
now - getTimeout() > lastBorderMessage.get(player.getName()) ) ||
now - timeout > lastBorderMessage.get(player.getName()) ) ||
lastBorderMessage.get(player.getName()) == null )
{
// show message

View File

@ -16,14 +16,12 @@
*/
package de.craftinc.borderprotection;
import org.bukkit.Location;
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.util.ArrayList;
public class Commands implements CommandExecutor
{
private BorderManager borderManager;
@ -56,45 +54,54 @@ public class Commands implements CommandExecutor
// set
if ( ( args.length == 2 || args.length == 3 ) && args[0].equalsIgnoreCase("set") )
{
if ( !sender.hasPermission("craftinc.borderprotection.set") )
if ( ! sender.hasPermission("craftinc.borderprotection.set") )
{
sender.sendMessage(Messages.noPermissionSet);
return false;
}
if ( args.length == 2 )
{
borderManager.setBorder(( (Player) sender ).getWorld().getName(), Double.parseDouble(args[1]));
try
{
borderManager.setBorder(( (Player) sender ).getWorld(), Double.parseDouble(args[1]));
}
catch ( Exception e )
{
sender.sendMessage(e.getMessage());
}
}
else if ( args.length == 3 )
else
{
String[] borderDefinition = { args[1], args[2] };
borderManager.setBorder(( (Player) sender ).getWorld().getName(), borderDefinition);
try
{
borderManager.setBorder(( (Player) sender ).getWorld(), args[1], args[2]);
}
catch ( Exception e )
{
sender.sendMessage(e.getMessage());
}
}
// save the new border
borderManager.getSerializer().saveDataFile(borderManager.getBorders());
Border.saveBorders();
return true;
}
// get
if ( args.length == 1 && ( args[0].equalsIgnoreCase("get") || args[0].equalsIgnoreCase("info") ) )
{
String worldName = ( (Player) sender ).getWorld().getName();
World world = ( (Player) sender ).getWorld();
// exit and send the player a message if no border is set
if ( borderManager.getBorders() == null ||
borderManager.getBorders().get(worldName) == null )
if ( ! Border.getBorders().containsKey(world) )
{
sender.sendMessage(Messages.borderInfoNoBorderSet);
return true;
}
ArrayList<Location> borderPoints = borderManager.getBorders()
.get(worldName);
String borderDef = borderPoints.get(0).getX() + "," + borderPoints.get(0).getZ() + " " +
borderPoints.get(1).getX() + "," + borderPoints.get(1).getZ();
Border border = Border.getBorders().get(world);
sender.sendMessage(Messages.borderInfo(worldName, borderDef));
sender.sendMessage(Messages.borderInfo(world.getName(), border.toString()));
return true;
}
}

View File

@ -0,0 +1,84 @@
package de.craftinc.borderprotection;
import org.bukkit.Location;
import org.bukkit.World;
import java.util.HashMap;
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
{
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);
if (world == null) {
throw new Exception("World '" + name + "' does not exists anymore! Cannot get instance!");
}
return world;
}
public static Map<String, Object> serializeLocation(Location l)
{
if (l == null) {
return null;
}
Map<String, Object> serializedLocation = new HashMap<String, Object>();
serializedLocation.put(worldKey, l.getWorld().getName());
serializedLocation.put(xKey, l.getX());
serializedLocation.put(yKey, l.getY());
serializedLocation.put(zKey, l.getZ());
return serializedLocation;
}
public static Location deserializeLocation(Map<String, Object> map) throws Exception
{
if (map == null) {
return null;
}
World w = getWorld((String)map.get(worldKey));
// 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;
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);
}
}

View File

@ -18,15 +18,14 @@ package de.craftinc.borderprotection;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
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;
import java.util.ArrayList;
public class PlayerMoveListener implements Listener
{
@ -37,11 +36,11 @@ public class PlayerMoveListener implements Listener
this.borderManager = borderManager;
}
private Double goUpUntilFreeSpot( Player player )
private Double goUpUntilFreeSpot( Location newLocation )
{
// go up in height until the player can stand in AIR
Block footBlock = player.getLocation().getBlock();
Block headBlock = player.getEyeLocation().getBlock();
Block footBlock = newLocation.getBlock();
Block headBlock = newLocation.getBlock().getRelative(BlockFace.UP);
while ( footBlock.getType() != Material.AIR || headBlock.getType() != Material.AIR )
{
byte offset = 1;
@ -66,7 +65,7 @@ public class PlayerMoveListener implements Listener
}
// do nothing if there are no border definitions at all
if ( borderManager.getBorders() == null )
if ( Border.getBorders().isEmpty() )
{
return;
}
@ -75,20 +74,20 @@ public class PlayerMoveListener implements Listener
Location playerLocation = e.getPlayer().getLocation();
// world where the player is in
String worldName = e.getPlayer().getWorld().getName();
World world= e.getPlayer().getWorld();
// borders of this world
ArrayList<Location> borderPoints = borderManager.getBorders().get(worldName);
// border of this world
Border border = Border.getBorders().get(world);
// do nothing if there are no borders for this specific world
if ( borderPoints == null )
if ( border == null )
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, borderPoints, borderManager.getBuffer());
newXZ = borderManager.checkBorder(playerLocation, border, BorderManager.buffer);
// Do nothing, if no new coordinates have been calculated.
if ( newXZ == null )
@ -101,7 +100,7 @@ public class PlayerMoveListener implements Listener
newXZ[1] = newXZ[1] == null ? playerLocation.getZ() : newXZ[1];
// change Y if necessary (when there is no free spot)
Double newY = goUpUntilFreeSpot(e.getPlayer());
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(

View File

@ -17,13 +17,12 @@
package de.craftinc.borderprotection;
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.PlayerTeleportEvent;
import java.util.ArrayList;
public class PlayerTeleportListener implements Listener
{
private BorderManager borderManager;
@ -43,7 +42,7 @@ public class PlayerTeleportListener implements Listener
}
// do nothing if there are no border definitions at all
if ( borderManager.getBorders() == null )
if ( Border.getBorders().isEmpty() )
{
return;
}
@ -52,13 +51,13 @@ public class PlayerTeleportListener implements Listener
Location targetLocation = e.getTo();
// world where the player is in
String worldName = targetLocation.getWorld().getName();
World world = targetLocation.getWorld();
// borders of this world
ArrayList<Location> borderPoints = borderManager.getBorders().get(worldName);
Border border = Border.getBorders().get(world);
// do nothing if there are no borders for this specific world
if ( borderPoints == null )
if ( border == null )
{
return;
}
@ -67,7 +66,7 @@ public class PlayerTeleportListener implements Listener
Double[] newXZ;
// check if target is inside the borders. null if yes, otherwise a tuple which defines the new position
newXZ = borderManager.checkBorder(targetLocation, borderPoints, borderManager.getBuffer());
newXZ = borderManager.checkBorder(targetLocation, border, BorderManager.buffer);
// Cancel event, if new coordinates have been calculated.

View File

@ -1,113 +0,0 @@
/* CraftInc BorderProtection
Copyright (C) 2012 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;
import org.bukkit.Location;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
public class Serializer
{
private File dataFile;
public Serializer( File dataFile )
{
this.dataFile = dataFile;
}
public HashMap<String, ArrayList<Location>> loadDataFile()
{
try
{
FileReader fr = new FileReader(dataFile);
BufferedReader br = new BufferedReader(fr);
StringBuilder fileContents = new StringBuilder();
String line;
while ( ( line = br.readLine() ) != null )
{
fileContents.append(line);
}
JSONParser jsonParser = new JSONParser();
JSONArray json = (JSONArray) jsonParser.parse(fileContents.toString());
return Util.decodeJSON(json);
}
catch ( IOException e )
{
if ( e instanceof FileNotFoundException )
{
Plugin.getPlugin().getLogger().info("Data file not found.");
}
else
{
e.printStackTrace();
}
}
catch ( ParseException e )
{
Plugin.getPlugin().getLogger()
.severe("Could not parse json data file. When you set up a new border, the corrupt data file will be overwritten!");
e.printStackTrace();
}
return null;
}
public void saveDataFile( HashMap<String, ArrayList<Location>> data )
{
// if there is not data, do nothing and log it to console
if ( data == null )
{
Plugin.getPlugin().getLogger().severe("Could not save data, because it is null");
return;
}
// create plugin directory if it doesn't exists
if ( !Plugin.getPlugin().getDataFolder().exists() )
{
Plugin.getPlugin().getLogger().info("Creating plugin directory...");
if ( !Plugin.getPlugin().getDataFolder().mkdir() )
{
Plugin.getPlugin().getLogger().severe("Could not create plugin directory");
}
else
{
Plugin.getPlugin().getLogger().info("Plugin directory created successfully");
}
}
JSONArray json = Util.encodeJSON(data);
// Write to file
try
{
FileWriter fw = new FileWriter(dataFile);
fw.write(json.toJSONString());
fw.close();
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}

View File

@ -1,106 +0,0 @@
/* CraftInc BorderProtection
Copyright (C) 2012 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;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class Util
{
public static HashMap<String, ArrayList<Location>> decodeJSON( JSONArray json )
{
HashMap<String, ArrayList<Location>> data = new HashMap<String, ArrayList<Location>>();
for ( Object jsonEntry : json.toArray() )
{
JSONObject j = (JSONObject) jsonEntry;
// // check if border for this world is enabled. continue if not
// String enabled = (String) j.get("enabled");
// if (enabled != "1") {
// continue;
// }
String worldname = (String) j.get("worldname");
ArrayList<Location> locations = new ArrayList<Location>();
JSONArray borderPoints = (JSONArray) j.get("borderPoints");
for ( Object pointObj : borderPoints )
{
JSONArray point = (JSONArray) pointObj;
locations
.add(new Location(Bukkit.getWorld(worldname), (Double) point.get(0), 0, (Double) point.get(1)));
}
data.put(worldname, locations);
}
if ( data.size() > 0 )
{
return data;
}
return null;
}
public static JSONArray encodeJSON( HashMap<String, ArrayList<Location>> data )
{
JSONArray json = new JSONArray();
int i = 0;
for ( ArrayList<Location> border : data.values() )
{
// add point 1 as json array
JSONArray point1 = new JSONArray();
point1.add(0, border.get(0).getX());
point1.add(1, border.get(0).getZ());
// add point 2 as json array
JSONArray point2 = new JSONArray();
point2.add(0, border.get(1).getX());
point2.add(1, border.get(1).getZ());
// add both points to points json array
JSONArray points = new JSONArray();
points.add(point1);
points.add(point2);
// Add points and worldname to world json object
JSONObject borderOfAWorld = new JSONObject();
try
{
borderOfAWorld.put("worldname", border.get(0).getWorld().getName());
borderOfAWorld.put("borderPoints", points);
json.add(i, borderOfAWorld);
i++;
}
catch ( NullPointerException e )
{
if ( border.get(0).getWorld() == null )
{
Plugin.getPlugin().getLogger()
.warning("A world is null. Ignoring this border (not saving this border).");
}
}
}
return json;
}
}

View File

@ -14,9 +14,9 @@
# 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: CraftincBorderProtection
name: Craft Inc. BorderProtection
main: de.craftinc.borderprotection.Plugin
version: 0.9.9
version: 1.0.0
author: ddidderr
website: http://www.craftinc.de/plugins/borderprotection
@ -27,8 +27,8 @@ commands:
permissions:
craftinc.borderprotection.set:
default: false
default: op
description: Allows to set the border for a world.
craftinc.borderprotection.ignoreborders:
default: false
default: op
description: Allows to be everywhere on the map (ignoring the borders).