Search in sources :

Example 61 with BlockState

use of org.spongepowered.api.block.BlockState in project SpongeCommon by SpongePowered.

the class ObjectArrayMutableEntityBuffer method blockStateStream.

@Override
public VolumeStream<EntityVolume.Mutable, BlockState> blockStateStream(final Vector3i min, final Vector3i max, final StreamOptions options) {
    VolumeStreamUtils.validateStreamArgs(min, max, this.min(), this.max(), options);
    final ArrayMutableBlockBuffer buffer;
    if (options.carbonCopy()) {
        buffer = this.blockBuffer.copy();
    } else {
        buffer = this.blockBuffer;
    }
    final Stream<VolumeElement<EntityVolume.Mutable, BlockState>> stateStream = IntStream.range(min.x(), max.x() + 1).mapToObj(x -> IntStream.range(min.z(), max.z() + 1).mapToObj(z -> IntStream.range(min.y(), max.y() + 1).mapToObj(y -> VolumeElement.of((EntityVolume.Mutable) this, () -> buffer.block(x, y, z), new Vector3d(x, y, z)))).flatMap(Function.identity())).flatMap(Function.identity());
    return new SpongeVolumeStream<>(stateStream, () -> this);
}
Also used : IntStream(java.util.stream.IntStream) DataContainer(org.spongepowered.api.data.persistence.DataContainer) FluidState(org.spongepowered.api.fluid.FluidState) ArrayMutableBlockBuffer(org.spongepowered.common.world.volume.buffer.block.ArrayMutableBlockBuffer) VolumeElement(org.spongepowered.api.world.volume.stream.VolumeElement) AABB(org.spongepowered.api.util.AABB) Function(java.util.function.Function) ArrayList(java.util.ArrayList) EntityVolume(org.spongepowered.api.world.volume.entity.EntityVolume) ImmutableList(com.google.common.collect.ImmutableList) SpongeVolumeStream(org.spongepowered.common.world.volume.SpongeVolumeStream) StreamSupport(java.util.stream.StreamSupport) Nullable(org.checkerframework.checker.nullness.qual.Nullable) AbstractBlockBuffer(org.spongepowered.common.world.volume.buffer.block.AbstractBlockBuffer) VolumeStream(org.spongepowered.api.world.volume.stream.VolumeStream) Predicate(java.util.function.Predicate) Collection(java.util.Collection) StreamOptions(org.spongepowered.api.world.volume.stream.StreamOptions) Palette(org.spongepowered.api.world.schematic.Palette) UUID(java.util.UUID) Entity(org.spongepowered.api.entity.Entity) Collectors(java.util.stream.Collectors) BlockState(org.spongepowered.api.block.BlockState) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Vector3d(org.spongepowered.math.vector.Vector3d) VolumeStreamUtils(org.spongepowered.common.world.volume.VolumeStreamUtils) BlockType(org.spongepowered.api.block.BlockType) EntityType(org.spongepowered.api.entity.EntityType) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) Vector3i(org.spongepowered.math.vector.Vector3i) EntityVolume(org.spongepowered.api.world.volume.entity.EntityVolume) Vector3d(org.spongepowered.math.vector.Vector3d) ArrayMutableBlockBuffer(org.spongepowered.common.world.volume.buffer.block.ArrayMutableBlockBuffer) VolumeElement(org.spongepowered.api.world.volume.stream.VolumeElement) SpongeVolumeStream(org.spongepowered.common.world.volume.SpongeVolumeStream)

Example 62 with BlockState

use of org.spongepowered.api.block.BlockState in project SpongeCommon by SpongePowered.

the class SpongeParticleHelper method getNamedPacket.

