hornedcommando

Minecraft Turtle Packager

Apr 25th, 2024 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.33 KB | Gaming | 0 0
  1. --Turtle program to Package items which can be 3x3 crafted between a front chest and a bottom chest
  2. --Will be integrated into the Butler.lua program at some point.
  3. --by hornedcommando
  4.  
  5. --Function to drop all inventory
  6. function emptyInventory()
  7.     for slot = 1, 16 do
  8.         turtle.select(slot)
  9.         turtle.drop()
  10.     end
  11.     --reselect the first slot (just in case)
  12.     turtle.select(1)
  13. end
  14.  
  15. --Function to package items
  16. function threeByThree()
  17.     --make sure we're on the first inventory slot
  18.     turtle.select(1)
  19.     --get the count, and name of the item
  20.     local slotDetail = turtle.getItemDetail()
  21.     if slotDetail and slotDetail.name then
  22.         if isCraftable(slotDetail.name) then
  23.             local count = turtle.getItemCount(1)
  24.             print("i'm holding " .. count .. " " .. slotDetail.name)
  25.             --verify at least 1 craft can be done
  26.             if count >= 9 then
  27.                 --calculate how many crafts can be done
  28.                 local maxCrafts = math.floor(count / 9)
  29.                 --distribute the named item into Slots 1,2,3,5,6,7,9,10,11.
  30.                 turtle.transferTo(2, maxCrafts)
  31.                 turtle.transferTo(3, maxCrafts)
  32.                 turtle.transferTo(5, maxCrafts)
  33.                 turtle.transferTo(6, maxCrafts)
  34.                 turtle.transferTo(7, maxCrafts)
  35.                 turtle.transferTo(9, maxCrafts)
  36.                 turtle.transferTo(10, maxCrafts)
  37.                 turtle.transferTo(11, maxCrafts)
  38.                 --craft the items
  39.                 turtle.craft(maxCrafts)
  40.                 --drop crafts and leftovers
  41.                 emptyInventory()
  42.             else
  43.                 --if not enough of an item to craft, drop it
  44.                 print("Not enough " .. slotDetail.name)
  45.                 turtle.drop()
  46.                 return nil
  47.             end
  48.         else
  49.             --if the item is not craftable, drop it
  50.             print("I can't package this")
  51.             turtle.drop()
  52.             return nil
  53.         end
  54.     end
  55. end
  56.  
  57.  
  58. --Function to verify if an item can be packaged
  59. function isCraftable(name)
  60.     local craftable = {
  61.         "minecraft:wheat",
  62.         "minecraft:potato",
  63.         "minecraft:carrot",
  64.         "minecraft:beetroot",
  65.         "minecraft:melon_slice",
  66.         "minecraft:charcoal",
  67.         "minecraft:coal",
  68.         "farmersdelight:tomato",
  69.         "farmersdelight:onion",
  70.         "farmersdelight:rice",
  71.         "farmersdelight:cabbage",
  72.         "thermal:apatite",
  73.         "thermal:sulfur",
  74.         "thermal:niter",
  75.         "thermal:cinnabar",
  76.         "minecraft:bone_meal",
  77.         "minecraft:lapis_lazuli",
  78.         "minecraft:redstone",
  79.         "minecraft:iron_ingot",
  80.         "minecraft:gold_ingot",
  81.         "minecraft:emerald",
  82.         "minecraft:diamond",
  83.         "supplementaries:flax"
  84.     }
  85.     for _, craftable in ipairs(craftable) do
  86.         if name == craftable then
  87.             return true
  88.         end
  89.     end
  90.     return false
  91. end
  92.  
  93. -- Function to empty the front chest and restart the packaging process
  94. function empty_filter_chest()
  95.     while turtle.suck() do
  96.         turtle.dropDown()
  97.     end
  98. end
  99.  
  100. -- Loops through functions to continually package between the two chests
  101. while true do
  102.     turtle.select(1)
  103.     while turtle.suckDown() do
  104.         threeByThree()
  105.     end
  106.     empty_filter_chest()
  107. end
Add Comment
Please, Sign In to add comment