Add new destination arch. This will break all plugins.

This commit is contained in:
Eric Stokes 2011-07-22 20:57:56 -06:00
parent 0532564431
commit 83aa79deed
9 changed files with 429 additions and 137 deletions

View File

@ -3,6 +3,7 @@ package com.onarandombox.MultiverseCore;
import java.util.List;
import java.util.logging.Level;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -30,7 +31,7 @@ public class MVPermissions implements PermissionsInterface {
}
/**
* Check if a Player can teleport to the Destination world from there current world. This checks against the Worlds Blacklist
* Check if a Player can teleport to the Destination world from there current world.
*
* @param p
* @param w
@ -51,6 +52,13 @@ public class MVPermissions implements PermissionsInterface {
return returnValue;
}
public boolean canTravelFromLocation(Player teleporter, Location location) {
if(!this.plugin.isMVWorld(location.getWorld().getName())){
return false;
}
return canTravelFromWorld(teleporter, this.plugin.getMVWorld(location.getWorld().getName()));
}
/**
* Check if the Player has the permissions to enter this world.
*
@ -61,6 +69,14 @@ public class MVPermissions implements PermissionsInterface {
public Boolean canEnterWorld(Player p, MVWorld w) {
return this.hasPermission(p, "multiverse.access." + w.getName(), false);
}
public Boolean canEnterLocation(Player p, Location l) {
String worldName = l.getWorld().getName();
if(!this.plugin.isMVWorld(worldName)) {
return false;
}
return this.hasPermission(p, "multiverse.access." + worldName, false);
}
public void setPermissions(PermissionHandler handler) {
this.permissions = handler;
@ -101,4 +117,5 @@ public class MVPermissions implements PermissionsInterface {
}
return "Bukkit Permissions/OPs.txt";
}
}

View File

@ -52,8 +52,10 @@ import com.onarandombox.MultiverseCore.commands.VersionCommand;
import com.onarandombox.MultiverseCore.commands.WhoCommand;
import com.onarandombox.MultiverseCore.configuration.DefaultConfiguration;
import com.onarandombox.utils.DebugLog;
import com.onarandombox.utils.DestinationFactory;
import com.onarandombox.utils.PurgeWorlds;
import com.onarandombox.utils.UpdateChecker;
import com.onarandombox.utils.WorldDestination;
import com.pneumaticraft.commandhandler.CommandHandler;
public class MultiverseCore extends JavaPlugin {
@ -96,6 +98,7 @@ public class MultiverseCore extends JavaPlugin {
public static boolean defaultConfigsCreated = false;
protected MVConfigMigrator migrator = new MVConfigMigrator(this);
protected int pluginCount;
private DestinationFactory destFactory;
@Override
public void onLoad() {
@ -119,6 +122,7 @@ public class MultiverseCore extends JavaPlugin {
this.log(Level.INFO, "- Version " + this.getDescription().getVersion() + " Enabled - By " + getAuthors());
// Setup all the Events the plugin needs to Monitor.
this.initializeDestinationFactory();
this.registerEvents();
// Setup Permissions, we'll do an initial check for the Permissions plugin then fall back on isOP().
this.ph = new MVPermissions(this);
@ -150,6 +154,12 @@ public class MultiverseCore extends JavaPlugin {
}
}
private void initializeDestinationFactory() {
this.destFactory = new DestinationFactory(this);
this.destFactory.registerDestinationType(WorldDestination.class, "");
this.destFactory.registerDestinationType(WorldDestination.class, "w");
}
/**
* Function to Register all the Events needed.
*/
@ -694,4 +704,8 @@ public class MultiverseCore extends JavaPlugin {
public void setBank(GenericBank bank) {
this.bank = bank;
}
public DestinationFactory getDestinationFactory() {
return this.destFactory;
}
}

View File

@ -15,7 +15,10 @@ import com.onarandombox.MultiverseCore.MVTeleport;
import com.onarandombox.MultiverseCore.MVWorld;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.utils.Destination;
import com.onarandombox.utils.DestinationFactory;
import com.onarandombox.utils.DestinationType;
import com.onarandombox.utils.InvalidDestination;
import com.onarandombox.utils.WorldDestination;
public class TeleportCommand extends MultiverseCommand {
private MVTeleport playerTeleporter;
@ -24,7 +27,7 @@ public class TeleportCommand extends MultiverseCommand {
super(plugin);
Permission self = new Permission("multiverse.core.tp.self", "Allows you to teleport yourself to other worlds.", PermissionDefault.OP);
Permission other = new Permission("multiverse.core.tp.other", "Allows you to teleport yourself to other worlds.", PermissionDefault.OP);
this.plugin.getServer().getPluginManager().addPermission(self);
this.plugin.getServer().getPluginManager().addPermission(other);
Map<String, Boolean> children = new HashMap<String, Boolean>();
@ -80,32 +83,29 @@ public class TeleportCommand extends MultiverseCommand {
teleportee = (Player) sender;
}
Destination d = Destination.parseDestination(worldName, (MultiverseCore) this.plugin);
if (!(d.getType() == DestinationType.World) || !this.plugin.isMVWorld(d.getName())) {
sender.sendMessage("Multiverse does not know about this world: " + worldName);
DestinationFactory df = this.plugin.getDestinationFactory();// .parseDestination(worldName, (MultiverseCore) this.plugin);
Destination d = df.getDestination(worldName);
if (d != null && d instanceof InvalidDestination) {
sender.sendMessage("Multiverse does not know how to take you to: " + ChatColor.RED + worldName);
return;
}
MVWorld world = this.plugin.getMVWorld(d.getName());
if (teleporter != null && !this.plugin.getPermissions().canEnterWorld(teleporter, world)) {
if (teleporter != null && !this.plugin.getPermissions().canEnterLocation(teleporter, d.getLocation())) {
if (teleportee.equals(teleporter)) {
teleporter.sendMessage("Doesn't look like you're allowed to go " + ChatColor.RED + "there...");
} else {
teleporter.sendMessage("Doesn't look like you're allowed to send " + ChatColor.GOLD + teleportee.getName() + ChatColor.WHITE + " to " + ChatColor.RED + "there...");
}
return;
} else if (teleporter != null && !this.plugin.getPermissions().canTravelFromWorld(teleporter, world)) {
} else if (teleporter != null && !this.plugin.getPermissions().canTravelFromLocation(teleporter, d.getLocation())) {
if (teleportee.equals(teleporter)) {
teleporter.sendMessage("DOH! Doesn't look like you can get to " + ChatColor.RED + d.getName() + " from " + ChatColor.GREEN + teleporter.getWorld().getName());
teleporter.sendMessage("DOH! Doesn't look like you can get to " + ChatColor.RED + "THERE from " + ChatColor.GREEN + teleporter.getWorld().getName());
} else {
teleporter.sendMessage("DOH! Doesn't look like " + ChatColor.GREEN + teleporter.getWorld().getName() + " can get to " + ChatColor.RED + d.getName() + " from where they are...");
teleporter.sendMessage("DOH! Doesn't look like " + ChatColor.GREEN + teleporter.getWorld().getName() + " can get to " + ChatColor.RED + "THERE from where they are...");
}
return;
}
Location l = this.playerTeleporter.getSafeDestination(this.plugin.getServer().getWorld(world.getName()).getSpawnLocation());
if(l == null) {
l = this.plugin.getServer().getWorld(world.getName()).getSpawnLocation();
}
Location l = d.getLocation();
if (l == null) {
teleporter.sendMessage("Sorry Boss, I tried everything, but just couldn't teleport ya there!");
return;

View File

@ -1,129 +1,15 @@
package com.onarandombox.utils;
import java.util.Arrays;
import java.util.List;
import org.bukkit.Location;
import com.onarandombox.MultiverseCore.MultiverseCore;
public class Destination {
private String name;
private double x;
private double y;
private double z;
private double pitch;
private double yaw;
private DestinationType type;
public Destination(String name, DestinationType type) {
this.name = name;
this.type = type;
if (name.split(":").length > 1) {
this.type = DestinationType.Invalid;
}
}
public Destination(String coordString) {
this.type = DestinationType.Invalid;
if (this.setExactDest(coordString.split(":"))) {
this.type = DestinationType.Exact;
}
}
@Override
public String toString() {
if (this.type == DestinationType.Portal) {
return "p:" + this.name;
} else if (this.type == DestinationType.World) {
return "w:" + this.name;
} else if (this.type == DestinationType.Exact) {
return "e:" + this.name + ":" + this.x + "," + this.y + "," + this.z + ":" + this.pitch + ":" + this.yaw;
}
return "i:" + this.name;
}
public String getName() {
return this.name;
}
public DestinationType getType() {
return this.type;
}
private static Destination getBadDestination() {
return new Destination("", DestinationType.Invalid);
}
/**
* Takes the given string and returns a Destination. This will NEVER be NULL. It will return a destination of type Invalid if the destination is bad.
*
* @param dest The parceable string, ex: w:My World
* @param plugin The MultiverseCore plugin used to find valid worlds/portals
* @return A new Destination from the parsed string.
*/
public static Destination parseDestination(String dest, MultiverseCore plugin) {
if (dest == null) {
return getBadDestination();
}
String[] items = dest.split(":");
if (items.length == 0) {
return getBadDestination();
}
if (items.length == 1 && items[0].equalsIgnoreCase("here")) {
return new Destination(items[0], DestinationType.Exact);
}
// If we only found one param, assume world, but still check for validity
if (items.length == 1 && plugin.isMVWorld(items[0])) {
return new Destination(items[0], DestinationType.World);
}
if (items[0].equalsIgnoreCase("w") && plugin.isMVWorld(items[1])) {
return new Destination(items[1], DestinationType.World);
} else if (items[0].equalsIgnoreCase("p")) {
// TODO: Check for a valid portal, we can't right now, as portals aren't really in yet.
return new Destination(items[1], DestinationType.Portal);
} else if (items[0].equalsIgnoreCase("e")) {
return new Destination(dest);
}
System.out.print("Nothing valid found!!");
return getBadDestination();
}
private boolean setExactDest(String[] items) {
List<String> parsed = Arrays.asList(items);
// we already know this is an 'e'
parsed.remove(0);
// e:name:x,y,z:pitch:yaw
this.name = parsed.remove(0);
String[] coordString = parsed.get(0).split(",");
Double[] coords = new Double[3];
for (int i = 0; i < 3; i++) {
try {
coords[i] = Double.parseDouble(coordString[0]);
} catch (NumberFormatException e) {
return false;
}
}
x = coords[0];
y = coords[1];
z = coords[2];
if (parsed.size() == 1) {
// We parsed with X, Y, And Z, we're good to go!
return true;
}
if (parsed.size() == 3) {
try {
this.pitch = Double.parseDouble(parsed.get(1));
this.yaw = Double.parseDouble(parsed.get(1));
} catch (NumberFormatException e) {
return false;
}
return true;
}
return false;
}
public abstract class Destination {
public abstract String getIdentifer();
public abstract boolean isThisType(String destination);
public abstract Location getLocation();
public abstract boolean isValid();
public abstract void setDestination(MultiverseCore plugin, String dest);
public abstract String getType();
public abstract String getName();
}

View File

@ -0,0 +1,39 @@
package com.onarandombox.utils;
import java.util.HashMap;
import java.util.Map;
import com.onarandombox.MultiverseCore.MultiverseCore;
public class DestinationFactory {
private MultiverseCore plugin;
private Map<String, Class<? extends Destination>> destList;
public DestinationFactory(MultiverseCore plugin) {
this.plugin = plugin;
this.destList = new HashMap<String, Class<? extends Destination>>();
}
public Destination getDestination(String dest) {
String idenChar = dest.substring(0, 1);
if (this.destList.containsKey(idenChar)) {
Class<? extends Destination> myClass = this.destList.get(idenChar);
try {
Destination mydest = myClass.newInstance();
mydest.setDestination(this.plugin, dest);
return mydest;
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
}
}
return new InvalidDestination();
}
public boolean registerDestinationType(Class<? extends Destination> c, String identifier) {
if (this.destList.containsKey(identifier)) {
return false;
}
this.destList.put(identifier, c);
return true;
}
}

View File

@ -0,0 +1,93 @@
package com.onarandombox.utils;
import java.util.Arrays;
import java.util.List;
import org.bukkit.Location;
import com.onarandombox.MultiverseCore.MVWorld;
import com.onarandombox.MultiverseCore.MultiverseCore;
public class ExactDestination extends Destination {
private MultiverseCore plugin;
private MVWorld world;
private final String coordRegex = "([\\d]+\\.?[\\d]*),([\\d]+\\.?[\\d]*),([\\d]+\\.?[\\d]*)";
private String name;
public ExactDestination(MultiverseCore plugin) {
this.plugin = plugin;
}
@Override
public String getIdentifer() {
return "e";
}
@Override
public boolean isThisType(String destination) {
List<String> parsed = Arrays.asList(destination.split(":"));
// Need at least: e:world:x,y,z
// OR e:world:x,y,z:pitch:yaw
// so basically 3 or 5
if (!(parsed.size() == 3 || parsed.size() == 5)) {
return false;
}
// If it's not an Exact type
if (!parsed.get(0).equalsIgnoreCase("e")) {
return false;
}
parsed.remove(0);
// If it's not a MV world
if (!this.plugin.isMVWorld(parsed.get(0))) {
return false;
}
parsed.remove(0);
if (!parsed.get(0).matches(coordRegex)) {
return false;
}
// This is 1 now, because we've removed 2
if (parsed.size() == 1) {
return true;
}
parsed.remove(0);
try {
Double.parseDouble(parsed.get(0));
} catch (NumberFormatException e) {
return false;
}
parsed.remove(0);
try {
Double.parseDouble(parsed.get(0));
} catch (NumberFormatException e) {
return false;
}
return true;
}
@Override
public Location getLocation() {
return null;
}
@Override
public boolean isValid() {
// TODO Auto-generated method stub
return false;
}
@Override
public void setDestination(MultiverseCore plugin, String dest) {
// TODO Auto-generated method stub
}
@Override
public String getType() {
return "Portal";
}
@Override
public String getName() {
return this.name;
}
}

View File

@ -0,0 +1,45 @@
package com.onarandombox.utils;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import com.onarandombox.MultiverseCore.MultiverseCore;
public class InvalidDestination extends Destination {
@Override
public String getIdentifer() {
return "i";
}
@Override
public boolean isThisType(String destination) {
return false;
}
@Override
public Location getLocation() {
return null;
}
@Override
public boolean isValid() {
return false;
}
@Override
public void setDestination(MultiverseCore plugin, String dest) {
// Nothing needed, it's invalid.
}
@Override
public String getType() {
return ChatColor.RED + "Invalid Destination";
}
@Override
public String getName() {
return ChatColor.RED + "Invalid Destination";
}
}

View File

@ -0,0 +1,128 @@
package com.onarandombox.utils;
import java.util.Arrays;
import java.util.List;
import org.bukkit.Location;
import org.bukkit.World;
import com.onarandombox.MultiverseCore.MultiverseCore;
@Deprecated
public class OldDestination {
private String name;
private Location location;
private DestinationType type;
private World world;
public OldDestination(String name, DestinationType type) {
this.name = name;
this.type = type;
if (name.split(":").length > 1) {
this.type = DestinationType.Invalid;
}
}
public OldDestination(String coordString) {
this.type = DestinationType.Invalid;
// if (this.setExactDest(coordString.split(":"))) {
// this.type = DestinationType.Exact;
// }
}
@Override
public String toString() {
// if (this.type == DestinationType.Portal) {
// return "p:" + this.name;
// } else if (this.type == DestinationType.World) {
// return "w:" + this.name;
// } else if (this.type == DestinationType.Exact) {
// return "e:" + this.name + ":" + this.x + "," + this.y + "," + this.z + ":" + this.pitch + ":" + this.yaw;
// }
return "i:" + this.name;
}
public String getName() {
return this.name;
}
public DestinationType getType() {
return this.type;
}
private static OldDestination getBadDestination() {
return new OldDestination("", DestinationType.Invalid);
}
/**
* Takes the given string and returns a Destination. This will NEVER be NULL. It will return a destination of type Invalid if the destination is bad.
*
* @param dest The parceable string, ex: w:My World
* @param plugin The MultiverseCore plugin used to find valid worlds/portals
* @return A new Destination from the parsed string.
*/
public static OldDestination parseDestination(String dest, MultiverseCore plugin) {
if (dest == null) {
return getBadDestination();
}
String[] items = dest.split(":");
if (items.length == 0) {
return getBadDestination();
}
if (items.length == 1 && items[0].equalsIgnoreCase("here")) {
return new OldDestination(items[0], DestinationType.Exact);
}
// If we only found one param, assume world, but still check for validity
if (items.length == 1 && plugin.isMVWorld(items[0])) {
return new OldDestination(items[0], DestinationType.World);
}
if (items[0].equalsIgnoreCase("w") && plugin.isMVWorld(items[1])) {
return new OldDestination(items[1], DestinationType.World);
} else if (items[0].equalsIgnoreCase("p")) {
// TODO: Check for a valid portal, we can't right now, as portals aren't really in yet.
return new OldDestination(items[1], DestinationType.Portal);
} else if (items[0].equalsIgnoreCase("e")) {
return new OldDestination(dest);
}
System.out.print("Nothing valid found!!");
return getBadDestination();
}
// private boolean setExactDest(String[] items) {
// List<String> parsed = Arrays.asList(items);
// // we already know this is an 'e'
//
// parsed.remove(0);
// // e:name:x,y,z:pitch:yaw
// this.name = parsed.remove(0);
// this.
// String[] coordString = parsed.get(0).split(",");
// Double[] coords = new Double[3];
// for (int i = 0; i < 3; i++) {
// try {
// coords[i] = Double.parseDouble(coordString[0]);
// } catch (NumberFormatException e) {
// return false;
// }
// }
// if (parsed.size() == 1) {
// // We parsed with X, Y, And Z, we're good to go!
// location = new Location(this.world, coords[0], coords[1], coords[2]);
// return true;
// }
// if (parsed.size() == 3) {
// try {
// this.pitch = Double.parseDouble(parsed.get(1));
// this.yaw = Double.parseDouble(parsed.get(1));
// } catch (NumberFormatException e) {
// return false;
// }
// return true;
// }
// return false;
// }
}

View File

@ -0,0 +1,70 @@
package com.onarandombox.utils;
import org.bukkit.Location;
import com.onarandombox.MultiverseCore.MVWorld;
import com.onarandombox.MultiverseCore.MultiverseCore;
public class WorldDestination extends Destination {
private MultiverseCore plugin;
private boolean isValid;
private MVWorld world;
@Override
public String getIdentifer() {
return "w";
}
@Override
public boolean isThisType(String destination) {
String[] items = destination.split(":");
if (items.length != 2) {
return false;
}
if (items[0].equalsIgnoreCase("w") && plugin.isMVWorld(items[1])) {
return true;
}
return false;
}
@Override
public Location getLocation() {
return this.world.getCBWorld().getSpawnLocation();
}
@Override
public boolean isValid() {
return this.isValid;
}
@Override
public void setDestination(MultiverseCore plugin, String dest) {
this.plugin = plugin;
String[] items = dest.split(":");
if (items.length > 2) {
isValid = false;
return;
}
if(items.length == 1 && plugin.isMVWorld(items[0])) {
isValid = true;
this.world = plugin.getMVWorld(items[0]);
return;
}
if (items[0].equalsIgnoreCase("w") && plugin.isMVWorld(items[1])) {
this.world = plugin.getMVWorld(items[1]);
isValid = true;
return;
}
}
@Override
public String getType() {
return "World";
}
@Override
public String getName() {
return this.world.getColoredWorldString();
}
}