Search in sources :

Example 96 with ItemStack

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);
    }
}
Also used : ItemStack(net.minecraft.world.item.ItemStack) Nonnull(javax.annotation.Nonnull)

Example 97 with ItemStack

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);
    }
}
Also used : ItemEntity(net.minecraft.world.entity.item.ItemEntity) PlayerMainInvWrapper(net.minecraftforge.items.wrapper.PlayerMainInvWrapper) Level(net.minecraft.world.level.Level) ItemStack(net.minecraft.world.item.ItemStack)

Example 98 with ItemStack

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;
}
Also used : ItemStack(net.minecraft.world.item.ItemStack) Nonnull(javax.annotation.Nonnull)

Example 99 with ItemStack

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);
    }
}
Also used : ItemStack(net.minecraft.world.item.ItemStack)

Example 100 with ItemStack

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;
        }
    }
}
Also used : 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