CCCPaste

minecolonies_refinedstorage_1.21.1.lua

-- RSWarehouse.lua
-- Construction: https://cdn.domekologe.eu/gameserver/minecraft/cc/Minecolonies_RefinedStorage_Integration.png
-- IMPORTANT: Entangled Block MUST be on left Side of Computer, if not, change direction variable!


local time_between_runs = 30
local MAX_EXPORT = 256
local direction = "left"


-------------------------------------------------------------------------------
-- INIT
-------------------------------------------------------------------------------

local monitor = peripheral.find("monitor")
if not monitor then error("Monitor not found") end
monitor.setTextScale(0.5)

local bridge = peripheral.find("rs_bridge")
if not bridge then error("RS Bridge not found") end

local colony = peripheral.find("colony_integrator")
if not colony then error("Colony Integrator not found") end

local deliveredRequests = {}
local craftingRequests = {}

-------------------------------------------------------------------------------
-- HELPERS
-------------------------------------------------------------------------------

local function isBuilder(colonistName)
  return string.find(colonistName, "Builder") ~= nil
end

local function isEquipmentRequest(desc)
  return desc and string.find(desc, "level")
end

local function makeRequestId(data)
  return string.format(
    "%s|%s|%d",
    data.item.name,
    data.colonist,
    data.needed
  )
end

-------------------------------------------------------------------------------
-- MONITOR UI
-------------------------------------------------------------------------------

local function drawHeader(mon, remaining)
  local w, _ = mon.getSize()
  local now = os.time()

  local cycle = "day"
  local color = colors.lightBlue
  if now >= 18.5 or now < 5 then
    cycle = "night"
    color = colors.red
  elseif now >= 18 then
    cycle = "sunset"
    color = colors.orange
  elseif now < 6 then
    cycle = "sunrise"
    color = colors.yellow
  end

  mon.setCursorPos(1,1)
  mon.setTextColor(color)
  mon.write("Time: "..textutils.formatTime(now, false).." ["..cycle.."]")

  mon.setCursorPos(w - 16, 1)
  mon.setTextColor(colors.green)
  mon.write(string.format("Remaining: %02ds", remaining))
end

local function drawSectionTitle(mon, row, title)
  local w, _ = mon.getSize()
  mon.setCursorPos(math.floor((w - #title) / 2), row)
  mon.setTextColor(colors.white)
  mon.write(title)
  return row + 1
end

local function drawLine(mon, row, left, right, color)
  local w, _ = mon.getSize()
  mon.setCursorPos(1, row)
  mon.setTextColor(color)
  mon.write(left)

  mon.setCursorPos(w - #right, row)
  mon.write(right)

  return row + 1
end

-------------------------------------------------------------------------------
-- REQUEST FETCH
-------------------------------------------------------------------------------

local function getRequests()
  local list = {}
  for _, r in pairs(colony.getRequests()) do
    if r.items and r.items[1] and r.items[1].name then
      table.insert(list, {
        name = r.name,
        desc = r.desc,
        needed = r.count or 0,
        item = {
          name = r.items[1].name,
          count = math.min(r.count or 0, MAX_EXPORT)
        },
        colonist = r.target,
        building = r.building or r.target
      })
    end
  end
  return list
end

-------------------------------------------------------------------------------
-- MAIN SCAN
-------------------------------------------------------------------------------

local function scan(monitor, remaining)
  local builder = {}
  local nonBuilder = {}
  local equipment = {}
  local activeRequests = {}

  for _, data in ipairs(getRequests()) do
    local requestId = makeRequestId(data)
    activeRequests[requestId] = true

    local provided = 0
    local color = colors.blue   -- default: nicht lieferbar
    local crafting = false

    -- ===============================
    -- LIEFERLOGIK
    -- ===============================
    if not deliveredRequests[requestId] then
      provided = bridge.exportItem(data.item, direction)

      -- vollständig geliefert
      if provided >= data.needed then
        deliveredRequests[requestId] = true
        color = colors.green

      -- teilweise geliefert
      elseif provided > 0 then
        color = colors.yellow

      -- nichts lieferbar → Crafting prüfen
      else
        if not craftingRequests[requestId] then
          local ok = bridge.craftItem({
            name = data.item.name,
            count = data.needed
          })

          if ok then
            craftingRequests[requestId] = true
            crafting = true
            color = colors.pink
          else
            color = colors.blue
          end
        else
          -- Craft läuft bereits
          crafting = true
          color = colors.pink
        end
      end
    else
      provided = 0
      color = colors.green
    end

    -- ===============================
    -- UI-EINTRAG
    -- ===============================
    local entry = {
      left = string.format(
        "%d %s (%d)",
        provided,
        data.name:gsub("^[%d%-]+%s*", ""),
        data.needed
      ),
      provided = provided,
      needed = data.needed,
      color = color
    }

    -- Equipment → immer Kolonist
    if isEquipmentRequest(data.desc) then
      entry.right = data.colonist
      table.insert(equipment, entry)

    -- Builder → Gebäude
    elseif isBuilder(data.colonist) then
      entry.right = data.building or "Builder"
      table.insert(builder, entry)

    -- Non-Builder → Kolonist
    else
      entry.right = data.colonist
      table.insert(nonBuilder, entry)
    end

    -- ===============================
    -- TERMINAL LOG
    -- ===============================
    if color == colors.pink then
      print(string.format(
        "[CRAFT] %s x%d -> %s",
        data.item.name,
        data.needed,
        entry.right
      ))
    elseif color == colors.blue then
      print(string.format(
        "[MISS]  %s x%d -> %s (no pattern)",
        data.item.name,
        data.needed,
        entry.right
      ))
    else
      print(string.format(
        "[%s] %s x%d -> %s",
        provided > 0 and "OK" or "FAIL",
        data.item.name,
        data.item.count,
        entry.right
      ))
    end
  end

  -- ===============================
  -- CLEANUP DELIVERED + CRAFTING
  -- ===============================
  for requestId, _ in pairs(deliveredRequests) do
    if not activeRequests[requestId] then
      deliveredRequests[requestId] = nil
      craftingRequests[requestId] = nil
    end
  end

  -- ===============================
  -- MONITOR UI
  -- ===============================
  monitor.clear()
  drawHeader(monitor, remaining)

  local row = 3

  if #builder > 0 then
    row = drawSectionTitle(monitor, row, "Builder Requests")
    for _, r in ipairs(builder) do
      row = drawLine(monitor, row, r.left, r.right, r.color)
    end
    row = row + 1
  end

  if #nonBuilder > 0 then
    row = drawSectionTitle(monitor, row, "Non Builder Requests")
    for _, r in ipairs(nonBuilder) do
      row = drawLine(monitor, row, r.left, r.right, r.color)
    end
    row = row + 1
  end

  if #equipment > 0 then
    row = drawSectionTitle(monitor, row, "Equipment Requests")
    for _, r in ipairs(equipment) do
      row = drawLine(monitor, row, r.left, r.right, r.color)
    end
  end
end


-------------------------------------------------------------------------------
-- LOOP
-------------------------------------------------------------------------------

local counter = time_between_runs
scan(monitor, counter)

local timer = os.startTimer(1)
while true do
  local e = { os.pullEvent() }
  if e[1] == "timer" and e[2] == timer then
    counter = counter - 1
    if counter <= 0 then
      scan(monitor, time_between_runs)
      counter = time_between_runs
    else
      drawHeader(monitor, counter)
    end
    timer = os.startTimer(1)
  end
end