Prevent getting jail worlds before world load (#4216)

This commit is contained in:
triagonal 2021-06-09 22:34:14 +10:00 committed by GitHub
parent 54c70581a2
commit 0d71fe65f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 16 deletions

View File

@ -2,11 +2,10 @@ package com.earth2me.essentials;
import com.earth2me.essentials.config.ConfigurateUtil; import com.earth2me.essentials.config.ConfigurateUtil;
import com.earth2me.essentials.config.EssentialsConfiguration; import com.earth2me.essentials.config.EssentialsConfiguration;
import com.earth2me.essentials.config.entities.LazyLocation;
import net.ess3.api.IEssentials; import net.ess3.api.IEssentials;
import net.ess3.api.IUser; import net.ess3.api.IUser;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -44,7 +43,7 @@ public class Jails implements net.ess3.api.IJails {
private static transient boolean enabled = false; private static transient boolean enabled = false;
private final IEssentials ess; private final IEssentials ess;
private final EssentialsConfiguration config; private final EssentialsConfiguration config;
private final Map<String, Location> jails = new HashMap<>(); private final Map<String, LazyLocation> jails = new HashMap<>();
public Jails(final IEssentials ess) { public Jails(final IEssentials ess) {
this.ess = ess; this.ess = ess;
@ -60,17 +59,11 @@ public class Jails implements net.ess3.api.IJails {
final CommentedConfigurationNode jailsNode = config.getSection("jails"); final CommentedConfigurationNode jailsNode = config.getSection("jails");
for (final Map.Entry<String, CommentedConfigurationNode> entry : ConfigurateUtil.getMap(jailsNode).entrySet()) { for (final Map.Entry<String, CommentedConfigurationNode> entry : ConfigurateUtil.getMap(jailsNode).entrySet()) {
final CommentedConfigurationNode jailNode = entry.getValue(); final CommentedConfigurationNode jailNode = entry.getValue();
final String worldName = jailNode.node("world").getString(); final String worldId = jailNode.node("world").getString();
if (worldName == null || worldName.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(),
final World world = Bukkit.getWorld(worldName);
if (world == null) {
LOGGER.log(Level.WARNING, "Invalid world name, " + worldName + ", for jail name " + entry.getKey());
continue;
}
jails.put(entry.getKey().toLowerCase(Locale.ENGLISH), new Location(world, 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();
@ -119,7 +112,11 @@ public class Jails implements net.ess3.api.IJails {
if (!jails.containsKey(jailName)) { if (!jails.containsKey(jailName)) {
throw new Exception(tl("jailNotExist")); throw new Exception(tl("jailNotExist"));
} }
return jails.get(jailName); final Location location = jails.get(jailName).location();
if (location == null) {
throw new Exception(tl("jailWorldNotExist"));
}
return location;
} }
} }
@ -160,7 +157,7 @@ public class Jails implements net.ess3.api.IJails {
synchronized (jails) { synchronized (jails) {
if (jails.containsKey(jail)) { if (jails.containsKey(jail)) {
if (user.getBase().isOnline()) { if (user.getBase().isOnline()) {
user.getTeleport().now(jails.get(jail), false, TeleportCause.COMMAND); user.getTeleport().now(getJail(jail), false, TeleportCause.COMMAND);
} }
user.setJail(jail); user.setJail(jail);
} }
@ -177,7 +174,7 @@ public class Jails implements net.ess3.api.IJails {
synchronized (jails) { synchronized (jails) {
if (jails.containsKey(jail)) { if (jails.containsKey(jail)) {
if (user.getBase().isOnline()) { if (user.getBase().isOnline()) {
user.getAsyncTeleport().now(jails.get(jail), false, TeleportCause.COMMAND, future); user.getAsyncTeleport().now(getJail(jail), false, TeleportCause.COMMAND, future);
future.thenAccept(success -> user.setJail(jail)); future.thenAccept(success -> user.setJail(jail));
return; return;
} }
@ -190,7 +187,7 @@ public class Jails implements net.ess3.api.IJails {
public void setJail(String jailName, final Location loc) throws Exception { public void setJail(String jailName, final Location loc) throws Exception {
jailName = jailName.toLowerCase(Locale.ENGLISH); jailName = jailName.toLowerCase(Locale.ENGLISH);
synchronized (jails) { synchronized (jails) {
jails.put(jailName, loc); jails.put(jailName, LazyLocation.fromLocation(loc));
config.setProperty("jails." + jailName, loc); config.setProperty("jails." + jailName, loc);
config.save(); config.save();
} }

View File

@ -526,6 +526,7 @@ jailReleased=\u00a76Player \u00a7c{0}\u00a76 unjailed.
jailReleasedPlayerNotify=\u00a76You have been released\! jailReleasedPlayerNotify=\u00a76You have been released\!
jailSentenceExtended=\u00a76Jail time extended to \u00a7c{0}\u00a76. jailSentenceExtended=\u00a76Jail time extended to \u00a7c{0}\u00a76.
jailSet=\u00a76Jail\u00a7c {0} \u00a76has been set. jailSet=\u00a76Jail\u00a7c {0} \u00a76has been set.
jailWorldNotExist=\u00a74That jail's world does not exist.
jumpEasterDisable=\u00a76Flying wizard mode disabled. jumpEasterDisable=\u00a76Flying wizard mode disabled.
jumpEasterEnable=\u00a76Flying wizard mode enabled. jumpEasterEnable=\u00a76Flying wizard mode enabled.
jailsCommandDescription=List all jails. jailsCommandDescription=List all jails.