use of mekanism.common.item.block.ItemBlockBin in project Mekanism by mekanism.
the class BinExtractRecipe method getRemainingItems.
@Override
public NonNullList<ItemStack> getRemainingItems(CraftingInventory inv) {
NonNullList<ItemStack> remaining = NonNullList.withSize(inv.getContainerSize(), ItemStack.EMPTY);
for (int i = 0; i < remaining.size(); ++i) {
ItemStack stackInSlot = inv.getItem(i);
if (stackInSlot.getItem() instanceof ItemBlockBin) {
ItemStack binStack = stackInSlot.copy();
BinInventorySlot slot = convertToSlot(binStack);
ItemStack bottomStack = slot.getBottomStack();
if (!bottomStack.isEmpty()) {
// Only attempt to do anything if there are items to try and remove
MekanismUtils.logMismatchedStackSize(slot.shrinkStack(bottomStack.getCount(), Action.EXECUTE), bottomStack.getCount());
remaining.set(i, binStack);
}
break;
}
}
return remaining;
}
use of mekanism.common.item.block.ItemBlockBin in project Mekanism by mekanism.
the class BinInsertRecipe method getRemainingItems.
@Override
public NonNullList<ItemStack> getRemainingItems(CraftingInventory inv) {
NonNullList<ItemStack> remainingItems = NonNullList.withSize(inv.getContainerSize(), ItemStack.EMPTY);
ItemStack binStack = ItemStack.EMPTY;
ItemStack foundType = ItemStack.EMPTY;
IntList foundSlots = new IntArrayList();
for (int i = 0; i < inv.getContainerSize(); ++i) {
ItemStack stackInSlot = inv.getItem(i);
if (!stackInSlot.isEmpty()) {
if (stackInSlot.getItem() instanceof ItemBlockBin) {
if (!binStack.isEmpty()) {
// If we already have a bin then this is not a bin recipe
return remainingItems;
}
binStack = stackInSlot;
continue;
} else if (foundType.isEmpty()) {
foundType = stackInSlot;
} else if (!ItemHandlerHelper.canItemStacksStack(foundType, stackInSlot)) {
// then we cannot combine them both into the bin
return remainingItems;
}
foundSlots.add(i);
}
}
if (binStack.isEmpty() || foundType.isEmpty()) {
// If we didn't find a bin or an item to add it, we don't match the bin insertion recipe
return remainingItems;
}
// Copy the stack
binStack = binStack.copy();
BinInventorySlot slot = convertToSlot(binStack);
for (int i = 0; i < foundSlots.size(); i++) {
int index = foundSlots.getInt(i);
ItemStack stack = StackUtils.size(inv.getItem(index), 1);
ItemStack remaining = slot.insertItem(stack, Action.EXECUTE, AutomationType.MANUAL);
remainingItems.set(index, remaining);
}
return remainingItems;
}
use of mekanism.common.item.block.ItemBlockBin in project Mekanism by mekanism.
the class ItemRecipeData method applyToStack.
@Override
public boolean applyToStack(ItemStack stack) {
if (slots.isEmpty()) {
return true;
}
Item item = stack.getItem();
boolean isBin = item instanceof ItemBlockBin;
Optional<IItemHandler> capability = stack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).resolve();
List<IInventorySlot> slots = new ArrayList<>();
if (capability.isPresent()) {
IItemHandler itemHandler = capability.get();
for (int i = 0; i < itemHandler.getSlots(); i++) {
int slot = i;
slots.add(new DummyInventorySlot(itemHandler.getSlotLimit(slot), itemStack -> itemHandler.isItemValid(slot, itemStack), isBin));
}
} else if (item instanceof BlockItem) {
TileEntityMekanism tile = getTileFromBlock(((BlockItem) item).getBlock());
if (tile == null || !tile.persistInventory()) {
// Something went wrong
return false;
}
for (int i = 0; i < tile.getSlots(); i++) {
int slot = i;
slots.add(new DummyInventorySlot(tile.getSlotLimit(slot), itemStack -> tile.isItemValid(slot, itemStack), isBin));
}
} else if (item instanceof ItemRobit) {
// Inventory slots
for (int slotY = 0; slotY < 3; slotY++) {
for (int slotX = 0; slotX < 9; slotX++) {
slots.add(new DummyInventorySlot(BasicInventorySlot.DEFAULT_LIMIT, BasicInventorySlot.alwaysTrue, false));
}
}
// Energy slot
slots.add(new DummyInventorySlot(BasicInventorySlot.DEFAULT_LIMIT, itemStack -> {
if (EnergyCompatUtils.hasStrictEnergyHandler(itemStack)) {
return true;
}
ItemStackToEnergyRecipe foundRecipe = MekanismRecipeType.ENERGY_CONVERSION.getInputCache().findTypeBasedRecipe(null, itemStack);
return foundRecipe != null && !foundRecipe.getOutput(itemStack).isZero();
}, false));
// Smelting input slot
slots.add(new DummyInventorySlot(BasicInventorySlot.DEFAULT_LIMIT, itemStack -> MekanismRecipeType.SMELTING.getInputCache().containsInput(null, itemStack), false));
// Smelting output slot
slots.add(new DummyInventorySlot(BasicInventorySlot.DEFAULT_LIMIT, BasicInventorySlot.alwaysTrue, false));
} else if (item instanceof ISustainedInventory) {
// Fallback just save it all
for (IInventorySlot slot : this.slots) {
if (!slot.isEmpty()) {
// We have no information about what our item supports, but we have at least some stacks we want to transfer
((ISustainedInventory) stack.getItem()).setInventory(DataHandlerUtils.writeContainers(this.slots), stack);
return true;
}
}
return true;
} else {
return false;
}
if (slots.isEmpty()) {
// We don't actually have any tanks in the output
return true;
}
// TODO: Improve the logic so that it maybe tries multiple different slot combinations
IMekanismInventory outputHandler = new IMekanismInventory() {
@Nonnull
@Override
public List<IInventorySlot> getInventorySlots(@Nullable Direction side) {
return slots;
}
@Override
public void onContentsChanged() {
}
};
boolean hasData = false;
for (IInventorySlot slot : this.slots) {
if (!slot.isEmpty()) {
if (!ItemHandlerHelper.insertItemStacked(outputHandler, slot.getStack(), false).isEmpty()) {
// If we have a remainder something failed so bail
return false;
}
hasData = true;
}
}
if (hasData) {
// We managed to transfer it all into valid slots, so save it to the stack
((ISustainedInventory) stack.getItem()).setInventory(DataHandlerUtils.writeContainers(slots), stack);
}
return true;
}
use of mekanism.common.item.block.ItemBlockBin in project Mekanism by mekanism.
the class BinInsertRecipe method matches.
@Override
public boolean matches(CraftingInventory inv, World world) {
ItemStack binStack = ItemStack.EMPTY;
ItemStack foundType = ItemStack.EMPTY;
for (int i = 0; i < inv.getContainerSize(); ++i) {
ItemStack stackInSlot = inv.getItem(i);
if (!stackInSlot.isEmpty()) {
if (stackInSlot.getItem() instanceof ItemBlockBin) {
if (!binStack.isEmpty()) {
// If we already have a bin then this is not a bin recipe
return false;
}
binStack = stackInSlot;
} else if (foundType.isEmpty()) {
foundType = stackInSlot;
} else if (!ItemHandlerHelper.canItemStacksStack(foundType, stackInSlot)) {
// then we cannot combine them both into the bin
return false;
}
}
}
if (binStack.isEmpty() || foundType.isEmpty()) {
// If we didn't find a bin or an item to add it, we don't match the bin insertion recipe
return false;
}
BinInventorySlot slot = convertToSlot(binStack);
ItemStack remaining = slot.insertItem(foundType, Action.SIMULATE, AutomationType.MANUAL);
// Return that it doesn't match if our simulation claims we would not be able to accept any items into the bin
return !ItemStack.matches(remaining, foundType);
}
use of mekanism.common.item.block.ItemBlockBin in project Mekanism by mekanism.
the class BinInsertRecipe method assemble.
@Override
public ItemStack assemble(CraftingInventory inv) {
ItemStack binStack = ItemStack.EMPTY;
ItemStack foundType = ItemStack.EMPTY;
List<ItemStack> foundItems = new ArrayList<>();
for (int i = 0; i < inv.getContainerSize(); ++i) {
ItemStack stackInSlot = inv.getItem(i);
if (!stackInSlot.isEmpty()) {
if (stackInSlot.getItem() instanceof ItemBlockBin) {
if (!binStack.isEmpty()) {
// If we already have a bin then this is not a bin recipe
return ItemStack.EMPTY;
}
binStack = stackInSlot;
continue;
} else if (foundType.isEmpty()) {
foundType = stackInSlot;
} else if (!ItemHandlerHelper.canItemStacksStack(foundType, stackInSlot)) {
// then we cannot combine them both into the bin
return ItemStack.EMPTY;
}
foundItems.add(StackUtils.size(stackInSlot, 1));
}
}
if (binStack.isEmpty() || foundType.isEmpty()) {
// If we didn't find a bin or an item to add it, we don't match the bin insertion recipe
return ItemStack.EMPTY;
}
// Copy the stack
binStack = binStack.copy();
BinInventorySlot slot = convertToSlot(binStack);
boolean hasInserted = false;
for (ItemStack stack : foundItems) {
if (ItemStack.matches(stack, slot.insertItem(stack, Action.EXECUTE, AutomationType.MANUAL))) {
if (hasInserted) {
// exit and return our stack
break;
}
// Return that it doesn't match if our simulation claims we would not be able to accept any items into the bin
return ItemStack.EMPTY;
}
hasInserted = true;
}
ItemDataUtils.setBoolean(binStack, NBTConstants.FROM_RECIPE, true);
return binStack;
}
Aggregations