Search in sources :

Example 6 with LevelChunk

use of net.minecraft.world.level.chunk.LevelChunk in project SpongeCommon by SpongePowered.

the class LevelChunkMixin_API method biomeStream.

@Override
public VolumeStream<WorldChunk, Biome> biomeStream(final Vector3i min, final Vector3i max, final StreamOptions options) {
    VolumeStreamUtils.validateStreamArgs(Objects.requireNonNull(min, "min"), Objects.requireNonNull(max, "max"), Objects.requireNonNull(options, "options"));
    final boolean shouldCarbonCopy = options.carbonCopy();
    final Vector3i size = max.sub(min).add(1, 1, 1);
    @MonotonicNonNull final ObjectArrayMutableBiomeBuffer backingVolume;
    if (shouldCarbonCopy) {
        final Registry<net.minecraft.world.level.biome.Biome> biomeRegistry = this.level.registryAccess().registry(Registry.BIOME_REGISTRY).map(wr -> ((Registry<net.minecraft.world.level.biome.Biome>) wr)).orElse(BuiltinRegistries.BIOME);
        backingVolume = new ObjectArrayMutableBiomeBuffer(min, size, VolumeStreamUtils.nativeToSpongeRegistry(biomeRegistry));
    } else {
        backingVolume = null;
    }
    return VolumeStreamUtils.<WorldChunk, Biome, net.minecraft.world.level.biome.Biome, ChunkAccess, BlockPos>generateStream(options, // Ref
    (WorldChunk) this, (LevelChunk) (Object) this, // Entity Accessor
    VolumeStreamUtils.getBiomesForChunkByPos((LevelReader) (Object) this, min, max), // IdentityFunction
    (pos, biome) -> {
        if (shouldCarbonCopy) {
            backingVolume.setBiome(pos, biome);
        }
    }, // Biome by block position
    (key, biome) -> key, // Filtered Position Entity Accessor
    (blockPos, world) -> {
        final net.minecraft.world.level.biome.Biome biome = shouldCarbonCopy ? backingVolume.getNativeBiome(blockPos.getX(), blockPos.getY(), blockPos.getZ()) : ((LevelReader) world.world()).getBiome(blockPos);
        return new Tuple<>(blockPos, biome);
    });
}
Also used : Arrays(java.util.Arrays) WorldChunk(org.spongepowered.api.world.chunk.WorldChunk) Registry(net.minecraft.core.Registry) MonotonicNonNull(org.checkerframework.checker.nullness.qual.MonotonicNonNull) Biome(org.spongepowered.api.world.biome.Biome) Mixin(org.spongepowered.asm.mixin.Mixin) ChunkBiomeContainerAccessor(org.spongepowered.common.accessor.world.level.chunk.ChunkBiomeContainerAccessor) Map(java.util.Map) DifficultyInstance(net.minecraft.world.DifficultyInstance) BuiltinRegistries(net.minecraft.data.BuiltinRegistries) ObjectArrayMutableBiomeBuffer(org.spongepowered.common.world.volume.buffer.biome.ObjectArrayMutableBiomeBuffer) Predicate(java.util.function.Predicate) Collection(java.util.Collection) StreamOptions(org.spongepowered.api.world.volume.stream.StreamOptions) Remap(org.spongepowered.asm.mixin.Interface.Remap) UUID(java.util.UUID) LevelReader(net.minecraft.world.level.LevelReader) Final(org.spongepowered.asm.mixin.Final) Collectors(java.util.stream.Collectors) ObjectArrayMutableEntityBuffer(org.spongepowered.common.world.volume.buffer.entity.ObjectArrayMutableEntityBuffer) ChunkAccess(net.minecraft.world.level.chunk.ChunkAccess) EntityUtil(org.spongepowered.common.entity.EntityUtil) BlockState(org.spongepowered.api.block.BlockState) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) BlockPos(net.minecraft.core.BlockPos) ChunkBiomeContainer(net.minecraft.world.level.chunk.ChunkBiomeContainer) VolumeStreamUtils(org.spongepowered.common.world.volume.VolumeStreamUtils) HeightTypes(org.spongepowered.api.world.HeightTypes) Shadow(org.spongepowered.asm.mixin.Shadow) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) Level(net.minecraft.world.level.Level) BlockEntity(org.spongepowered.api.block.entity.BlockEntity) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) NonNull(org.checkerframework.checker.nullness.qual.NonNull) DataContainer(org.spongepowered.api.data.persistence.DataContainer) ArrayMutableBlockBuffer(org.spongepowered.common.world.volume.buffer.block.ArrayMutableBlockBuffer) AABB(org.spongepowered.api.util.AABB) ClassInstanceMultiMap(net.minecraft.util.ClassInstanceMultiMap) ArrayList(java.util.ArrayList) BlockChangeFlag(org.spongepowered.api.world.BlockChangeFlag) ImmutableList(com.google.common.collect.ImmutableList) ObjectArrayMutableBlockEntityBuffer(org.spongepowered.common.world.volume.buffer.blockentity.ObjectArrayMutableBlockEntityBuffer) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Ticks(org.spongepowered.api.util.Ticks) VolumeStream(org.spongepowered.api.world.volume.stream.VolumeStream) LevelBridge(org.spongepowered.common.bridge.world.level.LevelBridge) Tuple(net.minecraft.util.Tuple) Entity(org.spongepowered.api.entity.Entity) WorldLike(org.spongepowered.api.world.WorldLike) ChunkPos(net.minecraft.world.level.ChunkPos) Implements(org.spongepowered.asm.mixin.Implements) SpongeChunkLayout(org.spongepowered.common.world.storage.SpongeChunkLayout) Vector3d(org.spongepowered.math.vector.Vector3d) Heightmap(net.minecraft.world.level.levelgen.Heightmap) VecHelper(org.spongepowered.common.util.VecHelper) EntityType(org.spongepowered.api.entity.EntityType) SpongeTicks(org.spongepowered.common.util.SpongeTicks) Collections(java.util.Collections) Interface(org.spongepowered.asm.mixin.Interface) Intrinsic(org.spongepowered.asm.mixin.Intrinsic) Vector3i(org.spongepowered.math.vector.Vector3i) ObjectArrayMutableBiomeBuffer(org.spongepowered.common.world.volume.buffer.biome.ObjectArrayMutableBiomeBuffer) LevelReader(net.minecraft.world.level.LevelReader) ChunkAccess(net.minecraft.world.level.chunk.ChunkAccess) Biome(org.spongepowered.api.world.biome.Biome) MonotonicNonNull(org.checkerframework.checker.nullness.qual.MonotonicNonNull) WorldChunk(org.spongepowered.api.world.chunk.WorldChunk) Vector3i(org.spongepowered.math.vector.Vector3i) BlockPos(net.minecraft.core.BlockPos) Tuple(net.minecraft.util.Tuple)

