diff --git a/src/main/java/de/craftinc/borderprotection/Border.java b/src/main/java/de/craftinc/borderprotection/Border.java index 33049cd..be3c0de 100644 --- a/src/main/java/de/craftinc/borderprotection/Border.java +++ b/src/main/java/de/craftinc/borderprotection/Border.java @@ -4,20 +4,29 @@ 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 +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 rectPoint2; - private static String rectPoint1Name = "p1"; + + private Location rectPoint2; private static String rectPoint2Name = "p2"; + + private static String rectBordersKey = "rectBorders"; private static final HashMap borders = new HashMap(); @@ -40,7 +49,12 @@ public class Border return rectPoint2; } - @SuppressWarnings("unchecked") + public Boolean isActive() + { + return isActive; + } + + @SuppressWarnings("unchecked unused") public Border( Map map ) { try @@ -48,6 +62,8 @@ public class Border rectPoint1 = LocationSerializer.deserializeLocation((Map) map.get(rectPoint1Name)); rectPoint2 = LocationSerializer.deserializeLocation((Map) map.get(rectPoint2Name)); + isActive = (Boolean) map.get(isActiveKey); + if ( rectPoint1.getWorld().equals(rectPoint2.getWorld()) ) { borders.put(rectPoint1.getWorld(), this); @@ -67,6 +83,10 @@ public class Border { rectPoint1 = p1; rectPoint2 = p2; + + // new border is active by default + isActive = true; + if ( rectPoint1.getWorld().equals(rectPoint2.getWorld()) ) { borders.put(rectPoint1.getWorld(), this); @@ -78,11 +98,15 @@ public class Border } + @SuppressWarnings("unused") public Map serialize() { - HashMap map = new HashMap(); + Map map = new HashMap(); map.put(rectPoint1Name, LocationSerializer.serializeLocation(rectPoint1)); map.put(rectPoint2Name, LocationSerializer.serializeLocation(rectPoint2)); + map.put(isActiveKey, isActive); + + System.out.println(map); return map; } @@ -92,13 +116,24 @@ public class Border bordersFileConf.getList(rectBordersKey); } - public static void saveBorders() + public static void saveBorders() throws IOException { - bordersFileConf.set(rectBordersKey, borders.values()); + bordersFileConf.set(rectBordersKey, new ArrayList(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; + } } diff --git a/src/main/java/de/craftinc/borderprotection/Commands.java b/src/main/java/de/craftinc/borderprotection/Commands.java index 16361bc..ba1773c 100644 --- a/src/main/java/de/craftinc/borderprotection/Commands.java +++ b/src/main/java/de/craftinc/borderprotection/Commands.java @@ -22,6 +22,8 @@ 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; @@ -54,22 +56,25 @@ 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; } + // set if ( args.length == 2 ) { try { borderManager.setBorder(( (Player) sender ).getWorld(), Double.parseDouble(args[1])); + sender.sendMessage(Messages.borderCreationSuccessful); } catch ( Exception e ) { sender.sendMessage(e.getMessage()); } } + // set else { try @@ -83,7 +88,14 @@ public class Commands implements CommandExecutor } // save the new border - Border.saveBorders(); + try + { + Border.saveBorders(); + } + catch ( IOException e ) + { + sender.sendMessage(Messages.borderSaveException); + } return true; } @@ -93,7 +105,7 @@ public class Commands implements CommandExecutor World world = ( (Player) sender ).getWorld(); // 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); return true; @@ -104,7 +116,33 @@ public class Commands implements CommandExecutor 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; } } diff --git a/src/main/java/de/craftinc/borderprotection/Messages.java b/src/main/java/de/craftinc/borderprotection/Messages.java index 073d970..e880f9c 100644 --- a/src/main/java/de/craftinc/borderprotection/Messages.java +++ b/src/main/java/de/craftinc/borderprotection/Messages.java @@ -73,6 +73,11 @@ public class Messages makeCmd("set", "Border rectangle is defined by the two points. A point is specified as: x,z", "", ""); + public static String borderCreationSuccessful + = ChatColor.YELLOW + "New border was set " + + ChatColor.GREEN + "successfully" + + ChatColor.YELLOW + "!"; + public static String commandIssuedByNonPlayer = ChatColor.RED + "Only a player can use CraftInc BorderProtection commands!"; @@ -90,4 +95,16 @@ public class Messages public static String noPermissionSet = 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!"; } diff --git a/src/main/java/de/craftinc/borderprotection/PlayerMoveListener.java b/src/main/java/de/craftinc/borderprotection/PlayerMoveListener.java index 6c11624..9e6e0e3 100644 --- a/src/main/java/de/craftinc/borderprotection/PlayerMoveListener.java +++ b/src/main/java/de/craftinc/borderprotection/PlayerMoveListener.java @@ -74,14 +74,21 @@ public class PlayerMoveListener implements Listener Location playerLocation = e.getPlayer().getLocation(); // world where the player is in - World world= e.getPlayer().getWorld(); + 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; + } + + if ( !border.isActive() ) + { + return; + } // change x or z. default: do not change Double[] newXZ; diff --git a/src/main/java/de/craftinc/borderprotection/PlayerTeleportListener.java b/src/main/java/de/craftinc/borderprotection/PlayerTeleportListener.java index 71ee50a..b532851 100644 --- a/src/main/java/de/craftinc/borderprotection/PlayerTeleportListener.java +++ b/src/main/java/de/craftinc/borderprotection/PlayerTeleportListener.java @@ -62,6 +62,11 @@ public class PlayerTeleportListener implements Listener return; } + if ( !border.isActive() ) + { + return; + } + // change x or z. default: do not change Double[] newXZ; diff --git a/src/main/java/de/craftinc/borderprotection/Plugin.java b/src/main/java/de/craftinc/borderprotection/Plugin.java index 28b27d1..d965372 100644 --- a/src/main/java/de/craftinc/borderprotection/Plugin.java +++ b/src/main/java/de/craftinc/borderprotection/Plugin.java @@ -16,6 +16,7 @@ */ package de.craftinc.borderprotection; +import org.bukkit.configuration.serialization.ConfigurationSerialization; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; @@ -28,6 +29,12 @@ public class Plugin extends JavaPlugin return cibpPlugin; } + @Override + public void onLoad() + { + ConfigurationSerialization.registerClass(Border.class); + } + @Override public void onDisable() {