Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Allow world name fallback for LazyLocation #4428

Merged
merged 4 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, jailsNode.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