Search in sources :

Example 6 with ItemStack

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

the class InvWrapper method insertItem.

@Override
@Nonnull
public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) {
    if (stack.isEmpty())
        return ItemStack.EMPTY;
    ItemStack stackInSlot = getInv().getItem(slot);
    int m;
    if (!stackInSlot.isEmpty()) {
        if (stackInSlot.getCount() >= Math.min(stackInSlot.getMaxStackSize(), getSlotLimit(slot)))
            return stack;
        if (!ItemHandlerHelper.canItemStacksStack(stack, stackInSlot))
            return stack;
        if (!getInv().canPlaceItem(slot, 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());
                getInv().setItem(slot, copy);
                getInv().setChanged();
            }
            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());
                getInv().setItem(slot, copy);
                getInv().setChanged();
                return stack;
            } else {
                stack.shrink(m);
                return stack;
            }
        }
    } else {
        if (!getInv().canPlaceItem(slot, 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) {
                getInv().setItem(slot, stack.split(m));
                getInv().setChanged();
                return stack;
            } else {
                stack.shrink(m);
                return stack;
            }
        } else {
            if (!simulate) {
                getInv().setItem(slot, stack);
                getInv().setChanged();
            }
            return ItemStack.EMPTY;
        }
    }
}
Also used : ItemStack(net.minecraft.world.item.ItemStack) Nonnull(javax.annotation.Nonnull)

Example 7 with ItemStack

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

the class ItemStackHandler method insertItem.

@Override
@Nonnull
public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) {
    if (stack.isEmpty())
        return ItemStack.EMPTY;
    if (!isItemValid(slot, stack))
        return stack;
    validateSlotIndex(slot);
    ItemStack existing = this.stacks.get(slot);
    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()) {
            this.stacks.set(slot, reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, limit) : stack);
        } else {
            existing.grow(reachedLimit ? limit : stack.getCount());
        }
        onContentsChanged(slot);
    }
    return reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, stack.getCount() - limit) : ItemStack.EMPTY;
}
Also used : ItemStack(net.minecraft.world.item.ItemStack) Nonnull(javax.annotation.Nonnull)

Example 8 with ItemStack

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

the class SlotItemHandler method getMaxStackSize.

@Override
public int getMaxStackSize(@Nonnull ItemStack stack) {
    ItemStack maxAdd = stack.copy();
    int maxInput = stack.getMaxStackSize();
    maxAdd.setCount(maxInput);
    IItemHandler handler = this.getItemHandler();
    ItemStack currentStack = handler.getStackInSlot(index);
    if (handler instanceof IItemHandlerModifiable) {
        IItemHandlerModifiable handlerModifiable = (IItemHandlerModifiable) handler;
        handlerModifiable.setStackInSlot(index, ItemStack.EMPTY);
        ItemStack remainder = handlerModifiable.insertItem(index, maxAdd, true);
        handlerModifiable.setStackInSlot(index, currentStack);
        return maxInput - remainder.getCount();
    } else {
        ItemStack remainder = handler.insertItem(index, maxAdd, true);
        int current = currentStack.getCount();
        int added = maxInput - remainder.getCount();
        return current + added;
    }
}
Also used : ItemStack(net.minecraft.world.item.ItemStack)

Example 9 with ItemStack

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

the class SidedInvWrapper method extractItem.

@Override
@Nonnull
public ItemStack extractItem(int slot, int amount, boolean simulate) {
    if (amount == 0)
        return ItemStack.EMPTY;
    int slot1 = getSlot(inv, slot, side);
    if (slot1 == -1)
        return ItemStack.EMPTY;
    ItemStack stackInSlot = inv.getItem(slot1);
    if (stackInSlot.isEmpty())
        return ItemStack.EMPTY;
    if (!inv.canTakeItemThroughFace(slot1, stackInSlot, side))
        return ItemStack.EMPTY;
    if (simulate) {
        if (stackInSlot.getCount() < amount) {
            return stackInSlot.copy();
        } else {
            ItemStack copy = stackInSlot.copy();
            copy.setCount(amount);
            return copy;
        }
    } else {
        int m = Math.min(stackInSlot.getCount(), amount);
        ItemStack ret = inv.removeItem(slot1, m);
        inv.setChanged();
        return ret;
    }
}
Also used : ItemStack(net.minecraft.world.item.ItemStack) Nonnull(javax.annotation.Nonnull)

Example 10 with ItemStack

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

the class ForgeRecipeProvider method enhance.

private Ingredient enhance(ResourceLocation name, Ingredient vanilla) {
    if (excludes.contains(name))
        return null;
    boolean modified = false;
    List<Value> items = new ArrayList<>();
    // This will probably crash between versions, if null fix index
    Value[] vanillaItems = getField(Ingredient.class, vanilla, 2);
    for (Value entry : vanillaItems) {
        if (entry instanceof ItemValue) {
            ItemStack stack = entry.getItems().stream().findFirst().orElse(ItemStack.EMPTY);
            Tag<Item> replacement = replacements.get(stack.getItem());
            if (replacement != null) {
                items.add(new TagValue(replacement));
                modified = true;
            } else
                items.add(entry);
        } else
            items.add(entry);
    }
    return modified ? Ingredient.fromValues(items.stream()) : null;
}
Also used : Item(net.minecraft.world.item.Item) ItemValue(net.minecraft.world.item.crafting.Ingredient.ItemValue) TagValue(net.minecraft.world.item.crafting.Ingredient.TagValue) ItemValue(net.minecraft.world.item.crafting.Ingredient.ItemValue) Value(net.minecraft.world.item.crafting.Ingredient.Value) ItemStack(net.minecraft.world.item.ItemStack) TagValue(net.minecraft.world.item.crafting.Ingredient.TagValue)

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