craftinc-borderprotection initial commit

This commit is contained in:
Paul Schulze 2012-09-22 16:41:45 +02:00
commit 329508ab41
10 changed files with 666 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
lib/
target/
*.iml
.idea/

47
pom.xml Normal file
View File

@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>CraftincBorderProtection</groupId>
<artifactId>CraftincBorderProtection</artifactId>
<packaging>jar</packaging>
<version>0.9.5</version>
<name>CraftincGates</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>bukkit-repo</id>
<url>http://repo.bukkit.org/content/groups/public</url>
</repository>
</repositories>
</project>

View File

@ -0,0 +1,106 @@
package de.craftinc.borderprotection;
import org.bukkit.Location;
import org.bukkit.World;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
public class BorderManager
{
/**
* *********************************************************
* GLOBAL VARIABLES
* **********************************************************
*/
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;
/**
* 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 Serializer getSerializer()
{
return serializer;
}
/**
* Serializer, which is used for loading and saving data to harddisk
*/
private Serializer serializer;
/**
* *********************************************************
* CONSTRUCTOR
* **********************************************************
*/
public BorderManager()
{
// initialize Serializer and load data file
serializer = new Serializer(new File(Plugin.getPlugin().getDataFolder(), dataFileName));
borders = serializer.loadDataFile();
}
/**
* *********************************************************
* GETTERS AND SETTERS
* **********************************************************
*/
public double getBuffer()
{
return buffer;
}
public HashMap<String, ArrayList<Location>> getBorders()
{
return borders;
}
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);
}
}

View File

@ -0,0 +1,87 @@
package de.craftinc.borderprotection;
import org.bukkit.Location;
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;
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;
}
if ( args.length == 2 )
{
borderManager.setBorder(( (Player) sender ).getWorld().getName(), Double.parseDouble(args[1]));
}
else if ( args.length == 3 )
{
String[] borderDefinition = { args[1], args[2] };
borderManager.setBorder(( (Player) sender ).getWorld().getName(), borderDefinition);
}
// save the new border
borderManager.getSerializer().saveDataFile(borderManager.getBorders());
return true;
}
// get
if ( args.length == 1 && ( args[0].equalsIgnoreCase("get") || args[0].equalsIgnoreCase("info") ) )
{
String worldName = ( (Player) sender ).getWorld().getName();
// exit and send the player a message if no border is set
if ( borderManager.getBorders() == null ||
borderManager.getBorders().get(worldName) == null )
{
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();
sender.sendMessage(Messages.borderInfo(worldName, borderDef));
return true;
}
}
return false;
}
}

View File

@ -0,0 +1,70 @@
package de.craftinc.borderprotection;
import org.bukkit.ChatColor;
public class Messages
{
private static final String NEWLINE = "\n";
private static String makeCmd( String command, String explanation, String... args )
{
StringBuilder sb = new StringBuilder();
// command
sb.append(ChatColor.YELLOW);
sb.append(command);
if ( args.length > 0 )
{
sb.append(" ");
sb.append(ChatColor.BLUE);
for ( int i = 0; i < args.length; i++ )
{
String s = args[i];
sb.append(s);
if ( i != args.length - 1 )
{
sb.append(" ");
}
}
}
sb.append(ChatColor.YELLOW);
sb.append(": ");
sb.append(ChatColor.WHITE);
sb.append(explanation);
sb.append(NEWLINE);
return sb.toString();
}
public static String borderMessage =
"Sorry Dude! This is the border... the final frontier! One day the holy mods " +
"and administrators will expand the border. It is then your mission to explore " +
"strange new worlds, to seek out new life and new civilizations, to boldly go " +
"where no one has gone before.";
public static String helpGeneral =
ChatColor.GREEN + "CraftInc BorderProtection - Usage:" + NEWLINE +
makeCmd("help", "shows this help") +
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>");
public static String commandIssuedByNonPlayer
= ChatColor.RED + "Only a player can use CraftInc BorderProtection commands!";
public static String borderInfo( String worldName, String borderDef )
{
return ChatColor.WHITE + "Border definition of world " +
ChatColor.YELLOW + worldName +
ChatColor.WHITE + ": " +
ChatColor.YELLOW + borderDef;
}
public static String borderInfoNoBorderSet =
ChatColor.YELLOW + "No border here.";
public static String noPermissionSet =
ChatColor.RED + "Sorry, you don't have permission to set the border.";
}

View File

@ -0,0 +1,125 @@
package de.craftinc.borderprotection;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
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.PlayerMoveEvent;
import java.util.ArrayList;
public class PlayerMoveListener implements Listener
{
private BorderManager borderManager;
public PlayerMoveListener( BorderManager borderManager )
{
this.borderManager = borderManager;
}
/**
* Checks if the player is outside of one specific border.
*
* @param player part of the player coordinates
* @param border1 one side of the rectangle
* @param border2 opposite side of the rectangle
* @return null if the player is inside, otherwise a new player location
*/
private Double checkBorder( double player, double border1, double border2 )
{
double bigBorder = Math.max(border1, border2);
double smallBorder = Math.min(border1, border2);
// if player is between borders do nothing
if ( player >= smallBorder && player <= bigBorder )
{
return null;
}
else
{
if ( player > bigBorder )
{
// if player is outside of the bigBorder, teleport him to the bigBorder
return bigBorder - borderManager.getBuffer();
}
else
{
// if player is outside of the smallBorder, teleport him to the smallBorder
return smallBorder + borderManager.getBuffer();
}
}
}
private Double goUpUntilFreeSpot( Player player )
{
// go up in height until the player can stand in AIR
Block footBlock = player.getLocation().getBlock();
Block headBlock = player.getEyeLocation().getBlock();
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();
}
@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 is no border defined
if ( borderManager.getBorders() == null )
{
return;
}
Location pl = e.getPlayer().getLocation();
String worldName = e.getPlayer().getWorld().getName();
ArrayList<Location> borderPoints = borderManager.getBorders().get(worldName);
// do nothing if there are no borders for this specific world
if ( borderPoints == null )
return;
// change x or z. default: do not change
Double newX, newY, newZ;
newX = checkBorder(pl.getX(), borderPoints.get(0).getX(), borderPoints.get(1).getX());
newZ = checkBorder(pl.getZ(), borderPoints.get(0).getZ(), borderPoints.get(1).getZ());
// Do nothing, if no new coordinates have been calculated.
if ( newX == null && newZ == null )
{
return;
}
// if one of the coordinates is null, set it to the player's value
newX = newX == null ? pl.getX() : newX;
newZ = newZ == null ? pl.getZ() : newZ;
// change Y if necessary (when there is no free spot)
newY = goUpUntilFreeSpot(e.getPlayer());
// teleport the player to the new X and Z coordinates
e.getPlayer().teleport(
new Location(e.getPlayer().getWorld(), newX, newY, newZ, pl.getYaw(), pl.getPitch()));
// send a message to the player
e.getPlayer().sendMessage(Messages.borderMessage);
}
}

View File

@ -0,0 +1,33 @@
package de.craftinc.borderprotection;
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;
}
@Override
public void onDisable()
{
}
@Override
public void onEnable()
{
Plugin.cibpPlugin = this;
BorderManager borderManager = new BorderManager();
PlayerMoveListener eventListener = new PlayerMoveListener(borderManager);
Commands commandExecutor = new Commands(borderManager);
PluginManager pm = this.getServer().getPluginManager();
getCommand("cibp").setExecutor(commandExecutor);
pm.registerEvents(eventListener, this);
}
}

View File

@ -0,0 +1,97 @@
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

@ -0,0 +1,79 @@
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;
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

@ -0,0 +1,18 @@
name: CraftincBorderProtection
main: de.craftinc.borderprotection.Plugin
version: 0.9.5
author: ddidderr
website: http://www.craftinc.de/plugins/borderprotection
commands:
cibp:
description: Shows help for CraftInc BorderProtection
permissions:
craftinc.borderprotection.set:
default: false
description: Allows to set the border for a world.
craftinc.borderprotection.ignoreborders:
default: false
description: Allows to be everywhere on the map (ignoring the borders).