use of net.minecraft.inventory.ISidedInventory in project Bookshelf by Darkhax-Minecraft.
the class InventoryUtils method getInventory.
/**
* Gets an inventory from a position within the world. If no tile exists or the tile does
* not have the inventory capability the empty inventory handler will be returned.
*
* @param world The world instance.
* @param pos The position of the expected tile entity.
* @param side The side to access the inventory from.
* @return The inventory handler. Will be empty if none was found.
*/
public static IItemHandler getInventory(World world, BlockPos pos, Direction side) {
final TileEntity tileEntity = world.getBlockEntity(pos);
if (tileEntity != null) {
final LazyOptional<IItemHandler> inventoryCap = tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side);
return inventoryCap.orElse(EmptyHandler.INSTANCE);
} else {
// Some blocks like composters are not tile entities so their inv can not be
// accessed through the normal capability system.
final BlockState state = world.getBlockState(pos);
if (state.getBlock() instanceof ISidedInventoryProvider) {
final ISidedInventoryProvider inventoryProvider = (ISidedInventoryProvider) state.getBlock();
final ISidedInventory inventory = inventoryProvider.getContainer(state, world, pos);
if (inventory != null) {
return new SidedInvWrapper(inventory, side);
}
}
}
return EmptyHandler.INSTANCE;
}
use of net.minecraft.inventory.ISidedInventory in project ArsMagica2 by Mithion.
the class InventoryUtilities method getFirstStackStartingFromSlot.
public static GetFirstStackStartingFromSlotResult getFirstStackStartingFromSlot(IInventory inventory, ItemStack itemStack, int slot, int side) {
if (inventory instanceof ISidedInventory) {
ISidedInventory sidededInventory = (ISidedInventory) inventory;
int[] slots = sidededInventory.getAccessibleSlotsFromSide(side);
for (int i = slot; i < slots.length; i++) {
itemStack = inventory.getStackInSlot(slots[i]);
if (itemStack != null && canExtractItemFromInventory(sidededInventory, itemStack, slots[i], side)) {
return new GetFirstStackStartingFromSlotResult(i, itemStack);
}
}
} else {
return getFirstStackStartingFromSlot(inventory, itemStack, slot);
}
return new GetFirstStackStartingFromSlotResult(-1, null);
}
use of net.minecraft.inventory.ISidedInventory in project ArsMagica2 by Mithion.
the class InventoryUtilities method inventoryHasItem.
public static boolean inventoryHasItem(IInventory inventory, ItemStack search, int quantity, int side) {
if (inventory instanceof ISidedInventory) {
ISidedInventory sidedInventory = (ISidedInventory) inventory;
int qtyFound = 0;
int[] slots = sidedInventory.getAccessibleSlotsFromSide(side);
for (int i = 0; i < slots.length; i++) {
ItemStack inventoryStack = inventory.getStackInSlot(slots[i]);
if (inventoryStack == null)
continue;
else if (compareItemStacks(inventoryStack, search, true, false, true, true)) {
qtyFound += inventoryStack.stackSize;
if (qtyFound >= quantity)
return true;
}
}
return false;
} else {
return inventoryHasItem(inventory, search, quantity);
}
}
use of net.minecraft.inventory.ISidedInventory in project ArsMagica2 by Mithion.
the class InventoryUtilities method getLikeItemCount.
public static int getLikeItemCount(IInventory inventory, ItemStack stack, int side) {
if (inventory instanceof ISidedInventory) {
int totalCount = 0;
ISidedInventory sidedInventory = (ISidedInventory) inventory;
int[] slots = sidedInventory.getAccessibleSlotsFromSide(side);
for (int i = 0; i < slots.length; i++) {
ItemStack invStack = inventory.getStackInSlot(slots[i]);
if (invStack != null && compareItemStacks(invStack, stack, true, false, true, true))
totalCount += invStack.stackSize;
}
return totalCount;
} else {
return getLikeItemCount(inventory, stack);
}
}
use of net.minecraft.inventory.ISidedInventory in project ArsMagica2 by Mithion.
the class InventoryUtilities method mergeIntoInventory.
public static boolean mergeIntoInventory(IInventory inventory, ItemStack toMerge, int quantity, int side) {
if (inventory instanceof ISidedInventory) {
ItemStack stack = toMerge.splitStack(Math.min(toMerge.stackSize, quantity));
ISidedInventory sidedInventory = (ISidedInventory) inventory;
int[] slots = sidedInventory.getAccessibleSlotsFromSide(side);
boolean flag = false;
for (int i = 0; i < slots.length && stack != null && stack.stackSize > 0; ++i) {
// For each slot that can be accessed from this side
ItemStack prvStack = sidedInventory.getStackInSlot(slots[i]);
if (InventoryUtilities.canInsertItemToInventory(sidedInventory, stack, slots[i], side)) {
// if the items can be inserted into the current slot
if (prvStack == null) {
// if the stack in the slot is null then get the max value that can be moved and transfer the stack to the inventory
int max = Math.min(stack.getMaxStackSize(), sidedInventory.getInventoryStackLimit());
if (max >= stack.stackSize) {
sidedInventory.setInventorySlotContents(slots[i], stack.copy());
stack.stackSize = 0;
flag = true;
} else {
sidedInventory.setInventorySlotContents(slots[i], stack.splitStack(max));
flag = true;
}
} else if (InventoryUtilities.canStacksMerge(prvStack, stack)) {
// if the stack in the slot can be merged with the stack we are trying to move get the max items that can exist in the slot
// and insert as many as will fit from the stack we are trying to move
int max = Math.min(stack.getMaxStackSize(), sidedInventory.getInventoryStackLimit());
if (max > prvStack.stackSize) {
int qty = Math.min(stack.stackSize, max - prvStack.stackSize);
prvStack.stackSize += qty;
stack.stackSize -= qty;
flag = qty > 0;
}
}
}
}
toMerge.stackSize = toMerge.stackSize + stack.stackSize;
return flag;
} else {
return mergeIntoInventory(inventory, toMerge, quantity);
}
}
Aggregations