Search in sources :

Example 1 with ItemStack

use of net.minecraft.world.item.ItemStack in project MinecraftForge by MinecraftForge.

the class DispenseFluidContainer method dumpContainer.

/**
 * Drains a filled container and places the fluid in front of the Dispenser.
 */
@Nonnull
private ItemStack dumpContainer(BlockSource source, @Nonnull ItemStack stack) {
    ItemStack singleStack = stack.copy();
    singleStack.setCount(1);
    IFluidHandlerItem fluidHandler = FluidUtil.getFluidHandler(singleStack).orElse(null);
    if (fluidHandler == null) {
        return super.execute(source, stack);
    }
    FluidStack fluidStack = fluidHandler.drain(FluidAttributes.BUCKET_VOLUME, IFluidHandler.FluidAction.EXECUTE);
    Direction dispenserFacing = source.getBlockState().getValue(DispenserBlock.FACING);
    BlockPos blockpos = source.getPos().relative(dispenserFacing);
    FluidActionResult result = FluidUtil.tryPlaceFluid(null, source.getLevel(), InteractionHand.MAIN_HAND, blockpos, stack, fluidStack);
    if (result.isSuccess()) {
        ItemStack drainedStack = result.getResult();
        if (drainedStack.getCount() == 1) {
            return drainedStack;
        } else if (!drainedStack.isEmpty() && ((DispenserBlockEntity) source.getEntity()).addItem(drainedStack) < 0) {
            this.dispenseBehavior.dispense(source, drainedStack);
        }
        ItemStack stackCopy = drainedStack.copy();
        stackCopy.shrink(1);
        return stackCopy;
    } else {
        return this.dispenseBehavior.dispense(source, stack);
    }
}
Also used : IFluidHandlerItem(net.minecraftforge.fluids.capability.IFluidHandlerItem) BlockPos(net.minecraft.core.BlockPos) ItemStack(net.minecraft.world.item.ItemStack) Direction(net.minecraft.core.Direction) Nonnull(javax.annotation.Nonnull)

Example 2 with ItemStack

use of net.minecraft.world.item.ItemStack in project MinecraftForge by MinecraftForge.

the class VanillaInventoryCodeHooks method insertStack.

/**
 * Copied from TileEntityHopper#insertStack and added capability support
 */
private static ItemStack insertStack(BlockEntity source, Object destination, IItemHandler destInventory, ItemStack stack, int slot) {
    ItemStack itemstack = destInventory.getStackInSlot(slot);
    if (destInventory.insertItem(slot, stack, true).isEmpty()) {
        boolean insertedItem = false;
        boolean inventoryWasEmpty = isEmpty(destInventory);
        if (itemstack.isEmpty()) {
            destInventory.insertItem(slot, stack, false);
            stack = ItemStack.EMPTY;
            insertedItem = true;
        } else if (ItemHandlerHelper.canItemStacksStack(itemstack, stack)) {
            int originalSize = stack.getCount();
            stack = destInventory.insertItem(slot, stack, false);
            insertedItem = originalSize < stack.getCount();
        }
        if (insertedItem) {
            if (inventoryWasEmpty && destination instanceof HopperBlockEntity) {
                HopperBlockEntity destinationHopper = (HopperBlockEntity) destination;
                if (!destinationHopper.isOnCustomCooldown()) {
                    int k = 0;
                    if (source instanceof HopperBlockEntity) {
                        if (destinationHopper.getLastUpdateTime() >= ((HopperBlockEntity) source).getLastUpdateTime()) {
                            k = 1;
                        }
                    }
                    destinationHopper.setCooldown(8 - k);
                }
            }
        }
    }
    return stack;
}
Also used : HopperBlockEntity(net.minecraft.world.level.block.entity.HopperBlockEntity) ItemStack(net.minecraft.world.item.ItemStack)

Example 3 with ItemStack

use of net.minecraft.world.item.ItemStack 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 4 with ItemStack

use of net.minecraft.world.item.ItemStack in project MinecraftForge by MinecraftForge.

the class EntityEquipmentInvWrapper method insertItem.

@Nonnull
@Override
public ItemStack insertItem(final int slot, @Nonnull final ItemStack stack, final boolean simulate) {
    if (stack.isEmpty())
        return ItemStack.EMPTY;
    final EquipmentSlot equipmentSlot = validateSlotIndex(slot);
    final ItemStack existing = entity.getItemBySlot(equipmentSlot);
    int limit = getStackLimit(slot, stack);
    if (!existing.isEmpty()) {
        if (!ItemHandlerHelper.canItemStacksStack(stack, existing))
            return stack;
        limit -= existing.getCount();
    }
    if (limit <= 0)
        return stack;
    boolean reachedLimit = stack.getCount() > limit;
    if (!simulate) {
        if (existing.isEmpty()) {
            entity.setItemSlot(equipmentSlot, reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, limit) : stack);
        } else {
            existing.grow(reachedLimit ? limit : stack.getCount());
        }
    }
    return reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, stack.getCount() - limit) : ItemStack.EMPTY;
}
Also used : EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) ItemStack(net.minecraft.world.item.ItemStack) Nonnull(javax.annotation.Nonnull)

Example 5 with ItemStack

use of net.minecraft.world.item.ItemStack in project MinecraftForge by MinecraftForge.

the class EntityEquipmentInvWrapper method extractItem.

@Nonnull
@Override
public ItemStack extractItem(final int slot, final int amount, final boolean simulate) {
    if (amount == 0)
        return ItemStack.EMPTY;
    final EquipmentSlot equipmentSlot = validateSlotIndex(slot);
    final ItemStack existing = entity.getItemBySlot(equipmentSlot);
    if (existing.isEmpty())
        return ItemStack.EMPTY;
    final int toExtract = Math.min(amount, existing.getMaxStackSize());
    if (existing.getCount() <= toExtract) {
        if (!simulate) {
            entity.setItemSlot(equipmentSlot, ItemStack.EMPTY);
        }
        return existing;
    } else {
        if (!simulate) {
            entity.setItemSlot(equipmentSlot, ItemHandlerHelper.copyStackWithSize(existing, existing.getCount() - toExtract));
        }
        return ItemHandlerHelper.copyStackWithSize(existing, toExtract);
    }
}
Also used : EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) ItemStack(net.minecraft.world.item.ItemStack) Nonnull(javax.annotation.Nonnull)

Aggregations

ItemStack (net.minecraft.world.item.ItemStack)178 Level (net.minecraft.world.level.Level)62 ItemEntity (net.minecraft.world.entity.item.ItemEntity)61 InteractionHand (net.minecraft.world.InteractionHand)58 Player (net.minecraft.world.entity.player.Player)57 Items (net.minecraft.world.item.Items)55 InvocationTargetException (java.lang.reflect.InvocationTargetException)54 InteractionResult (net.minecraft.world.InteractionResult)54 EntitySize (de.Keyle.MyPet.api.entity.EntitySize)52 MyPet (de.Keyle.MyPet.api.entity.MyPet)52 EntityDataAccessor (net.minecraft.network.syncher.EntityDataAccessor)44 EntityDataSerializers (net.minecraft.network.syncher.EntityDataSerializers)44 SynchedEntityData (net.minecraft.network.syncher.SynchedEntityData)44 ServerLevel (net.minecraft.server.level.ServerLevel)44 MyPetApi (de.Keyle.MyPet.MyPetApi)40 Pair (com.mojang.datafixers.util.Pair)39 Bukkit (org.bukkit.Bukkit)39 CraftItemStack (org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack)39 CraftItemStack (org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack)39 Util (de.Keyle.MyPet.api.Util)38