Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local crafted = 0
- local function get_used_slots()
- local used = 0
- for s = 1,15 do
- if turtle.getItemCount(s) > 0 then
- used = used + 1
- end
- end
- return used
- end
- local function get_items_in_slots(slots)
- local items = 0
- for s = 1, slots do
- items = items + turtle.getItemCount(s)
- end
- return items
- end
- local function dump_bread()
- turtle.turnRight()
- turtle.turnRight()
- turtle.select(16)
- crafted = crafted + turtle.getItemCount(16)
- turtle.drop(64)
- turtle.turnLeft()
- turtle.turnLeft()
- end
- local function do_crafting()
- -- Select slot 16 for the destination
- turtle.select(16)
- -- Should only have bread-stuff now, craft
- turtle.craft()
- -- Will now have bread in slot 16
- -- move the bread into the destination chest
- dump_bread()
- end
- local function selected_is_wheat()
- if turtle.getItemCount() == 0 then
- return false
- end
- if turtle.getItemDetail().name == "minecraft:wheat" then
- return true
- else
- return false
- end
- end
- local function selected_is_seeds()
- if turtle.getItemCount() == 0 then
- return false
- end
- if turtle.getItemDetail().name == "minecraft:wheat_seeds" then
- return true
- else
- return false
- end
- end
- local function check_fuel(required)
- -- Always assumes the turtle is currently at the
- -- bread creation place
- if turtle.getFuelLevel() == 'unlimited' or turtle.getFuelLevel() > required + 1 then
- return
- end
- -- Not enough fuel
- -- Try to refuel through inventory
- turtle.select(16)
- if turtle.refuel() then
- return
- end
- -- Turn right and check the chest for fuel
- turtle.turnRight()
- -- We need an additional action now (turn left)
- -- so increment required
- required = required + 1
- empty_spots = {}
- -- Suck it all up and call refuel on every item
- for s = 1, 16 do
- if turtle.getItemCount(s) == 0 then
- empty_spots[s] = 1
- turtle.suck()
- turtle.refuel()
- if turtle.getFuelLevel() > required + 1 then
- return
- end
- else
- empty_spots[s] = 0
- end
- end
- for s = 1, 16 do
- if empty_spots[s] == 1 then
- turtle.dump()
- end
- end
- -- Ask for fuel
- turtle.select(16)
- print("Need fuel!")
- -- Will be reduced by 3 at the destination
- redstone.setAnalogOutput("bottom", 14)
- while not turtle.refuel() or turtle.getFuelLevel() < required + 1 do
- os.pullEvent( "turtle_inventory")
- end
- redstone.setOutput("bottom", false)
- print("Got fuel!")
- turtle.turnLeft()
- end
- local function dump_drops()
- turtle.turnRight()
- for s = 1, 15 do
- turtle.select(s)
- turtle.drop(64)
- end
- turtle.turnLeft()
- end
- local function dump_fuel(slot)
- if not slot then
- slot = 16
- end
- turtle.turnLeft()
- turtle.select(slot)
- turtle.drop()
- turtle.turnRight()
- end
- -- Returns 0 if it got wheat,
- -- 1 if it got seeds and
- -- 2 if it got fuel or nothing
- local function get_wheat_stack(slot)
- turtle.select(slot)
- turtle.suck(64)
- if turtle.getItemCount() == 0 then
- -- Picked up nothing, end
- return 2
- end
- if selected_is_seeds() == true then
- -- Drop seeds off at the seed box
- turtle.turnLeft()
- turtle.turnLeft()
- turtle.drop(64)
- turtle.turnRight()
- turtle.turnRight()
- return 1
- end
- if selected_is_wheat() == false then
- -- Just picked up fuel,
- -- drop it off again
- dump_fuel(s)
- return 2
- end
- return 0
- end
- local function get_wheat()
- turtle.turnRight()
- for s = 1, 3 do
- while get_wheat_stack(s) == 1 do
- -- Repeat this to remove all seeds from
- -- storage
- end
- end
- turtle.turnLeft()
- end
- local function balance(used_slots, items_per_slot)
- if used_slots == 1 then
- -- Just move one third at a time from the
- -- first slot to the second and third
- turtle.select(1)
- turtle.transferTo(2, items_per_slot)
- turtle.transferTo(3, items_per_slot)
- else
- -- Get the amount the first slot has to "lose"
- -- then check howmuch we can transfer to slot
- -- 2 and then transfer the rest to slot 3
- local first_too_many = turtle.getItemCount(1) - items_per_slot
- local second_needed = items_per_slot - turtle.getItemCount(2)
- if second_needed < 0 then
- -- Must get rid of some items,
- -- this means we just have to dump the first's items in
- -- the third slot
- turtle.select(1)
- turtle.transferTo(3, first_too_many)
- -- as well as the second's
- turtle.select(2)
- turtle.transferTo(3, -second_needed)
- else
- -- Transfer what the second one needs to
- -- the second one and the rest to the third
- turtle.select(1)
- turtle.transferTo(2, second_needed)
- -- as well as the second's
- turtle.select(1)
- turtle.transferTo(3, first_too_many - second_needed)
- end
- end
- end
- local function suck_all()
- for s = 1, 15 do
- turtle.select(s)
- turtle.suck()
- end
- end
- local function get_fuel()
- turtle.turnLeft()
- for s = 1, 15 do
- turtle.select(s)
- turtle.suck()
- if not selected_is_wheat() and not selected_is_seeds() then
- -- Picked up fuel
- turtle.transferTo(16, 64)
- break
- end
- end
- -- Dump it all again
- for s = 1, 15 do
- turtle.select(s)
- if turtle.getItemCount() > 0 then
- turtle.drop(64)
- end
- end
- turtle.turnRight()
- end
- local function make_bread()
- dump_fuel()
- -- Suck it all up
- suck_all()
- -- If we picked up nothing, just end
- if get_used_slots() == 0 then
- get_fuel()
- return
- end
- print("Making bread")
- -- Dump all wheat for now
- dump_drops()
- local used_slots = get_used_slots()
- local items = get_items_in_slots(used_slots)
- while true do
- -- Pick up a max of 3 stacks from the chest
- get_wheat()
- local used_slots = get_used_slots()
- local items = get_items_in_slots(used_slots)
- if items == 0 then
- break
- elseif items < 3 then
- -- Spit it out
- turtle.select(1)
- turtle.drop(64)
- end
- local remaining_items = items % 3
- if remaining_items > 0 then
- -- Got some items left, dump those in the chest
- turtle.select(used_slots)
- turtle.turnRight()
- turtle.drop(items % 3)
- turtle.turnLeft()
- items = get_items_in_slots(used_slots)
- end
- local items_per_slot = items / 3
- balance(used_slots, items_per_slot)
- do_crafting()
- end
- get_fuel()
- print("Made a total of ", crafted, " breads")
- print("Going back to sleep")
- end
- local function faces_chest()
- local success, data = turtle.inspect()
- if not success then
- return false
- end
- if data.name == "minecraft:chest" or data.name == "IronChest:BlockIronChest" then
- return true
- else
- return false
- end
- end
- local function is_at_start()
- turtle.turnLeft()
- if not faces_chest() then
- turtle.turnRight()
- return false
- end
- turtle.turnLeft()
- if not faces_chest() then
- turtle.turnRight()
- turtle.turnRight()
- return false
- end
- turtle.turnLeft()
- if not faces_chest() then
- turtle.turnRight()
- turtle.turnRight()
- turtle.turnRight()
- return false
- end
- turtle.turnLeft()
- return true
- end
- local function init()
- -- Check fuel first
- check_fuel(1)
- print("Fuel remaining: ", turtle.getFuelLevel())
- make_bread()
- end
- init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement