Search in sources :

Example 86 with BlockState

use of net.minecraft.world.level.block.state.BlockState in project SpongeCommon by SpongePowered.

the class EntityMixin method impl$onStepOnCollide.

@Redirect(method = "move", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/Block;stepOn(Lnet/minecraft/world/level/Level;Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/entity/Entity;)V"))
private void impl$onStepOnCollide(final Block block, final Level world, final BlockPos pos, final Entity entity) {
    if (!ShouldFire.COLLIDE_BLOCK_EVENT_STEP_ON || world.isClientSide) {
        block.stepOn(world, pos, entity);
        return;
    }
    final BlockState state = world.getBlockState(pos);
    final org.spongepowered.api.util.Direction dir = org.spongepowered.api.util.Direction.NONE;
    if (!SpongeCommonEventFactory.handleCollideBlockEvent(block, world, pos, state, entity, dir, SpongeCommonEventFactory.CollisionType.STEP_ON)) {
        block.stepOn(world, pos, entity);
        this.impl$lastCollidedBlockPos = pos;
    }
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 87 with BlockState

use of net.minecraft.world.level.block.state.BlockState in project SpongeCommon by SpongePowered.

the class ServerPlayerGameModeMixin_Tracker method useItemOn.

/**
 * @author Morph
 * @reason Fire interact block event.
 */
@Overwrite
public InteractionResult useItemOn(final ServerPlayer playerIn, final Level worldIn, final ItemStack stackIn, final InteractionHand handIn, final BlockHitResult blockRaytraceResultIn) {
    final BlockPos blockpos = blockRaytraceResultIn.getBlockPos();
    final BlockState blockstate = worldIn.getBlockState(blockpos);
    // Sponge start
    final BlockSnapshot snapshot = ((ServerWorld) (worldIn)).createSnapshot(VecHelper.toVector3i(blockpos));
    final Vector3d hitVec = Vector3d.from(blockRaytraceResultIn.getBlockPos().getX(), blockRaytraceResultIn.getBlockPos().getY(), blockRaytraceResultIn.getBlockPos().getZ());
    final org.spongepowered.api.util.Direction direction = DirectionFacingProvider.INSTANCE.getKey(blockRaytraceResultIn.getDirection()).get();
    final InteractBlockEvent.Secondary event = SpongeCommonEventFactory.callInteractBlockEventSecondary(playerIn, stackIn, hitVec, snapshot, direction, handIn);
    if (event.isCancelled()) {
        return InteractionResult.FAIL;
    }
    final Tristate useItem = event.useItemResult();
    final Tristate useBlock = event.useBlockResult();
    // Sponge end
    if (this.gameModeForPlayer == GameType.SPECTATOR) {
        final MenuProvider inamedcontainerprovider = blockstate.getMenuProvider(worldIn, blockpos);
        if (inamedcontainerprovider != null) {
            playerIn.openMenu(inamedcontainerprovider);
            final Vector3i pos = VecHelper.toVector3i(blockRaytraceResultIn.getBlockPos());
            final ServerLocation location = ServerLocation.of((ServerWorld) worldIn, pos);
            try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
                frame.pushCause(playerIn);
                frame.addContext(EventContextKeys.BLOCK_HIT, ((ServerWorld) (worldIn)).createSnapshot(pos));
                ((ContainerBridge) playerIn.containerMenu).bridge$setOpenLocation(location);
                if (!InventoryEventFactory.callInteractContainerOpenEvent(playerIn)) {
                    return InteractionResult.SUCCESS;
                }
            }
            return InteractionResult.SUCCESS;
        } else {
            return InteractionResult.PASS;
        }
    } else {
        final boolean flag = !playerIn.getMainHandItem().isEmpty() || !playerIn.getOffhandItem().isEmpty();
        final boolean flag1 = playerIn.isSecondaryUseActive() && flag;
        final ItemStack copiedStack = stackIn.copy();
        if (useBlock != Tristate.FALSE && !flag1) {
            // Sponge check useBlock
            final AbstractContainerMenu lastOpenContainer = playerIn.containerMenu;
            final InteractionResult result = blockstate.use(worldIn, playerIn, handIn, blockRaytraceResultIn);
            if (result.consumesAction() && lastOpenContainer != playerIn.containerMenu) {
                final Vector3i pos = VecHelper.toVector3i(blockRaytraceResultIn.getBlockPos());
                final ServerLocation location = ServerLocation.of((ServerWorld) worldIn, pos);
                try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
                    frame.pushCause(playerIn);
                    frame.addContext(EventContextKeys.BLOCK_HIT, ((ServerWorld) (worldIn)).createSnapshot(pos));
                    ((ContainerBridge) playerIn.containerMenu).bridge$setOpenLocation(location);
                    if (!InventoryEventFactory.callInteractContainerOpenEvent(playerIn)) {
                        return InteractionResult.FAIL;
                    }
                }
            }
            if (result.consumesAction()) {
                CriteriaTriggers.ITEM_USED_ON_BLOCK.trigger(playerIn, blockpos, copiedStack);
                return result;
            }
        }
        if (!stackIn.isEmpty() && !playerIn.getCooldowns().isOnCooldown(stackIn.getItem())) {
            // Sponge start
            if (useItem == Tristate.FALSE) {
                return InteractionResult.PASS;
            }
            // Sponge end
            final UseOnContext itemusecontext = new UseOnContext(playerIn, handIn, blockRaytraceResultIn);
            final InteractionResult result;
            if (this.isCreative()) {
                final int i = stackIn.getCount();
                result = stackIn.useOn(itemusecontext);
                stackIn.setCount(i);
            } else {
                result = stackIn.useOn(itemusecontext);
                // Sponge start - log change in hand
                final PhaseContext<@NonNull ?> context = PhaseTracker.SERVER.getPhaseContext();
                final TransactionalCaptureSupplier transactor = context.getTransactor();
                transactor.logPlayerInventoryChange(this.player, PlayerInventoryTransaction.EventCreator.STANDARD);
                this.player.inventoryMenu.broadcastChanges();
            // Sponge end
            }
            if (result.consumesAction()) {
                CriteriaTriggers.ITEM_USED_ON_BLOCK.trigger(playerIn, blockpos, copiedStack);
            }
            return result;
        } else {
            return InteractionResult.PASS;
        }
    }
}
Also used : ServerLocation(org.spongepowered.api.world.server.ServerLocation) Tristate(org.spongepowered.api.util.Tristate) ContainerBridge(org.spongepowered.common.bridge.world.inventory.container.ContainerBridge) ServerWorld(org.spongepowered.api.world.server.ServerWorld) InteractionResult(net.minecraft.world.InteractionResult) InteractBlockEvent(org.spongepowered.api.event.block.InteractBlockEvent) TransactionalCaptureSupplier(org.spongepowered.common.event.tracking.context.transaction.TransactionalCaptureSupplier) AbstractContainerMenu(net.minecraft.world.inventory.AbstractContainerMenu) CauseStackManager(org.spongepowered.api.event.CauseStackManager) BlockPos(net.minecraft.core.BlockPos) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) UseOnContext(net.minecraft.world.item.context.UseOnContext) MenuProvider(net.minecraft.world.MenuProvider) BlockState(net.minecraft.world.level.block.state.BlockState) Vector3d(org.spongepowered.math.vector.Vector3d) Vector3i(org.spongepowered.math.vector.Vector3i) ItemStack(net.minecraft.world.item.ItemStack) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 88 with BlockState

