Search in sources :

Example 16 with ChunkSection

use of net.minecraft.world.chunk.ChunkSection in project LoliServer by Loli-Server.

the class CraftChunk method contains.

@Override
public boolean contains(BlockData block) {
    Preconditions.checkArgument(block != null, "Block cannot be null");
    Predicate<net.minecraft.block.BlockState> nms = Predicates.equalTo(((CraftBlockData) block).getState());
    for (ChunkSection section : getHandle().getSections()) {
        if (section != null && section.getStates().maybeHas(nms)) {
            return true;
        }
    }
    return false;
}
Also used : BlockState(org.bukkit.block.BlockState) ChunkSection(net.minecraft.world.chunk.ChunkSection)

Example 17 with ChunkSection

use of net.minecraft.world.chunk.ChunkSection in project Magma-1.16.x by magmafoundation.

the class CraftChunk method getChunkSnapshot.

@Override
public ChunkSnapshot getChunkSnapshot(boolean includeMaxBlockY, boolean includeBiome, boolean includeBiomeTempRain) {
    net.minecraft.world.chunk.Chunk chunk = getHandle();
    ChunkSection[] cs = chunk.getSections();
    PalettedContainer[] sectionBlockIDs = new PalettedContainer[cs.length];
    byte[][] sectionSkyLights = new byte[cs.length][];
    byte[][] sectionEmitLights = new byte[cs.length][];
    boolean[] sectionEmpty = new boolean[cs.length];
    for (int i = 0; i < cs.length; i++) {
        if (cs[i] == null) {
            // Section is empty?
            sectionBlockIDs[i] = emptyBlockIDs;
            sectionSkyLights[i] = emptyLight;
            sectionEmitLights[i] = emptyLight;
            sectionEmpty[i] = true;
        } else {
            // Not empty
            CompoundNBT data = new CompoundNBT();
            cs[i].getStates().write(data, "Palette", "BlockStates");
            // TODO: snapshot whole ChunkSection
            PalettedContainer blockids = new PalettedContainer<net.minecraft.block.BlockState>(ChunkSection.GLOBAL_BLOCKSTATE_PALETTE, net.minecraft.block.Block.BLOCK_STATE_REGISTRY, NBTUtil::readBlockState, NBTUtil::writeBlockState, Blocks.AIR.defaultBlockState());
            blockids.read(data.getList("Palette", CraftMagicNumbers.NBT.TAG_COMPOUND), data.getLongArray("BlockStates"));
            sectionBlockIDs[i] = blockids;
            WorldLightManager lightengine = chunk.level.getChunkSource().getLightEngine();
            NibbleArray skyLightArray = lightengine.getLayerListener(LightType.SKY).getDataLayerData(SectionPos.of(x, i, z));
            if (skyLightArray == null) {
                sectionSkyLights[i] = emptyLight;
            } else {
                sectionSkyLights[i] = new byte[2048];
                System.arraycopy(skyLightArray.getData(), 0, sectionSkyLights[i], 0, 2048);
            }
            NibbleArray emitLightArray = lightengine.getLayerListener(LightType.BLOCK).getDataLayerData(SectionPos.of(x, i, z));
            if (emitLightArray == null) {
                sectionEmitLights[i] = emptyLight;
            } else {
                sectionEmitLights[i] = new byte[2048];
                System.arraycopy(emitLightArray.getData(), 0, sectionEmitLights[i], 0, 2048);
            }
        }
    }
    Heightmap hmap = null;
    if (includeMaxBlockY) {
        hmap = new Heightmap(null, Heightmap.Type.MOTION_BLOCKING);
        hmap.setRawData(chunk.heightmaps.get(Heightmap.Type.MOTION_BLOCKING).getRawData());
    }
    BiomeContainer biome = null;
    if (includeBiome || includeBiomeTempRain) {
        biome = chunk.getBiomes();
    }
    World world = getWorld();
    return new CraftChunkSnapshot(getX(), getZ(), world.getName(), world.getFullTime(), sectionBlockIDs, sectionSkyLights, sectionEmitLights, sectionEmpty, hmap, biome);
}
Also used : Heightmap(net.minecraft.world.gen.Heightmap) CompoundNBT(net.minecraft.nbt.CompoundNBT) NBTUtil(net.minecraft.nbt.NBTUtil) ServerWorld(net.minecraft.world.server.ServerWorld) World(org.bukkit.World) NibbleArray(net.minecraft.world.chunk.NibbleArray) WorldLightManager(net.minecraft.world.lighting.WorldLightManager) ChunkSection(net.minecraft.world.chunk.ChunkSection) BiomeContainer(net.minecraft.world.biome.BiomeContainer) PalettedContainer(net.minecraft.util.palette.PalettedContainer)

Example 18 with ChunkSection

use of net.minecraft.world.chunk.ChunkSection in project Valkyrien-Skies-2 by ValkyrienSkies.

the class MixinClientChunkManager method preLoadChunkFromPacket.

@Inject(method = "loadChunkFromPacket", at = @At("HEAD"), cancellable = true)
private void preLoadChunkFromPacket(final int x, final int z, final BiomeArray biomes, final PacketByteBuf buf, final CompoundTag tag, final int verticalStripBitmask, final boolean complete, final CallbackInfoReturnable<WorldChunk> cir) {
    final ClientChunkManagerClientChunkMapAccessor clientChunkMapAccessor = ClientChunkManagerClientChunkMapAccessor.class.cast(chunks);
    if (!clientChunkMapAccessor.callIsInRadius(x, z)) {
        if (ChunkAllocator.isChunkInShipyard(x, z)) {
            final long chunkPosLong = ChunkPos.toLong(x, z);
            final WorldChunk worldChunk = new WorldChunk(this.world, new ChunkPos(x, z), biomes);
            worldChunk.loadFromPacket(biomes, buf, tag, verticalStripBitmask);
            shipChunks.put(chunkPosLong, worldChunk);
            final ChunkSection[] chunkSections = worldChunk.getSectionArray();
            final LightingProvider lightingProvider = this.getLightingProvider();
            lightingProvider.setColumnEnabled(new ChunkPos(x, z), true);
            for (int j = 0; j < chunkSections.length; ++j) {
                final ChunkSection chunkSection = chunkSections[j];
                lightingProvider.setSectionStatus(ChunkSectionPos.from(x, j, z), ChunkSection.isEmpty(chunkSection));
            }
            this.world.resetChunkColor(x, z);
            cir.setReturnValue(worldChunk);
        }
    }
}
Also used : WorldChunk(net.minecraft.world.chunk.WorldChunk) LightingProvider(net.minecraft.world.chunk.light.LightingProvider) ChunkPos(net.minecraft.util.math.ChunkPos) ChunkSection(net.minecraft.world.chunk.ChunkSection) ClientChunkManagerClientChunkMapAccessor(org.valkyrienskies.mod.mixin.accessors.client.world.ClientChunkManagerClientChunkMapAccessor) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 19 with ChunkSection

use of net.minecraft.world.chunk.ChunkSection in project Valkyrien-Skies-2 by ValkyrienSkies.

the class MixinServerWorld method postTick.

