skypop

CC ChickenFarm sorting machine

Jun 18th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --
  2. -- Chicken Farm sorting machine
  3. --
  4.  
  5. local delay = .5
  6. local CHICKEN = "minecraft:cooked_chicken"
  7. local FEATHER = "minecraft:feather"
  8. local w,h = term.getSize()
  9. local count = {
  10.     [CHICKEN] = 0,
  11.     [FEATHER] = 0
  12. }
  13.  
  14. local function initFacing()
  15.     local b, d = turtle.inspect()
  16.     if not b then
  17.         return
  18.     elseif d.name == "minecraft:hopper" then
  19.         turtle.turnLeft()
  20.         turtle.turnLeft()
  21.         return
  22.     else
  23.         turtle.turnLeft()
  24.         initFacing()
  25.         return
  26.     end
  27. end
  28.  
  29. local function selectSlot(n)
  30.     if turtle.getSelectedSlot() ~= n then
  31.         turtle.select(n)
  32.     end
  33. end
  34.  
  35. local function facingChest(name)
  36.     initFacing()
  37.     if name == CHICKEN then
  38.         turtle.turnLeft()
  39.     elseif name == FEATHER then
  40.         turtle.turnRight()
  41.     else
  42.         return false
  43.     end
  44.     return true
  45. end
  46.  
  47. -- Formate un nombre de secondes en dur?e lisible
  48. local iniClock = os.clock()
  49. local function timeStr()
  50.     local sec = os.clock() - iniClock
  51.     local d = math.floor(sec/86400) --24*60*60 sec
  52.     local h = math.floor(sec/3600)%24 -- 60x60 sec
  53.     local m = math.floor(sec/60)%60 -- 60sec
  54.     local s = math.floor(sec%60) --Reste
  55.     local str = ""
  56.     if d>0 then str = d>1 and d.." days " or "1 day " end
  57.     if h>0 then str = str..h.."h" end
  58.     if m>0 then str = str..(m>9 and m or "0"..m) end
  59.     if h==0 then str = m>0 and str..":"..(s>9 and s or "0"..s) or s.."s" end
  60.     return str
  61. end
  62.  
  63. local function store()
  64.     for slot=1,16 do
  65.         local d = turtle.getItemDetail(slot)
  66.         if d and d.name then
  67.             count[d.name] = count[d.name] and count[d.name] + d.count or d.count
  68.             if facingChest(d.name) then
  69.                 selectSlot(slot)
  70.                 turtle.drop()
  71.             end
  72.             selectSlot(slot%16+1)
  73.         end
  74.     end
  75.     initFacing()
  76. end
  77.  
  78. local function output(str,line)
  79.     local x,y = term.getCursorPos()
  80.     term.setCursorPos(1,line)
  81.     term.clearLine()
  82.     term.write(tostring(str))
  83.     term.setCursorPos(x,y)
  84. end
  85.  
  86. local function display()
  87.     term.clear()
  88.     --Header
  89.     term.setTextColor(colors.black)
  90.     term.setBackgroundColor(colors.white)
  91.     output(string.format("KFC Sorter | %s", timeStr()),1)
  92.     term.setTextColor(colors.white)
  93.     term.setBackgroundColor(colors.black)
  94.     output(string.rep(string.char(131), w), 2)
  95.     --Item count
  96.     output(string.format("Chicken: %s", count[CHICKEN]), 3)
  97.     output(string.format("Feather: %s", count[FEATHER]), 4)
  98.     local line = 5
  99.     for n, c in pairs(count) do
  100.         if n ~= CHICKEN and n ~= FEATHER then
  101.             output(string.format("%s %s", n, c), line)
  102.             line = line+1
  103.         end
  104.         if line > h then
  105.             break
  106.         end
  107.     end
  108. end
  109.  
  110. local function main()
  111.     local tick, e, p = os.startTimer(delay), 0
  112.     while true do
  113.         selectSlot(1)
  114.         display()
  115.         e, p = os.pullEvent()
  116.         if e == "timer" and p == tick then
  117.             tick = os.startTimer(delay)
  118.             store()
  119.         elseif e == "turtle_inventory" then
  120.             os.cancelTimer(tick)
  121.             store()
  122.             tick = os.startTimer(delay)
  123.         end
  124.     end
  125. end
  126.  
  127. main()
Add Comment
Please, Sign In to add comment