First idea implementation
This commit is contained in:
parent
c8801dbb8c
commit
b0f0a4deb5
13 changed files with 243 additions and 79 deletions
|
|
@ -0,0 +1,56 @@
|
|||
package org.dynmap.bukkit.helper.v118_2;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.server.level.WorldServer;
|
||||
import net.minecraft.world.level.World;
|
||||
import net.minecraft.world.level.chunk.Chunk;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* The provider used to work with paper libs
|
||||
* Because paper libs need java 17 we can't interact with them directly
|
||||
*/
|
||||
public class AsyncChunkProvider118_2 {
|
||||
private static final Thread ioThread;
|
||||
private static final Method getChunk;
|
||||
private static final Predicate<NBTTagCompound> ifFailed;
|
||||
static {
|
||||
try {
|
||||
Predicate<NBTTagCompound> ifFailed1 = null;
|
||||
Method getChunk1 = null;
|
||||
Thread ioThread1 = null;
|
||||
try {
|
||||
Class<?> threadClass = Class.forName("com.destroystokyo.paper.io.PaperFileIOThread");
|
||||
Class<?>[] classes = threadClass.getClasses();
|
||||
Class<?> holder = Arrays.stream(classes).filter(aClass -> aClass.getSimpleName().equals("Holder")).findAny().orElseThrow(RuntimeException::new);
|
||||
ioThread1 = (Thread) holder.getField("INSTANCE").get(null);
|
||||
getChunk1 = threadClass.getMethod("loadChunkDataAsync", WorldServer.class, int.class, int.class, int.class, Consumer.class, boolean.class, boolean.class, boolean.class);
|
||||
NBTTagCompound failure = (NBTTagCompound) threadClass.getField("FAILURE_VALUE").get(null);
|
||||
ifFailed1 = nbtTagCompound -> nbtTagCompound == failure;
|
||||
} catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException | NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
ifFailed = Objects.requireNonNull(ifFailed1);
|
||||
getChunk = Objects.requireNonNull(getChunk1);
|
||||
ioThread = Objects.requireNonNull(ioThread1);
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
public NBTTagCompound getChunk(WorldServer world, int x, int y) throws InvocationTargetException, IllegalAccessException, NoSuchFieldException {
|
||||
CompletableFuture<Object> future = new CompletableFuture<>();
|
||||
getChunk.invoke(ioThread,world,x,y,5,(Consumer<Object>) future::complete, false, true, true);
|
||||
Object resultFuture = future.join();
|
||||
if (resultFuture == null) return null;
|
||||
NBTTagCompound result = (NBTTagCompound) resultFuture.getClass().getField("chunkData").get(resultFuture);
|
||||
return ifFailed.test(result) ? null : result;
|
||||
}
|
||||
}
|
||||
|
|
@ -64,12 +64,25 @@ import java.util.Set;
|
|||
* Helper for isolation of bukkit version specific issues
|
||||
*/
|
||||
public class BukkitVersionHelperSpigot118_2 extends BukkitVersionHelper {
|
||||
|
||||
private final boolean unsafeAsync;
|
||||
|
||||
public BukkitVersionHelperSpigot118_2() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
boolean unsafeAsync1;
|
||||
try {
|
||||
Class.forName("com.destroystokyo.paper.io.PaperFileIOThread");
|
||||
unsafeAsync1 = false;
|
||||
} catch (ClassNotFoundException e) {
|
||||
unsafeAsync1 = true;
|
||||
}
|
||||
this.unsafeAsync = unsafeAsync1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUnsafeAsync() {
|
||||
return unsafeAsync;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get block short name list
|
||||
*/
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
package org.dynmap.bukkit.helper.v118_2;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.craftbukkit.v1_18_R2.CraftServer;
|
||||
import org.bukkit.craftbukkit.v1_18_R2.CraftWorld;
|
||||
import org.dynmap.DynmapChunk;
|
||||
import org.dynmap.bukkit.helper.BukkitVersionHelper;
|
||||
import org.dynmap.bukkit.helper.BukkitWorld;
|
||||
import org.dynmap.common.chunk.GenericChunk;
|
||||
import org.dynmap.common.chunk.GenericChunkCache;
|
||||
|
|
@ -14,12 +17,15 @@ import net.minecraft.world.level.chunk.storage.ChunkRegionLoader;
|
|||
import net.minecraft.world.level.chunk.Chunk;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* Container for managing chunks - dependent upon using chunk snapshots, since rendering is off server thread
|
||||
*/
|
||||
public class MapChunkCache118_2 extends GenericMapChunkCache {
|
||||
private final AsyncChunkProvider118_2 provider = BukkitVersionHelper.helper.isUnsafeAsync() ? null : new AsyncChunkProvider118_2();
|
||||
private World w;
|
||||
/**
|
||||
* Construct empty cache
|
||||
|
|
@ -30,20 +36,24 @@ public class MapChunkCache118_2 extends GenericMapChunkCache {
|
|||
|
||||
// Load generic chunk from existing and already loaded chunk
|
||||
protected GenericChunk getLoadedChunk(DynmapChunk chunk) {
|
||||
CraftWorld cw = (CraftWorld) w;
|
||||
NBTTagCompound nbt = null;
|
||||
GenericChunk gc = null;
|
||||
if (cw.isChunkLoaded(chunk.x, chunk.z)) {
|
||||
Chunk c = cw.getHandle().getChunkIfLoaded(chunk.x, chunk.z);
|
||||
if ((c != null) && c.o) { // c.loaded
|
||||
nbt = ChunkRegionLoader.a(cw.getHandle(), c);
|
||||
}
|
||||
if (nbt != null) {
|
||||
gc = parseChunkFromNBT(new NBT.NBTCompound(nbt));
|
||||
}
|
||||
CraftWorld cw = (CraftWorld) w;
|
||||
NBTTagCompound nbt = null;
|
||||
GenericChunk gc = null;
|
||||
if (cw.isChunkLoaded(chunk.x, chunk.z)) {
|
||||
Chunk c = cw.getHandle().getChunkIfLoaded(chunk.x, chunk.z); //already safe async on vanilla
|
||||
if ((c != null) && c.o) { // c.loaded
|
||||
if (provider == null) { //idk why, but paper uses this only sync, so I won't be smarter
|
||||
nbt = ChunkRegionLoader.a(cw.getHandle(), c);
|
||||
} else {
|
||||
nbt = CompletableFuture.supplyAsync(() -> ChunkRegionLoader.a(cw.getHandle(), c), ((CraftServer) Bukkit.getServer()).getServer()).join();
|
||||
}
|
||||
return gc;
|
||||
}
|
||||
}
|
||||
if (nbt != null) {
|
||||
gc = parseChunkFromNBT(new NBT.NBTCompound(nbt));
|
||||
}
|
||||
}
|
||||
return gc;
|
||||
}
|
||||
// Load generic chunk from unloaded chunk
|
||||
protected GenericChunk loadChunk(DynmapChunk chunk) {
|
||||
CraftWorld cw = (CraftWorld) w;
|
||||
|
|
@ -51,8 +61,12 @@ public class MapChunkCache118_2 extends GenericMapChunkCache {
|
|||
ChunkCoordIntPair cc = new ChunkCoordIntPair(chunk.x, chunk.z);
|
||||
GenericChunk gc = null;
|
||||
try {
|
||||
nbt = cw.getHandle().k().a.f(cc); // playerChunkMap
|
||||
} catch (IOException iox) {
|
||||
if (provider == null){
|
||||
nbt = cw.getHandle().k().a.f(cc); // playerChunkMap
|
||||
} else {
|
||||
nbt = provider.getChunk(cw.getHandle(),chunk.x, chunk.z);
|
||||
}
|
||||
} catch (IOException | InvocationTargetException | IllegalAccessException | NoSuchFieldException ignored) {
|
||||
}
|
||||
if (nbt != null) {
|
||||
gc = parseChunkFromNBT(new NBT.NBTCompound(nbt));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue