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

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.Condition;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import org.jetbrains.annotations.NotNull;
public class DelegateLock extends ReentrantLockWithGetOwner { public class DelegateLock extends ReentrantLockWithGetOwner {
private final Lock parent; private final ReentrantLock parent;
private volatile boolean modified; private volatile boolean modified;
private final AtomicInteger count; private final AtomicInteger count;
public DelegateLock(Lock parent) { public DelegateLock(@NotNull ReentrantLock parent) {
this.parent = parent; this.parent = parent;
if (!(parent instanceof ReentrantLock)) { this.count = null;
count = new AtomicInteger();
} else {
count = null;
}
} }
public boolean isModified() { public boolean isModified() {
@ -86,22 +83,15 @@ public class DelegateLock extends ReentrantLockWithGetOwner {
@Override @Override
public synchronized boolean isLocked() { public synchronized boolean isLocked() {
if (parent instanceof ReentrantLock) { return parent.isLocked();
return ((ReentrantLock) parent).isLocked();
}
return count.get() > 0;
} }
public void untilFree() { public void untilFree() {
if (parent instanceof ReentrantLock) { ReentrantLock rl = parent;
ReentrantLock rl = (ReentrantLock) parent;
if (rl.isLocked()) { if (rl.isLocked()) {
rl.lock(); rl.lock();
rl.unlock(); rl.unlock();
} }
return;
}
while (count.get() > 0);
} }
@Override @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.BlockState;
import com.sk89q.worldedit.world.block.BlockTypesCache; import com.sk89q.worldedit.world.block.BlockTypesCache;
import io.papermc.lib.PaperLib; import io.papermc.lib.PaperLib;
import java.util.concurrent.locks.ReentrantLock;
import net.jpountz.util.UnsafeUtils; import net.jpountz.util.UnsafeUtils;
import net.minecraft.server.v1_14_R1.Block; import net.minecraft.server.v1_14_R1.Block;
import net.minecraft.server.v1_14_R1.Chunk; import net.minecraft.server.v1_14_R1.Chunk;
@ -115,7 +116,7 @@ public final class BukkitAdapter_1_14 extends NMSAdapter {
try { try {
synchronized (section) { synchronized (section) {
DataPaletteBlock<IBlockData> blocks = section.getBlocks(); DataPaletteBlock<IBlockData> blocks = section.getBlocks();
Lock currentLock = (Lock) fieldLock.get(blocks); ReentrantLock currentLock = (ReentrantLock) fieldLock.get(blocks);
if (currentLock instanceof DelegateLock) { if (currentLock instanceof DelegateLock) {
return (DelegateLock) currentLock; 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.BlockState;
import com.sk89q.worldedit.world.block.BlockTypesCache; import com.sk89q.worldedit.world.block.BlockTypesCache;
import io.papermc.lib.PaperLib; import io.papermc.lib.PaperLib;
import java.util.concurrent.locks.ReentrantLock;
import net.jpountz.util.UnsafeUtils; import net.jpountz.util.UnsafeUtils;
import net.minecraft.server.v1_15_R1.*; import net.minecraft.server.v1_15_R1.*;
import org.bukkit.craftbukkit.v1_15_R1.CraftChunk; import org.bukkit.craftbukkit.v1_15_R1.CraftChunk;
@ -96,7 +97,7 @@ public final class BukkitAdapter_1_15 extends NMSAdapter {
try { try {
synchronized (section) { synchronized (section) {
DataPaletteBlock<IBlockData> blocks = section.getBlocks(); DataPaletteBlock<IBlockData> blocks = section.getBlocks();
Lock currentLock = (Lock) fieldLock.get(blocks); ReentrantLock currentLock = (ReentrantLock) fieldLock.get(blocks);
if (currentLock instanceof DelegateLock) { if (currentLock instanceof DelegateLock) {
return (DelegateLock) currentLock; 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.BlockState;
import com.sk89q.worldedit.world.block.BlockTypesCache; import com.sk89q.worldedit.world.block.BlockTypesCache;
import io.papermc.lib.PaperLib; import io.papermc.lib.PaperLib;
import java.util.concurrent.locks.ReentrantLock;
import net.jpountz.util.UnsafeUtils; import net.jpountz.util.UnsafeUtils;
import net.minecraft.server.v1_15_R1.*; import net.minecraft.server.v1_15_R1.*;
import org.bukkit.craftbukkit.v1_15_R1.CraftChunk; 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) { 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 { try {
synchronized (section) { synchronized (section) {
DataPaletteBlock<IBlockData> blocks = section.getBlocks(); DataPaletteBlock<IBlockData> blocks = section.getBlocks();
Lock currentLock = (Lock) fieldLock.get(blocks); ReentrantLock currentLock = (ReentrantLock) fieldLock.get(blocks);
if (currentLock instanceof DelegateLock) { if (currentLock instanceof DelegateLock) {
return (DelegateLock) currentLock; return (DelegateLock) currentLock;
} }

Datei anzeigen

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