Firnagzen

Hammer controller

Jun 11th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.46 KB | None | 0 0
  1. -- Turn it off, just in case
  2. redstone.setOutput("right", false)
  3.  
  4. -- Assumption: the slots are fixed
  5. cobble     = {name = "minecraft:cobblestone", slot = 9, target = 3, out = 10}
  6. netherrack = {name = "minecraft:netherrack" , slot = 7, target = 2, out =  8}
  7. endstone   = {name = "minecraft:end_stone"  , slot = 3, target = 4, out =  4}
  8. -- gravel     = {name = "minecraft:gravel"     , slot = 10, target = 8, out = 11}
  9. -- sand       = {name = "minecraft:sand"       , slot = 11, target = 2, out =  6}
  10.  
  11. -- Bind peripherals
  12. local drawers = peripheral.wrap("storagedrawers:controllerslave_0")
  13. local builder = peripheral.wrap("minecraft:builder_te_0")
  14.  
  15. -- Add names for easy reference
  16. drawers.name = "storagedrawers:controllerslave_0"
  17. builder.name = "minecraft:builder_te_0"
  18.  
  19. -- Calculate total number of items
  20. local target_total = 0
  21. for k, item in pairs({cobble, gravel, netherrack, sand, endstone}) do
  22.     target_total = target_total + item.target
  23. end
  24.  
  25. -- Various variables for mainloop
  26. local items_short = 0
  27. local items_moved = 0
  28. local place_phase = false
  29. local item_limit  = 31 * 64
  30.  
  31. function builderItemCount(name)
  32.     -- Totals up number of items in builder
  33.     local builder_items = 0
  34.     for k, item in pairs(builder.list()) do
  35.         builder_items = builder_items + item.count
  36.     end
  37.     return builder_items
  38. end
  39.  
  40. function errorIfSlotLessThan(slot, value)
  41.     if drawers.getItem(slot).getMetadata().count < value then
  42.         error()
  43.     end
  44. end
  45.  
  46. -- Main loop
  47. while true do
  48.     -- Sleep to yield
  49.     sleep(1)
  50.  
  51.     -- If placing, then do not top up
  52.     -- Placing phase remains on until the builder is empty
  53.     if place_phase then
  54.         -- Check if the builder is empty
  55.         if builderItemCount(builder.name) == 0 then
  56.             place_phase = false
  57.         end
  58.  
  59.     else
  60.         -- Build list of items to move
  61.         for k, item in pairs({netherrack, endstone}) do
  62.             -- Only move if there's more than the required value
  63.             local has_item = pcall(errorIfSlotLessThan, item.slot, item.target + 10)
  64.  
  65.             -- Only move if there's room
  66.             local has_room = not pcall(errorIfSlotLessThan, item.out, item_limit)
  67.  
  68.             -- Reset counter
  69.             items_moved = 0
  70.  
  71.             -- Do the actual move
  72.             if has_item and has_room then
  73.                 -- Move items to the builder, record how many were moved
  74.                 items_moved = drawers.pushItems(builder.name, item.slot, item.target)
  75.             end
  76.  
  77.             -- Find out how many items were short
  78.             items_short = items_short + item.target - items_moved
  79.         end
  80.  
  81.         -- Top up with cobblestone
  82.         drawers.pushItems(builder.name, cobble.slot, cobble.target + items_short)
  83.  
  84.         -- Reset
  85.         items_short = 0
  86.         items_moved = 0
  87.  
  88.         -- Total up items in builder
  89.         builder_items = 0
  90.         for k, item in pairs(builder.list()) do
  91.             builder_items = builder_items + item.count
  92.         end
  93.  
  94.         -- Switch to placement phase if target reached
  95.         if builderItemCount(builder.name) >= target_total then
  96.             place_phase = true
  97.         end
  98.     end
  99.  
  100.     -- Set redstone controlling builder
  101.     redstone.setOutput("right", place_phase)
  102. end
Add Comment
Please, Sign In to add comment