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

[lua] CCsensor - getting informations about items in chest.

Started by Marval, 19 September 2012 - 08:36 AM
Marval #1
Posted 19 September 2012 - 10:36 AM
I use CCsensors addon and I have a question.

How to get amount of items in chest in defined slot? When I type:

targets = sensors.getAvailableTargetsforProbe("left","Chest","InventoryInfo")
data = sensors.getSensorReadingAsDict("left","Chest",targets[1],"InventoryContent")

the data contain something like this: 1xUranium Cell@15 in one string.
Is the way to get only amount of item or only the name?
Now when I want amount i use string.sub, but there is no simpler way? Getting name will be difficult. Only way I think right now to do this is to get one character with string sub and doing this after it go to @.

In sensorsAPI is function:
-- returns a dict for each item in the given names table with .dmg and .qty
function getItemsInfo(names,content)
local r={total=0,};
for i,v in pairs(content) do
q,item,dmg= string.match(v,"(%d+)\*(.*)@(%d+)")
itb = string.sub(item,6)
item = itemsDict[itb] or item;
r.total = r.total+1;
for idx,name in ipairs(names) do
if item==name then
local vl=0;
local vq=0;
if r[item] then vl =r[item].dmg end
if r[item] then vq =r[item].qty end
r[item] = {dmg=vl+dmg,qty=q+vq};
end
end

end
for idx,name in ipairs(names) do
if r[name] == nil then
r[name] = {qty=0, dmg=0}
end
end
return r;
end

but I don't know how to use it.
Lyqyd #2
Posted 19 September 2012 - 03:52 PM

variable = "1xUranium Cell@15"
quantity, name, location = string.match(variable, "(%d+)x([%w%s]+)@(%d+)")

That should work.