Search in sources :

Example 1 with HashMapPalette

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;
    }
}
Also used : HashMap(java.util.HashMap) BitArrayUnstretched(com.fastasyncworldedit.core.math.BitArrayUnstretched) HashMapPalette(net.minecraft.world.level.chunk.HashMapPalette) NbtUtils(net.minecraft.nbt.NbtUtils) BlockVector3(com.sk89q.worldedit.math.BlockVector3) BlockState(com.sk89q.worldedit.world.block.BlockState) LevelChunkSection(net.minecraft.world.level.chunk.LevelChunkSection) BitStorage(net.minecraft.util.BitStorage)

Example 2 with HashMapPalette

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);
        }
    });
}
Also used : BeltBlock(com.simibubi.create.content.contraptions.relays.belt.BeltBlock) Arrays(java.util.Arrays) AABB(net.minecraft.world.phys.AABB) SeatBlock(com.simibubi.create.content.contraptions.components.actors.SeatBlock) DebugPackets(net.minecraft.network.protocol.game.DebugPackets) Dist(net.minecraftforge.api.distmarker.Dist) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) IItemHandlerModifiable(net.minecraftforge.items.IItemHandlerModifiable) CombinedInvWrapper(net.minecraftforge.items.wrapper.CombinedInvWrapper) NbtUtils(net.minecraft.nbt.NbtUtils) ChestBlock(net.minecraft.world.level.block.ChestBlock) SimpleWaterloggedBlock(net.minecraft.world.level.block.SimpleWaterloggedBlock) Set(java.util.Set) PoiType(net.minecraft.world.entity.ai.village.poi.PoiType) BooleanOp(net.minecraft.world.phys.shapes.BooleanOp) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) AllInteractionBehaviours(com.simibubi.create.AllInteractionBehaviours) WindmillBearingBlock(com.simibubi.create.content.contraptions.components.structureMovement.bearing.WindmillBearingBlock) MechanicalPistonHeadBlock(com.simibubi.create.content.contraptions.components.structureMovement.piston.MechanicalPistonHeadBlock) FluidState(net.minecraft.world.level.material.FluidState) PulleyBlock(com.simibubi.create.content.contraptions.components.structureMovement.pulley.PulleyBlock) FluidStack(net.minecraftforge.fluids.FluidStack) ItemStack(net.minecraft.world.item.ItemStack) VoxelShape(net.minecraft.world.phys.shapes.VoxelShape) GantryShaftBlock(com.simibubi.create.content.contraptions.relays.advanced.GantryShaftBlock) ICoordinate(com.simibubi.create.foundation.utility.ICoordinate) OnlyIn(net.minecraftforge.api.distmarker.OnlyIn) BlockState(net.minecraft.world.level.block.state.BlockState) CreativeCrateTileEntity(com.simibubi.create.content.logistics.block.inventories.CreativeCrateTileEntity) FilteringBehaviour(com.simibubi.create.foundation.tileEntity.behaviour.filtering.FilteringBehaviour) ArrayList(java.util.ArrayList) StickerBlock(com.simibubi.create.content.contraptions.components.structureMovement.chassis.StickerBlock) BiConsumer(java.util.function.BiConsumer) BlockFace(com.simibubi.create.foundation.utility.BlockFace) StructureBlockInfo(net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate.StructureBlockInfo) Nullable(javax.annotation.Nullable) SeatEntity(com.simibubi.create.content.contraptions.components.actors.SeatEntity) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) Iterate(com.simibubi.create.foundation.utility.Iterate) AllMovementBehaviours(com.simibubi.create.AllMovementBehaviours) MechanicalBearingBlock(com.simibubi.create.content.contraptions.components.structureMovement.bearing.MechanicalBearingBlock) CombinedTankWrapper(com.simibubi.create.foundation.fluid.CombinedTankWrapper) NBTHelper(com.simibubi.create.foundation.utility.NBTHelper) FluidTankTileEntity(com.simibubi.create.content.contraptions.fluids.tank.FluidTankTileEntity) ListTag(net.minecraft.nbt.ListTag) SuperGlueEntity(com.simibubi.create.content.contraptions.components.structureMovement.glue.SuperGlueEntity) EmptyLighter(com.simibubi.create.content.contraptions.components.structureMovement.render.EmptyLighter) IMultiTileContainer(com.simibubi.create.foundation.tileEntity.IMultiTileContainer) Direction(net.minecraft.core.Direction) RedstoneContactBlock(com.simibubi.create.content.logistics.block.redstone.RedstoneContactBlock) PressurePlateBlock(net.minecraft.world.level.block.PressurePlateBlock) FluidTank(net.minecraftforge.fluids.capability.templates.FluidTank) GantryCarriageBlock(com.simibubi.create.content.contraptions.components.structureMovement.gantry.GantryCarriageBlock) Rotation(net.minecraft.world.level.block.Rotation) MutablePair(org.apache.commons.lang3.tuple.MutablePair) GameData(net.minecraftforge.registries.GameData) RopeBlock(com.simibubi.create.content.contraptions.components.structureMovement.pulley.PulleyBlock.RopeBlock) MechanicalPistonBlock(com.simibubi.create.content.contraptions.components.structureMovement.piston.MechanicalPistonBlock) AllConfigs(com.simibubi.create.foundation.config.AllConfigs) PulleyTileEntity(com.simibubi.create.content.contraptions.components.structureMovement.pulley.PulleyTileEntity) UniqueLinkedList(com.simibubi.create.foundation.utility.UniqueLinkedList) BlockStateProperties(net.minecraft.world.level.block.state.properties.BlockStateProperties) NBTProcessors(com.simibubi.create.foundation.utility.NBTProcessors) UUID(java.util.UUID) StabilizedContraption(com.simibubi.create.content.contraptions.components.structureMovement.bearing.StabilizedContraption) MagnetBlock(com.simibubi.create.content.contraptions.components.structureMovement.pulley.PulleyBlock.MagnetBlock) Collectors(java.util.stream.Collectors) Blocks(net.minecraft.world.level.block.Blocks) Objects(java.util.Objects) AbstractChassisBlock(com.simibubi.create.content.contraptions.components.structureMovement.chassis.AbstractChassisBlock) PistonExtensionPoleBlock(com.simibubi.create.content.contraptions.components.structureMovement.piston.PistonExtensionPoleBlock) List(java.util.List) CompoundTag(net.minecraft.nbt.CompoundTag) BlockPos(net.minecraft.core.BlockPos) HashMapPalette(net.minecraft.world.level.chunk.HashMapPalette) Entry(java.util.Map.Entry) Optional(java.util.Optional) LevelAccessor(net.minecraft.world.level.LevelAccessor) IFluidTank(net.minecraftforge.fluids.IFluidTank) Queue(java.util.Queue) Shapes(net.minecraft.world.phys.shapes.Shapes) Level(net.minecraft.world.level.Level) Tag(net.minecraft.nbt.Tag) PistonType(net.minecraft.world.level.block.state.properties.PistonType) KineticTileEntity(com.simibubi.create.content.contraptions.base.KineticTileEntity) FluidAction(net.minecraftforge.fluids.capability.IFluidHandler.FluidAction) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ServerLevel(net.minecraft.server.level.ServerLevel) HashSet(java.util.HashSet) ItemVaultTileEntity(com.simibubi.create.content.logistics.block.vault.ItemVaultTileEntity) Axis(net.minecraft.core.Direction.Axis) IRotate(com.simibubi.create.content.contraptions.base.IRotate) PushReaction(net.minecraft.world.level.material.PushReaction) ButtonBlock(net.minecraft.world.level.block.ButtonBlock) Fluids(net.minecraft.world.level.material.Fluids) AllBlocks(com.simibubi.create.AllBlocks) ChestType(net.minecraft.world.level.block.state.properties.ChestType) Iterator(java.util.Iterator) SuperGlueHandler(com.simibubi.create.content.contraptions.components.structureMovement.glue.SuperGlueHandler) MechanicalPistonBlock.isPistonHead(com.simibubi.create.content.contraptions.components.structureMovement.piston.MechanicalPistonBlock.isPistonHead) PistonState(com.simibubi.create.content.contraptions.components.structureMovement.piston.MechanicalPistonBlock.PistonState) Entity(net.minecraft.world.entity.Entity) MechanicalPistonBlock.isExtensionPole(com.simibubi.create.content.contraptions.components.structureMovement.piston.MechanicalPistonBlock.isExtensionPole) ChassisTileEntity(com.simibubi.create.content.contraptions.components.structureMovement.chassis.ChassisTileEntity) Vec3(net.minecraft.world.phys.Vec3) Block(net.minecraft.world.level.block.Block) KineticTileEntity(com.simibubi.create.content.contraptions.base.KineticTileEntity) ListTag(net.minecraft.nbt.ListTag) BlockState(net.minecraft.world.level.block.state.BlockState) BeltBlock(com.simibubi.create.content.contraptions.relays.belt.BeltBlock) SeatBlock(com.simibubi.create.content.contraptions.components.actors.SeatBlock) ChestBlock(net.minecraft.world.level.block.ChestBlock) SimpleWaterloggedBlock(net.minecraft.world.level.block.SimpleWaterloggedBlock) WindmillBearingBlock(com.simibubi.create.content.contraptions.components.structureMovement.bearing.WindmillBearingBlock) MechanicalPistonHeadBlock(com.simibubi.create.content.contraptions.components.structureMovement.piston.MechanicalPistonHeadBlock) PulleyBlock(com.simibubi.create.content.contraptions.components.structureMovement.pulley.PulleyBlock) GantryShaftBlock(com.simibubi.create.content.contraptions.relays.advanced.GantryShaftBlock) StickerBlock(com.simibubi.create.content.contraptions.components.structureMovement.chassis.StickerBlock) MechanicalBearingBlock(com.simibubi.create.content.contraptions.components.structureMovement.bearing.MechanicalBearingBlock) RedstoneContactBlock(com.simibubi.create.content.logistics.block.redstone.RedstoneContactBlock) PressurePlateBlock(net.minecraft.world.level.block.PressurePlateBlock) GantryCarriageBlock(com.simibubi.create.content.contraptions.components.structureMovement.gantry.GantryCarriageBlock) RopeBlock(com.simibubi.create.content.contraptions.components.structureMovement.pulley.PulleyBlock.RopeBlock) MechanicalPistonBlock(com.simibubi.create.content.contraptions.components.structureMovement.piston.MechanicalPistonBlock) MagnetBlock(com.simibubi.create.content.contraptions.components.structureMovement.pulley.PulleyBlock.MagnetBlock) AbstractChassisBlock(com.simibubi.create.content.contraptions.components.structureMovement.chassis.AbstractChassisBlock) PistonExtensionPoleBlock(com.simibubi.create.content.contraptions.components.structureMovement.piston.PistonExtensionPoleBlock) ButtonBlock(net.minecraft.world.level.block.ButtonBlock) Block(net.minecraft.world.level.block.Block) StructureBlockInfo(net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate.StructureBlockInfo) CompoundTag(net.minecraft.nbt.CompoundTag) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Example 3 with HashMapPalette

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;
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) HashMapPalette(net.minecraft.world.level.chunk.HashMapPalette) StructureBlockInfo(net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate.StructureBlockInfo) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

HashMapPalette (net.minecraft.world.level.chunk.HashMapPalette)3 HashMap (java.util.HashMap)2 NbtUtils (net.minecraft.nbt.NbtUtils)2 BitArrayUnstretched (com.fastasyncworldedit.core.math.BitArrayUnstretched)1 AllBlocks (com.simibubi.create.AllBlocks)1 AllInteractionBehaviours (com.simibubi.create.AllInteractionBehaviours)1 AllMovementBehaviours (com.simibubi.create.AllMovementBehaviours)1 IRotate (com.simibubi.create.content.contraptions.base.IRotate)1 KineticTileEntity (com.simibubi.create.content.contraptions.base.KineticTileEntity)1 SeatBlock (com.simibubi.create.content.contraptions.components.actors.SeatBlock)1 SeatEntity (com.simibubi.create.content.contraptions.components.actors.SeatEntity)1 MechanicalBearingBlock (com.simibubi.create.content.contraptions.components.structureMovement.bearing.MechanicalBearingBlock)1 StabilizedContraption (com.simibubi.create.content.contraptions.components.structureMovement.bearing.StabilizedContraption)1 WindmillBearingBlock (com.simibubi.create.content.contraptions.components.structureMovement.bearing.WindmillBearingBlock)1 AbstractChassisBlock (com.simibubi.create.content.contraptions.components.structureMovement.chassis.AbstractChassisBlock)1 ChassisTileEntity (com.simibubi.create.content.contraptions.components.structureMovement.chassis.ChassisTileEntity)1 StickerBlock (com.simibubi.create.content.contraptions.components.structureMovement.chassis.StickerBlock)1 GantryCarriageBlock (com.simibubi.create.content.contraptions.components.structureMovement.gantry.GantryCarriageBlock)1 SuperGlueEntity (com.simibubi.create.content.contraptions.components.structureMovement.glue.SuperGlueEntity)1 SuperGlueHandler (com.simibubi.create.content.contraptions.components.structureMovement.glue.SuperGlueHandler)1