use of net.minecraft.world.level.block.state.BlockState in project SpongeCommon by SpongePowered.

the class BlockTickContext method source.

@Override
public BlockTickContext source(final Object owner) {
    super.source(owner);
    if (owner instanceof LocatableBlock) {
        final LocatableBlock locatableBlock = (LocatableBlock) owner;
        final Block block = ((BlockState) locatableBlock.blockState()).getBlock();
        this.tickingBlock = (BlockBridge) block;
        this.providesModifier = !(block instanceof LiquidBlock);
        this.world = locatableBlock.world();
        if (block instanceof TrackableBridge) {
            final TrackableBridge trackable = (TrackableBridge) block;
            this.setBlockEvents(trackable.bridge$allowsBlockEventCreation()).setBulkBlockCaptures(trackable.bridge$allowsBlockBulkCaptures()).setEntitySpawnEvents(trackable.bridge$allowsEntityEventCreation()).setBulkEntityCaptures(trackable.bridge$allowsEntityBulkCaptures());
        }
    }
    return this;
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) LocatableBlock(org.spongepowered.api.world.LocatableBlock) Block(net.minecraft.world.level.block.Block) LiquidBlock(net.minecraft.world.level.block.LiquidBlock) LocatableBlock(org.spongepowered.api.world.LocatableBlock) TrackableBridge(org.spongepowered.common.bridge.TrackableBridge) LiquidBlock(net.minecraft.world.level.block.LiquidBlock)