Example 7 with LevelChunk

use of net.minecraft.world.level.chunk.LevelChunk in project SpongeCommon by SpongePowered.

the class ServerLevelMixin_Tracker method bridge$startBlockChange.

@Override
public Optional<WorldPipeline.Builder> bridge$startBlockChange(final BlockPos pos, final BlockState newState, final int flags) {
    if (net.minecraft.world.level.Level.isOutsideBuildHeight(pos)) {
        return Optional.empty();
    } else if (this.shadow$isDebug()) {
        // isClientSide is always false since this is WorldServer
        return Optional.empty();
    }
    // Sponge Start - Sanity check against the PhaseTracker for instances
    if (this.bridge$isFake()) {
        return Optional.empty();
    }
    final PhaseTracker instance = PhaseTracker.getInstance();
    if (instance.getSidedThread() != PhaseTracker.SERVER.getSidedThread() && instance != PhaseTracker.SERVER) {
        throw new UnsupportedOperationException("Cannot perform a tracked Block Change on a ServerWorld while not on the main thread!");
    }
    final SpongeBlockChangeFlag spongeFlag = BlockChangeFlagManager.fromNativeInt(flags);
    final LevelChunk chunk = this.shadow$getChunkAt(pos);
    if (chunk.isEmpty()) {
        return Optional.empty();
    }
    final net.minecraft.world.level.block.state.BlockState currentState = chunk.getBlockState(pos);
    return Optional.of(this.bridge$makePipeline(pos, currentState, newState, chunk, spongeFlag, Constants.World.DEFAULT_BLOCK_CHANGE_LIMIT));
}
Also used : SpongeBlockChangeFlag(org.spongepowered.common.world.SpongeBlockChangeFlag) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) BlockState(net.minecraft.world.level.block.state.BlockState)

Example 8 with LevelChunk

use of net.minecraft.world.level.chunk.LevelChunk in project SpongeCommon by SpongePowered.

the class ServerLevelMixin_Tracker method shadow$addBlockEntity.

@SuppressWarnings({ "ConstantConditions", "RedundantCast" })
@Override
public boolean shadow$addBlockEntity(final net.minecraft.world.level.block.entity.BlockEntity tileEntity) {
    if (this.bridge$isFake() || PhaseTracker.SERVER.getSidedThread() != Thread.currentThread()) {
        // out whoever is trying to remove tile entities asynchronously....
        return super.shadow$addBlockEntity(tileEntity);
    }
    // Otherwise, let's go on and check if we're recording transactions,
    // and if so, log the tile entity removal (may associate with an existing transaction,
    // or create a new transaction.
    final PhaseContext<@NonNull ?> current = PhaseTracker.SERVER.getPhaseContext();
    if (current.doesBlockEventTracking()) {
        final BlockPos immutable = tileEntity.getBlockPos().immutable();
        if (tileEntity.getLevel() != (ServerLevel) (Object) this) {
            tileEntity.setLevelAndPosition((ServerLevel) (Object) this, immutable);
        }
        final ChunkAccess iChunk = this.shadow$getChunk(immutable.getX() >> 4, immutable.getZ() >> 4, ChunkStatus.FULL, false);
        if (!(iChunk instanceof LevelChunk)) {
            return super.shadow$addBlockEntity(tileEntity);
        }
        final LevelChunk chunk = this.shadow$getChunkAt(immutable);
        if (current.getTransactor().logTileAddition(tileEntity, () -> (ServerLevel) (Object) this, chunk)) {
            final TileEntityPipeline pipeline = TileEntityPipeline.kickOff((ServerLevel) (Object) this, immutable).addEffect(AddTileEntityToWorldWhileProcessingEffect.getInstance()).addEffect(AddTileEntityToLoadedListInWorldEffect.getInstance()).addEffect(AddTileEntityToTickableListEffect.getInstance()).addEffect(TileOnLoadDuringAddToWorldEffect.getInstance()).build();
            return pipeline.processEffects(current, new PipelineCursor(tileEntity.getBlockState(), 0, immutable, tileEntity, (Entity) null, Constants.World.DEFAULT_BLOCK_CHANGE_LIMIT));
        }
    }
    return super.shadow$addBlockEntity(tileEntity);
}
Also used : ChunkAccess(net.minecraft.world.level.chunk.ChunkAccess) ServerLevel(net.minecraft.server.level.ServerLevel) TickableBlockEntity(net.minecraft.world.level.block.entity.TickableBlockEntity) RemoveBlockEntity(org.spongepowered.common.event.tracking.context.transaction.block.RemoveBlockEntity) BlockEntity(org.spongepowered.api.block.entity.BlockEntity) Entity(net.minecraft.world.entity.Entity) TileEntityPipeline(org.spongepowered.common.event.tracking.context.transaction.pipeline.TileEntityPipeline) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) PipelineCursor(org.spongepowered.common.event.tracking.context.transaction.pipeline.PipelineCursor) BlockPos(net.minecraft.core.BlockPos)

Example 9 with LevelChunk

use of net.minecraft.world.level.chunk.LevelChunk in project SpongeCommon by SpongePowered.

the class ServerLevelMixin_Tracker method destroyBlock.

@Override
public boolean destroyBlock(final BlockPos pos, final boolean doDrops, @Nullable final Entity p_241212_3_, final int limit) {
    final BlockState currentState = this.shadow$getBlockState(pos);
    if (currentState.isAir()) {
        return false;
    } else {
        // Sponge Start - Sanity check against the PhaseTracker for instances
        if (this.bridge$isFake()) {
            return super.destroyBlock(pos, doDrops, p_241212_3_, limit);
        }
        final PhaseTracker instance = PhaseTracker.getInstance();
        if (instance.getSidedThread() != PhaseTracker.SERVER.getSidedThread() && instance != PhaseTracker.SERVER) {
            throw new UnsupportedOperationException("Cannot perform a tracked Block Change on a ServerWorld while not on the main thread!");
        }
        final FluidState fluidstate = this.shadow$getFluidState(pos);
        final BlockState emptyBlock = fluidstate.createLegacyBlock();
        final SpongeBlockChangeFlag spongeFlag = BlockChangeFlagManager.fromNativeInt(3);
        final LevelChunk chunk = this.shadow$getChunkAt(pos);
        if (chunk.isEmpty()) {
            return false;
        }
        final WorldPipeline.Builder pipelineBuilder = this.bridge$makePipeline(pos, currentState, emptyBlock, chunk, spongeFlag, limit).addEffect(WorldDestroyBlockLevelEffect.getInstance());
        if (doDrops) {
            pipelineBuilder.addEffect(PerformBlockDropsFromDestruction.getInstance());
        }
        final WorldPipeline pipeline = pipelineBuilder.addEffect(WorldBlockChangeCompleteEffect.getInstance()).build();
        return pipeline.processEffects(instance.getPhaseContext(), currentState, emptyBlock, pos, p_241212_3_, spongeFlag, limit);
    }
}
Also used : SpongeBlockChangeFlag(org.spongepowered.common.world.SpongeBlockChangeFlag) BlockState(net.minecraft.world.level.block.state.BlockState) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) WorldPipeline(org.spongepowered.common.event.tracking.context.transaction.pipeline.WorldPipeline) FluidState(net.minecraft.world.level.material.FluidState)

