Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- how many items to craft at once?
- targetItemCount = 16
- -- sleep time between inv checks
- sleepTimeInventory = 2
- -- which item from /craftingGrid/ to craft?
- targetItemType = "compressedSawdust"
- craftingGrid = {
- compressedSawdust = {
- true, true, true, false,
- true, false, true, false,
- true, true, true, false,
- false, false, false, false
- },
- compressedCobblestone = {
- true, true, true, false,
- true, true, true, false,
- true, true, true, false,
- false, false, false, false
- }
- }
- craftingGrid_meta = {
- -- itemType = {
- -- resultSlot = number,
- -- }
- }
- function initCraftingGrid()
- -- Main function
- for gridKey, gridValue in pairs(craftingGrid) do
- if craftingGrid_meta[ gridKey ] == nil then
- craftingGrid_meta[ gridKey ] = {}
- end
- local requiredCraftingSlots = 0
- -- that's the first slot where the items would go to
- local craftResultSlot
- for i = 1, #gridValue do
- local slotValue = gridValue[i]
- if slotValue then
- requiredCraftingSlots = requiredCraftingSlots + 1
- else
- if not craftResultSlot then
- craftResultSlot = i
- end
- gridValue[ i ] = nil -- erase value from table
- end
- end
- if requiredCraftingSlots <= 9 then
- craftingGrid_meta[ gridKey ].requiredCraftingSlots = requiredCraftingSlots
- else
- print("Error: craftingGrid for [" .. gridKey .. "] requires more than 9 crafting slots!")
- print("Double-check this")
- error()
- end
- craftingGrid_meta[ gridKey ].resultSlot = craftResultSlot
- craftingGrid[ gridKey ] = gridValue -- apply changes to the global table from above
- end
- end
- function craftingGridSort( targetItem )
- local readyForCrafting = false
- for i, v in pairs( craftingGrid[ targetItem ] ) do
- -- item in slot /i/?
- if v then
- local currentSlotItems = turtle.getItemCount(i)
- -- return next slot or, if at the end, the first slot:
- local nextSlot = ( next(craftingGrid[ targetItem ], i) or next(craftingGrid[ targetItem ]) )
- print( type(nextSlot), " ", nextSlot)
- if currentSlotItems > targetItemCount then
- turtle.select(i)
- print("Transfer to: ", nextSlot, " ", (currentSlotItems - targetItemCount))
- turtle.transferTo(nextSlot, (currentSlotItems - targetItemCount))
- elseif nextSlot ~= 1 then
- return false
- end
- if nextSlot == 1 then
- readyForCrafting = true
- end
- end
- end
- return readyForCrafting
- end
- function inputCraftingItems()
- -- fetch from craftingGrid table
- return turtle.suckUp()
- end
- function outputCraftedItems( targetItem )
- -- same as above
- turtle.select( craftingGrid_meta[ targetItem ].resultSlot )
- return turtle.dropDown()
- end
- -- runtime
- initCraftingGrid()
- while true do
- print("Iteration")
- turtle.select(1)
- while inputCraftingItems( targetItemType ) == false do
- -- no new input items
- sleep( sleepTimeInventory )
- end
- -- ready to craft?
- if craftingGridSort( targetItemType ) then
- turtle.select( craftingGrid_meta[ targetItemType ].resultSlot )
- turtle.craft( targetItemCount )
- while outputCraftedItems( targetItemType ) == false do
- print("Can't output crafted Items!")
- sleep(5)
- end
- end
- sleep( sleepTimeInventory )
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement