use of logisticspipes.interfaces.IInventoryUtil in project LogisticsPipes by RS485.
the class ModuleCrafter method extractFromIInventory.
private ItemStack extractFromIInventory(IInventory inv, IResource wanteditem, int count, ForgeDirection dir) {
IInventoryUtil invUtil = SimpleServiceLocator.inventoryUtilFactory.getInventoryUtil(inv, dir);
ItemIdentifier itemToExtract = null;
if (wanteditem instanceof ItemResource) {
itemToExtract = ((ItemResource) wanteditem).getItem();
} else if (wanteditem instanceof DictResource) {
int max = Integer.MIN_VALUE;
ItemIdentifier toExtract = null;
for (Map.Entry<ItemIdentifier, Integer> content : invUtil.getItemsAndCount().entrySet()) {
if (wanteditem.matches(content.getKey(), IResource.MatchSettings.NORMAL)) {
if (content.getValue() > max) {
max = content.getValue();
toExtract = content.getKey();
}
}
}
if (toExtract == null) {
return null;
}
itemToExtract = toExtract;
}
int available = invUtil.itemCount(itemToExtract);
if (available == 0) {
return null;
}
if (!_service.useEnergy(neededEnergy() * Math.min(count, available))) {
return null;
}
return invUtil.getMultipleItems(itemToExtract, Math.min(count, available));
}
use of logisticspipes.interfaces.IInventoryUtil in project LogisticsPipes by RS485.
the class ModuleElectricBuffer method tick.
@Override
public void tick() {
if (++currentTickCount < ticksToAction) {
return;
}
currentTickCount = 0;
IInventoryUtil inv = _service.getPointedInventory(true);
if (inv == null) {
return;
}
for (int i = 0; i < inv.getSizeInventory(); i++) {
ItemStack stack = inv.getStackInSlot(i);
if (stack == null) {
continue;
}
if (SimpleServiceLocator.IC2Proxy.isElectricItem(stack)) {
Triplet<Integer, SinkReply, List<IFilter>> reply = SimpleServiceLocator.logisticsManager.hasDestinationWithMinPriority(ItemIdentifier.get(stack), _service.getSourceID(), true, FixedPriority.ElectricManager);
if (reply == null) {
continue;
}
_service.spawnParticle(Particles.OrangeParticle, 2);
_service.sendStack(inv.decrStackSize(i, 1), reply, ItemSendMode.Normal);
return;
}
continue;
}
}
use of logisticspipes.interfaces.IInventoryUtil in project LogisticsPipes by RS485.
the class ModuleExtractor method tick.
@Override
public void tick() {
if (++currentTick < ticksToAction()) {
return;
}
currentTick = 0;
//Extract Item
IInventory realInventory = _service.getRealInventory();
if (realInventory == null) {
return;
}
ForgeDirection extractOrientation = _sneakyDirection;
if (extractOrientation == ForgeDirection.UNKNOWN) {
extractOrientation = _service.inventoryOrientation().getOpposite();
}
IInventoryUtil targetUtil = _service.getSneakyInventory(extractOrientation, true);
for (int i = 0; i < targetUtil.getSizeInventory(); i++) {
ItemStack slot = targetUtil.getStackInSlot(i);
if (slot == null) {
continue;
}
ItemIdentifier slotitem = ItemIdentifier.get(slot);
List<Integer> jamList = new LinkedList<>();
Pair<Integer, SinkReply> reply = _service.hasDestination(slotitem, true, jamList);
if (reply == null) {
continue;
}
int itemsleft = itemsToExtract();
while (reply != null) {
int count = Math.min(itemsleft, slot.stackSize);
count = Math.min(count, slotitem.getMaxStackSize());
if (reply.getValue2().maxNumberOfItems > 0) {
count = Math.min(count, reply.getValue2().maxNumberOfItems);
}
while (!_service.useEnergy(neededEnergy() * count) && count > 0) {
_service.spawnParticle(Particles.OrangeParticle, 2);
count--;
}
if (count <= 0) {
break;
}
ItemStack stackToSend = targetUtil.decrStackSize(i, count);
if (stackToSend == null || stackToSend.stackSize == 0) {
break;
}
count = stackToSend.stackSize;
_service.sendStack(stackToSend, reply, itemSendMode());
itemsleft -= count;
if (itemsleft <= 0) {
break;
}
slot = targetUtil.getStackInSlot(i);
if (slot == null) {
break;
}
jamList.add(reply.getValue1());
reply = _service.hasDestination(ItemIdentifier.get(slot), true, jamList);
}
break;
}
}
use of logisticspipes.interfaces.IInventoryUtil in project LogisticsPipes by RS485.
the class ModuleCCBasedQuickSort method tick.
@Override
public void tick() {
IInventoryUtil invUtil = _service.getPointedInventory(true);
if (invUtil == null) {
return;
}
handleSinkResponses(invUtil);
if (--currentTick > 0) {
return;
}
if (stalled) {
currentTick = stalledDelay;
} else {
currentTick = normalDelay;
}
if (!_service.canUseEnergy(500)) {
stalled = true;
return;
}
if ((!(invUtil instanceof SpecialInventoryHandler) && invUtil.getSizeInventory() == 0) || !_service.canUseEnergy(500)) {
stalled = true;
return;
}
//incremented at the end of the previous loop.
if (lastStackLookedAt >= invUtil.getSizeInventory()) {
lastStackLookedAt = 0;
}
ItemStack slot = invUtil.getStackInSlot(lastStackLookedAt);
while (slot == null) {
lastStackLookedAt++;
if (lastStackLookedAt >= invUtil.getSizeInventory()) {
lastStackLookedAt = 0;
}
slot = invUtil.getStackInSlot(lastStackLookedAt);
if (lastStackLookedAt == lastSuceededStack) {
stalled = true;
send();
// then we have been around the list without sending, halt for now
return;
}
}
send();
if (!sinkResponses.containsKey(lastStackLookedAt)) {
createSinkMessage(lastStackLookedAt, ItemIdentifierStack.getFromStack(slot));
}
lastStackLookedAt++;
checkSize();
}
use of logisticspipes.interfaces.IInventoryUtil 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;
}
}
}
Aggregations