use of net.minecraft.inventory.ISidedInventory in project PneumaticCraft by MineMaarten.
the class IOHelper method extract.
public static ItemStack extract(IInventory inventory, ForgeDirection direction, boolean simulate) {
if (inventory instanceof ISidedInventory) {
ISidedInventory isidedinventory = (ISidedInventory) inventory;
int[] accessibleSlotsFromSide = isidedinventory.getAccessibleSlotsFromSide(direction.ordinal());
for (int anAccessibleSlotsFromSide : accessibleSlotsFromSide) {
ItemStack stack = extract(inventory, direction, anAccessibleSlotsFromSide, simulate);
if (stack != null)
return stack;
}
} else {
int j = inventory.getSizeInventory();
for (int k = 0; k < j; ++k) {
ItemStack stack = extract(inventory, direction, k, simulate);
if (stack != null)
return stack;
}
}
return null;
}
use of net.minecraft.inventory.ISidedInventory in project Engine by VoltzEngine-Project.
the class InternalInventoryHandler method tryPlaceInPosition.
/**
* Tries to place an itemStack in a specific position if it is an inventory.
*
* @return The ItemStack remained after place attempt
*/
public ItemStack tryPlaceInPosition(ItemStack itemStack, Pos position, ForgeDirection dir) {
TileEntity tileEntity = position.getTileEntity(world);
ForgeDirection direction = dir.getOpposite();
if (tileEntity != null && itemStack != null) {
if (tileEntity instanceof TileEntityChest) {
TileEntityChest[] chests = { (TileEntityChest) tileEntity, null };
/**
* Try to find a double chest.
*/
for (int i = 2; i < 6; i++) {
ForgeDirection searchDirection = ForgeDirection.getOrientation(i);
Pos searchPosition = position.clone();
searchPosition.add(searchDirection);
if (searchPosition.getTileEntity(world) != null) {
if (searchPosition.getTileEntity(world).getClass() == chests[0].getClass()) {
chests[1] = (TileEntityChest) searchPosition.getTileEntity(world);
break;
}
}
}
for (TileEntityChest chest : chests) {
if (chest != null) {
for (int i = 0; i < chest.getSizeInventory(); i++) {
itemStack = this.addStackToInventory(i, chest, itemStack);
if (itemStack == null) {
return null;
}
}
}
}
} else if (tileEntity instanceof IExtendedStorage) {
return ((IExtendedStorage) tileEntity).addStackToStorage(itemStack);
} else if (tileEntity instanceof ISidedInventory) {
ISidedInventory inventory = (ISidedInventory) tileEntity;
int[] slots = inventory.getAccessibleSlotsFromSide(direction.ordinal());
for (int i = 0; i < slots.length; i++) {
if (inventory.canInsertItem(slots[i], itemStack, direction.ordinal())) {
itemStack = this.addStackToInventory(slots[i], inventory, itemStack);
}
if (itemStack == null) {
return null;
}
}
} else if (tileEntity instanceof IInventory) {
IInventory inventory = (IInventory) tileEntity;
for (int i = 0; i < inventory.getSizeInventory(); i++) {
itemStack = this.addStackToInventory(i, inventory, itemStack);
if (itemStack == null) {
return null;
}
}
}
}
if (itemStack == null || itemStack.stackSize <= 0) {
return null;
}
return itemStack;
}
use of net.minecraft.inventory.ISidedInventory in project Engine by VoltzEngine-Project.
the class InventoryUtility method putStackInInventory.
/**
* Tries to place an item into the inventory. If the inventory is not an instance of {@link ISidedInventory} it
* will ignore the side param.
*
* @param inventory - inventory to insert the item into
* @param itemStack - stack to insert
* @param side - side to inser the item into (0-5)
* @param force- overrides {@link IInventory#isItemValidForSlot(int, ItemStack)} check
* @return what is left of the toInsert stack
*/
public static ItemStack putStackInInventory(IInventory inventory, ItemStack itemStack, int side, boolean force) {
ItemStack toInsert = itemStack != null ? itemStack.copy() : null;
if (toInsert != null) {
if (!(inventory instanceof ISidedInventory)) {
return putStackInInventory(inventory, toInsert, force);
} else {
ISidedInventory sidedInventory = (ISidedInventory) inventory;
int[] slots = sidedInventory.getAccessibleSlotsFromSide(side);
if (slots != null && slots.length != 0) {
for (int get = 0; get < slots.length; get++) {
int slotID = slots[get];
if (force || sidedInventory.isItemValidForSlot(slotID, toInsert) && sidedInventory.canInsertItem(slotID, toInsert, side)) {
ItemStack inSlot = inventory.getStackInSlot(slotID);
if (inSlot == null) {
inventory.setInventorySlotContents(slotID, toInsert);
return null;
} else if (inSlot.isItemEqual(toInsert) && inSlot.stackSize < inSlot.getMaxStackSize()) {
if (inSlot.stackSize + toInsert.stackSize <= inSlot.getMaxStackSize()) {
ItemStack toSet = toInsert.copy();
toSet.stackSize += inSlot.stackSize;
inventory.setInventorySlotContents(slotID, toSet);
return null;
} else {
int rejects = (inSlot.stackSize + toInsert.stackSize) - inSlot.getMaxStackSize();
ItemStack toSet = toInsert.copy();
toSet.stackSize = inSlot.getMaxStackSize();
ItemStack remains = toInsert.copy();
remains.stackSize = rejects;
inventory.setInventorySlotContents(slotID, toSet);
toInsert = remains;
}
}
}
}
}
}
}
return toInsert;
}
use of net.minecraft.inventory.ISidedInventory in project Engine by VoltzEngine-Project.
the class InventoryUtility method findFirstItemInInventory.
/**
* Gets the first slot containing items
* <p>
* Does not actually consume the items
*
* @param inventory - inventory to search for items
* @param side - side to access, used for {@link ISidedInventory}
* If this value is not between 0-5 it will not use
* ISideInventory and instead bypass the sided checks
* @param stackSize - amount to remove
* @return pair containing the removed stack, and item
*/
public static Pair<ItemStack, Integer> findFirstItemInInventory(IInventory inventory, int side, int stackSize, IInventoryFilter filter) {
if (!(inventory instanceof ISidedInventory) || side == -1 || side > 5) {
for (int i = inventory.getSizeInventory() - 1; i >= 0; i--) {
final ItemStack slotStack = inventory.getStackInSlot(i);
if (slotStack != null && (filter == null || filter.isStackInFilter(slotStack))) {
int amountToTake = stackSize <= 0 ? slotStack.getMaxStackSize() : Math.min(stackSize, slotStack.getMaxStackSize());
amountToTake = Math.min(amountToTake, slotStack.stackSize);
ItemStack toSend = slotStack.copy();
toSend.stackSize = amountToTake;
// inventory.decrStackSize(i, amountToTake);
return new Pair(toSend, i);
}
}
} else {
ISidedInventory sidedInventory = (ISidedInventory) inventory;
int[] slots = sidedInventory.getAccessibleSlotsFromSide(side);
if (slots != null) {
for (int get = slots.length - 1; get >= 0; get--) {
int slotID = slots[get];
final ItemStack slotStack = sidedInventory.getStackInSlot(slotID);
if (slotStack != null && (filter == null || filter.isStackInFilter(slotStack))) {
int amountToTake = stackSize <= 0 ? slotStack.getMaxStackSize() : Math.min(stackSize, slotStack.getMaxStackSize());
amountToTake = Math.min(amountToTake, slotStack.stackSize);
ItemStack toSend = slotStack.copy();
toSend.stackSize = amountToTake;
if (sidedInventory.canExtractItem(slotID, toSend, side)) {
// sidedInventory.decrStackSize(slotID, amountToTake);
return new Pair(toSend, slotID);
}
}
}
}
}
return null;
}
use of net.minecraft.inventory.ISidedInventory in project Engine by VoltzEngine-Project.
the class InternalInventoryHandler method tryGrabFromPosition.
/**
* Tries to get an item from a position
*
* @param position - location of item
* @param dir - direction this item is from the original
* @param ammount - amount up to one stack to grab
* @return the grabbed item stack
*/
public ItemStack tryGrabFromPosition(Pos position, ForgeDirection dir, int ammount) {
ItemStack returnStack = null;
TileEntity tileEntity = position.getTileEntity(world);
ForgeDirection direction = dir.getOpposite();
if (tileEntity != null) {
if (tileEntity.getClass() == TileEntityChest.class) {
TileEntityChest[] chests = { (TileEntityChest) tileEntity, null };
/**
* Try to find a double chest.
*/
for (int i = 2; i < 6; i++) {
ForgeDirection searchDirection = ForgeDirection.getOrientation(i);
Pos searchPosition = position.clone();
searchPosition.add(searchDirection);
if (searchPosition.getTileEntity(world) != null) {
if (searchPosition.getTileEntity(world).getClass() == chests[0].getClass()) {
chests[1] = (TileEntityChest) searchPosition.getTileEntity(world);
break;
}
}
}
chestSearch: for (TileEntityChest chest : chests) {
if (chest != null) {
for (int i = 0; i < chest.getSizeInventory(); i++) {
ItemStack itemStack = this.removeStackFromInventory(i, chest, ammount);
if (itemStack != null) {
returnStack = itemStack;
break chestSearch;
}
}
}
}
} else if (tileEntity instanceof ISidedInventory) {
ISidedInventory inventory = (ISidedInventory) tileEntity;
int[] slots = inventory.getAccessibleSlotsFromSide(direction.ordinal());
for (int i = 0; i < slots.length; i++) {
int slot = slots[i];
ItemStack slotStack = inventory.getStackInSlot(slot);
if (inventory.canExtractItem(slot, slotStack, direction.ordinal())) {
ItemStack itemStack = this.removeStackFromInventory(slot, inventory, ammount);
if (itemStack != null) {
returnStack = itemStack;
break;
}
}
}
} else if (tileEntity instanceof IInventory) {
IInventory inventory = (IInventory) tileEntity;
for (int i = 0; i < inventory.getSizeInventory(); i++) {
ItemStack itemStack = this.removeStackFromInventory(i, inventory, ammount);
if (itemStack != null) {
returnStack = itemStack;
break;
}
}
}
}
return returnStack;
}
Aggregations