Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --
- -- Chicken Farm sorting machine
- --
- local delay = .5
- local CHICKEN = "minecraft:cooked_chicken"
- local FEATHER = "minecraft:feather"
- local w,h = term.getSize()
- local count = {
- [CHICKEN] = 0,
- [FEATHER] = 0
- }
- local function initFacing()
- local b, d = turtle.inspect()
- if not b then
- return
- elseif d.name == "minecraft:hopper" then
- turtle.turnLeft()
- turtle.turnLeft()
- return
- else
- turtle.turnLeft()
- initFacing()
- return
- end
- end
- local function selectSlot(n)
- if turtle.getSelectedSlot() ~= n then
- turtle.select(n)
- end
- end
- local function facingChest(name)
- initFacing()
- if name == CHICKEN then
- turtle.turnLeft()
- elseif name == FEATHER then
- turtle.turnRight()
- else
- return false
- end
- return true
- end
- -- Formate un nombre de secondes en dur?e lisible
- local iniClock = os.clock()
- local function timeStr()
- local sec = os.clock() - iniClock
- local d = math.floor(sec/86400) --24*60*60 sec
- local h = math.floor(sec/3600)%24 -- 60x60 sec
- local m = math.floor(sec/60)%60 -- 60sec
- local s = math.floor(sec%60) --Reste
- local str = ""
- if d>0 then str = d>1 and d.." days " or "1 day " end
- if h>0 then str = str..h.."h" end
- if m>0 then str = str..(m>9 and m or "0"..m) end
- if h==0 then str = m>0 and str..":"..(s>9 and s or "0"..s) or s.."s" end
- return str
- end
- local function store()
- for slot=1,16 do
- local d = turtle.getItemDetail(slot)
- if d and d.name then
- count[d.name] = count[d.name] and count[d.name] + d.count or d.count
- if facingChest(d.name) then
- selectSlot(slot)
- turtle.drop()
- end
- selectSlot(slot%16+1)
- end
- end
- initFacing()
- end
- local function output(str,line)
- local x,y = term.getCursorPos()
- term.setCursorPos(1,line)
- term.clearLine()
- term.write(tostring(str))
- term.setCursorPos(x,y)
- end
- local function display()
- term.clear()
- --Header
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.white)
- output(string.format("KFC Sorter | %s", timeStr()),1)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- output(string.rep(string.char(131), w), 2)
- --Item count
- output(string.format("Chicken: %s", count[CHICKEN]), 3)
- output(string.format("Feather: %s", count[FEATHER]), 4)
- local line = 5
- for n, c in pairs(count) do
- if n ~= CHICKEN and n ~= FEATHER then
- output(string.format("%s %s", n, c), line)
- line = line+1
- end
- if line > h then
- break
- end
- end
- end
- local function main()
- local tick, e, p = os.startTimer(delay), 0
- while true do
- selectSlot(1)
- display()
- e, p = os.pullEvent()
- if e == "timer" and p == tick then
- tick = os.startTimer(delay)
- store()
- elseif e == "turtle_inventory" then
- os.cancelTimer(tick)
- store()
- tick = os.startTimer(delay)
- end
- end
- end
- main()
Add Comment
Please, Sign In to add comment