@SuppressWarnings({ "unchecked", "ConstantConditions" })
private static CachedParticlePacket getNamedPacket(final ParticleEffect effect, final net.minecraft.core.particles.ParticleType<?> internalType) {
    // Named particles always support OFFSET and QUANTITY.
    final Vector3f offset = effect.optionOrDefault(ParticleOptions.OFFSET).get().toFloat();
    final int quantity = effect.optionOrDefault(ParticleOptions.QUANTITY).get();
    final Vector3f velocity = effect.optionOrDefault(ParticleOptions.VELOCITY).orElse(Vector3d.ZERO).toFloat();
    if (internalType instanceof SimpleParticleType) {
        // Simple named particle without any additional supported options.
        return new NamedCachedPacket((net.minecraft.core.particles.ParticleOptions) internalType, offset, quantity, velocity);
    }
    // If only mojang had some type akin to our ParticleEffect...
    if (internalType.getDeserializer() == BlockParticleOption.DESERIALIZER) {
        // This particle type supports a block state option.
        final BlockState state = effect.optionOrDefault(ParticleOptions.BLOCK_STATE).get();
        final BlockParticleOption particleData = new BlockParticleOption((net.minecraft.core.particles.ParticleType<BlockParticleOption>) internalType, (net.minecraft.world.level.block.state.BlockState) state);
        return new NamedCachedPacket(particleData, offset, quantity, velocity);
    } else if (internalType.getDeserializer() == ItemParticleOption.DESERIALIZER) {
        // This particle type supports an item option.
        final ItemStackSnapshot snapshot = effect.optionOrDefault(ParticleOptions.ITEM_STACK_SNAPSHOT).get();
        final ItemParticleOption particleData = new ItemParticleOption((net.minecraft.core.particles.ParticleType<ItemParticleOption>) internalType, (net.minecraft.world.item.ItemStack) (Object) snapshot.createStack());
        return new NamedCachedPacket(particleData, offset, quantity, velocity);
    } else if (internalType.getDeserializer() == DustParticleOptions.DESERIALIZER) {
        // This particle type supports a color option.
        final Color color = effect.optionOrDefault(ParticleOptions.COLOR).get();
        final double scale = effect.optionOrDefault(ParticleOptions.SCALE).get();
        final DustParticleOptions particleData = new DustParticleOptions((float) color.red() / 255, (float) color.green() / 255, (float) color.blue() / 255, (float) scale);
        return new NamedCachedPacket(particleData, offset, quantity, velocity);
    }
    // Otherwise, we don't really know how to get a valid IParticleData. Sorry mods!
    return EmptyCachedPacket.INSTANCE;
}
Also used : BlockParticleOption(net.minecraft.core.particles.BlockParticleOption) Color(org.spongepowered.api.util.Color) ParticleType(org.spongepowered.api.effect.particle.ParticleType) SimpleParticleType(net.minecraft.core.particles.SimpleParticleType) SimpleParticleType(net.minecraft.core.particles.SimpleParticleType) BlockState(org.spongepowered.api.block.BlockState) DustParticleOptions(net.minecraft.core.particles.DustParticleOptions) Vector3f(org.spongepowered.math.vector.Vector3f) ItemParticleOption(net.minecraft.core.particles.ItemParticleOption) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot)

Example 63 with BlockState

use of org.spongepowered.api.block.BlockState in project SpongeCommon by SpongePowered.

the class LevelChunkMixin_API method blockStateStream.

@Override
public VolumeStream<WorldChunk, BlockState> blockStateStream(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 ArrayMutableBlockBuffer backingVolume;
    if (shouldCarbonCopy) {
        backingVolume = new ArrayMutableBlockBuffer(min, size);
    } else {
        backingVolume = null;
    }
    return VolumeStreamUtils.<WorldChunk, BlockState, net.minecraft.world.level.block.state.BlockState, ChunkAccess, BlockPos>generateStream(options, // Ref
    (WorldChunk) this, (LevelChunk) (Object) this, // Entity Accessor
    VolumeStreamUtils.getBlockStatesForSections(min, max), // IdentityFunction
    (pos, blockState) -> {
        if (shouldCarbonCopy) {
            backingVolume.setBlock(pos, blockState);
        }
    }, // Biome by block position
    (key, biome) -> key, // Filtered Position Entity Accessor
    (blockPos, world) -> {
        final net.minecraft.world.level.block.state.BlockState tileEntity = shouldCarbonCopy ? backingVolume.getBlock(blockPos) : ((LevelReader) world).getBlockState(blockPos);
        return new Tuple<>(blockPos, tileEntity);
    });
}
Also used : ArrayMutableBlockBuffer(org.spongepowered.common.world.volume.buffer.block.ArrayMutableBlockBuffer) ChunkAccess(net.minecraft.world.level.chunk.ChunkAccess) BlockState(org.spongepowered.api.block.BlockState) 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 64 with BlockState

use of org.spongepowered.api.block.BlockState in project SpongeCommon by SpongePowered.

the class BlockEntityMixin_API method locatableBlock.

@Override
public LocatableBlock locatableBlock() {
    if (this.api$LocatableBlock == null) {
        final BlockState blockState = this.block();
        this.api$LocatableBlock = new SpongeLocatableBlockBuilder().world((ServerWorld) this.level).position(this.shadow$getBlockPos().getX(), this.shadow$getBlockPos().getY(), this.shadow$getBlockPos().getZ()).state(blockState).build();
    }
    return this.api$LocatableBlock;
}
Also used : SpongeLocatableBlockBuilder(org.spongepowered.common.world.server.SpongeLocatableBlockBuilder) BlockState(org.spongepowered.api.block.BlockState)

Example 65 with BlockState

use of org.spongepowered.api.block.BlockState in project SpongeCommon by SpongePowered.

the class SpongeCommonEventFactory method handleCollideBlockEvent.

public static boolean handleCollideBlockEvent(final Block block, final Level world, final BlockPos pos, final net.minecraft.world.level.block.state.BlockState state, final net.minecraft.world.entity.Entity entity, final Direction direction, final CollisionType type) {
    if (world.isClientSide() || pos.getY() <= 0) {
        return false;
    }
    try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
        frame.pushCause(entity);
        if (entity instanceof CreatorTrackedBridge) {
            final CreatorTrackedBridge spongeEntity = (CreatorTrackedBridge) entity;
            spongeEntity.tracker$getCreatorUUID().ifPresent(user -> frame.addContext(EventContextKeys.CREATOR, user));
        }
        // TODO: Add target side support
        final ServerLocation loc = ServerLocation.of((ServerWorld) world, VecHelper.toVector3d(pos));
        final CollideBlockEvent event;
        switch(type) {
            case MOVE:
                event = SpongeEventFactory.createCollideBlockEventMove(frame.currentCause(), (BlockState) state, loc, direction);
                break;
            case FALL:
                event = SpongeEventFactory.createCollideBlockEventFall(frame.currentCause(), (BlockState) state, loc, direction);
                break;
            case STEP_ON:
                event = SpongeEventFactory.createCollideBlockEventStepOn(frame.currentCause(), (BlockState) state, loc, direction);
                break;
            case INSIDE:
                event = SpongeEventFactory.createCollideBlockEventInside(frame.currentCause(), (BlockState) state, loc, direction);
                break;
            default:
                throw new IllegalArgumentException("Unknown type " + type);
        }
        final boolean cancelled = SpongeCommon.post(event);
        if (!cancelled) {
            final EntityBridge spongeEntity = (EntityBridge) entity;
            if (!pos.equals(spongeEntity.bridge$getLastCollidedBlockPos())) {
                final PhaseContext<?> context = PhaseTracker.getInstance().getPhaseContext();
                context.applyNotifierIfAvailable(notifier -> {
                    LevelChunkBridge spongeChunk = ((ActiveChunkReferantBridge) entity).bridge$getActiveChunk();
                    if (spongeChunk == null) {
                        spongeChunk = (LevelChunkBridge) world.getChunkAt(pos);
                    }
                    spongeChunk.bridge$addTrackedBlockPosition(block, pos, notifier, PlayerTracker.Type.NOTIFIER);
                });
            }
        }
        return cancelled;
    }
}
Also used : CreatorTrackedBridge(org.spongepowered.common.bridge.CreatorTrackedBridge) BlockState(org.spongepowered.api.block.BlockState) ServerLocation(org.spongepowered.api.world.server.ServerLocation) CauseStackManager(org.spongepowered.api.event.CauseStackManager) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) CollideBlockEvent(org.spongepowered.api.event.block.CollideBlockEvent) EntityBridge(org.spongepowered.common.bridge.world.entity.EntityBridge) PlatformEntityBridge(org.spongepowered.common.bridge.world.entity.PlatformEntityBridge) ActiveChunkReferantBridge(org.spongepowered.common.bridge.world.level.chunk.ActiveChunkReferantBridge)

Aggregations

BlockState (org.spongepowered.api.block.BlockState)133 World (org.spongepowered.api.world.World)39 IBlockState (net.minecraft.block.state.IBlockState)29 BlockType (org.spongepowered.api.block.BlockType)27 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)22 Direction (org.spongepowered.api.util.Direction)21 Optional (java.util.Optional)20 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)20 Vector3i (com.flowpowered.math.vector.Vector3i)19 Location (org.spongepowered.api.world.Location)18 ItemStack (org.spongepowered.api.item.inventory.ItemStack)17 LocatableBlock (org.spongepowered.api.world.LocatableBlock)14 Sponge (org.spongepowered.api.Sponge)13 ItemType (org.spongepowered.api.item.ItemType)13 ArrayList (java.util.ArrayList)12 Player (org.spongepowered.api.entity.living.player.Player)12 List (java.util.List)11 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)11 Vector3d (com.flowpowered.math.vector.Vector3d)10 Listener (org.spongepowered.api.event.Listener)10