Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- craft: Craft the named item
- -- Version 2.0
- usage = "usage: craft <itemName>"
- recipes = {
- {"2x2", 1, 1, 0, 1, 1, 0, 0, 0, 0},
- {"3x3", 1, 1, 1, 1, 1, 1, 1, 1, 1},
- {"chest", 1, 1, 1, 1, 0, 1, 1, 1, 1},
- {"furnace", 1, 1, 1, 1, 0, 1, 1, 1, 1},
- {"stairs", 1, 0, 0, 1, 1, 0, 1, 1, 1},
- {"slab", 1, 1, 1, 0, 0, 0, 0, 0, 0},
- {"door", 1, 1, 0, 1, 1, 0, 1, 1, 0},
- {"wall", 1, 1, 1, 1, 1, 1, 0, 0, 0},
- {"fence", 1, 1, 1, 1, 1, 1, 0, 0, 0},
- }
- -- getIndex: Return the array index of the entry containing <name>
- function printList()
- for i=1,#recipes do
- print(" ", recipes[i][1])
- end
- end
- function getIndex(name)
- rv = 0 -- return value
- nItems = #recipes
- for i= 1,nItems do
- if name == recipes[i][1] then
- rv = i
- break
- end
- end
- return(rv)
- end
- function requiredQty(recipe)
- qty = 0
- for i=2,10 do
- if recipe[i] > 0 then
- qty = qty + 1
- end
- end
- return qty
- end
- function occupiedSlotCount()
- count = 0
- for i=1,16 do
- if turtle.getItemCount(i) > 0 then
- count = count + 1
- end
- end
- return count
- end
- function highestMultipleOf(number, multiplier)
- return number - (number % multiplier)
- end
- function forceGoodMultiple(recipe)
- req = requiredQty(recipe)
- maxSlotCount = 64
- slotCount = 0
- maxSlotMultiple = highestMultipleOf(maxSlotCount, req)
- slotMultiple = 0
- turtle.select(1)
- turtle.dropUp() -- clear this slot for a fresh start
- turtle.suckUp()
- slotCount = turtle.getItemCount(1) -- see how much we got
- if slotCount > 64 then
- print("Holy crap! It gave us ", slotCount)
- return
- else
- -- print("Actual items from chest: ", slotCount)
- end
- slotMultiple = highestMultipleOf(slotCount, req)
- -- overage = maxSlotMultiple - slotMultiple
- overage = slotCount % req
- -- print("slotCount: ", slotCount, ", req: ", req, ", slotMultiple: ", slotMultiple)
- -- print(" overage = ", overage)
- if overage > 0 then
- turtle.dropUp(overage)
- count = turtle.getItemCount(1)
- end
- -- print("Forced count = ", slotCount)
- return count
- end
- function getMaterial(recipe)
- count = forceGoodMultiple(recipe)
- -- print(" Slot now contains: ", count)
- sleep(3)
- return count
- end
- function loadTable(recipe, qty)
- turtle.select(1)
- if recipe[2] > 0 then turtle.transferTo(5, qty) end
- if recipe[3] > 0 then turtle.transferTo(6, qty) end
- if recipe[4] > 0 then turtle.transferTo(7, qty) end
- if recipe[5] > 0 then turtle.transferTo(9, qty) end
- if recipe[6] > 0 then turtle.transferTo(10, qty) end
- if recipe[7] > 0 then turtle.transferTo(11, qty) end
- if recipe[8] > 0 then turtle.transferTo(13, qty) end
- if recipe[9] > 0 then turtle.transferTo(14, qty) end
- if recipe[10] > 0 then turtle.transferTo(15, qty) end
- end
- function clearInventory()
- for i=1,16 do
- turtle.select(i)
- turtle.dropUp()
- end
- turtle.select(1)
- end
- function craftRecipe(recipe)
- req = requiredQty(recipe)
- count = getMaterial(recipe)
- occupiedSlots = occupiedSlotCount()
- slotCount = turtle.getItemCount(1)
- if occupiedSlots ~= req then
- qty = slotCount / req
- loadTable(recipe, qty)
- slotCount = turtle.getItemCount(1)
- if slotCount ~= 0 then
- print("Wait. Slot 1 still has ", slotCount)
- end
- end
- occupiedSlots = occupiedSlotCount()
- -- print("occupied slots: ", occupiedSlots, ", req: ", req)
- if occupiedSlotCount() == req then
- turtle.select(16)
- if not turtle.craft() then
- print("Crafting failed. Wrong material?")
- turtle.select(1)
- return false
- else
- turtle.select(16)
- turtle.drop()
- turtle.select(1)
- end
- end
- return true
- end
- -- Main program
- args = {...}
- nArgs = #args
- if nArgs == 0 then
- print(usage)
- printList()
- return
- end
- if nArgs ~= 1 then
- print(usage)
- return
- end
- itemName = args[1]
- index = getIndex(itemName)
- if index == 0 then
- print("Can't find \"", itemName, "\"")
- return
- else
- recipe = recipes[index]
- req = requiredQty(recipe)
- print("Crafting recipe: ", recipe[1])
- print(req, " blocks material required for each.")
- print("Run must be terminated manually.")
- clearInventory()
- turtle.select(1)
- while true do
- if not craftRecipe(recipe) then
- break
- end
- sleep(0)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement