(Ability to disable border definitions per world)
some bug fixes
This commit is contained in:
Paul Schulze 2013-02-09 21:21:32 +01:00
parent d104f79830
commit 2481dd3fcf
6 changed files with 121 additions and 12 deletions

View File

@ -4,20 +4,29 @@ import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class Border public class Border implements ConfigurationSerializable
{ {
private static final String dataFileName = "borders.json"; private static final String dataFileName = "borders.yml";
private Boolean isActive;
private static String isActiveKey = "enabled";
private Location rectPoint1; private Location rectPoint1;
private Location rectPoint2;
private static String rectPoint1Name = "p1"; private static String rectPoint1Name = "p1";
private Location rectPoint2;
private static String rectPoint2Name = "p2"; private static String rectPoint2Name = "p2";
private static String rectBordersKey = "rectBorders"; private static String rectBordersKey = "rectBorders";
private static final HashMap<World, Border> borders = new HashMap<World, Border>(); private static final HashMap<World, Border> borders = new HashMap<World, Border>();
@ -40,7 +49,12 @@ public class Border
return rectPoint2; return rectPoint2;
} }
@SuppressWarnings("unchecked") public Boolean isActive()
{
return isActive;
}
@SuppressWarnings("unchecked unused")
public Border( Map<String, Object> map ) public Border( Map<String, Object> map )
{ {
try try
@ -48,6 +62,8 @@ public class Border
rectPoint1 = LocationSerializer.deserializeLocation((Map<String, Object>) map.get(rectPoint1Name)); rectPoint1 = LocationSerializer.deserializeLocation((Map<String, Object>) map.get(rectPoint1Name));
rectPoint2 = LocationSerializer.deserializeLocation((Map<String, Object>) map.get(rectPoint2Name)); rectPoint2 = LocationSerializer.deserializeLocation((Map<String, Object>) map.get(rectPoint2Name));
isActive = (Boolean) map.get(isActiveKey);
if ( rectPoint1.getWorld().equals(rectPoint2.getWorld()) ) if ( rectPoint1.getWorld().equals(rectPoint2.getWorld()) )
{ {
borders.put(rectPoint1.getWorld(), this); borders.put(rectPoint1.getWorld(), this);
@ -67,6 +83,10 @@ public class Border
{ {
rectPoint1 = p1; rectPoint1 = p1;
rectPoint2 = p2; rectPoint2 = p2;
// new border is active by default
isActive = true;
if ( rectPoint1.getWorld().equals(rectPoint2.getWorld()) ) if ( rectPoint1.getWorld().equals(rectPoint2.getWorld()) )
{ {
borders.put(rectPoint1.getWorld(), this); borders.put(rectPoint1.getWorld(), this);
@ -78,11 +98,15 @@ public class Border
} }
@SuppressWarnings("unused")
public Map<String, Object> serialize() public Map<String, Object> serialize()
{ {
HashMap<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
map.put(rectPoint1Name, LocationSerializer.serializeLocation(rectPoint1)); map.put(rectPoint1Name, LocationSerializer.serializeLocation(rectPoint1));
map.put(rectPoint2Name, LocationSerializer.serializeLocation(rectPoint2)); map.put(rectPoint2Name, LocationSerializer.serializeLocation(rectPoint2));
map.put(isActiveKey, isActive);
System.out.println(map);
return map; return map;
} }
@ -92,13 +116,24 @@ public class Border
bordersFileConf.getList(rectBordersKey); bordersFileConf.getList(rectBordersKey);
} }
public static void saveBorders() public static void saveBorders() throws IOException
{ {
bordersFileConf.set(rectBordersKey, borders.values()); bordersFileConf.set(rectBordersKey, new ArrayList<Object>(borders.values()));
bordersFileConf.save(bordersFile);
} }
public String toString() public String toString()
{ {
return rectPoint1.getX() + "," + rectPoint1.getZ() + " " + rectPoint2.getX() + "," + rectPoint2.getZ(); return rectPoint1.getX() + "," + rectPoint1.getZ() + " " + rectPoint2.getX() + "," + rectPoint2.getZ();
} }
public void enable()
{
isActive = true;
}
public void disable()
{
isActive = false;
}
} }

View File

@ -22,6 +22,8 @@ import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.io.IOException;
public class Commands implements CommandExecutor public class Commands implements CommandExecutor
{ {
private BorderManager borderManager; private BorderManager borderManager;
@ -54,22 +56,25 @@ public class Commands implements CommandExecutor
// set // set
if ( ( args.length == 2 || args.length == 3 ) && args[0].equalsIgnoreCase("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); sender.sendMessage(Messages.noPermissionSet);
return false; return false;
} }
// set <distance>
if ( args.length == 2 ) if ( args.length == 2 )
{ {
try try
{ {
borderManager.setBorder(( (Player) sender ).getWorld(), Double.parseDouble(args[1])); borderManager.setBorder(( (Player) sender ).getWorld(), Double.parseDouble(args[1]));
sender.sendMessage(Messages.borderCreationSuccessful);
} }
catch ( Exception e ) catch ( Exception e )
{ {
sender.sendMessage(e.getMessage()); sender.sendMessage(e.getMessage());
} }
} }
// set <point1> <point2>
else else
{ {
try try
@ -83,7 +88,14 @@ public class Commands implements CommandExecutor
} }
// save the new border // save the new border
try
{
Border.saveBorders(); Border.saveBorders();
}
catch ( IOException e )
{
sender.sendMessage(Messages.borderSaveException);
}
return true; return true;
} }
@ -93,7 +105,7 @@ public class Commands implements CommandExecutor
World world = ( (Player) sender ).getWorld(); World world = ( (Player) sender ).getWorld();
// exit and send the player a message if no border is set // exit and send the player a message if no border is set
if ( ! Border.getBorders().containsKey(world) ) if ( !Border.getBorders().containsKey(world) )
{ {
sender.sendMessage(Messages.borderInfoNoBorderSet); sender.sendMessage(Messages.borderInfoNoBorderSet);
return true; return true;
@ -104,7 +116,33 @@ public class Commands implements CommandExecutor
sender.sendMessage(Messages.borderInfo(world.getName(), border.toString())); sender.sendMessage(Messages.borderInfo(world.getName(), border.toString()));
return true; 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; return false;
} }
} }

