Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function refuelFromChest()
- -- version 1.02
- -- using pull items to test direction
- -- version 1.01
- -- fixed empty chest causing stack size -1
- -- turtle must be be looking at chest
- local directions = {"north", "south", "east", "west"}
- local chestDirection = ""
- -- these item IDs can be taken as fuel
- local fuel = {327, 4600, 7796, 263}
- -- connect to the chest
- local chest = peripheral.wrap("front")
- if not chest then
- print("Failed to connect to chest at front side.")
- return false
- end
- -- get all stacks from chest
- local chestStacks = chest.getAllStacks()
- if not chestStacks then
- print("Could not scan chest.")
- return false
- end
- -- check if there is a free slot. If not try to condense
- if #chestStacks == chest.getInventorySize() then
- chest.condenseItems();
- -- space available now?
- if #chestStacks == chest.getInventorySize() then
- print("Chest is full. Please clean at least one slot. Preferebaly Slot 1.")
- return false
- end
- end
- -- check if slot 1 is free. If not, try to swap with the next available
- local swappedStack = 0
- if chestStacks[1] then
- for i = 2, chest.getInventorySize() do
- if not chestStacks[i] then
- chest.swapStacks(1, i)
- chestStacks = chest.getAllStacks()
- swappedStack = i
- break
- end
- end
- end
- -- take slot 1 from turtle into chest
- turtle.select(1)
- -- probe test direction
- for i = 1, #directions do
- chestDirection = directions[i]
- if chest.pullItem(chestDirection, 1) > 0 then
- break
- end
- end
- -- take fuel from chest
- local refueled = false
- for k,v in pairs(chestStacks) do
- local item = chestStacks[k].id
- for i = 1, #fuel do
- if item == fuel[i] then
- chest.pushItem(chestDirection, k)
- if turtle.refuel() then
- refuel = true
- end
- turtle.select(1)
- chest.pullItem(chestDirection, 1)
- end
- end
- end
- -- give turtle back its first slot stack
- turtle.select(1)
- chest.pushItem(chestDirection, 1)
- -- when swapped then but item back to slot 1
- if swappedStack > 1 then
- chest.swapStacks(swappedStack, 1)
- end
- return refueled
- end
- refuelFromChest()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement