use of net.minecraft.inventory.ISidedInventory in project LogisticsPipes by RS485.
the class ModuleCrafter method spaceFor.
protected int spaceFor(ItemIdentifier item, boolean includeInTransit) {
Pair<String, ItemIdentifier> key = new Pair<>("spaceFor", item);
Object cache = _service.getCacheHolder().getCacheFor(CacheTypes.Inventory, key);
if (cache != null) {
int count = (Integer) cache;
if (includeInTransit) {
count -= _service.countOnRoute(item);
}
return count;
}
WorldCoordinatesWrapper worldCoordinates = new WorldCoordinatesWrapper(getWorld(), _service.getX(), _service.getY(), _service.getZ());
//@formatter:off
int count = worldCoordinates.getConnectedAdjacentTileEntities(ConnectionPipeType.ITEM).filter(adjacent -> adjacent.tileEntity instanceof IInventory).map(adjacent -> new Pair<>((IInventory) adjacent.tileEntity, adjacent.direction)).map(invDirPair -> {
if (invDirPair.getValue1() instanceof ISidedInventory) {
invDirPair.setValue1(new SidedInventoryMinecraftAdapter((ISidedInventory) invDirPair.getValue1(), invDirPair.getValue2().getOpposite(), false));
}
if (getUpgradeManager().hasSneakyUpgrade()) {
invDirPair.setValue2(getUpgradeManager().getSneakyOrientation());
}
IInventoryUtil inv = SimpleServiceLocator.inventoryUtilFactory.getInventoryUtil(invDirPair.getValue1(), invDirPair.getValue2());
return inv.roomForItem(item, 9999);
}).reduce(Integer::sum).orElse(0);
_service.getCacheHolder().setCache(CacheTypes.Inventory, key, count);
if (includeInTransit) {
count -= _service.countOnRoute(item);
}
return count;
}
use of net.minecraft.inventory.ISidedInventory in project LogisticsPipes by RS485.
the class SlotFinderNumberPacket method processPacket.
@Override
@SuppressWarnings("unchecked")
public void processPacket(EntityPlayer player) {
IInventory inv = this.getTile(player.worldObj, IInventory.class);
if (inv instanceof ISidedInventory) {
inv = new SidedInventoryMinecraftAdapter((ISidedInventory) inv, ForgeDirection.UNKNOWN, false);
}
IInventoryUtil util = SimpleServiceLocator.inventoryUtilFactory.getInventoryUtil(inv);
Slot result = null;
if (((List<Slot>) player.openContainer.inventorySlots).get(inventorySlot).slotNumber == inventorySlot) {
result = ((List<Slot>) player.openContainer.inventorySlots).get(inventorySlot);
}
if (result == null) {
for (Slot slotObject : (List<Slot>) player.openContainer.inventorySlots) {
if (slotObject.slotNumber == inventorySlot) {
result = slotObject;
break;
}
}
}
if (result == null) {
player.addChatComponentMessage(new ChatComponentTranslation("lp.chat.slotnotfound"));
}
int resultIndex = -1;
if (resultIndex == -1) {
ItemStack content = result.getStack();
if (content != null) {
for (int i = 0; i < util.getSizeInventory(); i++) {
if (content == util.getStackInSlot(i)) {
resultIndex = i;
break;
}
}
} else {
ItemStack dummyStack = new ItemStack(Blocks.stone, 0, 0);
NBTTagCompound nbt = new NBTTagCompound();
//Make it unique
nbt.setBoolean("LPStackFinderBoolean", true);
dummyStack.setTagCompound(nbt);
result.putStack(dummyStack);
for (int i = 0; i < util.getSizeInventory(); i++) {
if (dummyStack == util.getStackInSlot(i)) {
resultIndex = i;
break;
}
}
if (resultIndex == -1) {
for (int i = 0; i < util.getSizeInventory(); i++) {
ItemStack stack = util.getStackInSlot(i);
if (stack == null) {
continue;
}
if (ItemIdentifier.get(stack).equals(ItemIdentifier.get(dummyStack)) && stack.stackSize == dummyStack.stackSize) {
resultIndex = i;
break;
}
}
}
result.putStack(null);
}
}
if (resultIndex == -1) {
player.addChatComponentMessage(new ChatComponentTranslation("lp.chat.slotnotfound"));
} else {
//Copy pipe to coordinates to use the getPipe method
setPosX(getPipePosX());
setPosY(getPipePosY());
setPosZ(getPipePosZ());
ModuleActiveSupplier module = this.getLogisticsModule(player, ModuleActiveSupplier.class);
if (module != null) {
module.slotArray[slot] = resultIndex;
}
}
}
use of net.minecraft.inventory.ISidedInventory in project PneumaticCraft by MineMaarten.
the class DroneEntityAIInventoryExport method export.
private boolean export(ChunkPosition pos, boolean simulate) {
IInventory inv = IOHelper.getInventoryForTE(drone.getWorld().getTileEntity(pos.chunkPosX, pos.chunkPosY, pos.chunkPosZ));
if (inv != null) {
for (int i = 0; i < drone.getInventory().getSizeInventory(); i++) {
ItemStack droneStack = drone.getInventory().getStackInSlot(i);
if (droneStack != null) {
if (widget.isItemValidForFilters(droneStack)) {
for (int side = 0; side < 6; side++) {
droneStack = drone.getInventory().getStackInSlot(i);
if (((ISidedWidget) widget).getSides()[side] && droneStack != null) {
droneStack = droneStack.copy();
int oldCount = droneStack.stackSize;
if (((ICountWidget) widget).useCount())
droneStack.stackSize = Math.min(droneStack.stackSize, getRemainingCount());
ItemStack remainder = IOHelper.insert(inv, droneStack.copy(), side, simulate);
int stackSize = drone.getInventory().getStackInSlot(i).stackSize - (remainder == null ? droneStack.stackSize : droneStack.stackSize - remainder.stackSize);
droneStack.stackSize = stackSize;
int exportedItems = oldCount - stackSize;
if (!simulate) {
drone.getInventory().setInventorySlotContents(i, stackSize > 0 ? droneStack : null);
decreaseCount(exportedItems);
}
if (simulate && exportedItems > 0)
return true;
//doing it for every side for no side sensitive inventories would be a waste.
if (!(inv instanceof ISidedInventory))
break;
}
}
if (droneStack == null && !simulate)
drone.addAir(null, -PneumaticValues.DRONE_USAGE_INV);
else
drone.addDebugEntry("gui.progWidget.inventoryExport.debug.filledToMax", pos);
} else {
drone.addDebugEntry("gui.progWidget.inventoryExport.debug.stackdoesntPassFilter", pos);
}
}
}
} else {
drone.addDebugEntry("gui.progWidget.inventory.debug.noInventory", pos);
}
return false;
}
use of net.minecraft.inventory.ISidedInventory in project PneumaticCraft by MineMaarten.
the class IOHelper method extract.
/**
* Retrieves an item from the specified inventory. This item can be specified.
*
* @param tile
* @param direction
* @param requestedStack
* @param useItemCount
* if true, it'll only retrieve the stack of the exact item count given. it'll look in multiple slots of the inventory. if false, the
* first matching stack, ignoring item count, will be returned.
* @param simulate
* @param fuzzySetting
* ,
* @return
*/
public static ItemStack extract(TileEntity tile, ForgeDirection direction, ItemStack requestedStack, boolean useItemCount, boolean simulate, int fuzzySetting) {
if (requestedStack == null)
return requestedStack;
IInventory inv = getInventoryForTE(tile);
if (inv != null) {
int[] accessibleSlots;
if (inv instanceof ISidedInventory) {
accessibleSlots = ((ISidedInventory) inv).getAccessibleSlotsFromSide(direction.ordinal());
} else {
accessibleSlots = new int[inv.getSizeInventory()];
for (int i = 0; i < accessibleSlots.length; i++) accessibleSlots[i] = i;
}
int itemsFound = 0;
for (int slot : accessibleSlots) {
ItemStack stack = inv.getStackInSlot(slot);
if (stack != null && stack.stackSize > 0 && stack.isItemEqual(requestedStack) && IOHelper.canExtractItemFromInventory(inv, stack, slot, direction.ordinal())) {
if (!useItemCount) {
if (!simulate) {
inv.setInventorySlotContents(slot, null);
}
return stack;
}
itemsFound += stack.stackSize;
}
}
if (itemsFound >= requestedStack.stackSize) {
ItemStack exportedStack = null;
int itemsNeeded = requestedStack.stackSize;
for (int slot : accessibleSlots) {
ItemStack stack = inv.getStackInSlot(slot);
if (stack != null && stack.isItemEqual(requestedStack) && IOHelper.canExtractItemFromInventory(inv, stack, slot, direction.ordinal())) {
int itemsSubstracted = Math.min(itemsNeeded, stack.stackSize);
if (itemsSubstracted > 0)
exportedStack = stack;
itemsNeeded -= itemsSubstracted;
if (!simulate) {
stack.stackSize -= itemsSubstracted;
if (stack.stackSize == 0) {
inv.setInventorySlotContents(slot, null);
}
tile.markDirty();
}
}
}
exportedStack = exportedStack.copy();
exportedStack.stackSize = requestedStack.stackSize;
return exportedStack;
}
}
return null;
}
use of net.minecraft.inventory.ISidedInventory in project PneumaticCraft by MineMaarten.
the class IOHelper method extractOneItem.
public static ItemStack extractOneItem(TileEntity tile, ForgeDirection dir, boolean simulate) {
IInventory inv = getInventoryForTE(tile);
if (inv != null) {
int[] accessibleSlots;
if (inv instanceof ISidedInventory) {
accessibleSlots = ((ISidedInventory) inv).getAccessibleSlotsFromSide(dir.ordinal());
} else {
accessibleSlots = new int[inv.getSizeInventory()];
for (int i = 0; i < accessibleSlots.length; i++) accessibleSlots[i] = i;
}
for (int slot : accessibleSlots) {
ItemStack stack = inv.getStackInSlot(slot);
if (stack != null && stack.stackSize > 0 && IOHelper.canExtractItemFromInventory(inv, stack, slot, dir.ordinal())) {
if (simulate) {
ItemStack ret = stack.copy();
ret.stackSize = 1;
return ret;
}
ItemStack ret = stack.splitStack(1);
if (stack.stackSize == 0)
inv.setInventorySlotContents(slot, null);
tile.markDirty();
return ret;
}
}
}
return null;
}
Aggregations