use of net.minecraft.world.item.ItemStack in project MinecraftForge by MinecraftForge.
the class ItemStackHandler method extractItem.
@Override
@Nonnull
public ItemStack extractItem(int slot, int amount, boolean simulate) {
if (amount == 0)
return ItemStack.EMPTY;
validateSlotIndex(slot);
ItemStack existing = this.stacks.get(slot);
if (existing.isEmpty())
return ItemStack.EMPTY;
int toExtract = Math.min(amount, existing.getMaxStackSize());
if (existing.getCount() <= toExtract) {
if (!simulate) {
this.stacks.set(slot, ItemStack.EMPTY);
onContentsChanged(slot);
return existing;
} else {
return existing.copy();
}
} else {
if (!simulate) {
this.stacks.set(slot, ItemHandlerHelper.copyStackWithSize(existing, existing.getCount() - toExtract));
onContentsChanged(slot);
}
return ItemHandlerHelper.copyStackWithSize(existing, toExtract);
}
}
use of net.minecraft.world.item.ItemStack in project MinecraftForge by MinecraftForge.
the class ItemHandlerHelper method giveItemToPlayer.
/**
* Inserts the given itemstack into the players inventory.
* If the inventory can't hold it, the item will be dropped in the world at the players position.
*
* @param player The player to give the item to
* @param stack The itemstack to insert
*/
public static void giveItemToPlayer(Player player, @Nonnull ItemStack stack, int preferredSlot) {
if (stack.isEmpty())
return;
IItemHandler inventory = new PlayerMainInvWrapper(player.getInventory());
Level world = player.level;
// try adding it into the inventory
ItemStack remainder = stack;
// insert into preferred slot first
if (preferredSlot >= 0 && preferredSlot < inventory.getSlots()) {
remainder = inventory.insertItem(preferredSlot, stack, false);
}
// then into the inventory in general
if (!remainder.isEmpty()) {
remainder = insertItemStacked(inventory, remainder, false);
}
// play sound if something got picked up
if (remainder.isEmpty() || remainder.getCount() != stack.getCount()) {
world.playSound(null, player.getX(), player.getY() + 0.5, player.getZ(), SoundEvents.ITEM_PICKUP, SoundSource.PLAYERS, 0.2F, ((world.random.nextFloat() - world.random.nextFloat()) * 0.7F + 1.0F) * 2.0F);
}
// drop remaining itemstack into the world
if (!remainder.isEmpty() && !world.isClientSide) {
ItemEntity entityitem = new ItemEntity(world, player.getX(), player.getY() + 0.5, player.getZ(), remainder);
entityitem.setPickUpDelay(40);
entityitem.setDeltaMovement(entityitem.getDeltaMovement().multiply(0, 1, 0));
world.addFreshEntity(entityitem);
}
}
use of net.minecraft.world.item.ItemStack in project MinecraftForge by MinecraftForge.
the class ItemHandlerHelper method copyStackWithSize.
@Nonnull
public static ItemStack copyStackWithSize(@Nonnull ItemStack itemStack, int size) {
if (size == 0)
return ItemStack.EMPTY;
ItemStack copy = itemStack.copy();
copy.setCount(size);
return copy;
}
use of net.minecraft.world.item.ItemStack in project MinecraftForge by MinecraftForge.
the class ItemHandlerHelper method calcRedstoneFromInventory.
/**
* This method uses the standard vanilla algorithm to calculate a comparator output for how "full" the inventory is.
* This method is an adaptation of Container#calcRedstoneFromInventory(IInventory).
* @param inv The inventory handler to test.
* @return A redstone value in the range [0,15] representing how "full" this inventory is.
*/
public static int calcRedstoneFromInventory(@Nullable IItemHandler inv) {
if (inv == null) {
return 0;
} else {
int itemsFound = 0;
float proportion = 0.0F;
for (int j = 0; j < inv.getSlots(); ++j) {
ItemStack itemstack = inv.getStackInSlot(j);
if (!itemstack.isEmpty()) {
proportion += (float) itemstack.getCount() / (float) Math.min(inv.getSlotLimit(j), itemstack.getMaxStackSize());
++itemsFound;
}
}
proportion = proportion / (float) inv.getSlots();
return Mth.floor(proportion * 14.0F) + (itemsFound > 0 ? 1 : 0);
}
}
use of net.minecraft.world.item.ItemStack in project MinecraftForge by MinecraftForge.
the class SidedInvWrapper method insertItem.
@Override
@Nonnull
public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) {
if (stack.isEmpty())
return ItemStack.EMPTY;
int slot1 = getSlot(inv, slot, side);
if (slot1 == -1)
return stack;
ItemStack stackInSlot = inv.getItem(slot1);
int m;
if (!stackInSlot.isEmpty()) {
if (stackInSlot.getCount() >= Math.min(stackInSlot.getMaxStackSize(), getSlotLimit(slot)))
return stack;
if (!ItemHandlerHelper.canItemStacksStack(stack, stackInSlot))
return stack;
if (!inv.canPlaceItemThroughFace(slot1, stack, side) || !inv.canPlaceItem(slot1, stack))
return stack;
m = Math.min(stack.getMaxStackSize(), getSlotLimit(slot)) - stackInSlot.getCount();
if (stack.getCount() <= m) {
if (!simulate) {
ItemStack copy = stack.copy();
copy.grow(stackInSlot.getCount());
setInventorySlotContents(slot1, copy);
}
return ItemStack.EMPTY;
} else {
// copy the stack to not modify the original one
stack = stack.copy();
if (!simulate) {
ItemStack copy = stack.split(m);
copy.grow(stackInSlot.getCount());
setInventorySlotContents(slot1, copy);
return stack;
} else {
stack.shrink(m);
return stack;
}
}
} else {
if (!inv.canPlaceItemThroughFace(slot1, stack, side) || !inv.canPlaceItem(slot1, stack))
return stack;
m = Math.min(stack.getMaxStackSize(), getSlotLimit(slot));
if (m < stack.getCount()) {
// copy the stack to not modify the original one
stack = stack.copy();
if (!simulate) {
setInventorySlotContents(slot1, stack.split(m));
return stack;
} else {
stack.shrink(m);
return stack;
}
} else {
if (!simulate)
setInventorySlotContents(slot1, stack);
return ItemStack.EMPTY;
}
}
}
Aggregations