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
SignatureDeclareThrowsException

View File

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

View File

@ -280,7 +280,7 @@ public class EssentialsUpgrade
{
@SuppressWarnings("unchecked")
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)
{
config.setProperty("homes.home", defloc);
@ -300,7 +300,7 @@ public class EssentialsUpgrade
{
continue;
}
loc = config.getLocation("home.worlds." + world, ess.getServer());
loc = getFakeLocation(config, "home.worlds." + world);
if (loc == null)
{
continue;
@ -570,6 +570,25 @@ public class EssentialsUpgrade
}
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()
{

View File

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

View File

@ -193,7 +193,7 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser
return !ess.getSettings().itemSpawnBlacklist().contains(itemId);
}
public Location getHome()
public Location getHome() throws Exception
{
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());
if (loc == null)
@ -128,7 +128,7 @@ public abstract class UserData extends PlayerExtension implements IConf
return loc;
}
public Location getHome(Location world)
public Location getHome(Location world) throws Exception
{
Location loc;
for (String home : getHomes())
@ -166,9 +166,10 @@ public abstract class UserData extends PlayerExtension implements IConf
config.removeProperty("homes." + name);
config.save();
}
else {
else
{
//TODO: move this message to messages file
throw new Exception("Home "+name+" doesn't exist");
throw new Exception("Home " + name + " doesn't exist");
}
}
@ -261,7 +262,14 @@ public abstract class UserData extends PlayerExtension implements IConf
private Location _getLastLocation()
{
return config.getLocation("lastlocation", getServer());
try
{
return config.getLocation("lastlocation", getServer());
}
catch (Exception e)
{
return null;
}
}
public Location getLastLocation()

View File

@ -46,7 +46,7 @@ public class UserTest extends TestCase
OfflinePlayer base1alt = server.createPlayer(base1.getName(), ess);
assertEquals(base1alt, ess.getUser(base1alt).getBase());
}
public void testHome()
{
User user = ess.getUser(base1);
@ -54,13 +54,21 @@ public class UserTest extends TestCase
user.setHome();
OfflinePlayer base2 = server.createPlayer(base1.getName(), ess);
User user2 = ess.getUser(base2);
Location home = user2.getHome(loc);
assertEquals(loc.getWorld().getName(), home.getWorld().getName());
assertEquals(loc.getX(), home.getX());
assertEquals(loc.getY(), home.getY());
assertEquals(loc.getZ(), home.getZ());
assertEquals(loc.getYaw(), home.getYaw());
assertEquals(loc.getPitch(), home.getPitch());
try
{
Location home = user2.getHome(loc);
assertEquals(loc.getWorld().getName(), home.getWorld().getName());
assertEquals(loc.getX(), home.getX());
assertEquals(loc.getY(), home.getY());
assertEquals(loc.getZ(), home.getZ());
assertEquals(loc.getYaw(), home.getYaw());
assertEquals(loc.getPitch(), home.getPitch());
}
catch (Exception ex)
{
fail("Exception");
}
}
public void testMoney()