Merge pull request #3861 from mastermc05/chunks

Update used paper chunk api
This commit is contained in:
mikeprimm 2022-11-29 15:37:59 -06:00 committed by GitHub
commit 26fa3a24c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 35 deletions

View File

@ -17,8 +17,7 @@ import java.util.Arrays;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.function.Consumer; import java.util.function.BiConsumer;
import java.util.function.Predicate;
import java.util.function.Supplier; import java.util.function.Supplier;
/** /**
@ -27,59 +26,63 @@ import java.util.function.Supplier;
*/ */
@SuppressWarnings({"JavaReflectionMemberAccess"}) //java don't know about paper @SuppressWarnings({"JavaReflectionMemberAccess"}) //java don't know about paper
public class AsyncChunkProvider119 { public class AsyncChunkProvider119 {
private final Thread ioThread;
private final Method getChunk; private final Method getChunk;
private final Predicate<NBTTagCompound> ifFailed;
private final Method getAsyncSaveData; private final Method getAsyncSaveData;
private final Method save; private final Method save;
private final Enum<?> data;
private final Enum<?> priority;
private int currTick = MinecraftServer.currentTick; private int currTick = MinecraftServer.currentTick;
private int currChunks = 0; private int currChunks = 0;
AsyncChunkProvider119() { AsyncChunkProvider119() {
try { try {
Predicate<NBTTagCompound> ifFailed1 = null; Method getChunk1 = null;
Method getChunk1 = null, getAsyncSaveData1 = null, save1 = null; Method getAsyncSaveData1 = null;
Thread ioThread1 = null; Method save1 = null;
Enum<?> priority1 = null;
Enum<?> data1 = null;
try { try {
Class<?> threadClass = Class.forName("com.destroystokyo.paper.io.PaperFileIOThread"); Class<?> threadClass = Class.forName("io.papermc.paper.chunk.system.io.RegionFileIOThread");
Class<?> asyncChunkData = Arrays.stream(ChunkRegionLoader.class.getClasses())
.filter(c -> c.getSimpleName().equals("AsyncSaveData")) Class<?> dataclass = Arrays.stream(threadClass.getDeclaredClasses())
.findFirst() .filter(c -> c.getSimpleName().equals("RegionFileType"))
.orElseThrow(RuntimeException::new); .findAny()
.orElseThrow(NullPointerException::new);
data1 = Enum.valueOf(cast(dataclass), "CHUNK_DATA");
Class<?> priorityClass = Arrays.stream(Class.forName("ca.spottedleaf.concurrentutil.executor.standard.PrioritisedExecutor").getClasses())
.filter(c -> c.getSimpleName().equals("Priority"))
.findAny()
.orElseThrow(NullPointerException::new);
//Almost lowest priority, but not quite so low as to be considered idle
//COMPLETING->BLOCKING->HIGHEST->HIGHER->HIGH->NORMAL->LOW->LOWER->LOWEST->IDLE
priority1 = Enum.valueOf(cast(priorityClass), "LOWEST");
getAsyncSaveData1 = ChunkRegionLoader.class.getMethod("getAsyncSaveData", WorldServer.class, IChunkAccess.class); getAsyncSaveData1 = ChunkRegionLoader.class.getMethod("getAsyncSaveData", WorldServer.class, IChunkAccess.class);
save1 = ChunkRegionLoader.class.getMethod("saveChunk", WorldServer.class, IChunkAccess.class, asyncChunkData); save1 = ChunkRegionLoader.class.getMethod("saveChunk", WorldServer.class, IChunkAccess.class, getAsyncSaveData1.getReturnType());
Class<?>[] classes = threadClass.getClasses(); getChunk1 = threadClass.getMethod("loadDataAsync", WorldServer.class, int.class, int.class, data1.getClass(), BiConsumer.class, boolean.class, priority1.getClass());
Class<?> holder = Arrays.stream(classes).filter(aClass -> aClass.getSimpleName().equals("Holder")).findAny().orElseThrow(RuntimeException::new); } catch (ClassNotFoundException | NoSuchMethodException e) {
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(); e.printStackTrace();
} }
getAsyncSaveData = Objects.requireNonNull(getAsyncSaveData1); getAsyncSaveData = Objects.requireNonNull(getAsyncSaveData1);
save = Objects.requireNonNull(save1); save = Objects.requireNonNull(save1);
ifFailed = Objects.requireNonNull(ifFailed1);
getChunk = Objects.requireNonNull(getChunk1); getChunk = Objects.requireNonNull(getChunk1);
ioThread = Objects.requireNonNull(ioThread1); data = Objects.requireNonNull(data1);
priority = Objects.requireNonNull(priority1);
} catch (Throwable e) { } catch (Throwable e) {
e.printStackTrace(); e.printStackTrace();
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@SuppressWarnings("unchecked")
private <T> T cast(Object o) {
return (T) o;
}
public CompletableFuture<NBTTagCompound> getChunk(WorldServer world, int x, int y) throws InvocationTargetException, IllegalAccessException { public CompletableFuture<NBTTagCompound> getChunk(WorldServer world, int x, int y) throws InvocationTargetException, IllegalAccessException {
CompletableFuture<Object> future = new CompletableFuture<>(); CompletableFuture<NBTTagCompound> future = new CompletableFuture<>();
getChunk.invoke(ioThread,world,x,y,5,(Consumer<Object>) future::complete, false, true, true); getChunk.invoke(null, world, x, y, data, (BiConsumer<NBTTagCompound, Throwable>) (nbt, exception) -> future.complete(nbt), true, priority);
return future.thenApply((resultFuture) -> { return future;
if (resultFuture == null) return null;
try {
NBTTagCompound compound = (NBTTagCompound) resultFuture.getClass().getField("chunkData").get(resultFuture);
return ifFailed.test(compound) ? null : compound;
} catch (IllegalAccessException | NoSuchFieldException e) {
e.printStackTrace();
}
return null;
});
} }
public synchronized Supplier<NBTTagCompound> getLoadedChunk(CraftWorld world, int x, int z) { public synchronized Supplier<NBTTagCompound> getLoadedChunk(CraftWorld world, int x, int z) {

View File

@ -69,7 +69,7 @@ public class BukkitVersionHelperSpigot119 extends BukkitVersionHelper {
public BukkitVersionHelperSpigot119() { public BukkitVersionHelperSpigot119() {
boolean unsafeAsync1; boolean unsafeAsync1;
try { try {
Class.forName("com.destroystokyo.paper.io.PaperFileIOThread"); Class.forName("io.papermc.paper.chunk.system.io.RegionFileIOThread");
unsafeAsync1 = false; unsafeAsync1 = false;
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
unsafeAsync1 = true; unsafeAsync1 = true;