Search in sources :

Example 66 with BlockState

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

the class SpongeCommonEventFactory method handlePistonEvent.

/**
 * This simulates the blocks a piston moves and calls the event for saner
 * debugging.
 *
 * @return if the event was cancelled
 */
public static boolean handlePistonEvent(final TrackedWorldBridge world, final BlockPos pos, final net.minecraft.world.level.block.state.BlockState blockstate, final int eventId) {
    final boolean extending = (eventId == 0);
    final net.minecraft.core.Direction direction = blockstate.getValue(DirectionalBlock.FACING);
    final LocatableBlock locatable = new SpongeLocatableBlockBuilder().world((ServerWorld) world).state((BlockState) blockstate).position(pos.getX(), pos.getY(), pos.getZ()).build();
    // Sets toss out duplicate values (even though there shouldn't be any)
    final HashSet<ServerLocation> locations = new HashSet<>();
    locations.add(ServerLocation.of((ServerWorld) world, pos.getX(), pos.getY(), pos.getZ()));
    final PistonStructureResolver movedBlocks = new PistonStructureResolver((ServerLevel) world, pos, direction, extending);
    // calculates blocks to be moved
    movedBlocks.resolve();
    Stream.concat(movedBlocks.getToPush().stream(), movedBlocks.getToDestroy().stream()).map(block -> ServerLocation.of((ServerWorld) world, block.getX(), block.getY(), block.getZ())).collect(// SUPER
    Collectors.toCollection(() -> locations));
    // If the piston is extending and there are no blocks to destroy, add the offset location for protection purposes
    if (extending && movedBlocks.getToDestroy().isEmpty()) {
        final List<BlockPos> movedPositions = movedBlocks.getToPush();
        final BlockPos offsetPos;
        // If there are no blocks to move, add the offset of piston
        if (movedPositions.isEmpty()) {
            offsetPos = pos.relative(direction);
        } else {
            // Add the offset of last block set to move
            offsetPos = movedPositions.get(movedPositions.size() - 1).relative(direction);
        }
        locations.add(ServerLocation.of((ServerWorld) world, offsetPos.getX(), offsetPos.getY(), offsetPos.getZ()));
    }
    try (final CauseStackManager.StackFrame frame = PhaseTracker.getInstance().pushCauseFrame()) {
        if (extending) {
            frame.addContext(EventContextKeys.PISTON_EXTEND, (ServerWorld) world);
        } else {
            frame.addContext(EventContextKeys.PISTON_RETRACT, (ServerWorld) world);
        }
        return SpongeCommonEventFactory.callChangeBlockEventPre((ServerLevelBridge) world, ImmutableList.copyOf(locations), locatable).isCancelled();
    }
}
Also used : ServerLevelBridge(org.spongepowered.common.bridge.server.level.ServerLevelBridge) ChangeBlockEvent(org.spongepowered.api.event.block.ChangeBlockEvent) SpongeAdventure(org.spongepowered.common.adventure.SpongeAdventure) CreatorTrackedBridge(org.spongepowered.common.bridge.CreatorTrackedBridge) Living(org.spongepowered.api.entity.living.Living) JukeboxBlockEntity(net.minecraft.world.level.block.entity.JukeboxBlockEntity) ServerboundPlayerActionPacket(net.minecraft.network.protocol.game.ServerboundPlayerActionPacket) Set(java.util.Set) MapIdTrackerBridge(org.spongepowered.common.bridge.map.MapIdTrackerBridge) BlockState(org.spongepowered.api.block.BlockState) Direction(org.spongepowered.api.util.Direction) ConstructEntityEvent(org.spongepowered.api.event.entity.ConstructEntityEvent) Stream(java.util.stream.Stream) World(org.spongepowered.api.world.World) ItemStack(net.minecraft.world.item.ItemStack) Player(org.spongepowered.api.entity.living.player.Player) MoveEntityEvent(org.spongepowered.api.event.entity.MoveEntityEvent) ServerLocation(org.spongepowered.api.world.server.ServerLocation) SpongeMapStorage(org.spongepowered.common.map.SpongeMapStorage) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) EventContextKeys(org.spongepowered.api.event.EventContextKeys) ServerWorld(org.spongepowered.api.world.server.ServerWorld) DestructEntityEvent(org.spongepowered.api.event.entity.DestructEntityEvent) Agent(org.spongepowered.api.entity.living.Agent) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) EntityBridge(org.spongepowered.common.bridge.world.entity.EntityBridge) RotateEntityEvent(org.spongepowered.api.event.entity.RotateEntityEvent) PlaySoundEvent(org.spongepowered.api.event.sound.PlaySoundEvent) DetonateExplosiveEvent(org.spongepowered.api.event.entity.explosive.DetonateExplosiveEvent) InteractBlockEvent(org.spongepowered.api.event.block.InteractBlockEvent) CauseStackManager(org.spongepowered.api.event.CauseStackManager) GeneralPhase(org.spongepowered.common.event.tracking.phase.general.GeneralPhase) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) PlayerTracker(org.spongepowered.common.entity.PlayerTracker) UnknownProjectileSource(org.spongepowered.common.entity.projectile.UnknownProjectileSource) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) Entity(org.spongepowered.api.entity.Entity) Cause(org.spongepowered.api.event.Cause) Explosive(org.spongepowered.api.entity.explosive.Explosive) PistonStructureResolver(net.minecraft.world.level.block.piston.PistonStructureResolver) Audience(net.kyori.adventure.audience.Audience) VecHelper(org.spongepowered.common.util.VecHelper) InteractionHand(net.minecraft.world.InteractionHand) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) LivingEntity(net.minecraft.world.entity.LivingEntity) MapItemSavedData(net.minecraft.world.level.saveddata.maps.MapItemSavedData) MusicDisc(org.spongepowered.api.effect.sound.music.MusicDisc) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) HandType(org.spongepowered.api.data.type.HandType) DropItemEvent(org.spongepowered.api.event.item.inventory.DropItemEvent) ExplosiveBridge(org.spongepowered.common.bridge.explosives.ExplosiveBridge) CollideBlockEvent(org.spongepowered.api.event.block.CollideBlockEvent) SetAITargetEvent(org.spongepowered.api.event.entity.ai.SetAITargetEvent) TrackedWorldBridge(org.spongepowered.common.bridge.world.TrackedWorldBridge) ActiveChunkReferantBridge(org.spongepowered.common.bridge.world.level.chunk.ActiveChunkReferantBridge) CollideEntityEvent(org.spongepowered.api.event.entity.CollideEntityEvent) NotePitch(org.spongepowered.api.data.type.NotePitch) InteractEntityEvent(org.spongepowered.api.event.entity.InteractEntityEvent) LocatableBlock(org.spongepowered.api.world.LocatableBlock) SoundType(org.spongepowered.api.effect.sound.SoundType) BlockHitResult(net.minecraft.world.phys.BlockHitResult) GameRules(net.minecraft.world.level.GameRules) Sponge(org.spongepowered.api.Sponge) InstrumentType(org.spongepowered.api.data.type.InstrumentType) UUID(java.util.UUID) ProjectileSource(org.spongepowered.api.projectile.source.ProjectileSource) Collectors(java.util.stream.Collectors) Tristate(org.spongepowered.api.util.Tristate) List(java.util.List) Sound(net.kyori.adventure.sound.Sound) MapInfo(org.spongepowered.api.map.MapInfo) BlockPos(net.minecraft.core.BlockPos) SoundEvent(net.minecraft.sounds.SoundEvent) Explosion(org.spongepowered.api.world.explosion.Explosion) PhaseContext(org.spongepowered.common.event.tracking.PhaseContext) Optional(java.util.Optional) InteractItemEvent(org.spongepowered.api.event.item.inventory.InteractItemEvent) Level(net.minecraft.world.level.Level) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) NonNull(org.checkerframework.checker.nullness.qual.NonNull) Constants(org.spongepowered.common.util.Constants) ServerLevel(net.minecraft.server.level.ServerLevel) Value(org.spongepowered.api.data.value.Value) EntityDamageSource(net.minecraft.world.damagesource.EntityDamageSource) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) EntityTypes(org.spongepowered.api.entity.EntityTypes) ItemStackUtil(org.spongepowered.common.item.util.ItemStackUtil) Component(net.kyori.adventure.text.Component) DamageSource(net.minecraft.world.damagesource.DamageSource) SoundTypes(org.spongepowered.api.effect.sound.SoundTypes) PlatformEntityBridge(org.spongepowered.common.bridge.world.entity.PlatformEntityBridge) Nullable(org.checkerframework.checker.nullness.qual.Nullable) LevelBridge(org.spongepowered.common.bridge.world.level.LevelBridge) DirectionFacingProvider(org.spongepowered.common.registry.provider.DirectionFacingProvider) EntityHitResult(net.minecraft.world.phys.EntityHitResult) SpongeLocatableBlockBuilder(org.spongepowered.common.world.server.SpongeLocatableBlockBuilder) SpongeCommon(org.spongepowered.common.SpongeCommon) MapIndex(net.minecraft.world.level.saveddata.maps.MapIndex) HitResult(net.minecraft.world.phys.HitResult) MovementTypes(org.spongepowered.api.event.cause.entity.MovementTypes) Vector3d(org.spongepowered.math.vector.Vector3d) CreateMapEvent(org.spongepowered.api.event.action.CreateMapEvent) Block(net.minecraft.world.level.block.Block) SpawnTypes(org.spongepowered.api.event.cause.entity.SpawnTypes) Collections(java.util.Collections) DirectionalBlock(net.minecraft.world.level.block.DirectionalBlock) Jukebox(org.spongepowered.api.block.entity.Jukebox) SpongeLocatableBlockBuilder(org.spongepowered.common.world.server.SpongeLocatableBlockBuilder) ServerLocation(org.spongepowered.api.world.server.ServerLocation) ServerWorld(org.spongepowered.api.world.server.ServerWorld) CauseStackManager(org.spongepowered.api.event.CauseStackManager) PistonStructureResolver(net.minecraft.world.level.block.piston.PistonStructureResolver) LocatableBlock(org.spongepowered.api.world.LocatableBlock) BlockPos(net.minecraft.core.BlockPos) ServerLevelBridge(org.spongepowered.common.bridge.server.level.ServerLevelBridge) HashSet(java.util.HashSet)

Example 67 with BlockState

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

the class PlaceBlockPacketState method appendNotifierToBlockEvent.

@Override
public void appendNotifierToBlockEvent(final BasicPacketContext context, final TrackedWorldBridge mixinWorldServer, final BlockPos pos, final TrackableBlockEventDataBridge blockEvent) {
    final Player player = PhaseTracker.getCauseStackManager().currentCause().first(Player.class).get();
    final BlockState state = ((ServerWorld) mixinWorldServer).block(pos.getX(), pos.getY(), pos.getZ());
    final LocatableBlock locatable = new SpongeLocatableBlockBuilder().world((ServerWorld) mixinWorldServer).position(pos.getX(), pos.getY(), pos.getZ()).state(state).build();
    blockEvent.bridge$setTickingLocatable(locatable);
    blockEvent.bridge$setSourceUserUUID(player.uniqueId());
}
Also used : ServerWorld(org.spongepowered.api.world.server.ServerWorld) SpongeLocatableBlockBuilder(org.spongepowered.common.world.server.SpongeLocatableBlockBuilder) Player(org.spongepowered.api.entity.living.player.Player) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) BlockState(org.spongepowered.api.block.BlockState) LocatableBlock(org.spongepowered.api.world.LocatableBlock)

Example 68 with BlockState

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

