Hopefully allow for unloaded world to be referenced after GC.

Possibly fixes #504
This commit is contained in:
dordsor21 2020-07-03 13:27:26 +01:00
parent fae528ab64
commit 63bc151f6c
No known key found for this signature in database
GPG Key ID: 1E53E88969FFCF0B

View File

@ -55,6 +55,8 @@ import java.lang.ref.WeakReference;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.*; import java.util.*;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.bukkit.Bukkit;
import org.bukkit.Effect; import org.bukkit.Effect;
import org.bukkit.TreeType; import org.bukkit.TreeType;
import org.bukkit.World; import org.bukkit.World;
@ -78,7 +80,8 @@ public class BukkitWorld extends AbstractWorld {
} }
} }
private final WeakReference<World> worldRef; private WeakReference<World> worldRef;
private final String worldNameRef;
private final WorldNativeAccess<?, ?, ?> worldNativeAccess; private final WorldNativeAccess<?, ?, ?> worldNativeAccess;
/** /**
@ -88,6 +91,7 @@ public class BukkitWorld extends AbstractWorld {
*/ */
public BukkitWorld(World world) { public BukkitWorld(World world) {
this.worldRef = new WeakReference<>(world); this.worldRef = new WeakReference<>(world);
this.worldNameRef = world.getName();
BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter(); BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter();
if (adapter != null) { if (adapter != null) {
this.worldNativeAccess = adapter.createWorldNativeAccess(world); this.worldNativeAccess = adapter.createWorldNativeAccess(world);
@ -150,7 +154,12 @@ public class BukkitWorld extends AbstractWorld {
* @return the world * @return the world
*/ */
public World getWorld() { public World getWorld() {
return checkNotNull(worldRef.get(), "The world was unloaded and the reference is unavailable"); World tmp = worldRef.get();
if (tmp == null) {
tmp = Bukkit.getWorld(worldNameRef);
if (tmp != null) worldRef = new WeakReference<>(tmp);
}
return checkNotNull(tmp, "The world was unloaded and the reference is unavailable");
} }
/** /**