Skip to content

Commit

Permalink
Allow world name fallback for LazyLocation (EssentialsX#4428)
Browse files Browse the repository at this point in the history
Co-authored-by: MD <1917406+mdcfe@users.noreply.github.com>
  • Loading branch information
JRoy and mdcfe authored Aug 9, 2021
1 parent 14fbfe3 commit d56ecaa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void reloadConfig() {
if (worldId == null || worldId.isEmpty()) {
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()));
}
checkRegister();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
* Represents a Location but doesn't parse the location until it is requested via {@link LazyLocation#location()}.
*/
public class LazyLocation {
private final String world;
private String world;
private String worldName;
private final double x;
private final double y;
private final double z;
private final float yaw;
private final float pitch;

public LazyLocation(String world, double x, double y, double z, float yaw, float pitch) {
this.world = world;
public LazyLocation(String worldId, String worldName, double x, double y, double z, float yaw, float pitch) {
this.world = worldId;
this.worldName = worldName;
this.x = x;
this.y = y;
this.z = z;
Expand All @@ -30,6 +32,10 @@ public String world() {
return world;
}

public String worldName() {
return worldName;
}

public double x() {
return x;
}
Expand Down Expand Up @@ -67,14 +73,22 @@ public Location location() {
world = Bukkit.getWorld(this.world);
}

if (world == null && this.worldName != null && !this.worldName.isEmpty()) {
world = Bukkit.getWorld(this.worldName);
}

if (world == null) {
return null;
}

this.world = world.getUID().toString();
this.worldName = world.getName();

return new Location(world, x, y, z, yaw, pitch);
}

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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public LazyLocation deserialize(Type type, ConfigurationNode node) throws Serial

return new LazyLocation(
worldValue,
node.node("world-name").getString(""),
node.node("x").getDouble(),
node.node("y").getDouble(),
node.node("z").getDouble(),
Expand All @@ -36,6 +37,7 @@ public void serialize(Type type, @Nullable LazyLocation value, ConfigurationNode
}

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("y").set(Double.class, value.y());
node.node("z").set(Double.class, value.z());
Expand Down

0 comments on commit d56ecaa

Please # to comment.