use of net.minecraft.world.level.chunk.HashMapPalette in project FastAsyncWorldEdit by IntellectualSites.
the class PaperweightPlatformAdapter method newChunkSection.
public static LevelChunkSection newChunkSection(final int layer, final Function<Integer, char[]> get, char[] set, boolean fastmode, CachedBukkitAdapter adapter) {
if (set == null) {
return newChunkSection(layer);
}
final int[] blockToPalette = FaweCache.INSTANCE.BLOCK_TO_PALETTE.get();
final int[] paletteToBlock = FaweCache.INSTANCE.PALETTE_TO_BLOCK.get();
final long[] blockStates = FaweCache.INSTANCE.BLOCK_STATES.get();
final int[] blocksCopy = FaweCache.INSTANCE.SECTION_BLOCKS.get();
try {
int[] num_palette_buffer = new int[1];
Map<BlockVector3, Integer> ticking_blocks = new HashMap<>();
int air;
if (get == null) {
air = createPalette(blockToPalette, paletteToBlock, blocksCopy, num_palette_buffer, set, ticking_blocks, fastmode, adapter);
} else {
air = createPalette(layer, blockToPalette, paletteToBlock, blocksCopy, num_palette_buffer, get, set, ticking_blocks, fastmode, adapter);
}
int num_palette = num_palette_buffer[0];
// BlockStates
int bitsPerEntry = MathMan.log2nlz(num_palette - 1);
if (Settings.settings().PROTOCOL_SUPPORT_FIX || num_palette != 1) {
// Protocol support breaks <4 bits per entry
bitsPerEntry = Math.max(bitsPerEntry, 4);
} else {
// For some reason minecraft needs 4096 bits to store 0 entries
bitsPerEntry = Math.max(bitsPerEntry, 1);
}
if (bitsPerEntry > 8) {
bitsPerEntry = MathMan.log2nlz(Block.BLOCK_STATE_REGISTRY.size() - 1);
}
final int blocksPerLong = MathMan.floorZero((double) 64 / bitsPerEntry);
final int blockBitArrayEnd = MathMan.ceilZero((float) 4096 / blocksPerLong);
if (num_palette == 1) {
for (int i = 0; i < blockBitArrayEnd; i++) {
blockStates[i] = 0;
}
} else {
final BitArrayUnstretched bitArray = new BitArrayUnstretched(bitsPerEntry, 4096, blockStates);
bitArray.fromRaw(blocksCopy);
}
LevelChunkSection levelChunkSection = newChunkSection(layer);
// set palette & data bits
final PalettedContainer<net.minecraft.world.level.block.state.BlockState> dataPaletteBlocks = levelChunkSection.getStates();
// private DataPalette<T> h;
// protected DataBits a;
final long[] bits = Arrays.copyOfRange(blockStates, 0, blockBitArrayEnd);
final BitStorage nmsBits = new BitStorage(bitsPerEntry, 4096, bits);
final Palette<net.minecraft.world.level.block.state.BlockState> blockStatePalettedContainer;
if (bitsPerEntry <= 4) {
blockStatePalettedContainer = new LinearPalette<>(Block.BLOCK_STATE_REGISTRY, bitsPerEntry, dataPaletteBlocks, NbtUtils::readBlockState);
} else if (bitsPerEntry < 9) {
blockStatePalettedContainer = new HashMapPalette<>(Block.BLOCK_STATE_REGISTRY, bitsPerEntry, dataPaletteBlocks, NbtUtils::readBlockState, NbtUtils::writeBlockState);
} else {
blockStatePalettedContainer = LevelChunkSection.GLOBAL_BLOCKSTATE_PALETTE;
}
// set palette if required
if (bitsPerEntry < 9) {
for (int i = 0; i < num_palette; i++) {
final int ordinal = paletteToBlock[i];
blockToPalette[ordinal] = Integer.MAX_VALUE;
final BlockState state = BlockTypesCache.states[ordinal];
final net.minecraft.world.level.block.state.BlockState blockState = ((PaperweightBlockMaterial) state.getMaterial()).getState();
blockStatePalettedContainer.idFor(blockState);
}
}
try {
fieldStorage.set(dataPaletteBlocks, nmsBits);
fieldPalette.set(dataPaletteBlocks, blockStatePalettedContainer);
fieldBits.set(dataPaletteBlocks, bitsPerEntry);
setCount(ticking_blocks.size(), 4096 - air, levelChunkSection);
if (!fastmode) {
ticking_blocks.forEach((pos, ordinal) -> levelChunkSection.setBlockState(pos.getBlockX(), pos.getBlockY(), pos.getBlockZ(), Block.stateById(ordinal)));
}
} catch (final IllegalAccessException e) {
throw new RuntimeException(e);
}
return levelChunkSection;
} catch (final Throwable e) {
Arrays.fill(blockToPalette, Integer.MAX_VALUE);
throw e;
}
}
use of net.minecraft.world.level.chunk.HashMapPalette in project Create by Creators-of-Create.
the class Contraption method readBlocksCompound.
private void readBlocksCompound(Tag compound, Level world, boolean usePalettedDeserialization) {
HashMapPalette<BlockState> palette = null;
ListTag blockList;
if (usePalettedDeserialization) {
CompoundTag c = ((CompoundTag) compound);
palette = new HashMapPalette<>(GameData.getBlockStateIDMap(), 16, (i, s) -> {
throw new IllegalStateException("Palette Map index exceeded maximum");
});
ListTag list = c.getList("Palette", 10);
palette.values.clear();
for (int i = 0; i < list.size(); ++i) palette.values.add(NbtUtils.readBlockState(list.getCompound(i)));
blockList = c.getList("BlockList", 10);
} else {
blockList = (ListTag) compound;
}
HashMapPalette<BlockState> finalPalette = palette;
blockList.forEach(e -> {
CompoundTag c = (CompoundTag) e;
StructureBlockInfo info = usePalettedDeserialization ? readStructureBlockInfo(c, finalPalette) : legacyReadStructureBlockInfo(c);
this.blocks.put(info.pos, info);
if (world.isClientSide) {
Block block = info.state.getBlock();
CompoundTag tag = info.nbt;
MovementBehaviour movementBehaviour = AllMovementBehaviours.of(block);
if (tag == null)
return;
tag.putInt("x", info.pos.getX());
tag.putInt("y", info.pos.getY());
tag.putInt("z", info.pos.getZ());
BlockEntity te = BlockEntity.loadStatic(info.pos, info.state, tag);
if (te == null)
return;
te.setLevel(world);
if (te instanceof KineticTileEntity)
((KineticTileEntity) te).setSpeed(0);
te.getBlockState();
if (movementBehaviour == null || !movementBehaviour.hasSpecialInstancedRendering())
maybeInstancedTileEntities.add(te);
if (movementBehaviour != null && !movementBehaviour.renderAsNormalTileEntity())
return;
presentTileEntities.put(info.pos, te);
specialRenderedTileEntities.add(te);
}
});
}
use of net.minecraft.world.level.chunk.HashMapPalette in project Create by Creators-of-Create.
the class Contraption method writeBlocksCompound.
private CompoundTag writeBlocksCompound() {
CompoundTag compound = new CompoundTag();
HashMapPalette<BlockState> palette = new HashMapPalette<>(GameData.getBlockStateIDMap(), 16, (i, s) -> {
throw new IllegalStateException("Palette Map index exceeded maximum");
});
ListTag blockList = new ListTag();
for (StructureBlockInfo block : this.blocks.values()) {
int id = palette.idFor(block.state);
CompoundTag c = new CompoundTag();
c.putLong("Pos", block.pos.asLong());
c.putInt("State", id);
if (block.nbt != null)
c.put("Data", block.nbt);
blockList.add(c);
}
ListTag paletteNBT = new ListTag();
for (int i = 0; i < palette.getSize(); ++i) paletteNBT.add(NbtUtils.writeBlockState(palette.values.byId(i)));
compound.put("Palette", paletteNBT);
compound.put("BlockList", blockList);
return compound;
}
Aggregations