Synchronise on the nmsWorld so we're not attempting to load two chunks at the same time.

Possibly fixes #471
This commit is contained in:
dordsor21 2020-05-18 16:04:10 +01:00
parent 5be11c541b
commit 31cc5e0b60
3 changed files with 39 additions and 24 deletions

View File

@ -136,7 +136,10 @@ public final class BukkitAdapter_1_14 extends NMSAdapter {
} }
public static Chunk ensureLoaded(net.minecraft.server.v1_14_R1.World nmsWorld, int X, int Z) { public static Chunk ensureLoaded(net.minecraft.server.v1_14_R1.World nmsWorld, int X, int Z) {
Chunk nmsChunk = nmsWorld.getChunkIfLoaded(X, Z); Chunk nmsChunk;
synchronized (nmsWorld) {
nmsChunk = nmsWorld.getChunkIfLoaded(X, Z);
}
if (nmsChunk != null) { if (nmsChunk != null) {
return nmsChunk; return nmsChunk;
} }
@ -144,13 +147,15 @@ public final class BukkitAdapter_1_14 extends NMSAdapter {
return nmsWorld.getChunkAt(X, Z); return nmsWorld.getChunkAt(X, Z);
} }
if (PaperLib.isPaper()) { if (PaperLib.isPaper()) {
CraftWorld craftWorld = nmsWorld.getWorld(); synchronized (nmsWorld) {
CompletableFuture<org.bukkit.Chunk> future = craftWorld.getChunkAtAsync(X, Z, true); CraftWorld craftWorld = nmsWorld.getWorld();
try { CompletableFuture<org.bukkit.Chunk> future = craftWorld.getChunkAtAsync(X, Z, true);
CraftChunk chunk = (CraftChunk) future.get(); try {
return chunk.getHandle(); CraftChunk chunk = (CraftChunk) future.get();
} catch (Throwable e) { return chunk.getHandle();
e.printStackTrace(); } catch (Throwable e) {
e.printStackTrace();
}
} }
} }
// TODO optimize // TODO optimize

View File

@ -123,7 +123,10 @@ public final class BukkitAdapter_1_15 extends NMSAdapter {
} }
public static Chunk ensureLoaded(net.minecraft.server.v1_15_R1.World nmsWorld, int X, int Z) { public static Chunk ensureLoaded(net.minecraft.server.v1_15_R1.World nmsWorld, int X, int Z) {
Chunk nmsChunk = nmsWorld.getChunkIfLoaded(X, Z); Chunk nmsChunk;
synchronized (nmsWorld) {
nmsChunk = nmsWorld.getChunkIfLoaded(X, Z);
}
if (nmsChunk != null) { if (nmsChunk != null) {
return nmsChunk; return nmsChunk;
} }
@ -131,13 +134,15 @@ public final class BukkitAdapter_1_15 extends NMSAdapter {
return nmsWorld.getChunkAt(X, Z); return nmsWorld.getChunkAt(X, Z);
} }
if (PaperLib.isPaper()) { if (PaperLib.isPaper()) {
CraftWorld craftWorld = nmsWorld.getWorld(); synchronized (nmsWorld) {
CompletableFuture<org.bukkit.Chunk> future = craftWorld.getChunkAtAsync(X, Z, true); CraftWorld craftWorld = nmsWorld.getWorld();
try { CompletableFuture<org.bukkit.Chunk> future = craftWorld.getChunkAtAsync(X, Z, true);
CraftChunk chunk = (CraftChunk) future.get(); try {
return chunk.getHandle(); CraftChunk chunk = (CraftChunk) future.get();
} catch (Throwable e) { return chunk.getHandle();
e.printStackTrace(); } catch (Throwable e) {
e.printStackTrace();
}
} }
} }
// TODO optimize // TODO optimize

View File

@ -124,7 +124,10 @@ public final class BukkitAdapter_1_15_2 extends NMSAdapter {
} }
public static Chunk ensureLoaded(World nmsWorld, int X, int Z) { public static Chunk ensureLoaded(World nmsWorld, int X, int Z) {
Chunk nmsChunk = nmsWorld.getChunkIfLoaded(X, Z); Chunk nmsChunk;
synchronized (nmsWorld) {
nmsChunk = nmsWorld.getChunkIfLoaded(X, Z);
}
if (nmsChunk != null) { if (nmsChunk != null) {
return nmsChunk; return nmsChunk;
} }
@ -132,13 +135,15 @@ public final class BukkitAdapter_1_15_2 extends NMSAdapter {
return nmsWorld.getChunkAt(X, Z); return nmsWorld.getChunkAt(X, Z);
} }
if (PaperLib.isPaper()) { if (PaperLib.isPaper()) {
CraftWorld craftWorld = nmsWorld.getWorld(); synchronized (nmsWorld) {
CompletableFuture<org.bukkit.Chunk> future = craftWorld.getChunkAtAsync(X, Z, true); CraftWorld craftWorld = nmsWorld.getWorld();
try { CompletableFuture<org.bukkit.Chunk> future = craftWorld.getChunkAtAsync(X, Z, true);
CraftChunk chunk = (CraftChunk) future.get(); try {
return chunk.getHandle(); CraftChunk chunk = (CraftChunk) future.get();
} catch (Throwable e) { return chunk.getHandle();
e.printStackTrace(); } catch (Throwable e) {
e.printStackTrace();
}
} }
} }
// TODO optimize // TODO optimize