the class DefaultTeleportHelperFilter method isSafeBodyMaterial.

@Override
public boolean isSafeBodyMaterial(BlockState blockState) {
    net.minecraft.world.level.block.state.BlockState state = (net.minecraft.world.level.block.state.BlockState) blockState;
    Material material = state.getMaterial();
    // Deny blocks that suffocate
    if (state.isSuffocating(EmptyBlockGetter.INSTANCE, BlockPos.ZERO)) {
        return false;
    }
    // Deny dangerous lava
    if (material == Material.LAVA) {
        return false;
    }
    // Deny non-passable non "full" blocks
    return !(state.getBlock() instanceof SlabBlock || state.getBlock() instanceof CauldronBlock || state.getBlock() instanceof AnvilBlock || state.getBlock() instanceof FenceBlock || state.getBlock() instanceof ChorusPlantBlock || state.getBlock() instanceof SnowLayerBlock || material == Material.GLASS || material == Material.LEAVES);
}
Also used : AnvilBlock(net.minecraft.world.level.block.AnvilBlock) SlabBlock(net.minecraft.world.level.block.SlabBlock) Material(net.minecraft.world.level.material.Material) CauldronBlock(net.minecraft.world.level.block.CauldronBlock) FenceBlock(net.minecraft.world.level.block.FenceBlock) ChorusPlantBlock(net.minecraft.world.level.block.ChorusPlantBlock) BlockState(org.spongepowered.api.block.BlockState) SnowLayerBlock(net.minecraft.world.level.block.SnowLayerBlock)

Example 69 with BlockState

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

the class SchematicTranslator method deserializeBlockContainer.

private static void deserializeBlockContainer(final DataView view, final SpongeArchetypeVolume archetypeVolume, final int width, final int length, final Vector3i offset, final boolean needsFixers) {
    final MutableBimapPalette<BlockState, BlockType> palette;
    final DataView paletteMap = view.getView(Constants.Sponge.Schematic.BLOCK_PALETTE).orElseThrow(() -> new InvalidDataException("Missing BlockPalette as required by Schematic Specification"));
    final Set<DataQuery> paletteKeys = paletteMap.keys(false);
    // If we had a default palette_max we don't want to allocate all
    // that space for nothing so we use a sensible default instead
    palette = new MutableBimapPalette<>(PaletteTypes.BLOCK_STATE_PALETTE.get(), Sponge.game().registry(RegistryTypes.BLOCK_TYPE), RegistryTypes.BLOCK_TYPE, paletteKeys.size());
    for (final DataQuery key : paletteKeys) {
        final BlockState state = BlockStateSerializerDeserializer.deserialize(key.parts().get(0)).orElseGet(() -> BlockTypes.BEDROCK.get().defaultState());
        palette.assign(state, paletteMap.getInt(key).orElseThrow(() -> new IllegalStateException("Somehow got a missing biome in the palette map for schematic")));
    }
    final byte[] blockData = (byte[]) view.get(Constants.Sponge.Schematic.BLOCK_DATA).orElseThrow(() -> new InvalidDataException("Missing BlockData for Schematic"));
    SchematicTranslator.readByteArrayData(width, (width * length), offset, palette, blockData, archetypeVolume, BlockVolume.Modifiable::setBlock);
    view.getViewList(Constants.Sponge.Schematic.BLOCKENTITY_CONTAINER).ifPresent(tileData -> tileData.forEach(SchematicTranslator.deserializeBlockEntities(offset, archetypeVolume, needsFixers)));
}
Also used : DataView(org.spongepowered.api.data.persistence.DataView) BlockState(org.spongepowered.api.block.BlockState) BlockType(org.spongepowered.api.block.BlockType) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) DataQuery(org.spongepowered.api.data.persistence.DataQuery)

Example 70 with BlockState

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

the class VolumeTransformationTest method fillVolume.

private static SpongeArchetypeVolume fillVolume(final Vector3i min, final Vector3i max, final Vector3i origin) {
    final Vector3i rawMin = min.min(max);
    final Vector3i rawMax = max.max(min);
    final Vector3i size = rawMax.sub(rawMin).add(Vector3i.ONE);
    final Vector3i relativeMin = rawMin.sub(origin);
    final RegistryHolder holder = Sponge.game();
    final SpongeArchetypeVolume volume = new SpongeArchetypeVolume(relativeMin, size, holder);
    final StubbedRegistry<BlockType> blockRegistry = (StubbedRegistry<BlockType>) RegistryTypes.BLOCK_TYPE.get();
    final Vector3i volMax = volume.max().add(Vector3i.ONE);
    IntStream.range(relativeMin.x(), volMax.x()).forEach(x -> IntStream.range(relativeMin.z(), volMax.z()).forEach(z -> IntStream.range(relativeMin.y(), volMax.y()).forEach(y -> {
        final BlockType block = blockRegistry.createEntry("minecraft", String.format("volumetest{%d, %d, %d}", x, y, z));
        final BlockState blockState = block.defaultState();
        volume.setBlock(x, y, z, blockState);
    })));
    return volume;
}
Also used : Game(org.spongepowered.api.Game) SpongeBuilderProvider(org.spongepowered.common.registry.SpongeBuilderProvider) Inject(com.google.inject.Inject) StubBlock(org.spongepowered.common.test.stub.block.StubBlock) AbstractReferentArchetypeVolume(org.spongepowered.common.world.volume.buffer.archetype.AbstractReferentArchetypeVolume) Biome(org.spongepowered.api.world.biome.Biome) PaletteReference(org.spongepowered.api.world.schematic.PaletteReference) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) Mirror(org.spongepowered.api.util.mirror.Mirror) SpongeFactoryProvider(org.spongepowered.common.registry.SpongeFactoryProvider) PaletteType(org.spongepowered.api.world.schematic.PaletteType) ArchetypeVolume(org.spongepowered.api.world.volume.archetype.ArchetypeVolume) StubMirror(org.spongepowered.common.test.stub.util.StubMirror) StubBlockStatePaletteType(org.spongepowered.common.test.stub.world.schematic.StubBlockStatePaletteType) MethodSource(org.junit.jupiter.params.provider.MethodSource) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) StubPaletteType(org.spongepowered.common.test.stub.world.schematic.StubPaletteType) Sponge(org.spongepowered.api.Sponge) StreamOptions(org.spongepowered.api.world.volume.stream.StreamOptions) Arguments(org.junit.jupiter.params.provider.Arguments) BlockState(org.spongepowered.api.block.BlockState) Transformation(org.spongepowered.api.util.transformation.Transformation) Rotations(org.spongepowered.api.util.rotation.Rotations) Stream(java.util.stream.Stream) StubModule(org.spongepowered.common.test.stub.StubModule) SpongeArchetypeVolume(org.spongepowered.common.world.volume.buffer.archetype.SpongeArchetypeVolume) BlockType(org.spongepowered.api.block.BlockType) StubRegistryHolder(org.spongepowered.common.test.stub.registry.StubRegistryHolder) Strictness(org.mockito.quality.Strictness) IntStream(java.util.stream.IntStream) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) RegistryHolder(org.spongepowered.api.registry.RegistryHolder) StubbedRegistry(org.spongepowered.common.test.stub.registry.StubbedRegistry) Rotation(org.spongepowered.api.util.rotation.Rotation) SpongeRegistryType(org.spongepowered.common.registry.SpongeRegistryType) StubRotations(org.spongepowered.common.test.stub.util.StubRotations) ResourceKey(org.spongepowered.api.ResourceKey) StubKey(org.spongepowered.common.test.stub.StubKey) RegistryKey(org.spongepowered.api.registry.RegistryKey) VolumePositionTranslators(org.spongepowered.api.world.volume.stream.VolumePositionTranslators) SpongePaletteReferenceFactory(org.spongepowered.common.world.schematic.SpongePaletteReferenceFactory) RegistryType(org.spongepowered.api.registry.RegistryType) RegistryTypes(org.spongepowered.api.registry.RegistryTypes) SpongeRegistryKey(org.spongepowered.common.registry.SpongeRegistryKey) Injector(com.google.inject.Injector) StubRegistryFactory(org.spongepowered.common.test.stub.registry.StubRegistryFactory) Mockito(org.mockito.Mockito) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) StubState(org.spongepowered.common.test.stub.block.StubState) SpongeStreamOptionsBuilder(org.spongepowered.common.world.volume.stream.SpongeStreamOptionsBuilder) Vector3d(org.spongepowered.math.vector.Vector3d) Assertions(org.junit.jupiter.api.Assertions) Guice(com.google.inject.Guice) StubGame(org.spongepowered.common.test.stub.StubGame) Vector3i(org.spongepowered.math.vector.Vector3i) BlockState(org.spongepowered.api.block.BlockState) SpongeArchetypeVolume(org.spongepowered.common.world.volume.buffer.archetype.SpongeArchetypeVolume) BlockType(org.spongepowered.api.block.BlockType) Vector3i(org.spongepowered.math.vector.Vector3i) StubbedRegistry(org.spongepowered.common.test.stub.registry.StubbedRegistry) StubRegistryHolder(org.spongepowered.common.test.stub.registry.StubRegistryHolder) RegistryHolder(org.spongepowered.api.registry.RegistryHolder)

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