Example 89 with BlockState

use of net.minecraft.world.level.block.state.BlockState in project SpongeCommon by SpongePowered.

the class FluidTickContext method source.

@Override
public FluidTickContext source(final Object owner) {
    super.source(owner);
    if (owner instanceof LocatableBlock) {
        final LocatableBlock locatableBlock = (LocatableBlock) owner;
        final Block block = ((BlockState) locatableBlock.blockState()).getBlock();
        this.providesModifier = !(block instanceof LiquidBlock);
        this.world = locatableBlock.world();
        if (block instanceof TrackableBridge) {
            final TrackableBridge trackable = (TrackableBridge) block;
            this.setBlockEvents(trackable.bridge$allowsBlockEventCreation()).setBulkBlockCaptures(trackable.bridge$allowsBlockBulkCaptures()).setEntitySpawnEvents(trackable.bridge$allowsEntityEventCreation()).setBulkEntityCaptures(trackable.bridge$allowsEntityBulkCaptures());
        }
    }
    return this;
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) LocatableBlock(org.spongepowered.api.world.LocatableBlock) Block(net.minecraft.world.level.block.Block) LiquidBlock(net.minecraft.world.level.block.LiquidBlock) LocatableBlock(org.spongepowered.api.world.LocatableBlock) TrackableBridge(org.spongepowered.common.bridge.TrackableBridge) LiquidBlock(net.minecraft.world.level.block.LiquidBlock)

Example 90 with BlockState

use of net.minecraft.world.level.block.state.BlockState in project SpongeCommon by SpongePowered.

the class VolumeStreamUtils method getBlockEntityOrCloneToBackingVolume.

@NonNull
public static BiConsumer<BlockPos, net.minecraft.world.level.block.entity.BlockEntity> getBlockEntityOrCloneToBackingVolume(final boolean shouldCarbonCopy, final ObjectArrayMutableBlockEntityBuffer backingVolume, @Nullable final Level level) {
    return shouldCarbonCopy ? (pos, tile) -> {
        final CompoundTag nbt = tile.save(new CompoundTag());
        final net.minecraft.world.level.block.entity.@Nullable BlockEntity cloned = tile.getType().create();
        final BlockState state = tile.getBlockState();
        Objects.requireNonNull(cloned, () -> String.format("TileEntityType[%s] creates a null TileEntity!", BlockEntityType.getKey(tile.getType()))).load(state, nbt);
        if (level != null) {
            ((BlockEntityAccessor) cloned).accessor$level(level);
        }
        backingVolume.addBlockEntity(pos.getX(), pos.getY(), pos.getZ(), (BlockEntity) cloned);
    } : (pos, tile) -> {
    };
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) CompoundTag(net.minecraft.nbt.CompoundTag) Nullable(org.checkerframework.checker.nullness.qual.Nullable) BlockEntityAccessor(org.spongepowered.common.accessor.world.level.block.entity.BlockEntityAccessor) MonotonicNonNull(org.checkerframework.checker.nullness.qual.MonotonicNonNull) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Aggregations

BlockState (net.minecraft.world.level.block.state.BlockState)141 BlockPos (net.minecraft.core.BlockPos)75 Direction (net.minecraft.core.Direction)18 Level (net.minecraft.world.level.Level)14 Block (net.minecraft.world.level.block.Block)14 ArrayList (java.util.ArrayList)10 ServerLevel (net.minecraft.server.level.ServerLevel)10 SoundType (net.minecraft.world.level.block.SoundType)10 Vec3 (net.minecraft.world.phys.Vec3)10 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)9 Random (java.util.Random)8 ItemStack (net.minecraft.world.item.ItemStack)7 FluidState (net.minecraft.world.level.material.FluidState)7 AABB (net.minecraft.world.phys.AABB)6 BuildBlock (com.wuest.prefab.structures.base.BuildBlock)5 LevelChunk (net.minecraft.world.level.chunk.LevelChunk)5 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)5 LocationTag (com.denizenscript.denizen.objects.LocationTag)4 FakeBlock (com.denizenscript.denizen.utilities.blocks.FakeBlock)4 HashMap (java.util.HashMap)4