Search in sources :

Example 1 with DispenserBlockEntity

use of net.minecraft.world.level.block.entity.DispenserBlockEntity in project MinecraftForge by MinecraftForge.

the class VanillaInventoryCodeHooks method dropperInsertHook.

/**
 * Copied from BlockDropper#dispense and added capability support
 */
public static boolean dropperInsertHook(Level world, BlockPos pos, DispenserBlockEntity dropper, int slot, @Nonnull ItemStack stack) {
    Direction enumfacing = world.getBlockState(pos).getValue(DropperBlock.FACING);
    BlockPos blockpos = pos.relative(enumfacing);
    return getItemHandler(world, (double) blockpos.getX(), (double) blockpos.getY(), (double) blockpos.getZ(), enumfacing.getOpposite()).map(destinationResult -> {
        IItemHandler itemHandler = destinationResult.getKey();
        Object destination = destinationResult.getValue();
        ItemStack dispensedStack = stack.copy().split(1);
        ItemStack remainder = putStackInInventoryAllSlots(dropper, destination, itemHandler, dispensedStack);
        if (remainder.isEmpty()) {
            remainder = stack.copy();
            remainder.shrink(1);
        } else {
            remainder = stack.copy();
        }
        dropper.setItem(slot, remainder);
        return false;
    }).orElse(true);
}
Also used : HopperBlockEntity(net.minecraft.world.level.block.entity.HopperBlockEntity) Direction(net.minecraft.core.Direction) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) DropperBlock(net.minecraft.world.level.block.DropperBlock) Pair(org.apache.commons.lang3.tuple.Pair) BlockPos(net.minecraft.core.BlockPos) HopperBlock(net.minecraft.world.level.block.HopperBlock) Optional(java.util.Optional) ItemStack(net.minecraft.world.item.ItemStack) Hopper(net.minecraft.world.level.block.entity.Hopper) Level(net.minecraft.world.level.Level) Mth(net.minecraft.util.Mth) Nonnull(javax.annotation.Nonnull) DispenserBlockEntity(net.minecraft.world.level.block.entity.DispenserBlockEntity) Nullable(javax.annotation.Nullable) BlockPos(net.minecraft.core.BlockPos) ItemStack(net.minecraft.world.item.ItemStack) Direction(net.minecraft.core.Direction)

Example 2 with DispenserBlockEntity

use of net.minecraft.world.level.block.entity.DispenserBlockEntity in project MinecraftForge by MinecraftForge.

the class DispenseFluidContainer method fillContainer.

/**
 * Picks up fluid in front of a Dispenser and fills a container with it.
 */
@Nonnull
private ItemStack fillContainer(@Nonnull BlockSource source, @Nonnull ItemStack stack) {
    Level world = source.getLevel();
    Direction dispenserFacing = source.getBlockState().getValue(DispenserBlock.FACING);
    BlockPos blockpos = source.getPos().relative(dispenserFacing);
    FluidActionResult actionResult = FluidUtil.tryPickUpFluid(stack, null, world, blockpos, dispenserFacing.getOpposite());
    ItemStack resultStack = actionResult.getResult();
    if (!actionResult.isSuccess() || resultStack.isEmpty()) {
        return super.execute(source, stack);
    }
    if (stack.getCount() == 1) {
        return resultStack;
    } else if (((DispenserBlockEntity) source.getEntity()).addItem(resultStack) < 0) {
        this.dispenseBehavior.dispense(source, resultStack);
    }
    ItemStack stackCopy = stack.copy();
    stackCopy.shrink(1);
    return stackCopy;
}
Also used : DispenserBlockEntity(net.minecraft.world.level.block.entity.DispenserBlockEntity) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) ItemStack(net.minecraft.world.item.ItemStack) Direction(net.minecraft.core.Direction) Nonnull(javax.annotation.Nonnull)

Aggregations

Nonnull (javax.annotation.Nonnull)2 BlockPos (net.minecraft.core.BlockPos)2 Direction (net.minecraft.core.Direction)2 ItemStack (net.minecraft.world.item.ItemStack)2 Level (net.minecraft.world.level.Level)2 DispenserBlockEntity (net.minecraft.world.level.block.entity.DispenserBlockEntity)2 Optional (java.util.Optional)1 Nullable (javax.annotation.Nullable)1 Mth (net.minecraft.util.Mth)1 DropperBlock (net.minecraft.world.level.block.DropperBlock)1 HopperBlock (net.minecraft.world.level.block.HopperBlock)1 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)1 Hopper (net.minecraft.world.level.block.entity.Hopper)1 HopperBlockEntity (net.minecraft.world.level.block.entity.HopperBlockEntity)1 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)1 Pair (org.apache.commons.lang3.tuple.Pair)1