3
0
Mirror von https://github.com/IntellectualSites/FastAsyncWorldEdit.git synchronisiert 2024-11-20 09:50:06 +01:00

Implements method handles for folia

Dieser Commit ist enthalten in:
TheMeinerLP 2023-06-06 10:47:38 +02:00 committet von Phillipp Glanz
Ursprung ff4230602d
Commit c7316a9247
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden

Datei anzeigen

@ -5,6 +5,7 @@ import com.fastasyncworldedit.core.math.IntPair;
import com.fastasyncworldedit.core.util.TaskManager; import com.fastasyncworldedit.core.util.TaskManager;
import com.fastasyncworldedit.core.util.task.RunnableVal; import com.fastasyncworldedit.core.util.task.RunnableVal;
import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.adapter.ext.fawe.v1_19_R3.PaperweightAdapter;
import com.sk89q.worldedit.internal.block.BlockStateIdAccess; import com.sk89q.worldedit.internal.block.BlockStateIdAccess;
import com.sk89q.worldedit.internal.wna.WorldNativeAccess; import com.sk89q.worldedit.internal.wna.WorldNativeAccess;
import com.sk89q.worldedit.util.SideEffect; import com.sk89q.worldedit.util.SideEffect;
@ -26,6 +27,8 @@ import org.bukkit.craftbukkit.v1_19_R3.block.data.CraftBlockData;
import org.bukkit.event.block.BlockPhysicsEvent; import org.bukkit.event.block.BlockPhysicsEvent;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
@ -51,6 +54,10 @@ public class PaperweightFaweWorldNativeAccess implements WorldNativeAccess<Level
private final AtomicInteger lastTick; private final AtomicInteger lastTick;
private final Set<CachedChange> cachedChanges = new HashSet<>(); private final Set<CachedChange> cachedChanges = new HashSet<>();
private final Set<IntPair> cachedChunksToSend = new HashSet<>(); private final Set<IntPair> cachedChunksToSend = new HashSet<>();
private final MethodHandle globalTickData;
private final Class<?> regionScheduleHandleClass;
private final Class<?> regionizedServerClass;
private final MethodHandle globalCurrentTick;
private SideEffectSet sideEffectSet; private SideEffectSet sideEffectSet;
public PaperweightFaweWorldNativeAccess(PaperweightFaweAdapter paperweightFaweAdapter, WeakReference<Level> level) { public PaperweightFaweWorldNativeAccess(PaperweightFaweAdapter paperweightFaweAdapter, WeakReference<Level> level) {
@ -58,9 +65,32 @@ public class PaperweightFaweWorldNativeAccess implements WorldNativeAccess<Level
this.level = level; this.level = level;
// Use the actual tick as minecraft-defined so we don't try to force blocks into the world when the server's already lagging. // Use the actual tick as minecraft-defined so we don't try to force blocks into the world when the server's already lagging.
// - With the caveat that we don't want to have too many cached changed (1024) so we'd flush those at 1024 anyway. // - With the caveat that we don't want to have too many cached changed (1024) so we'd flush those at 1024 anyway.
PaperweightAdapter adapter = (PaperweightAdapter) paperweightFaweAdapter.getParent();
if (adapter.isFolia()) {
try {
regionizedServerClass = Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
regionScheduleHandleClass = Class.forName(
"io.papermc.paper.threadedregions.TickRegionScheduler.RegionScheduleHandle");
globalTickData = MethodHandles.lookup().unreflect(regionizedServerClass.getDeclaredMethod("getGlobalTickData"));
var data = globalTickData.invoke();
globalCurrentTick = MethodHandles.lookup().unreflect(regionScheduleHandleClass.getDeclaredMethod(
"getCurrentTick"));
final int tick = (int) globalCurrentTick.invoke(data);
this.lastTick = new AtomicInteger(tick);
} catch (Throwable e) {
throw new RuntimeException(e);
}
} else {
this.globalTickData = null;
this.regionScheduleHandleClass = null;
this.regionizedServerClass = null;
this.globalCurrentTick = null;
this.lastTick = new AtomicInteger(MinecraftServer.currentTick); this.lastTick = new AtomicInteger(MinecraftServer.currentTick);
} }
}
private Level getLevel() { private Level getLevel() {
return Objects.requireNonNull(level.get(), "The reference to the world was lost"); return Objects.requireNonNull(level.get(), "The reference to the world was lost");
} }
@ -94,13 +124,24 @@ public class PaperweightFaweWorldNativeAccess implements WorldNativeAccess<Level
LevelChunk levelChunk, BlockPos blockPos, LevelChunk levelChunk, BlockPos blockPos,
net.minecraft.world.level.block.state.BlockState blockState net.minecraft.world.level.block.state.BlockState blockState
) { ) {
int currentTick = MinecraftServer.currentTick; int currentTick;
PaperweightAdapter adapter = (PaperweightAdapter) paperweightFaweAdapter.getParent();
if (adapter.isFolia()) {
try {
var data = globalTickData.invoke();
currentTick = (int) globalCurrentTick.invoke(data);
} catch (Throwable e) {
throw new RuntimeException(e);
}
} else {
currentTick = MinecraftServer.currentTick;
}
if (Fawe.isTickThread()) { if (Fawe.isTickThread()) {
return levelChunk.setBlockState(blockPos, blockState, return levelChunk.setBlockState(blockPos, blockState,
this.sideEffectSet != null && this.sideEffectSet.shouldApply(SideEffect.UPDATE) this.sideEffectSet != null && this.sideEffectSet.shouldApply(SideEffect.UPDATE)
); );
} }
// Since FAWE is.. Async we need to do it on the main thread (wooooo.. :( )
cachedChanges.add(new CachedChange(levelChunk, blockPos, blockState)); cachedChanges.add(new CachedChange(levelChunk, blockPos, blockState));
cachedChunksToSend.add(new IntPair(levelChunk.locX, levelChunk.locZ)); cachedChunksToSend.add(new IntPair(levelChunk.locX, levelChunk.locZ));
boolean nextTick = lastTick.get() > currentTick; boolean nextTick = lastTick.get() > currentTick;
@ -110,6 +151,7 @@ public class PaperweightFaweWorldNativeAccess implements WorldNativeAccess<Level
} }
flushAsync(nextTick); flushAsync(nextTick);
} }
return blockState; return blockState;
} }