Advertisement
dpc-profile

drawerStockPileSwitch

Sep 25th, 2022 (edited)
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.97 KB | Source Code | 0 0
  1. local objDrawer = peripheral.find("storagedrawers:standard_drawers_1") -- Work only with the drawer 1x1
  2. local objMonitor = peripheral.find("monitor") -- can be use with only one monitor
  3. local fileName = "lastSignal.txt"
  4. local sideOutput = "left"
  5.  
  6. local maxStack = 64 -- Place the max amount of itens per stack
  7. local upgradeMultiplier = 1 -- how much the upgrade increase. If not have, leave at 1
  8. local slot = 2 -- For drawer 1x1, aways will be 2
  9.  
  10. local minPercent = 0.20
  11. local maxPercent = 0.80
  12.  
  13. local redstoneSignal = true
  14.  
  15. function main()
  16.     shell.run("clear")
  17.  
  18.     returnMaxCapacityDrawer =  maxCapacityDrawer(objDrawer, upgradeMultiplier, maxStack)
  19.  
  20.     local currentCount = objDrawer.getItemDetail(slot).count
  21.  
  22.     local quantMin = returnMaxCapacityDrawer * minPercent
  23.     local quantMax = returnMaxCapacityDrawer * maxPercent
  24.  
  25.     if objMonitor ~= nil then
  26.         drawMonitor(objMonitor, currentCount, returnMaxCapacityDrawer)
  27.     end
  28.  
  29.     local savedSignal = loadLastSignal(fileName)
  30.     print("Value get from file: " .. tostring(savedSignal))
  31.     redstoneSignal = savedSignal
  32.  
  33.     local returnSetRedstoneSignal = setRedstoneSignal(quantMin, quantMax, currentCount)
  34.  
  35.     if returnSetRedstoneSignal ~= nil then
  36.         redstoneSignal = returnSetRedstoneSignal
  37.         print("Signal change to: " .. tostring(redstoneSignal))
  38.         saveLastSignal(fileName, redstoneSignal)
  39.     end
  40.  
  41.     redstone.setOutput(sideOutput, redstoneSignal)
  42. end
  43.  
  44. -- ==========================================================================
  45. -- =============================== FUNCTIONS ================================
  46. -- ==========================================================================
  47.  
  48. function maxCapacityDrawer(obj, multiplier, stack)
  49.     local maxCount = obj.getItemDetail(slot).maxCount * stack
  50.     maxCount = maxCount * multiplier
  51.     return maxCount
  52. end
  53.  
  54. function setRedstoneSignal(min, max, current)
  55.     if current < min then
  56.         return true
  57.     end
  58.  
  59.     if current > max then
  60.         return false
  61.     end
  62. end
  63.  
  64. function drawMonitor(monitor, currentItens, maxItens)
  65.     monitor.setTextColor(colors.white)
  66.     monitor.setBackgroundColor(colors.black)
  67.     monitor.setTextScale(0.5)
  68.     monitor.clear()
  69.  
  70.     monitor.setCursorPos(1,1)
  71.     monitor.write("Itens: " .. currentItens)
  72.  
  73.     monitor.setCursorPos(1,2)
  74.     monitor.write("Max Itens: " .. maxItens)
  75. end
  76.  
  77. function loadLastSignal(name)
  78.     if (fs.exists(name) == false) then
  79.         saveLastSignal(name, redstoneSignal)
  80.         return redstoneSignal
  81.     else
  82.         local file = fs.open(name,"r")
  83.         local line = {}
  84.         line = file.readLine()
  85.         file.close()
  86.         print("file readed")
  87.         return line
  88.     end
  89. end
  90.  
  91. function saveLastSignal(name, content)
  92.     local file = fs.open(name,"w")
  93.     file.write(content)        
  94.     file.close()
  95.     print("file writed")
  96. end
  97.  
  98. function toBool(param)
  99.     if param == "true" then
  100.         return true
  101.     end
  102.  
  103.     return false
  104. end
  105.  
  106. -- ==========================================================================
  107. -- ==========================================================================
  108. while true do
  109.     main()
  110.     sleep(2)
  111. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement