From ce80fc190d10fbd965bb1b786cde54705b623c27 Mon Sep 17 00:00:00 2001 From: Ryan Leach Date: Fri, 1 Feb 2013 08:17:54 +1030 Subject: [PATCH] Inital extra bed commit with back-compatibility Needs review and permissions advice before continuing. line ending derp --- Modified by @main--: * formatting fixed * removed changes to travis.yml and gitattributes * improved code style --- .../destination/BedDestination.java | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/destination/BedDestination.java b/src/main/java/com/onarandombox/MultiverseCore/destination/BedDestination.java index 5b73d0a9..70568cf5 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/destination/BedDestination.java +++ b/src/main/java/com/onarandombox/MultiverseCore/destination/BedDestination.java @@ -9,8 +9,11 @@ package com.onarandombox.MultiverseCore.destination; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVDestination; + +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; +import org.bukkit.OfflinePlayer; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; @@ -20,7 +23,8 @@ import org.bukkit.util.Vector; * A bed-{@link MVDestination}. */ public class BedDestination implements MVDestination { - + public static final String OLD_BED_STRING = "b:playerbed"; + private String playername = ""; private boolean isValid; private Location knownBedLoc; private MultiverseCore plugin; @@ -39,7 +43,15 @@ public class BedDestination implements MVDestination { @Override public boolean isThisType(JavaPlugin plugin, String destination) { String[] split = destination.split(":"); - this.isValid = split.length >= 1 && split.length <= 2 && split[0].equals(this.getIdentifier()); + boolean validFormat = split.length >= 1 && split.length <= 2 && split[0].equals(this.getIdentifier()); + + OfflinePlayer p = Bukkit.getOfflinePlayer(split[1]); + boolean validPlayer = (p != null); + + if (validFormat && validPlayer) this.playername = p.getName(); + + this.isValid = destination.equals(OLD_BED_STRING) || (validFormat && validPlayer); + return this.isValid; } @@ -49,9 +61,13 @@ public class BedDestination implements MVDestination { @Override public Location getLocation(Entity entity) { if (entity instanceof Player) { - this.knownBedLoc = this.plugin.getBlockSafety().getSafeBedSpawn(((Player) entity).getBedSpawnLocation()); + if (this.playername.isEmpty()) + this.knownBedLoc = this.plugin.getBlockSafety().getSafeBedSpawn(((Player) entity).getBedSpawnLocation()); + else + this.knownBedLoc = this.plugin.getBlockSafety().getSafeBedSpawn(Bukkit.getOfflinePlayer(this.playername).getBedSpawnLocation()); + if (this.knownBedLoc == null) { - ((Player) entity).sendMessage("Your bed was " + ChatColor.RED + "invalid or blocked" + ChatColor.RESET + ". Sorry."); + ((Player) entity).sendMessage("The bed was " + ChatColor.RED + "invalid or blocked" + ChatColor.RESET + ". Sorry."); } return this.knownBedLoc; } @@ -120,6 +136,6 @@ public class BedDestination implements MVDestination { @Override public String toString() { - return "b:playerbed"; + return playername.isEmpty() ? OLD_BED_STRING : ("b:" + playername); } }