Adding menril sapling deployer auto restock program
authorOlav Bakke Svendsen <mail@olavbs.no>
Fri, 13 Dec 2024 02:17:23 +0000 (03:17 +0100)
committerOlav Bakke Svendsen <mail@olavbs.no>
Fri, 13 Dec 2024 02:17:23 +0000 (03:17 +0100)
menril-deployers/menril-deployers.lua [new file with mode: 0644]
menril-deployers/package [new file with mode: 0644]

diff --git a/menril-deployers/menril-deployers.lua b/menril-deployers/menril-deployers.lua
new file mode 100644 (file)
index 0000000..e4e3451
--- /dev/null
@@ -0,0 +1,62 @@
+-- Automatically detects deployers on startup.
+-- Keeps deployers stocked with exactly one
+-- menril sapling, sleeps for 2s between restocks.
+-- Warns if deployers are holding wrong item or
+-- if out of saplings.
+
+-- configuration
+local sapling_inventory = ""
+
+-- imports
+local inv = dofile("/lib/inventory.lua")
+
+-- program starts here
+local ds = {}
+do
+  for _,p in ipairs(peripheral.getNames()) do
+    if string.match(p, "^create:deployer") then
+      table.insert(ds, p)
+    end
+  end
+end
+
+local restock_deployers = function()
+  local skip = false
+  for _, d in pairs(ds) do
+    if skip then return skip end
+    local empty = { name = "integrateddynamics:menril_sapling", count = 0, maxCount = 64 }
+    local item = peripheral.call(d, "getItemDetail", 1) or empty
+    if item.name ~= "integrateddynamics:menril_sapling" then
+      print(string.sub(d, 17).." holding "..item.displayName)
+    end
+    if item.name == "integrateddynamics:menril_sapling" and item.count < 1 then
+      local found_all, index = inv.find(sapling_inventory, { ["integrateddynamics:menril_sapling"] = 1 })
+      if found_all then
+        for slot, amount in pairs(index["integrateddynamics:menril_sapling"].slots) do
+          peripheral.call(sapling_inventory, "pushItems", d, slot, amount)
+        end
+      else
+        skip = "out of saplings"
+      end
+--     if not found_all then
+--       print("low on saplings")
+--     end
+--     if not index["integrateddynamics:menril_sapling"] then
+--       log:warn("out of redstone")
+--     else
+    end
+  end
+  return skip
+end
+
+while true do
+  term.clear()
+  term.setCursorPos(1,1)
+  term.setTextColor(colors.gray)
+  print("restocking deployers")
+  print("warnings:")
+  term.setTextColor(colors.white)
+  local e = restock_deployers()
+  if e then print(e) end
+  sleep(2)
+end
diff --git a/menril-deployers/package b/menril-deployers/package
new file mode 100644 (file)
index 0000000..a10bec2
--- /dev/null
@@ -0,0 +1,2 @@
+cc menril-deployers/menril-deployers.lua:/bin/menril-deployers.lua:o
+cc lib/inventory.lua:/lib/inventory.lua:o