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);
}
}
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;
}
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);
}
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;
}
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);
}
}
Aggregations