Example 10 with LevelChunk

use of net.minecraft.world.level.chunk.LevelChunk in project SpongeCommon by SpongePowered.

the class ServerLevelMixin_Tracker method setBlock.

/**
 * @author gabizou, March 12th, 2016
 * <p>
 * Move this into WorldServer as we should not be modifying the client world.
 * <p>
 * Purpose: Rewritten to support capturing blocks
 */
@Override
public boolean setBlock(final BlockPos pos, final net.minecraft.world.level.block.state.BlockState newState, final int flags, final int limit) {
    if (net.minecraft.world.level.Level.isOutsideBuildHeight(pos)) {
        return false;
    } else if (this.shadow$isDebug()) {
        // isClientSide is always false since this is WorldServer
        return false;
    }
    // Sponge Start - Sanity check against the PhaseTracker for instances
    if (this.bridge$isFake()) {
        return super.setBlock(pos, newState, flags, limit);
    }
    final PhaseTracker instance = PhaseTracker.getInstance();
    if (instance.getSidedThread() != PhaseTracker.SERVER.getSidedThread() && instance != PhaseTracker.SERVER) {
        throw new UnsupportedOperationException("Cannot perform a tracked Block Change on a ServerWorld while not on the main thread!");
    }
    final SpongeBlockChangeFlag spongeFlag = BlockChangeFlagManager.fromNativeInt(flags);
    final LevelChunk chunk = this.shadow$getChunkAt(pos);
    if (chunk.isEmpty()) {
        return false;
    }
    final net.minecraft.world.level.block.state.BlockState currentState = chunk.getBlockState(pos);
    // change of "don't do anything if the block is the same".
    if (currentState == newState) {
        return false;
    }
    final WorldPipeline pipeline = this.bridge$makePipeline(pos, currentState, newState, chunk, spongeFlag, limit).addEffect(WorldBlockChangeCompleteEffect.getInstance()).build();
    return pipeline.processEffects(instance.getPhaseContext(), currentState, newState, pos, null, spongeFlag, limit);
}
Also used : SpongeBlockChangeFlag(org.spongepowered.common.world.SpongeBlockChangeFlag) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) BlockState(net.minecraft.world.level.block.state.BlockState) WorldPipeline(org.spongepowered.common.event.tracking.context.transaction.pipeline.WorldPipeline)

Aggregations

LevelChunk (net.minecraft.world.level.chunk.LevelChunk)36 BlockPos (net.minecraft.core.BlockPos)13 ChunkAccess (net.minecraft.world.level.chunk.ChunkAccess)10 ServerLevel (net.minecraft.server.level.ServerLevel)9 LevelChunkBridge (org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge)8 Vector3i (org.spongepowered.math.vector.Vector3i)7 BlockState (net.minecraft.world.level.block.state.BlockState)6 Nullable (org.checkerframework.checker.nullness.qual.Nullable)6 BlockEntity (org.spongepowered.api.block.entity.BlockEntity)5 BlockLight (com.denizenscript.denizen.nms.abstracts.BlockLight)4 ChunkCoordinate (com.denizenscript.denizen.utilities.blocks.ChunkCoordinate)4 UUID (java.util.UUID)4 Tuple (net.minecraft.util.Tuple)4 Level (net.minecraft.world.level.Level)4 MonotonicNonNull (org.checkerframework.checker.nullness.qual.MonotonicNonNull)4 CompoundTag (net.minecraft.nbt.CompoundTag)3 ChunkPos (net.minecraft.world.level.ChunkPos)3 PhaseTracker (org.spongepowered.common.event.tracking.PhaseTracker)3 SpongeBlockChangeFlag (org.spongepowered.common.world.SpongeBlockChangeFlag)3 BitSet (java.util.BitSet)2