diff --git a/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightFaweAdapter.java b/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightFaweAdapter.java index d5071a1bf..e176743c4 100644 --- a/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightFaweAdapter.java +++ b/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightFaweAdapter.java @@ -147,17 +147,17 @@ public final class PaperweightFaweAdapter extends FaweAdapter>> properties = new HashMap<>(); @@ -365,18 +365,18 @@ public final class PaperweightFaweAdapter extends FaweAdapter get, + final IntFunction get, char[] set, CachedBukkitAdapter adapter, Registry biomeRegistry, @@ -436,9 +436,9 @@ public final class PaperweightPlatformAdapter extends NMSAdapter { try { int num_palette; if (get == null) { - num_palette = createPalette(blockToPalette, paletteToBlock, blocksCopy, set, adapter, null); + num_palette = createPalette(blockToPalette, paletteToBlock, blocksCopy, set, adapter); } else { - num_palette = createPalette(layer, blockToPalette, paletteToBlock, blocksCopy, get, set, adapter, null); + num_palette = createPalette(layer, blockToPalette, paletteToBlock, blocksCopy, get, set, adapter); } int bitsPerEntry = MathMan.log2nlz(num_palette - 1); diff --git a/worldedit-bukkit/adapters/adapter-1_20_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R3/PaperweightFaweAdapter.java b/worldedit-bukkit/adapters/adapter-1_20_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R3/PaperweightFaweAdapter.java index ef9f5da32..c5f4772a0 100644 --- a/worldedit-bukkit/adapters/adapter-1_20_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R3/PaperweightFaweAdapter.java +++ b/worldedit-bukkit/adapters/adapter-1_20_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R3/PaperweightFaweAdapter.java @@ -146,17 +146,17 @@ public final class PaperweightFaweAdapter extends FaweAdapter>> properties = new HashMap<>(); @@ -364,18 +364,18 @@ public final class PaperweightFaweAdapter extends FaweAdapter get, + final IntFunction get, char[] set, CachedBukkitAdapter adapter, Registry biomeRegistry, @@ -436,9 +436,9 @@ public final class PaperweightPlatformAdapter extends NMSAdapter { try { int num_palette; if (get == null) { - num_palette = createPalette(blockToPalette, paletteToBlock, blocksCopy, set, adapter, null); + num_palette = createPalette(blockToPalette, paletteToBlock, blocksCopy, set, adapter); } else { - num_palette = createPalette(layer, blockToPalette, paletteToBlock, blocksCopy, get, set, adapter, null); + num_palette = createPalette(layer, blockToPalette, paletteToBlock, blocksCopy, get, set, adapter); } int bitsPerEntry = MathMan.log2nlz(num_palette - 1); diff --git a/worldedit-bukkit/adapters/adapter-1_20_5/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R4/PaperweightFaweAdapter.java b/worldedit-bukkit/adapters/adapter-1_20_5/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R4/PaperweightFaweAdapter.java index 1f3975d9c..6c5a5330e 100644 --- a/worldedit-bukkit/adapters/adapter-1_20_5/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R4/PaperweightFaweAdapter.java +++ b/worldedit-bukkit/adapters/adapter-1_20_5/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R4/PaperweightFaweAdapter.java @@ -156,17 +156,17 @@ public final class PaperweightFaweAdapter extends FaweAdapter>> properties = new HashMap<>(); @@ -374,18 +374,18 @@ public final class PaperweightFaweAdapter extends FaweAdapter get, + final IntFunction get, char[] set, CachedBukkitAdapter adapter, Registry biomeRegistry, @@ -431,9 +429,9 @@ public final class PaperweightPlatformAdapter extends NMSAdapter { try { int num_palette; if (get == null) { - num_palette = createPalette(blockToPalette, paletteToBlock, blocksCopy, set, adapter, null); + num_palette = createPalette(blockToPalette, paletteToBlock, blocksCopy, set, adapter); } else { - num_palette = createPalette(layer, blockToPalette, paletteToBlock, blocksCopy, get, set, adapter, null); + num_palette = createPalette(layer, blockToPalette, paletteToBlock, blocksCopy, get, set, adapter); } int bitsPerEntry = MathMan.log2nlz(num_palette - 1); diff --git a/worldedit-bukkit/adapters/adapter-1_21/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_R1/PaperweightFaweAdapter.java b/worldedit-bukkit/adapters/adapter-1_21/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_R1/PaperweightFaweAdapter.java index 1f953f40c..415a462c9 100644 --- a/worldedit-bukkit/adapters/adapter-1_21/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_R1/PaperweightFaweAdapter.java +++ b/worldedit-bukkit/adapters/adapter-1_21/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_R1/PaperweightFaweAdapter.java @@ -156,17 +156,17 @@ public final class PaperweightFaweAdapter extends FaweAdapter>> properties = new HashMap<>(); @@ -374,18 +374,18 @@ public final class PaperweightFaweAdapter extends FaweAdapter get, + final IntFunction get, char[] set, CachedBukkitAdapter adapter, Registry biomeRegistry, @@ -415,9 +413,9 @@ public final class PaperweightPlatformAdapter extends NMSAdapter { try { int num_palette; if (get == null) { - num_palette = createPalette(blockToPalette, paletteToBlock, blocksCopy, set, adapter, null); + num_palette = createPalette(blockToPalette, paletteToBlock, blocksCopy, set, adapter); } else { - num_palette = createPalette(layer, blockToPalette, paletteToBlock, blocksCopy, get, set, adapter, null); + num_palette = createPalette(layer, blockToPalette, paletteToBlock, blocksCopy, get, set, adapter); } int bitsPerEntry = MathMan.log2nlz(num_palette - 1); diff --git a/worldedit-bukkit/adapters/adapter-1_21_3/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_3/PaperweightFaweAdapter.java b/worldedit-bukkit/adapters/adapter-1_21_3/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_3/PaperweightFaweAdapter.java index e2a192a6a..47aba4871 100644 --- a/worldedit-bukkit/adapters/adapter-1_21_3/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_3/PaperweightFaweAdapter.java +++ b/worldedit-bukkit/adapters/adapter-1_21_3/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_3/PaperweightFaweAdapter.java @@ -175,17 +175,17 @@ public final class PaperweightFaweAdapter extends FaweAdapter>> properties = new HashMap<>(); @@ -356,18 +356,18 @@ public final class PaperweightFaweAdapter extends FaweAdapter get, + final IntFunction get, char[] set, CachedBukkitAdapter adapter, Registry biomeRegistry, @@ -420,9 +420,9 @@ public final class PaperweightPlatformAdapter extends NMSAdapter { try { int num_palette; if (get == null) { - num_palette = createPalette(blockToPalette, paletteToBlock, blocksCopy, set, adapter, null); + num_palette = createPalette(blockToPalette, paletteToBlock, blocksCopy, set, adapter); } else { - num_palette = createPalette(layer, blockToPalette, paletteToBlock, blocksCopy, get, set, adapter, null); + num_palette = createPalette(layer, blockToPalette, paletteToBlock, blocksCopy, get, set, adapter); } int bitsPerEntry = MathMan.log2nlz(num_palette - 1); diff --git a/worldedit-bukkit/adapters/adapter-1_21_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_4/PaperweightFaweAdapter.java b/worldedit-bukkit/adapters/adapter-1_21_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_4/PaperweightFaweAdapter.java index 8f053162c..b0ec904cd 100644 --- a/worldedit-bukkit/adapters/adapter-1_21_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_4/PaperweightFaweAdapter.java +++ b/worldedit-bukkit/adapters/adapter-1_21_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_4/PaperweightFaweAdapter.java @@ -175,17 +175,17 @@ public final class PaperweightFaweAdapter extends FaweAdapter>> properties = new HashMap<>(); @@ -356,18 +356,18 @@ public final class PaperweightFaweAdapter extends FaweAdapter get, + final IntFunction get, char[] set, CachedBukkitAdapter adapter, Registry biomeRegistry, @@ -416,9 +415,9 @@ public final class PaperweightPlatformAdapter extends NMSAdapter { try { int num_palette; if (get == null) { - num_palette = createPalette(blockToPalette, paletteToBlock, blocksCopy, set, adapter, null); + num_palette = createPalette(blockToPalette, paletteToBlock, blocksCopy, set, adapter); } else { - num_palette = createPalette(layer, blockToPalette, paletteToBlock, blocksCopy, get, set, adapter, null); + num_palette = createPalette(layer, blockToPalette, paletteToBlock, blocksCopy, get, set, adapter); } int bitsPerEntry = MathMan.log2nlz(num_palette - 1); diff --git a/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/CachedBukkitAdapter.java b/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/CachedBukkitAdapter.java index 666b13e25..57dfe680a 100644 --- a/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/CachedBukkitAdapter.java +++ b/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/CachedBukkitAdapter.java @@ -100,7 +100,7 @@ public abstract class CachedBukkitAdapter implements IBukkitAdapter { } } - protected abstract char[] getIbdToStateOrdinal(); + protected abstract int[] getIbdToOrdinal(); protected abstract int[] getOrdinalToIbdID(); diff --git a/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/FaweAdapter.java b/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/FaweAdapter.java index 441cd1d87..14f4bd794 100644 --- a/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/FaweAdapter.java +++ b/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/FaweAdapter.java @@ -25,7 +25,7 @@ import java.util.Map; public abstract class FaweAdapter extends CachedBukkitAdapter implements IDelegateBukkitImplAdapter { protected final BukkitImplAdapter parent; - protected char[] ibdToStateOrdinal = null; + protected int[] ibdToOrdinal = null; protected int[] ordinalToIbdID = null; protected boolean initialised = false; protected Map>> allBlockProperties = null; diff --git a/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/NMSAdapter.java b/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/NMSAdapter.java index f8b240bb9..52035aa69 100644 --- a/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/NMSAdapter.java +++ b/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/NMSAdapter.java @@ -7,65 +7,34 @@ import com.fastasyncworldedit.core.queue.IChunkGet; import com.fastasyncworldedit.core.util.MathMan; import com.fastasyncworldedit.core.util.ReflectionUtils; import com.sk89q.worldedit.world.block.BlockTypesCache; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.locks.StampedLock; -import java.util.function.Function; +import java.util.function.IntFunction; public class NMSAdapter implements FAWEPlatformAdapterImpl { - private static final Logger LOGGER = LogManager.getLogger(); - public static int createPalette( int[] blockToPalette, int[] paletteToBlock, int[] blocksCopy, char[] set, - CachedBukkitAdapter adapter, - short[] nonEmptyBlockCount + CachedBukkitAdapter adapter ) { - short nonAir = 4096; - int num_palette = 0; + int numPaletteEntries = 0; for (int i = 0; i < 4096; i++) { - char ordinal = set[i]; - switch (ordinal) { - case BlockTypesCache.ReservedIDs.__RESERVED__ -> { - ordinal = BlockTypesCache.ReservedIDs.AIR; - nonAir--; - } - case BlockTypesCache.ReservedIDs.AIR, BlockTypesCache.ReservedIDs.CAVE_AIR, BlockTypesCache.ReservedIDs.VOID_AIR -> nonAir--; - } + int ordinal = set[i]; + ordinal = Math.max(ordinal, BlockTypesCache.ReservedIDs.AIR); int palette = blockToPalette[ordinal]; if (palette == Integer.MAX_VALUE) { - blockToPalette[ordinal] = num_palette; - paletteToBlock[num_palette] = ordinal; - num_palette++; + blockToPalette[ordinal] = numPaletteEntries; + paletteToBlock[numPaletteEntries] = ordinal; + numPaletteEntries++; } } - int bitsPerEntry = MathMan.log2nlz(num_palette - 1); - // If bits per entry is over 8, the game uses the global palette. - if (bitsPerEntry > 8 && adapter != null) { - // Cannot System#array copy char[] -> int[]; - for (int i = 0; i < adapter.getIbdToStateOrdinal().length; i++) { - paletteToBlock[i] = adapter.getIbdToStateOrdinal()[i]; - } - System.arraycopy(adapter.getOrdinalToIbdID(), 0, blockToPalette, 0, adapter.getOrdinalToIbdID().length); - } - for (int i = 0; i < 4096; i++) { - char ordinal = set[i]; - if (ordinal == BlockTypesCache.ReservedIDs.__RESERVED__) { - ordinal = BlockTypesCache.ReservedIDs.AIR; - } - int palette = blockToPalette[ordinal]; - blocksCopy[i] = palette; - } + mapPalette(blockToPalette, paletteToBlock, blocksCopy, set, adapter, numPaletteEntries); - if (nonEmptyBlockCount != null) { - nonEmptyBlockCount[0] = nonAir; - } - return num_palette; + return numPaletteEntries; } public static int createPalette( @@ -73,68 +42,55 @@ public class NMSAdapter implements FAWEPlatformAdapterImpl { int[] blockToPalette, int[] paletteToBlock, int[] blocksCopy, - Function get, + IntFunction get, char[] set, - CachedBukkitAdapter adapter, - short[] nonEmptyBlockCount + CachedBukkitAdapter adapter ) { - short nonAir = 4096; - int num_palette = 0; + int numPaletteEntries = 0; char[] getArr = null; for (int i = 0; i < 4096; i++) { char ordinal = set[i]; - switch (ordinal) { - case BlockTypesCache.ReservedIDs.__RESERVED__ -> { - if (getArr == null) { - getArr = get.apply(layer); - } - // write to set array as this should be a copied array, and will be important when the changes are written - // to the GET chunk cached by FAWE. Future dords, actually read this comment please. - set[i] = switch (ordinal = getArr[i]) { - case BlockTypesCache.ReservedIDs.__RESERVED__ -> { - nonAir--; - yield (ordinal = BlockTypesCache.ReservedIDs.AIR); - } - case BlockTypesCache.ReservedIDs.AIR, BlockTypesCache.ReservedIDs.CAVE_AIR, - BlockTypesCache.ReservedIDs.VOID_AIR -> { - nonAir--; - yield ordinal; - } - default -> ordinal; - }; + if (ordinal == BlockTypesCache.ReservedIDs.__RESERVED__) { + if (getArr == null) { + getArr = get.apply(layer); } - case BlockTypesCache.ReservedIDs.AIR, BlockTypesCache.ReservedIDs.CAVE_AIR, BlockTypesCache.ReservedIDs.VOID_AIR -> nonAir--; + ordinal = getArr[i]; + // write to set array as this should be a copied array, and will be important when the changes are written + // to the GET chunk cached by FAWE. Future dords, actually read this comment please. + set[i] = (char) Math.max(ordinal, BlockTypesCache.ReservedIDs.AIR); } int palette = blockToPalette[ordinal]; if (palette == Integer.MAX_VALUE) { - blockToPalette[ordinal] = num_palette; - paletteToBlock[num_palette] = ordinal; - num_palette++; + blockToPalette[ordinal] = numPaletteEntries; + paletteToBlock[numPaletteEntries] = ordinal; + numPaletteEntries++; } } - int bitsPerEntry = MathMan.log2nlz(num_palette - 1); + mapPalette(blockToPalette, paletteToBlock, blocksCopy, set, adapter, numPaletteEntries); + + return numPaletteEntries; + } + + private static void mapPalette( + int[] blockToPalette, + int[] paletteToBlock, + int[] blocksCopy, + char[] set, + CachedBukkitAdapter adapter, + int numPaletteEntries + ) { + int bitsPerEntry = MathMan.log2nlz(numPaletteEntries - 1); // If bits per entry is over 8, the game uses the global palette. if (bitsPerEntry > 8 && adapter != null) { - // Cannot System#array copy char[] -> int[]; - for (int i = 0; i < adapter.getIbdToStateOrdinal().length; i++) { - paletteToBlock[i] = adapter.getIbdToStateOrdinal()[i]; - } + System.arraycopy(adapter.getIbdToOrdinal(), 0, paletteToBlock, 0, adapter.getIbdToOrdinal().length); System.arraycopy(adapter.getOrdinalToIbdID(), 0, blockToPalette, 0, adapter.getOrdinalToIbdID().length); } for (int i = 0; i < 4096; i++) { - char ordinal = set[i]; - if (ordinal == BlockTypesCache.ReservedIDs.__RESERVED__) { - LOGGER.error("Empty (__RESERVED__) ordinal given where not expected, default to air."); - ordinal = BlockTypesCache.ReservedIDs.AIR; - } + int ordinal = set[i]; + ordinal = Math.max(ordinal, BlockTypesCache.ReservedIDs.AIR); int palette = blockToPalette[ordinal]; blocksCopy[i] = palette; } - - if (nonEmptyBlockCount != null) { - nonEmptyBlockCount[0] = nonAir; - } - return num_palette; } @Override diff --git a/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/SimpleBukkitAdapter.java b/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/SimpleBukkitAdapter.java index 6dc63f179..34bc9d7c9 100644 --- a/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/SimpleBukkitAdapter.java +++ b/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/SimpleBukkitAdapter.java @@ -23,8 +23,8 @@ public class SimpleBukkitAdapter extends CachedBukkitAdapter { } @Override - protected char[] getIbdToStateOrdinal() { - return new char[Character.MAX_VALUE + 1]; + protected int[] getIbdToOrdinal() { + return new int[Character.MAX_VALUE + 1]; } @Override