Minor code quality changes

Changed Lock to subclass ReentrantLock since that is the only subclass DelegateLock should be using. The lock should also never be null so I added an annotation in the constructor.
I also removed some code and replaced it with PaperLib code to just clean up things a little bit.
Dieser Commit ist enthalten in:
MattBDev 2020-02-07 14:33:51 -05:00
Ursprung 5feac07bf0
Commit 0047f20d5d
5 geänderte Dateien mit 17 neuen und 29 gelöschten Zeilen

Datei anzeigen

@ -7,19 +7,16 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.jetbrains.annotations.NotNull;
public class DelegateLock extends ReentrantLockWithGetOwner {
private final Lock parent;
private final ReentrantLock parent;
private volatile boolean modified;
private final AtomicInteger count;
public DelegateLock(Lock parent) {
public DelegateLock(@NotNull ReentrantLock parent) {
this.parent = parent;
if (!(parent instanceof ReentrantLock)) {
count = new AtomicInteger();
} else {
count = null;
}
this.count = null;
}
public boolean isModified() {
@ -86,22 +83,15 @@ public class DelegateLock extends ReentrantLockWithGetOwner {
@Override
public synchronized boolean isLocked() {
if (parent instanceof ReentrantLock) {
return ((ReentrantLock) parent).isLocked();
}
return count.get() > 0;
return parent.isLocked();
}
public void untilFree() {
if (parent instanceof ReentrantLock) {
ReentrantLock rl = (ReentrantLock) parent;
if (rl.isLocked()) {
rl.lock();
rl.unlock();
}
return;
ReentrantLock rl = parent;
if (rl.isLocked()) {
rl.lock();
rl.unlock();
}
while (count.get() > 0);
}
@Override

Datei anzeigen

@ -12,6 +12,7 @@ import com.boydti.fawe.util.TaskManager;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockTypesCache;
import io.papermc.lib.PaperLib;
import java.util.concurrent.locks.ReentrantLock;
import net.jpountz.util.UnsafeUtils;
import net.minecraft.server.v1_14_R1.Block;
import net.minecraft.server.v1_14_R1.Chunk;
@ -115,7 +116,7 @@ public final class BukkitAdapter_1_14 extends NMSAdapter {
try {
synchronized (section) {
DataPaletteBlock<IBlockData> blocks = section.getBlocks();
Lock currentLock = (Lock) fieldLock.get(blocks);
ReentrantLock currentLock = (ReentrantLock) fieldLock.get(blocks);
if (currentLock instanceof DelegateLock) {
return (DelegateLock) currentLock;
}

Datei anzeigen

@ -12,6 +12,7 @@ import com.boydti.fawe.util.TaskManager;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockTypesCache;
import io.papermc.lib.PaperLib;
import java.util.concurrent.locks.ReentrantLock;
import net.jpountz.util.UnsafeUtils;
import net.minecraft.server.v1_15_R1.*;
import org.bukkit.craftbukkit.v1_15_R1.CraftChunk;
@ -96,7 +97,7 @@ public final class BukkitAdapter_1_15 extends NMSAdapter {
try {
synchronized (section) {
DataPaletteBlock<IBlockData> blocks = section.getBlocks();
Lock currentLock = (Lock) fieldLock.get(blocks);
ReentrantLock currentLock = (ReentrantLock) fieldLock.get(blocks);
if (currentLock instanceof DelegateLock) {
return (DelegateLock) currentLock;
}

Datei anzeigen

@ -12,6 +12,7 @@ import com.boydti.fawe.util.TaskManager;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockTypesCache;
import io.papermc.lib.PaperLib;
import java.util.concurrent.locks.ReentrantLock;
import net.jpountz.util.UnsafeUtils;
import net.minecraft.server.v1_15_R1.*;
import org.bukkit.craftbukkit.v1_15_R1.CraftChunk;
@ -93,10 +94,11 @@ public final class BukkitAdapter_1_15_2 extends NMSAdapter {
}
protected static DelegateLock applyLock(ChunkSection section) {
//todo there has to be a better way to do this. Maybe using a() in DataPaletteBlock which aquires the lock in NMS?
try {
synchronized (section) {
DataPaletteBlock<IBlockData> blocks = section.getBlocks();
Lock currentLock = (Lock) fieldLock.get(blocks);
ReentrantLock currentLock = (ReentrantLock) fieldLock.get(blocks);
if (currentLock instanceof DelegateLock) {
return (DelegateLock) currentLock;
}

Datei anzeigen

@ -342,13 +342,7 @@ public class BukkitWorld extends AbstractWorld {
if (Fawe.isMainThread()) {
world.getChunkAt(X, Z);
} else if (!world.isChunkLoaded(X, Z)) {
if (PaperLib.isPaper()) {
world.getChunkAtAsync(X, Z, true);
} else {
Fawe.get().getQueueHandler().sync(() -> {
world.getChunkAt(X, Z);
});
}
PaperLib.getChunkAtAsync(world,X, Z, true);
}
}
}