Allow world name fallback for LazyLocation (#4428)

Co-authored-by: MD <1917406+mdcfe@users.noreply.github.com>
This commit is contained in:
Josh Roy 2021-08-09 12:39:55 -07:00 committed by GitHub
parent 14fbfe360e
commit d56ecaacdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 5 deletions

View File

@ -63,7 +63,7 @@ public class Jails implements net.ess3.api.IJails {
if (worldId == null || worldId.isEmpty()) { if (worldId == null || worldId.isEmpty()) {
continue; continue;
} }
jails.put(entry.getKey().toLowerCase(Locale.ENGLISH), new LazyLocation(worldId, jailNode.node("x").getDouble(), jailNode.node("y").getDouble(), jails.put(entry.getKey().toLowerCase(Locale.ENGLISH), new LazyLocation(worldId, jailNode.node("world-name").getString(""), jailNode.node("x").getDouble(), jailNode.node("y").getDouble(),
jailNode.node("z").getDouble(), jailNode.node("yaw").getFloat(), jailNode.node("pitch").getFloat())); jailNode.node("z").getDouble(), jailNode.node("yaw").getFloat(), jailNode.node("pitch").getFloat()));
} }
checkRegister(); checkRegister();

View File

@ -10,15 +10,17 @@ import java.util.UUID;
* Represents a Location but doesn't parse the location until it is requested via {@link LazyLocation#location()}. * Represents a Location but doesn't parse the location until it is requested via {@link LazyLocation#location()}.
*/ */
public class LazyLocation { public class LazyLocation {
private final String world; private String world;
private String worldName;
private final double x; private final double x;
private final double y; private final double y;
private final double z; private final double z;
private final float yaw; private final float yaw;
private final float pitch; private final float pitch;
public LazyLocation(String world, double x, double y, double z, float yaw, float pitch) { public LazyLocation(String worldId, String worldName, double x, double y, double z, float yaw, float pitch) {
this.world = world; this.world = worldId;
this.worldName = worldName;
this.x = x; this.x = x;
this.y = y; this.y = y;
this.z = z; this.z = z;
@ -30,6 +32,10 @@ public class LazyLocation {
return world; return world;
} }
public String worldName() {
return worldName;
}
public double x() { public double x() {
return x; return x;
} }
@ -67,14 +73,22 @@ public class LazyLocation {
world = Bukkit.getWorld(this.world); world = Bukkit.getWorld(this.world);
} }
if (world == null && this.worldName != null && !this.worldName.isEmpty()) {
world = Bukkit.getWorld(this.worldName);
}
if (world == null) { if (world == null) {
return null; return null;
} }
this.world = world.getUID().toString();
this.worldName = world.getName();
return new Location(world, x, y, z, yaw, pitch); return new Location(world, x, y, z, yaw, pitch);
} }
public static LazyLocation fromLocation(final Location location) { public static LazyLocation fromLocation(final Location location) {
return new LazyLocation(location.getWorld().getUID().toString(), location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch()); //noinspection ConstantConditions
return new LazyLocation(location.getWorld().getUID().toString(), location.getWorld().getName(), location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
} }
} }

View File

@ -21,6 +21,7 @@ public class LocationTypeSerializer implements TypeSerializer<LazyLocation> {
return new LazyLocation( return new LazyLocation(
worldValue, worldValue,
node.node("world-name").getString(""),
node.node("x").getDouble(), node.node("x").getDouble(),
node.node("y").getDouble(), node.node("y").getDouble(),
node.node("z").getDouble(), node.node("z").getDouble(),
@ -36,6 +37,7 @@ public class LocationTypeSerializer implements TypeSerializer<LazyLocation> {
} }
node.node("world").set(String.class, value.world()); node.node("world").set(String.class, value.world());
node.node("world-name").set(String.class, value.worldName());
node.node("x").set(Double.class, value.x()); node.node("x").set(Double.class, value.x());
node.node("y").set(Double.class, value.y()); node.node("y").set(Double.class, value.y());
node.node("z").set(Double.class, value.z()); node.node("z").set(Double.class, value.z());