Search in sources :

Example 1 with DispenserTileEntity

use of net.minecraft.tileentity.DispenserTileEntity in project Arclight by IzzelAliz.

the class DropperBlockMixin method dispense.

/**
 * @author IzzelAliz
 * @reason
 */
@Overwrite
public void dispense(World worldIn, BlockPos pos) {
    ProxyBlockSource proxyblocksource = new ProxyBlockSource(worldIn, pos);
    DispenserTileEntity dispensertileentity = proxyblocksource.getBlockTileEntity();
    int i = dispensertileentity.getDispenseSlot();
    if (i < 0) {
        worldIn.playEvent(1001, pos, 0);
    } else {
        ItemStack itemstack = dispensertileentity.getStackInSlot(i);
        if (!itemstack.isEmpty() && net.minecraftforge.items.VanillaInventoryCodeHooks.dropperInsertHook(worldIn, pos, dispensertileentity, i, itemstack)) {
            Direction direction = worldIn.getBlockState(pos).get(DispenserBlock.FACING);
            IInventory iinventory = HopperTileEntity.getInventoryAtPosition(worldIn, pos.offset(direction));
            ItemStack itemstack1;
            if (iinventory == null) {
                itemstack1 = DISPENSE_BEHAVIOR.dispense(proxyblocksource, itemstack);
            } else {
                ItemStack split = itemstack.copy().split(1);
                CraftItemStack craftItemStack = CraftItemStack.asCraftMirror(split);
                Inventory destinationInventory;
                // Have to special case large chests as they work oddly
                if (iinventory instanceof DoubleSidedInventory) {
                    destinationInventory = new CraftInventoryDoubleChest((DoubleSidedInventory) iinventory);
                } else {
                    destinationInventory = ((IInventoryBridge) iinventory).getOwner().getInventory();
                }
                InventoryMoveItemEvent event = new InventoryMoveItemEvent(((IInventoryBridge) dispensertileentity).getOwner().getInventory(), craftItemStack, destinationInventory, true);
                Bukkit.getPluginManager().callEvent(event);
                if (event.isCancelled()) {
                    return;
                }
                itemstack1 = HopperTileEntity.putStackInInventoryAllSlots(dispensertileentity, iinventory, CraftItemStack.asNMSCopy(event.getItem()), direction.getOpposite());
                if (event.getItem().equals(craftItemStack) && itemstack1.isEmpty()) {
                    itemstack1 = itemstack.copy();
                    itemstack1.shrink(1);
                } else {
                    itemstack1 = itemstack.copy();
                }
            }
            dispensertileentity.setInventorySlotContents(i, itemstack1);
        }
    }
}
Also used : IInventory(net.minecraft.inventory.IInventory) CraftItemStack(org.bukkit.craftbukkit.v.inventory.CraftItemStack) DoubleSidedInventory(net.minecraft.inventory.DoubleSidedInventory) ProxyBlockSource(net.minecraft.dispenser.ProxyBlockSource) IInventoryBridge(io.izzel.arclight.common.bridge.inventory.IInventoryBridge) DispenserTileEntity(net.minecraft.tileentity.DispenserTileEntity) CraftItemStack(org.bukkit.craftbukkit.v.inventory.CraftItemStack) ItemStack(net.minecraft.item.ItemStack) CraftInventoryDoubleChest(org.bukkit.craftbukkit.v.inventory.CraftInventoryDoubleChest) InventoryMoveItemEvent(org.bukkit.event.inventory.InventoryMoveItemEvent) Direction(net.minecraft.util.Direction) DoubleSidedInventory(net.minecraft.inventory.DoubleSidedInventory) Inventory(org.bukkit.inventory.Inventory) IInventory(net.minecraft.inventory.IInventory) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 2 with DispenserTileEntity

use of net.minecraft.tileentity.DispenserTileEntity in project LoliServer by Loli-Server.

the class VanillaInventoryCodeHooks method dropperInsertHook.

/**
 * Copied from BlockDropper#dispense and added capability support
 */
public static boolean dropperInsertHook(World world, BlockPos pos, DispenserTileEntity 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 : DispenserTileEntity(net.minecraft.tileentity.DispenserTileEntity) World(net.minecraft.world.World) BlockPos(net.minecraft.util.math.BlockPos) Direction(net.minecraft.util.Direction) HopperTileEntity(net.minecraft.tileentity.HopperTileEntity) DropperBlock(net.minecraft.block.DropperBlock) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) LazyOptional(net.minecraftforge.common.util.LazyOptional) ItemStack(net.minecraft.item.ItemStack) IHopper(net.minecraft.tileentity.IHopper) Block(net.minecraft.block.Block) Pair(org.apache.commons.lang3.tuple.Pair) MathHelper(net.minecraft.util.math.MathHelper) HopperBlock(net.minecraft.block.HopperBlock) Optional(java.util.Optional) TileEntity(net.minecraft.tileentity.TileEntity) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) Direction(net.minecraft.util.Direction)

Example 3 with DispenserTileEntity

use of net.minecraft.tileentity.DispenserTileEntity in project Magma-1.16.x by magmafoundation.

the class VanillaInventoryCodeHooks method dropperInsertHook.

/**
 * Copied from BlockDropper#dispense and added capability support
 */
public static boolean dropperInsertHook(World world, BlockPos pos, DispenserTileEntity 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 : DispenserTileEntity(net.minecraft.tileentity.DispenserTileEntity) World(net.minecraft.world.World) BlockPos(net.minecraft.util.math.BlockPos) Direction(net.minecraft.util.Direction) HopperTileEntity(net.minecraft.tileentity.HopperTileEntity) DropperBlock(net.minecraft.block.DropperBlock) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) LazyOptional(net.minecraftforge.common.util.LazyOptional) ItemStack(net.minecraft.item.ItemStack) IHopper(net.minecraft.tileentity.IHopper) Block(net.minecraft.block.Block) Pair(org.apache.commons.lang3.tuple.Pair) MathHelper(net.minecraft.util.math.MathHelper) HopperBlock(net.minecraft.block.HopperBlock) Optional(java.util.Optional) TileEntity(net.minecraft.tileentity.TileEntity) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) Direction(net.minecraft.util.Direction)

Example 4 with DispenserTileEntity

use of net.minecraft.tileentity.DispenserTileEntity in project Magma-1.16.x by magmafoundation.

the class DispenseFluidContainer method fillContainer.

/**
 * Picks up fluid in front of a Dispenser and fills a container with it.
 */
@Nonnull
private ItemStack fillContainer(@Nonnull IBlockSource source, @Nonnull ItemStack stack) {
    World 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 (((DispenserTileEntity) source.getEntity()).addItem(resultStack) < 0) {
        this.dispenseBehavior.dispense(source, resultStack);
    }
    ItemStack stackCopy = stack.copy();
    stackCopy.shrink(1);
    return stackCopy;
}
Also used : DispenserTileEntity(net.minecraft.tileentity.DispenserTileEntity) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) Direction(net.minecraft.util.Direction) Nonnull(javax.annotation.Nonnull)

Example 5 with DispenserTileEntity

use of net.minecraft.tileentity.DispenserTileEntity in project LoliServer by Loli-Server.

the class DispenseFluidContainer method fillContainer.

/**
 * Picks up fluid in front of a Dispenser and fills a container with it.
 */
@Nonnull
private ItemStack fillContainer(@Nonnull IBlockSource source, @Nonnull ItemStack stack) {
    World 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 (((DispenserTileEntity) source.getEntity()).addItem(resultStack) < 0) {
        this.dispenseBehavior.dispense(source, resultStack);
    }
    ItemStack stackCopy = stack.copy();
    stackCopy.shrink(1);
    return stackCopy;
}
Also used : DispenserTileEntity(net.minecraft.tileentity.DispenserTileEntity) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) Direction(net.minecraft.util.Direction) Nonnull(javax.annotation.Nonnull)

Aggregations

ItemStack (net.minecraft.item.ItemStack)5 DispenserTileEntity (net.minecraft.tileentity.DispenserTileEntity)5 Direction (net.minecraft.util.Direction)5 Nonnull (javax.annotation.Nonnull)4 BlockPos (net.minecraft.util.math.BlockPos)4 World (net.minecraft.world.World)4 Optional (java.util.Optional)2 Nullable (javax.annotation.Nullable)2 Block (net.minecraft.block.Block)2 DropperBlock (net.minecraft.block.DropperBlock)2 HopperBlock (net.minecraft.block.HopperBlock)2 HopperTileEntity (net.minecraft.tileentity.HopperTileEntity)2 IHopper (net.minecraft.tileentity.IHopper)2 TileEntity (net.minecraft.tileentity.TileEntity)2 MathHelper (net.minecraft.util.math.MathHelper)2 LazyOptional (net.minecraftforge.common.util.LazyOptional)2 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)2 Pair (org.apache.commons.lang3.tuple.Pair)2 IInventoryBridge (io.izzel.arclight.common.bridge.inventory.IInventoryBridge)1 ProxyBlockSource (net.minecraft.dispenser.ProxyBlockSource)1