linear clipboard get block

This commit is contained in:
Jesse Boyd 2019-11-01 23:12:31 +01:00
parent 6de30f8ed4
commit 1a48546f0c
No known key found for this signature in database
GPG Key ID: 59F1DE6293AF6E1F
4 changed files with 79 additions and 51 deletions

View File

@ -121,15 +121,25 @@ public class CPUOptimizedClipboard extends LinearClipboard {
@Override @Override
public BaseBlock getFullBlock(int index) { public BaseBlock getFullBlock(int index) {
char ordinal = states[index]; BlockState block = getBlock(index);
BlockState state = BlockState.getFromOrdinal(ordinal); if (block.getMaterial().hasContainer()) {
if (state.getMaterial().hasContainer()) {
CompoundTag nbt = getTag(index); CompoundTag nbt = getTag(index);
if (nbt != null) { 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 @Override

View File

@ -307,58 +307,63 @@ public class DiskOptimizedClipboard extends LinearClipboard implements Closeable
@Override @Override
public BaseBlock getFullBlock(int x, int y, int z) { public BaseBlock getFullBlock(int x, int y, int z) {
try { return toBaseBlock(getBlock(x, y, z), x, y, z);
int index = HEADER_SIZE + (getIndex(x, y, z) << 1); }
int combinedId = byteBuffer.getChar(index);
BlockState state = BlockState.getFromOrdinal(combinedId); private BaseBlock toBaseBlock(BlockState state, int i) {
if (state.getMaterial().hasContainer() && !nbtMap.isEmpty()) { if (state.getMaterial().hasContainer() && !nbtMap.isEmpty()) {
CompoundTag nbt = nbtMap.get(new IntegerTrio(x, y, z)); CompoundTag nbt;
return state.toBaseBlock(nbt); if (nbtMap.size() < 4) {
nbt = null;
for (Map.Entry<IntegerTrio, CompoundTag> 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(); return state.toBaseBlock(nbt);
} catch (IndexOutOfBoundsException ignore) {
} catch (Exception e) {
e.printStackTrace();
} }
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 @Override
public BaseBlock getFullBlock(int i) { public BaseBlock getFullBlock(int i) {
return toBaseBlock(getBlock(i), i);
}
@Override
public BlockState getBlock(int index) {
try { try {
int diskIndex = HEADER_SIZE + (i << 1); int diskIndex = HEADER_SIZE + (index << 1);
char ordinal = byteBuffer.getChar(diskIndex); char ordinal = byteBuffer.getChar(diskIndex);
BlockState state = BlockState.getFromOrdinal(ordinal); return BlockState.getFromOrdinal(ordinal);
if (state.getMaterial().hasContainer() && !nbtMap.isEmpty()) {
CompoundTag nbt;
if (nbtMap.size() < 4) {
nbt = null;
for (Map.Entry<IntegerTrio, CompoundTag> 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();
} catch (IndexOutOfBoundsException ignore) { } catch (IndexOutOfBoundsException ignore) {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); 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 @Override

View File

@ -10,6 +10,7 @@ import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.world.biome.BiomeType; import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BaseBlock; 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.BlockStateHolder;
import java.io.Closeable; import java.io.Closeable;
@ -29,6 +30,8 @@ public abstract class LinearClipboard extends SimpleClipboard implements Clipboa
public abstract BaseBlock getFullBlock(int i); public abstract BaseBlock getFullBlock(int i);
public abstract BlockState getBlock(int i);
public abstract void setBiome(int index, BiomeType biome); public abstract void setBiome(int index, BiomeType biome);
public abstract BiomeType getBiome(int index); public abstract BiomeType getBiome(int index);

View File

@ -16,6 +16,7 @@ import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.world.biome.BiomeType; import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.biome.BiomeTypes; import com.sk89q.worldedit.world.biome.BiomeTypes;
import com.sk89q.worldedit.world.block.BaseBlock; 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.BlockStateHolder;
import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes; import com.sk89q.worldedit.world.block.BlockTypes;
@ -227,16 +228,25 @@ public class MemoryOptimizedClipboard extends LinearClipboard {
@Override @Override
public BaseBlock getFullBlock(int index) { public BaseBlock getFullBlock(int index) {
int combinedId = getOrdinal(index); BlockState block = getBlock(index);
BlockType type = BlockTypes.getFromStateId(combinedId); if (block.getMaterial().hasContainer()) {
BaseBlock base = type.withStateId(combinedId).toBaseBlock();
if (type.getMaterial().hasContainer()) {
CompoundTag nbt = getTag(index); CompoundTag nbt = getTag(index);
if (nbt != null) { 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() { public int size() {