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 f174b3a03..666b13e25 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,4 +100,8 @@ public abstract class CachedBukkitAdapter implements IBukkitAdapter { } } + protected abstract char[] getIbdToStateOrdinal(); + + protected abstract int[] getOrdinalToIbdID(); + } 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 3c83def52..2f6dd6863 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 @@ -3,6 +3,7 @@ package com.fastasyncworldedit.bukkit.adapter; import com.fastasyncworldedit.core.FAWEPlatformAdapterImpl; import com.fastasyncworldedit.core.configuration.Settings; import com.fastasyncworldedit.core.queue.IChunkGet; +import com.fastasyncworldedit.core.util.MathMan; import com.fastasyncworldedit.core.world.block.BlockID; import com.sk89q.worldedit.bukkit.WorldEditPlugin; import com.sk89q.worldedit.math.BlockVector3; @@ -16,10 +17,32 @@ public class NMSAdapter implements FAWEPlatformAdapterImpl { public static int createPalette( int[] blockToPalette, int[] paletteToBlock, int[] blocksCopy, - int[] num_palette_buffer, char[] set, Map ticking_blocks, boolean fastmode + int[] num_palette_buffer, char[] set, Map ticking_blocks, boolean fastmode, + CachedBukkitAdapter adapter ) { int air = 0; int num_palette = 0; + for (int i = 0; i < 4096; i++) { + char ordinal = set[i]; + if (ordinal == BlockID.__RESERVED__) { + ordinal = BlockID.AIR; + } + int palette = blockToPalette[ordinal]; + if (palette == Integer.MAX_VALUE) { + blockToPalette[ordinal] = num_palette; + paletteToBlock[num_palette] = ordinal; + num_palette++; + } + } + 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); + } char lastOrdinal = BlockID.__RESERVED__; boolean lastticking = false; boolean tick_placed = Settings.IMP.EXPERIMENTAL.ALLOW_TICK_PLACED; @@ -55,11 +78,6 @@ public class NMSAdapter implements FAWEPlatformAdapterImpl { } } int palette = blockToPalette[ordinal]; - if (palette == Integer.MAX_VALUE) { - blockToPalette[ordinal] = palette = num_palette; - paletteToBlock[num_palette] = ordinal; - num_palette++; - } blocksCopy[i] = palette; } num_palette_buffer[0] = num_palette; @@ -69,11 +87,39 @@ public class NMSAdapter implements FAWEPlatformAdapterImpl { public static int createPalette( int layer, int[] blockToPalette, int[] paletteToBlock, int[] blocksCopy, int[] num_palette_buffer, Function get, char[] set, - Map ticking_blocks, boolean fastmode + Map ticking_blocks, boolean fastmode, + CachedBukkitAdapter adapter ) { int air = 0; int num_palette = 0; char[] getArr = null; + for (int i = 0; i < 4096; i++) { + char ordinal = set[i]; + if (ordinal == BlockID.__RESERVED__) { + if (getArr == null) { + getArr = get.apply(layer); + } + ordinal = getArr[i]; + if (ordinal == BlockID.__RESERVED__) { + ordinal = BlockID.AIR; + } + } + int palette = blockToPalette[ordinal]; + if (palette == Integer.MAX_VALUE) { + blockToPalette[ordinal] = num_palette; + paletteToBlock[num_palette] = ordinal; + num_palette++; + } + } + 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); + } char lastOrdinal = BlockID.__RESERVED__; boolean lastticking = false; boolean tick_placed = Settings.IMP.EXPERIMENTAL.ALLOW_TICK_PLACED; @@ -143,54 +189,8 @@ public class NMSAdapter implements FAWEPlatformAdapterImpl { } } int palette = blockToPalette[ordinal]; - if (palette == Integer.MAX_VALUE) { - blockToPalette[ordinal] = palette = num_palette; - paletteToBlock[num_palette] = ordinal; - num_palette++; - } blocksCopy[i] = palette; } - //Keeping this here for reference. - //if (setblocks != 4096) { - // char[] getArr = get.apply(layer); - // for (i = setblocks; i < 4096; i++) { - // char ordinal = set[i]; - // switch (ordinal) { - // case BlockID.__RESERVED__: - // ordinal = getArr[i]; - // switch (ordinal) { - // case BlockID.__RESERVED__: - // ordinal = BlockID.AIR; - // case BlockID.AIR: - // case BlockID.CAVE_AIR: - // case BlockID.VOID_AIR: - // air++; - // break; - // default: - // BlockState state = BlockState.getFromOrdinal(ordinal); - // if (state.getMaterial().isTicksRandomly()) { - // ticking_blocks - // .put(BlockVector3.at(i & 15, (i >> 8) & 15, (i >> 4) & 15), - // WorldEditPlugin.getInstance().getBukkitImplAdapter() - // .getInternalBlockStateId(state).orElse(0)); - // } - // set[i] = ordinal; - // } - // break; - // case BlockID.AIR: - // case BlockID.CAVE_AIR: - // case BlockID.VOID_AIR: - // air++; - // } - // int palette = blockToPalette[ordinal]; - // if (palette == Integer.MAX_VALUE) { - // blockToPalette[ordinal] = palette = num_palette; - // paletteToBlock[num_palette] = ordinal; - // num_palette++; - // } - // blocksCopy[i] = palette; - // } - //} num_palette_buffer[0] = num_palette; return air; 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 3f5462a6b..6dc63f179 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 @@ -22,6 +22,16 @@ public class SimpleBukkitAdapter extends CachedBukkitAdapter { return true; } + @Override + protected char[] getIbdToStateOrdinal() { + return new char[Character.MAX_VALUE + 1]; + } + + @Override + protected int[] getOrdinalToIbdID() { + return new int[Character.MAX_VALUE + 1]; + } + /** * Create a Bukkit BlockData from a WorldEdit BlockStateHolder * diff --git a/worldedit-bukkit/src/main/resources/worldedit-adapters.jar b/worldedit-bukkit/src/main/resources/worldedit-adapters.jar index 1a7e0ebe1..bcbb6eeba 100644 Binary files a/worldedit-bukkit/src/main/resources/worldedit-adapters.jar and b/worldedit-bukkit/src/main/resources/worldedit-adapters.jar differ