JaMaNi133

Farm manager

Mar 12th, 2026 (edited)
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.67 KB | None | 0 0
  1. FARM_ITEMS ={}
  2. FARM_ITEMS["minecraft:wheat_seeds"] = 128
  3. FARM_ITEMS["minecraft:oak_sapling"] = 128
  4.  
  5. STORAGE_ITEMS = {}
  6. STORAGE_ITEMS["minecraft:sugar_cane"] = 5120
  7. STORAGE_ITEMS["minecraft:wheat"] = 5120
  8. STORAGE_ITEMS["minecraft:wheat_seeds"] = 1280
  9. STORAGE_ITEMS["minecraft:kelp"] = 5120
  10. STORAGE_ITEMS["minecraft:oak_sapling"] = 1280
  11. STORAGE_ITEMS["minecraft:oak_log"] = 5120
  12. STORAGE_ITEMS["minecraft:apple"] = 640
  13. STORAGE_ITEMS["minecraft:stick"] = 640
  14.  
  15.  
  16. local farm_side = "left"
  17. local storage_side = "right"
  18. local void_side = "bottom"
  19.  
  20. farm = peripheral.wrap(farm_side)
  21. storage = peripheral.wrap(storage_side)
  22.  
  23.  
  24.  
  25. function itemList(inventory)
  26.     accumulator = {}
  27.     if inventory then
  28.         for i,slot in pairs(inventory) do
  29.             if accumulator[slot.name] then
  30.                accumulator[slot.name] = slot.count+accumulator[slot.name]
  31.             else
  32.                 accumulator[slot.name] = slot.count
  33.             end
  34.         end
  35.     end
  36.     return accumulator
  37. end
  38.  
  39. function printT(T)
  40.     print(textutils.serializeJSON(T))
  41. end
  42.  
  43. function getKey0(T,K)
  44.     if T[K] then
  45.         return T[K]
  46.     end
  47.     return 0
  48. end
  49.  
  50. function moveItems(source,dest,item_id,count)
  51.     c = count
  52.     i = source.list()
  53.     if not i then
  54.         return
  55.     end
  56.     for slot,item in pairs(i) do
  57.         if item.name  == item_id then
  58.             if slot < 1 then
  59.                 return
  60.             end
  61.             if c < 1 then
  62.                 return
  63.             end
  64.             r = source.pushItems(dest,slot,c)
  65.             n = 0
  66.             if r then
  67.                 n = r
  68.             end
  69.             c = c - n
  70.         end
  71.     end
  72. end
  73.  
  74. function min(a,b)
  75.     if a<b then
  76.         return a
  77.     end
  78.     return b
  79. end
  80. function max(a,b)
  81.     if a>b then
  82.         return a
  83.     end
  84.     return b
  85. end
  86.  
  87. while 1==1 do
  88.     inv_farm = itemList(farm.list())
  89.     inv_storage = itemList(storage.list())
  90.  
  91.     for item_id,item_count in pairs(inv_farm) do
  92.         overCount = item_count-getKey0(FARM_ITEMS,item_id)
  93.         if overCount > 0 then
  94.            
  95.             usingCount = max(0,min(getKey0(STORAGE_ITEMS,item_id)-getKey0(inv_storage,item_id),overCount))
  96.             voidingCount = max(0,overCount - usingCount)
  97.            
  98.             if usingCount > 0 then
  99.                 --store
  100.                 print("using",usingCount,item_id)
  101.                 moveItems(farm,storage_side,item_id,usingCount)
  102.             end
  103.             if voidingCount > 0 then
  104.                 --void
  105.                 print("voiding",voidingCount,item_id)
  106.                 moveItems(farm,void_side,item_id,voidingCount)
  107.             end
  108.         end
  109.     end
  110.     sleep(0.5)
  111. end
  112.  
  113.  
Advertisement
Add Comment
Please, Sign In to add comment