View File

@ -73,6 +73,11 @@ public class Messages
makeCmd("set", "Border rectangle is defined by the two points. A point is specified as: x,z", makeCmd("set", "Border rectangle is defined by the two points. A point is specified as: x,z",
"<point1>", "<point2>"); "<point1>", "<point2>");
public static String borderCreationSuccessful
= ChatColor.YELLOW + "New border was set " +
ChatColor.GREEN + "successfully" +
ChatColor.YELLOW + "!";
public static String commandIssuedByNonPlayer public static String commandIssuedByNonPlayer
= ChatColor.RED + "Only a player can use CraftInc BorderProtection commands!"; = ChatColor.RED + "Only a player can use CraftInc BorderProtection commands!";
@ -90,4 +95,16 @@ public class Messages
public static String noPermissionSet = 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 set the border.";
public static String borderEnabled =
ChatColor.YELLOW + "Border enabled.";
public static String borderDisabled =
ChatColor.YELLOW + "Border disabled.";
public static String borderSaveException =
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!";
} }

View File

@ -74,14 +74,21 @@ public class PlayerMoveListener implements Listener
Location playerLocation = e.getPlayer().getLocation(); Location playerLocation = e.getPlayer().getLocation();
// world where the player is in // world where the player is in
World world= e.getPlayer().getWorld(); World world = e.getPlayer().getWorld();
// border of this world // border of this world
Border border = Border.getBorders().get(world); Border border = Border.getBorders().get(world);
// do nothing if there are no borders for this specific world // do nothing if there are no borders for this specific world
if ( border == null ) if ( border == null )
{
return; return;
}
if ( !border.isActive() )
{
return;
}
// change x or z. default: do not change // change x or z. default: do not change
Double[] newXZ; Double[] newXZ;

View File

@ -62,6 +62,11 @@ public class PlayerTeleportListener implements Listener
return; return;
} }
if ( !border.isActive() )
{
return;
}
// change x or z. default: do not change // change x or z. default: do not change
Double[] newXZ; Double[] newXZ;

View File

@ -16,6 +16,7 @@
*/ */
package de.craftinc.borderprotection; package de.craftinc.borderprotection;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -28,6 +29,12 @@ public class Plugin extends JavaPlugin
return cibpPlugin; return cibpPlugin;
} }
@Override
public void onLoad()
{
ConfigurationSerialization.registerClass(Border.class);
}
@Override @Override
public void onDisable() public void onDisable()
{ {