From 1a48546f0c00f2f982b707e97f67a497b6d91bef Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Fri, 1 Nov 2019 23:12:31 +0100 Subject: [PATCH] linear clipboard get block --- .../clipboard/CPUOptimizedClipboard.java | 20 +++-- .../clipboard/DiskOptimizedClipboard.java | 85 ++++++++++--------- .../object/clipboard/LinearClipboard.java | 3 + .../clipboard/MemoryOptimizedClipboard.java | 22 +++-- 4 files changed, 79 insertions(+), 51 deletions(-) diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/clipboard/CPUOptimizedClipboard.java b/worldedit-core/src/main/java/com/boydti/fawe/object/clipboard/CPUOptimizedClipboard.java index aedb8e8aa..9d0b6d8d1 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/object/clipboard/CPUOptimizedClipboard.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/object/clipboard/CPUOptimizedClipboard.java @@ -121,15 +121,25 @@ public class CPUOptimizedClipboard extends LinearClipboard { @Override public BaseBlock getFullBlock(int index) { - char ordinal = states[index]; - BlockState state = BlockState.getFromOrdinal(ordinal); - if (state.getMaterial().hasContainer()) { + BlockState block = getBlock(index); + if (block.getMaterial().hasContainer()) { CompoundTag nbt = getTag(index); if (nbt != null) { - return state.toBaseBlock(nbt); + return block.toBaseBlock(nbt); } } - return state.toBaseBlock(); + return block.toBaseBlock(); + } + + @Override + public BlockState getBlock(int index) { + char ordinal = states[index]; + return BlockState.getFromOrdinal(ordinal); + } + + @Override + public BlockState getBlock(int x, int y, int z) { + return getBlock(getIndex(x, y, z)); } @Override diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/clipboard/DiskOptimizedClipboard.java b/worldedit-core/src/main/java/com/boydti/fawe/object/clipboard/DiskOptimizedClipboard.java index 0624ab354..3effee5b4 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/object/clipboard/DiskOptimizedClipboard.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/object/clipboard/DiskOptimizedClipboard.java @@ -307,58 +307,63 @@ public class DiskOptimizedClipboard extends LinearClipboard implements Closeable @Override public BaseBlock getFullBlock(int x, int y, int z) { - try { - int index = HEADER_SIZE + (getIndex(x, y, z) << 1); - int combinedId = byteBuffer.getChar(index); - BlockState state = BlockState.getFromOrdinal(combinedId); - if (state.getMaterial().hasContainer() && !nbtMap.isEmpty()) { - CompoundTag nbt = nbtMap.get(new IntegerTrio(x, y, z)); - return state.toBaseBlock(nbt); + return toBaseBlock(getBlock(x, y, z), x, y, z); + } + + private BaseBlock toBaseBlock(BlockState state, int i) { + if (state.getMaterial().hasContainer() && !nbtMap.isEmpty()) { + CompoundTag nbt; + if (nbtMap.size() < 4) { + nbt = null; + for (Map.Entry entry : nbtMap.entrySet()) { + IntegerTrio key = entry.getKey(); + int index = getIndex(key.x, key.y, key.z); + if (index == i) { + nbt = entry.getValue(); + break; + } + } + } else { + int y = i / getArea(); + int newI = i - y * getArea(); + int z = newI / getWidth(); + int x = newI - z * getWidth(); + nbt = nbtMap.get(new IntegerTrio(x, y, z)); } - return state.toBaseBlock(); - } catch (IndexOutOfBoundsException ignore) { - } catch (Exception e) { - e.printStackTrace(); + return state.toBaseBlock(nbt); } - return BlockTypes.AIR.getDefaultState().toBaseBlock(); + return state.toBaseBlock(); + } + + private BaseBlock toBaseBlock(BlockState state, int x, int y, int z) { + if (state.getMaterial().hasContainer() && !nbtMap.isEmpty()) { + CompoundTag nbt = nbtMap.get(new IntegerTrio(x, y, z)); + return state.toBaseBlock(nbt); + } + return state.toBaseBlock(); } @Override public BaseBlock getFullBlock(int i) { + return toBaseBlock(getBlock(i), i); + } + + @Override + public BlockState getBlock(int index) { try { - int diskIndex = HEADER_SIZE + (i << 1); + int diskIndex = HEADER_SIZE + (index << 1); char ordinal = byteBuffer.getChar(diskIndex); - BlockState state = BlockState.getFromOrdinal(ordinal); - if (state.getMaterial().hasContainer() && !nbtMap.isEmpty()) { - CompoundTag nbt; - if (nbtMap.size() < 4) { - nbt = null; - for (Map.Entry entry : nbtMap.entrySet()) { - IntegerTrio key = entry.getKey(); - int index = getIndex(key.x, key.y, key.z); - if (index == i) { - nbt = entry.getValue(); - break; - } - } - } else { - // x + z * getWidth() + y * area; - int y = i / getArea(); - int newI = i - y * getArea(); - int z = newI / getWidth(); - int x = newI - z * getWidth(); - nbt = nbtMap.get(new IntegerTrio(x, y, z)); - } - if (nbt != null) { - return state.toBaseBlock(nbt); - } - } - return state.toBaseBlock(); + return BlockState.getFromOrdinal(ordinal); } catch (IndexOutOfBoundsException ignore) { } catch (Exception e) { e.printStackTrace(); } - return BlockTypes.AIR.getDefaultState().toBaseBlock(); + return BlockTypes.AIR.getDefaultState(); + } + + @Override + public BlockState getBlock(int x, int y, int z) { + return getBlock(getIndex(x, y, z)); } @Override diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/clipboard/LinearClipboard.java b/worldedit-core/src/main/java/com/boydti/fawe/object/clipboard/LinearClipboard.java index a7ffbd996..6effc0cfe 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/object/clipboard/LinearClipboard.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/object/clipboard/LinearClipboard.java @@ -10,6 +10,7 @@ import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.world.biome.BiomeType; import com.sk89q.worldedit.world.block.BaseBlock; +import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockStateHolder; import java.io.Closeable; @@ -29,6 +30,8 @@ public abstract class LinearClipboard extends SimpleClipboard implements Clipboa public abstract BaseBlock getFullBlock(int i); + public abstract BlockState getBlock(int i); + public abstract void setBiome(int index, BiomeType biome); public abstract BiomeType getBiome(int index); diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/clipboard/MemoryOptimizedClipboard.java b/worldedit-core/src/main/java/com/boydti/fawe/object/clipboard/MemoryOptimizedClipboard.java index 7f668991f..659374cc6 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/object/clipboard/MemoryOptimizedClipboard.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/object/clipboard/MemoryOptimizedClipboard.java @@ -16,6 +16,7 @@ import com.sk89q.worldedit.util.Location; import com.sk89q.worldedit.world.biome.BiomeType; import com.sk89q.worldedit.world.biome.BiomeTypes; import com.sk89q.worldedit.world.block.BaseBlock; +import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockStateHolder; import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.block.BlockTypes; @@ -227,16 +228,25 @@ public class MemoryOptimizedClipboard extends LinearClipboard { @Override public BaseBlock getFullBlock(int index) { - int combinedId = getOrdinal(index); - BlockType type = BlockTypes.getFromStateId(combinedId); - BaseBlock base = type.withStateId(combinedId).toBaseBlock(); - if (type.getMaterial().hasContainer()) { + BlockState block = getBlock(index); + if (block.getMaterial().hasContainer()) { CompoundTag nbt = getTag(index); if (nbt != null) { - return base.toBaseBlock(nbt); + return block.toBaseBlock(nbt); } } - return base; + return block.toBaseBlock(); + } + + @Override + public BlockState getBlock(int index) { + int ordinal = getOrdinal(index); + return BlockState.getFromOrdinal(ordinal); + } + + @Override + public BlockState getBlock(int x, int y, int z) { + return getBlock(getIndex(x, y, z)); } public int size() {