@Inject(method = "tick", at = @At("TAIL"))
private void postTick(final BooleanSupplier shouldKeepTicking, final CallbackInfo ci) {
    // First update the IPlayer wrapper list
    updateVSPlayerWrappers();
    // Find newly loaded chunks
    final List<ChunkHolder> loadedChunksList = Lists.newArrayList(((ThreadedAnvilChunkStorageAccessor) serverChunkManager.threadedAnvilChunkStorage).callEntryIterator());
    // Create DenseVoxelShapeUpdate for new loaded chunks
    final List<IVoxelShapeUpdate> newLoadedChunks = new ArrayList<>();
    for (final ChunkHolder chunkHolder : loadedChunksList) {
        final Optional<WorldChunk> worldChunkOptional = chunkHolder.getTickingFuture().getNow(ChunkHolder.UNLOADED_WORLD_CHUNK).left();
        if (worldChunkOptional.isPresent()) {
            final WorldChunk worldChunk = worldChunkOptional.get();
            final int chunkX = worldChunk.getPos().x;
            final int chunkZ = worldChunk.getPos().z;
            final ChunkSection[] chunkSections = worldChunk.getSectionArray();
            // For now just assume chunkY goes from 0 to 16
            for (int chunkY = 0; chunkY < 16; chunkY++) {
                final ChunkSection chunkSection = chunkSections[chunkY];
                final Vector3ic chunkPos = new Vector3i(chunkX, chunkY, chunkZ);
                if (!knownChunkRegions.contains(chunkPos)) {
                    if (chunkSection != null) {
                        // Add this chunk to the ground rigid body
                        final DenseVoxelShapeUpdate voxelShapeUpdate = VSGameUtilsKt.toDenseVoxelUpdate(chunkSection, chunkPos);
                        newLoadedChunks.add(voxelShapeUpdate);
                    } else {
                        final EmptyVoxelShapeUpdate emptyVoxelShapeUpdate = new EmptyVoxelShapeUpdate(chunkPos.x(), chunkPos.y(), chunkPos.z(), false, false);
                        newLoadedChunks.add(emptyVoxelShapeUpdate);
                    }
                    knownChunkRegions.add(chunkPos);
                }
            }
        }
    }
    // Then tick the ship world
    shipObjectWorld.tickShips(newLoadedChunks);
    // Send ships to clients
    final IVSPacket shipDataPacket = VSPacketShipDataList.Companion.create(shipObjectWorld.getQueryableShipData().iterator());
    for (final ServerPlayerEntity playerEntity : players) {
        VSNetworking.shipDataPacketToClientSender.sendToClient(shipDataPacket, playerEntity);
    }
    // Then determine the chunk watch/unwatch tasks, and then execute them
    final ImmutableList<IPlayer> playersToTick = ImmutableList.copyOf(vsPlayerWrappers.values());
    final Pair<Spliterator<ChunkWatchTask>, Spliterator<ChunkUnwatchTask>> chunkWatchAndUnwatchTasksPair = shipObjectWorld.tickShipChunkLoading(playersToTick);
    // Use Spliterator instead of iterators so that we can multi thread the execution of these tasks
    final Spliterator<ChunkWatchTask> chunkWatchTasks = chunkWatchAndUnwatchTasksPair.getFirst();
    final Spliterator<ChunkUnwatchTask> chunkUnwatchTasks = chunkWatchAndUnwatchTasksPair.getSecond();
    // But for now just do it single threaded
    chunkWatchTasks.forEachRemaining(chunkWatchTask -> {
        System.out.println("Watch task for " + chunkWatchTask.getChunkX() + " : " + chunkWatchTask.getChunkZ());
        final Packet<?>[] chunkPacketBuffer = new Packet[2];
        final ChunkPos chunkPos = new ChunkPos(chunkWatchTask.getChunkX(), chunkWatchTask.getChunkZ());
        // TODO: Move this somewhere else
        serverChunkManager.setChunkForced(chunkPos, true);
        for (final IPlayer player : chunkWatchTask.getPlayersNeedWatching()) {
            final MinecraftPlayer minecraftPlayer = (MinecraftPlayer) player;
            final ServerPlayerEntity serverPlayerEntity = (ServerPlayerEntity) minecraftPlayer.getPlayerEntityReference().get();
            if (serverPlayerEntity != null) {
                ((ThreadedAnvilChunkStorageAccessor) serverChunkManager.threadedAnvilChunkStorage).callSendWatchPackets(serverPlayerEntity, chunkPos, chunkPacketBuffer, false, true);
            }
        }
        chunkWatchTask.onExecuteChunkWatchTask();
    });
    chunkUnwatchTasks.forEachRemaining(chunkUnwatchTask -> {
        System.out.println("Unwatch task for " + chunkUnwatchTask.getChunkX() + " : " + chunkUnwatchTask.getChunkZ());
        chunkUnwatchTask.onExecuteChunkUnwatchTask();
    });
}
Also used : IPlayer(org.valkyrienskies.core.game.IPlayer) ChunkWatchTask(org.valkyrienskies.core.chunk_tracking.ChunkWatchTask) ChunkHolder(net.minecraft.server.world.ChunkHolder) EmptyVoxelShapeUpdate(org.valkyrienskies.physics_api.voxel_updates.EmptyVoxelShapeUpdate) ArrayList(java.util.ArrayList) ChunkUnwatchTask(org.valkyrienskies.core.chunk_tracking.ChunkUnwatchTask) WorldChunk(net.minecraft.world.chunk.WorldChunk) Vector3ic(org.joml.Vector3ic) MinecraftPlayer(org.valkyrienskies.mod.common.util.MinecraftPlayer) IVoxelShapeUpdate(org.valkyrienskies.physics_api.voxel_updates.IVoxelShapeUpdate) ChunkPos(net.minecraft.util.math.ChunkPos) ThreadedAnvilChunkStorageAccessor(org.valkyrienskies.mod.mixin.accessors.server.world.ThreadedAnvilChunkStorageAccessor) Spliterator(java.util.Spliterator) DenseVoxelShapeUpdate(org.valkyrienskies.physics_api.voxel_updates.DenseVoxelShapeUpdate) Packet(net.minecraft.network.Packet) IVSPacket(org.valkyrienskies.core.networking.IVSPacket) ServerPlayerEntity(net.minecraft.server.network.ServerPlayerEntity) Vector3i(org.joml.Vector3i) IVSPacket(org.valkyrienskies.core.networking.IVSPacket) ChunkSection(net.minecraft.world.chunk.ChunkSection) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 20 with ChunkSection

use of net.minecraft.world.chunk.ChunkSection in project ChocolateQuestRepoured by TeamChocoQuest.

the class GeneratableDungeon method tryGeneratePart.

private void tryGeneratePart(World world) {
    long t = System.nanoTime();
    for (IDungeonPart part : this.parts) {
        part.generate(world, this);
    }
    for (ChunkInfo chunkInfo : this.chunkInfoMap.values()) {
        Chunk chunk = world.getChunk(chunkInfo.getChunkX(), chunkInfo.getChunkZ());
        if (world.provider.hasSkyLight()) {
            for (int chunkY = chunkInfo.topMarked(); chunkY >= 0; chunkY--) {
                ChunkSection blockStorage = chunk.getSections()[chunkY];
                if (blockStorage == Chunk.EMPTY_SECTION) {
                    blockStorage = new ChunkSection(chunkY << 4);
                    chunk.getSections()[chunkY] = blockStorage;
                }
                Arrays.fill(blockStorage.getSkyLight().getData(), (byte) 0);
            }
        }
        chunkInfo.forEach(chunkY -> {
            ChunkSection blockStorage = chunk.getSections()[chunkY];
            if (blockStorage != Chunk.EMPTY_SECTION) {
                Arrays.fill(blockStorage.getBlockLight().getData(), (byte) 0);
            }
            int r = 1;
            for (int x = -r; x <= r; x++) {
                for (int y = -r; y <= r; y++) {
                    for (int z = -r; z <= r; z++) {
                        this.chunkInfoMapExtended.mark(chunkInfo.getChunkX() + x, chunkY + y, chunkInfo.getChunkZ() + z);
                    }
                }
            }
        });
    }
    this.generationTimes[1] += System.nanoTime() - t;
}
Also used : IDungeonPart(team.cqr.cqrepoured.world.structure.generation.generation.part.IDungeonPart) Chunk(net.minecraft.world.chunk.Chunk) ChunkSection(net.minecraft.world.chunk.ChunkSection)

Aggregations

ChunkSection (net.minecraft.world.chunk.ChunkSection)36 Chunk (net.minecraft.world.chunk.Chunk)11 BlockState (net.minecraft.block.BlockState)10 BlockPos (net.minecraft.util.math.BlockPos)8 Overwrite (org.spongepowered.asm.mixin.Overwrite)8 WorldChunk (net.minecraft.world.chunk.WorldChunk)5 Inject (org.spongepowered.asm.mixin.injection.Inject)5 TileEntity (net.minecraft.tileentity.TileEntity)3 BiomeContainer (net.minecraft.world.biome.BiomeContainer)3 ArrayList (java.util.ArrayList)2 Block (net.minecraft.block.Block)2 ITileEntityProvider (net.minecraft.block.ITileEntityProvider)2 PathNodeType (net.minecraft.entity.ai.pathing.PathNodeType)2 FluidState (net.minecraft.fluid.FluidState)2 CompoundNBT (net.minecraft.nbt.CompoundNBT)2 NBTUtil (net.minecraft.nbt.NBTUtil)2 ServerPlayerEntity (net.minecraft.server.network.ServerPlayerEntity)2 ChunkPos (net.minecraft.util.math.ChunkPos)2 PalettedContainer (net.minecraft.util.palette.PalettedContainer)2 CollisionView (net.minecraft.world.CollisionView)2