This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Kezaraux's profile picture

How to account for redundancies.

Started by Kezaraux, 14 April 2016 - 12:34 AM
Kezaraux #1
Posted 14 April 2016 - 02:34 AM
You can skip my intro if you want, it is kinda long….

Hi, so let me start this off by saying that I'm playing on the Direwolf20 pack, Minecraft version 1.6.4. What I'm attempting to do is automate the removal of saplings from a barrel into a Forestry fermenter. The way I go about doing this is I have my computer wrap the barrel its next to as well as the fermenter over rednet. My program basically checks if there is more than 63 items in the barrel (I'm going to explain this part a little as well) and that there are "no items" in the "first" slot in the Fermenter. Now, this is where my issue occurs.

Thanks to OpenPeripherals, I'm actually able to check the inventories on these objects, but thanks to OpenPeripherals being the way it is, I use the .getAllStacks() method, assuming that this will ALWAYS return all the inventories, even if there isn't anything in them. NO. I AM WRONG. That method will only return a table IF there is something in the fermenter's inventory. However, lets say in my head I map slot 1 to where the saplings go, and slot 2 for fertilizer. Sounds simple, yeah? Well, no, it isn't. The method returns a table, first containing an index for each slot, then each slots information in another table. However. lets say I have no saplings (slot 1 is empty), you might assume that there would just be un-initialized information in the table. Well, no, slot one on the table now gathers information from slot 2, the fertilizer.

–Skip to here!–

My issue is this: when using OpenPeripheral's getAllStacks() methods, how do I account for an empty inventory slot, thus breaking my program (as follows)?


local barrel = peripheral.wrap("left")
local ferm = peripheral.wrap("factory_3_0")
local barInv = 0
local fermInv = 0

local fermTab = ferm.getAllStacks()
local barTab = barrel.getAllStacks()

function update()
  barTab = barrel.getAllStacks()
  fermTab = ferm.getAllStacks()
  barInv = barTab[2].qty
  fermInv = fermTab[1].qty
  term.clear()
  term.setCursorPos(1,1)
  print("Barrel: "..barInv)
  print("Fermenter: "..fermInv.." ("..fermTab[1].name..")")
end

function checkAmount()
  if (barInv > 63 and fermTab[1].name == "Fertilizer") then
	redstone.setOutput("bottom", true)
	sleep(2)
	redstone.setOutput("bottom", false)
  end
end

function mainLoop()
  while true do
	update()
	checkAmount()
	sleep(20)
  end
end

So, a quick rundown, I init all my variables, I run update everytime to get current information, I then use that information to pass some checks on whether or not to output redstone to draw some saplings out of the barrel and into my fermenter. You might notice, that I'm checking that the item in the first slot's name is Fertilizer. This is because if there is no item where the saplings are (usually slot 1), the new slot 1 is where the fertilizer goes (slot 2).

A list of what I'm using for this setup:
  • Factorization Barrels (I don't have JABBA :( )
  • ThermalExpansion ItemDucts
  • ComputerCraft computers
  • ProjectRed redstone cables
Since I don't have wonderful Ender IO conduits, I'm stuck using these (I don't have much experience with other mods for moving items), and I turned to this solution. If anyone can help me cover these issues I encounter in my code or simply suggest a different way to go about this task on Direwolf20 MC version 1.6.4, that would be appreciated!

Oh, last thing I wanted to mention, due to the way OpenPeripherals returns the amount of items in a barrel, 64 is the largest number I can get. even if I have a barrel full of 4096 items.

Edit: I should probably mention, the reason I went with this setup was because I wanted to ALWAYS have items in my barrel, since these "$#!7**" factorization barrels cannot be locked to keep their item, and I don't want excess logs/apples getting thrown in there if it's empty, I'm trying to toggle the redstone for a pipe only when the inventory is low and that I have the stock. I also don't want to flood the ThermalExpansion itemduct with numerous amounts of items.
Edited on 14 April 2016 - 12:49 AM
Bomb Bloke #2
Posted 14 April 2016 - 03:30 AM
If you want to know what's in a particular slot, why not use getStackInSlot()? That way you know exactly which one the result refers to.

Regarding item counts and barrels, it's been a looong time since I wrote this code (let alone had access to the server on which I ran it), but this is what I used to use:

local amount = 0

if barrel.getStackInSlot(2) then
	if barrel.getStackInSlot(1).qty > 0 then
		amount = barrel.getStackInSlot(2).qty * 63 + barrel.getStackInSlot(1).qty
	else
		amount = barrel.getStackInSlot(2).qty
	end
end

Can't quite remember which sort of barrel I was working with at the time, but I'm fairly sure it was Factorization.

Edit:

I believe you should be able to configure filters in your piping system, by the way, even if you can't configure the barrels themselves. Cheaply, too, if you're using Thermal Expansion - lot of shortcuts in that mod.
Edited on 14 April 2016 - 01:33 AM
Kezaraux #3
Posted 14 April 2016 - 10:25 AM
If you want to know what's in a particular slot, why not use getStackInSlot()? That way you know exactly which one the result refers to.

Regarding item counts and barrels, it's been a looong time since I wrote this code (let alone had access to the server on which I ran it), but this is what I used to use:

local amount = 0

if barrel.getStackInSlot(2) then
	if barrel.getStackInSlot(1).qty > 0 then
		amount = barrel.getStackInSlot(2).qty * 63 + barrel.getStackInSlot(1).qty
	else
		amount = barrel.getStackInSlot(2).qty
	end
end

Can't quite remember which sort of barrel I was working with at the time, but I'm fairly sure it was Factorization.

Edit:

I believe you should be able to configure filters in your piping system, by the way, even if you can't configure the barrels themselves. Cheaply, too, if you're using Thermal Expansion - lot of shortcuts in that mod.

Okay, thanks. I had only used getAllStacks. I'll try that out. Here's to hoping it works!
Kezaraux #4
Posted 14 April 2016 - 10:52 AM
Yup, I was able to get it to work.

Working program for reference:


local barrel = peripheral.wrap("left")
local ferm = peripheral.wrap("factory_3_0")
local barInv = 0
local fermInv = 0

function update()
  barInv = barrel.getStackInSlot(2).qty
  if (ferm.getStackInSlot(1)) then
    fermInv = ferm.getStackInSlot(1).qty
  else
    fermInv = 0
  end
  term.clear()
  term.setCursorPos(1,1)
  print("Barrel: "..barInv)
  print("Fermenter: "..fermInv)
end

function checkAmount()
  if (barInv > 63 and fermInv == 0) then
    redstone.setOutput("bottom", true)
    sleep(2)
    redstone.setOutput("bottom", false)
  end
end

function checkFert()
  if (ferm.getStackInSlot(2)) then
    print("Fertilizer left: "..ferm.getStackInSlot(2).qty)
  else
    print("Out of Fertilizer!")
  end
end

function mainLoop()
  while true do
    update()
    checkAmount()
    checkFert()
    sleep(20)
  end
end

mainLoop()