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