From 0047f20d5d09a05cb64f3256ce5388ac40f22347 Mon Sep 17 00:00:00 2001 From: MattBDev <4009945+MattBDev@users.noreply.github.com> Date: Fri, 7 Feb 2020 14:33:51 -0500 Subject: [PATCH] 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. --- .../fawe/bukkit/adapter/DelegateLock.java | 28 ++++++------------- .../adapter/mc1_14/BukkitAdapter_1_14.java | 3 +- .../adapter/mc1_15/BukkitAdapter_1_15.java | 3 +- .../mc1_15_2/BukkitAdapter_1_15_2.java | 4 ++- .../sk89q/worldedit/bukkit/BukkitWorld.java | 8 +----- 5 files changed, 17 insertions(+), 29 deletions(-) diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/DelegateLock.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/DelegateLock.java index e8f86448f..e351f9a23 100644 --- a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/DelegateLock.java +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/DelegateLock.java @@ -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 diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_14/BukkitAdapter_1_14.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_14/BukkitAdapter_1_14.java index 3c3a2f551..09f174e1d 100644 --- a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_14/BukkitAdapter_1_14.java +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_14/BukkitAdapter_1_14.java @@ -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 blocks = section.getBlocks(); - Lock currentLock = (Lock) fieldLock.get(blocks); + ReentrantLock currentLock = (ReentrantLock) fieldLock.get(blocks); if (currentLock instanceof DelegateLock) { return (DelegateLock) currentLock; } diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15/BukkitAdapter_1_15.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15/BukkitAdapter_1_15.java index b00001bd5..0496cc979 100644 --- a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15/BukkitAdapter_1_15.java +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15/BukkitAdapter_1_15.java @@ -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 blocks = section.getBlocks(); - Lock currentLock = (Lock) fieldLock.get(blocks); + ReentrantLock currentLock = (ReentrantLock) fieldLock.get(blocks); if (currentLock instanceof DelegateLock) { return (DelegateLock) currentLock; } diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15_2/BukkitAdapter_1_15_2.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15_2/BukkitAdapter_1_15_2.java index d0e617e20..e42b624fd 100644 --- a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15_2/BukkitAdapter_1_15_2.java +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15_2/BukkitAdapter_1_15_2.java @@ -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 blocks = section.getBlocks(); - Lock currentLock = (Lock) fieldLock.get(blocks); + ReentrantLock currentLock = (ReentrantLock) fieldLock.get(blocks); if (currentLock instanceof DelegateLock) { return (DelegateLock) currentLock; } diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java index 10f41e50c..d9b78909d 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java @@ -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); } } }