Make home throw an exception if the world doesn't exist.

Add multiverse/missing world support to home upgrade.
This commit is contained in:
KHobbits 2011-08-24 05:18:35 +01:00
parent 6c738294d1
commit 3d913f9fcc
7 changed files with 56 additions and 20 deletions

View File

@ -1 +1,2 @@
DoNotUseThreads DoNotUseThreads
SignatureDeclareThrowsException

View File

@ -155,7 +155,7 @@ public class EssentialsConf extends Configuration
return getProperty(path) != null; return getProperty(path) != null;
} }
public Location getLocation(String path, Server server) public Location getLocation(String path, Server server) throws Exception
{ {
String worldName = getString((path != null ? path + "." : "") + "world"); String worldName = getString((path != null ? path + "." : "") + "world");
if (worldName == null || worldName.isEmpty()) if (worldName == null || worldName.isEmpty())
@ -165,7 +165,7 @@ public class EssentialsConf extends Configuration
World world = server.getWorld(worldName); World world = server.getWorld(worldName);
if (world == null) if (world == null)
{ {
return null; throw new Exception(Util.i18n("invalidWorld"));
} }
return new Location(world, return new Location(world,
getDouble((path != null ? path + "." : "") + "x", 0), getDouble((path != null ? path + "." : "") + "x", 0),

View File

@ -280,7 +280,7 @@ public class EssentialsUpgrade
{ {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final String defworld = (String)config.getProperty("home.default"); final String defworld = (String)config.getProperty("home.default");
final Location defloc = config.getLocation("home.worlds." + defworld, ess.getServer()); final Location defloc = getFakeLocation(config,"home.worlds." + defworld);
if (defloc != null) if (defloc != null)
{ {
config.setProperty("homes.home", defloc); config.setProperty("homes.home", defloc);
@ -300,7 +300,7 @@ public class EssentialsUpgrade
{ {
continue; continue;
} }
loc = config.getLocation("home.worlds." + world, ess.getServer()); loc = getFakeLocation(config, "home.worlds." + world);
if (loc == null) if (loc == null)
{ {
continue; continue;
@ -570,6 +570,25 @@ public class EssentialsUpgrade
} }
return null; return null;
} }
public Location getFakeLocation(EssentialsConf config, String path)
{
String worldName = config.getString((path != null ? path + "." : "") + "world");
if (worldName == null || worldName.isEmpty())
{
return null;
}
World world = getFakeWorld(worldName);
if (world == null)
{
return null;
}
return new Location(world,
config.getDouble((path != null ? path + "." : "") + "x", 0),
config.getDouble((path != null ? path + "." : "") + "y", 0),
config.getDouble((path != null ? path + "." : "") + "z", 0),
(float)config.getDouble((path != null ? path + "." : "") + "yaw", 0),
(float)config.getDouble((path != null ? path + "." : "") + "pitch", 0));
}
public void beforeSettings() public void beforeSettings()
{ {

View File

@ -45,9 +45,9 @@ public interface IUser
void setLastLocation(); void setLastLocation();
Location getHome(String name); Location getHome(String name) throws Exception;
Location getHome(Location loc); Location getHome(Location loc) throws Exception;
String getName(); String getName();

View File

@ -193,7 +193,7 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser
return !ess.getSettings().itemSpawnBlacklist().contains(itemId); return !ess.getSettings().itemSpawnBlacklist().contains(itemId);
} }
public Location getHome() public Location getHome() throws Exception
{ {
return getHome(getHomes().get(0)); return getHome(getHomes().get(0));
} }

View File

@ -106,7 +106,7 @@ public abstract class UserData extends PlayerExtension implements IConf
} }
public Location getHome(String name) public Location getHome(String name) throws Exception
{ {
Location loc = config.getLocation("homes." + name, getServer()); Location loc = config.getLocation("homes." + name, getServer());
if (loc == null) if (loc == null)
@ -128,7 +128,7 @@ public abstract class UserData extends PlayerExtension implements IConf
return loc; return loc;
} }
public Location getHome(Location world) public Location getHome(Location world) throws Exception
{ {
Location loc; Location loc;
for (String home : getHomes()) for (String home : getHomes())
@ -166,9 +166,10 @@ public abstract class UserData extends PlayerExtension implements IConf
config.removeProperty("homes." + name); config.removeProperty("homes." + name);
config.save(); config.save();
} }
else { else
{
//TODO: move this message to messages file //TODO: move this message to messages file
throw new Exception("Home "+name+" doesn't exist"); throw new Exception("Home " + name + " doesn't exist");
} }
} }
@ -260,9 +261,16 @@ public abstract class UserData extends PlayerExtension implements IConf
private Location lastLocation; private Location lastLocation;
private Location _getLastLocation() private Location _getLastLocation()
{
try
{ {
return config.getLocation("lastlocation", getServer()); return config.getLocation("lastlocation", getServer());
} }
catch (Exception e)
{
return null;
}
}
public Location getLastLocation() public Location getLastLocation()
{ {

View File

@ -54,6 +54,8 @@ public class UserTest extends TestCase
user.setHome(); user.setHome();
OfflinePlayer base2 = server.createPlayer(base1.getName(), ess); OfflinePlayer base2 = server.createPlayer(base1.getName(), ess);
User user2 = ess.getUser(base2); User user2 = ess.getUser(base2);
try
{
Location home = user2.getHome(loc); Location home = user2.getHome(loc);
assertEquals(loc.getWorld().getName(), home.getWorld().getName()); assertEquals(loc.getWorld().getName(), home.getWorld().getName());
assertEquals(loc.getX(), home.getX()); assertEquals(loc.getX(), home.getX());
@ -62,6 +64,12 @@ public class UserTest extends TestCase
assertEquals(loc.getYaw(), home.getYaw()); assertEquals(loc.getYaw(), home.getYaw());
assertEquals(loc.getPitch(), home.getPitch()); assertEquals(loc.getPitch(), home.getPitch());
} }
catch (Exception ex)
{
fail("Exception");
}
}
public void testMoney() public void testMoney()
{ {