mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-06-27 18:06:40 +00:00
Avoid Unsafe on paper (#1678)
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
package com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_18_R1;
|
||||
|
||||
import com.fastasyncworldedit.bukkit.adapter.BukkitGetBlocks;
|
||||
import com.fastasyncworldedit.bukkit.adapter.DelegateSemaphore;
|
||||
import com.fastasyncworldedit.core.Fawe;
|
||||
import com.fastasyncworldedit.core.FaweCache;
|
||||
import com.fastasyncworldedit.core.configuration.Settings;
|
||||
@ -75,6 +74,7 @@ import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.locks.ReadWriteLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
import java.util.function.Function;
|
||||
@ -560,7 +560,7 @@ public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBloc
|
||||
if (PaperLib.isPaper()) {
|
||||
existingSection.tickingList.clear();
|
||||
}
|
||||
DelegateSemaphore lock = PaperweightPlatformAdapter.applyLock(existingSection);
|
||||
Semaphore lock = PaperweightPlatformAdapter.applyLock(existingSection);
|
||||
|
||||
synchronized (lock) {
|
||||
// lock.acquire();
|
||||
@ -895,7 +895,7 @@ public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBloc
|
||||
data = new char[4096];
|
||||
Arrays.fill(data, (char) BlockTypesCache.ReservedIDs.AIR);
|
||||
}
|
||||
DelegateSemaphore lock = PaperweightPlatformAdapter.applyLock(section);
|
||||
Semaphore lock = PaperweightPlatformAdapter.applyLock(section);
|
||||
synchronized (lock) {
|
||||
// Efficiently convert ChunkSection to raw data
|
||||
try {
|
||||
|
@ -133,11 +133,20 @@ public final class PaperweightPlatformAdapter extends NMSAdapter {
|
||||
methodGetVisibleChunk = MethodHandles.lookup().unreflect(getVisibleChunkIfPresent);
|
||||
|
||||
Unsafe unsafe = ReflectionUtils.getUnsafe();
|
||||
fieldThreadingDetector = PalettedContainer.class.getDeclaredField(Refraction.pickName("threadingDetector", "f"));
|
||||
fieldThreadingDetectorOffset = unsafe.objectFieldOffset(fieldThreadingDetector);
|
||||
if (!PaperLib.isPaper()) {
|
||||
fieldThreadingDetector = PalettedContainer.class.getDeclaredField(Refraction.pickName("threadingDetector", "f"));
|
||||
fieldThreadingDetectorOffset = unsafe.objectFieldOffset(fieldThreadingDetector);
|
||||
|
||||
fieldLock = ThreadingDetector.class.getDeclaredField(Refraction.pickName("lock", "c"));
|
||||
fieldLockOffset = unsafe.objectFieldOffset(fieldLock);
|
||||
fieldLock = ThreadingDetector.class.getDeclaredField(Refraction.pickName("lock", "c"));
|
||||
fieldLockOffset = unsafe.objectFieldOffset(fieldLock);
|
||||
} else {
|
||||
// in paper, the used methods are synchronized properly
|
||||
fieldThreadingDetector = null;
|
||||
fieldThreadingDetectorOffset = -1;
|
||||
|
||||
fieldLock = null;
|
||||
fieldLockOffset = -1;
|
||||
}
|
||||
|
||||
fieldGameEventDispatcherSections = LevelChunk.class.getDeclaredField(Refraction.pickName(
|
||||
"gameEventDispatcherSections", "t"));
|
||||
@ -181,7 +190,12 @@ public final class PaperweightPlatformAdapter extends NMSAdapter {
|
||||
return false;
|
||||
}
|
||||
|
||||
static DelegateSemaphore applyLock(LevelChunkSection section) {
|
||||
private static final ThreadLocal<Semaphore> SEMAPHORE_THREAD_LOCAL = ThreadLocal.withInitial(() -> new Semaphore(1));
|
||||
|
||||
static Semaphore applyLock(LevelChunkSection section) {
|
||||
if (PaperLib.isPaper()) {
|
||||
return SEMAPHORE_THREAD_LOCAL.get();
|
||||
}
|
||||
try {
|
||||
synchronized (section) {
|
||||
Unsafe unsafe = ReflectionUtils.getUnsafe();
|
||||
|
Reference in New Issue
Block a user