Advertisement
djgaven588

ItemSorter

Mar 13th, 2024
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 19.91 KB | None | 0 0
  1. os.loadAPI("turtleLib.lua")
  2.  
  3. turtleLib.setHome({x=234,y=70,z=-676}, 2)
  4. turtleLib.setRefuel({x=234,y=70,z=-676}, 1)
  5. local dumpPosition = {x=234,y=69,z=-676}
  6. local dumpHeading = 3
  7. turtleLib.setDump(dumpPosition,dumpHeading)
  8.  
  9. turtleLib.goHome(true)
  10.  
  11. local pickupPosition={x=234,y=70,z=-676}
  12. local pickupHeading=4
  13.  
  14. local overflowPosition={x=234,y=70,z=-676}
  15. local overflowHeading=3
  16.  
  17. local dumpList = {
  18.    
  19. }
  20.  
  21. local craftingRecipes = {
  22.     ["minecraft:spruce_planks"] = {
  23.         [1]="minecraft:spruce_log",
  24.         ["count"]=4
  25.     },
  26.     ["minecraft:chest"] = {
  27.         [1]="minecraft:spruce_planks",
  28.         [2]="minecraft:spruce_planks",
  29.         [3]="minecraft:spruce_planks",
  30.         [4]="minecraft:spruce_planks",
  31.         [5]=nil,
  32.         [6]="minecraft:spruce_planks",
  33.         [7]="minecraft:spruce_planks",
  34.         [8]="minecraft:spruce_planks",
  35.         [9]="minecraft:spruce_planks",
  36.         ["count"]=1
  37.     }
  38. }
  39.  
  40. local itemMap = {
  41.     ["Charcoal"] = {
  42.         location={x=234,y=70,z=-676},
  43.         heading=1,
  44.         storedItems = {
  45.             "betterend:charcoal_block",
  46.             "minecraft:charcoal"
  47.         },
  48.         isStack=true
  49.     },
  50.     ["Bones"] = {
  51.         location={x=235,y=70,z=-676},
  52.         heading=1,
  53.         storedItems = {
  54.             "minecraft:bone",
  55.             "minecraft:bone_meal"
  56.         }
  57.     },
  58.     ["Arrows"] = {
  59.         location={x=236,y=70,z=-676},
  60.         heading=1,
  61.         storedItems = {
  62.             "minecraft:arrow"
  63.         }
  64.     },
  65.     ["Bows"] = {
  66.         location={x=237,y=70,z=-676},
  67.         heading=1,
  68.         storedItems = {
  69.             "minecraft:bow"
  70.         },
  71.         discardUnenchanted=true
  72.     },
  73.     ["Blaze Rods"] = {
  74.         location={x=238,y=70,z=-676},
  75.         heading=1,
  76.         storedItems = {
  77.             "minecraft:blaze_rod"
  78.         }
  79.     },
  80.     ["Rotten Flesh"] = {
  81.         location={x=239,y=70,z=-676},
  82.         heading=1,
  83.         storedItems = {
  84.             "minecraft:rotten_flesh",
  85.         }
  86.     },
  87.     ["Carrots"] = {
  88.         location={x=240,y=70,z=-676},
  89.         heading=1,
  90.         storedItems = {
  91.             "minecraft:carrot"
  92.         }
  93.     },
  94.     ["Potatos"] = {
  95.         location={x=241,y=70,z=-676},
  96.         heading=1,
  97.         storedItems = {
  98.             "minecraft:potato"
  99.         }
  100.     },
  101.     ["Wheat"] = {
  102.         location={x=242,y=70,z=-676},
  103.         heading=1,
  104.         storedItems = {
  105.             "minecraft:wheat"
  106.         }
  107.     },
  108.     ["Shields"] = {
  109.         location={x=240,y=71,z=-676},
  110.         heading=1,
  111.         storedItems = {
  112.             "minecraft:shield"
  113.         },
  114.         discardUnenchanted=true
  115.     },
  116.     ["Swords"] = {
  117.         location={x=239,y=71,z=-676},
  118.         heading=1,
  119.         storedItems = {
  120.             "minecraft:wooden_sword",
  121.             "minecraft:stone_sword",
  122.             "minecraft:golden_sword",
  123.             "minecraft:iron_sword"
  124.         },
  125.         discardUnenchanted=true
  126.     },
  127.     ["Boots"] = {
  128.         location={x=238,y=71,z=-676},
  129.         heading=1,
  130.         storedItems = {
  131.             "minecraft:leather_boots",
  132.             "minecraft:iron_boots",
  133.             "minecraft:golden_boots",
  134.             "minecraft:chainmail_boots"
  135.         },
  136.         discardUnenchanted=true
  137.     },
  138.     ["Leggings"] = {
  139.         location={x=237,y=71,z=-676},
  140.         heading=1,
  141.         storedItems = {
  142.             "minecraft:leather_leggings",
  143.             "minecraft:iron_leggings",
  144.             "minecraft:golden_leggings",
  145.             "minecraft:chainmail_leggings",
  146.         },
  147.         discardUnenchanted=true
  148.     },
  149.     ["Chestplates"] = {
  150.         location={x=236,y=71,z=-676},
  151.         heading=1,
  152.         storedItems = {
  153.             "minecraft:leather_chestplate",
  154.             "minecraft:iron_chestplate",
  155.             "minecraft:golden_chestplate",
  156.             "minecraft:chainmail_chestplate"
  157.         },
  158.         discardUnenchanted=true
  159.     },
  160.     ["Helmets"] = {
  161.         location={x=235,y=71,z=-676},
  162.         heading=1,
  163.         storedItems = {
  164.             "minecraft:leather_helmet",
  165.             "minecraft:iron_helmet",
  166.             "minecraft:golden_helmet",
  167.             "minecraft:chainmail_helmet"
  168.         },
  169.         discardUnenchanted=true
  170.     },
  171.     ["Gems"] = {
  172.         location={x=235,y=70,z=-676},
  173.         heading=3,
  174.         storedItems = {
  175.             "minecraft:redstone",
  176.             "minecraft:coal",
  177.             "minecraft:coal_ore",
  178.             "minecraft:diamond",
  179.             "minecraft:lapis_lazuli",
  180.             "minecraft:quartz",
  181.             "minecraft:amethyst_shard",
  182.             "minecraft:emerald",
  183.             "minecraft:glowstone_dust",
  184.             "minecraft:glowstone_block",
  185.             "minecraft:large_amethyst_bud",
  186.             "minecraft:small_amethyst_bud",
  187.             "minecraft:amethyst_block"
  188.         },
  189.         isStack=true
  190.     },
  191.     ["Metals"] = {
  192.         location={x=236,y=70,z=-676},
  193.         heading=3,
  194.         storedItems = {
  195.             "minecraft:iron_ingot",
  196.             "minecraft:raw_iron",
  197.             "minecraft:iron_nugget",
  198.             "minecraft:iron_block",
  199.             "minecraft:gold_ingot",
  200.             "minecraft:raw_gold",
  201.             "minecraft:gold_nugget",
  202.             "minecraft:gold_block",
  203.             "minecraft:copper_ingot",
  204.             "minecraft:raw_copper",
  205.             "minecraft:copper_nugget",
  206.             "minecraft:copper_block",
  207.             "minecraft:copper_ore",
  208.             "minecraft:iron_ore",
  209.             "minecraft:deepslate_iron_ore",
  210.             "tconstruct:raw_cobalt"
  211.         },
  212.         isStack=true
  213.     },
  214.     ["Building Materials"] = {
  215.         location={x=237,y=70,z=-676},
  216.         heading=3,
  217.         storedItems = {
  218.             "minecraft:cobblestone",
  219.             "minecraft:cobblestone_wall",
  220.             "minecraft:cobblestone_slab",
  221.             "minecraft:stone",
  222.             "minecraft:stone_slab",
  223.             "minecraft:stone_bricks",
  224.             "minecraft:stone_brick_slab",
  225.             "minecraft:sand",
  226.             "minecraft:dirt",
  227.             "minecraft:grass_block",
  228.             "minecraft:andesite",
  229.             "minecraft:podzol",
  230.             "minecraft:moss_block",
  231.             "minecraft:coarse_dirt",
  232.             "minecraft:cobbled_deepslate",
  233.             "minecraft:packed_mud",
  234.             "minecraft:tuff",
  235.             "minecraft:acacia_log",
  236.             "minecraft:acacia_planks",
  237.             "minecraft:spruce_planks",
  238.             "minecraft:spruce_slab",
  239.             "minecraft:spruce_door",
  240.             "minecraft:spruce_trapdoor",
  241.             "minecraft:spruce_sign",
  242.             "minecraft:ladder",
  243.             "minecraft:magma_block",
  244.             "minecraft:snow_block",
  245.             "minecraft:obsidian",
  246.             "minecraft:crying_obsidian",
  247.             "minecraft:oak_log",
  248.             "minecraft:oak_door",
  249.             "minecraft:oak_stairs",
  250.             "minecraft:blackstone",
  251.             "minecraft:rooted_dirt",
  252.             "minecraft:glass",
  253.             "minecraft:glass_pane",
  254.             "minecraft:warped_nylium",
  255.             "tconstruct:clear_glass",
  256.             "minecraft:mossy_cobblestone",
  257.             "minecraft:calcite",
  258.             "minecraft:dripstone_block",
  259.             "minecraft:gravel",
  260.             "minecraft:dripstone_block",
  261.             "minecraft:clay_ball",
  262.             "minecraft:clay_block",
  263.             "minecraft:brick",
  264.             "minecraft:bricks",
  265.             "minecraft:netherrack",
  266.             "minecraft:moss_carpet",
  267.         },
  268.         isStack=true
  269.     },
  270.     ["Wood"] = {
  271.         location={x=243,y=70,z=-676},
  272.         heading=1,
  273.         storedItems = {
  274.             "minecraft:spruce_log"
  275.         }
  276.     },
  277.     ["Ender Pearls"] = {
  278.         location={x=244,y=70,z=-676},
  279.         heading=1,
  280.         storedItems = {
  281.             "minecraft:ender_pearl"
  282.         }
  283.     },
  284.     ["Organics"] = {
  285.         location={x=238,y=70,z=-676},
  286.         heading=3,
  287.         storedItems = {
  288.             "minecraft:acacia_sapling",
  289.             "minecraft:brown_mushroom",
  290.             "minecraft:red_mushroom",
  291.             "minecraft:warped_fungus",
  292.             "minecraft:dandelion",
  293.             "minecraft:warped_roots",
  294.             "minecraft:twisting_vines",
  295.             "minecraft:poppy",
  296.             "minecraft:bamboo",
  297.             "minecraft:sugar_cane",
  298.             "minecraft:spruce_sapling",
  299.             "minecraft:oak_sapling",
  300.             "minecraft:vines",
  301.             "minecraft:glow_berries",
  302.             "minecraft:melon_seeds",
  303.             "minecraft:cookie",
  304.             "minecraft:nether_wart",
  305.             "minecraft:cooked_porkchop",
  306.             "minecraft:chicken",
  307.             "minecraft:cooked_chicken",
  308.             "minecraft:apple",
  309.             "minecraft:magma_cream",
  310.             "minecraft:gunpowder",
  311.             "minecraft:slime_ball",
  312.             "minecraft:feather",
  313.             "minecraft:leather",
  314.             "minecraft:steak",
  315.             "minecraft:cooked_cod",
  316.             "minecraft:cooked_salmon",
  317.             "minecraft:vine",
  318.             "minecraft:cactus",
  319.             "minecraft:kelp",
  320.             "minecraft:sea_pickle",
  321.             "botania:yellow_mystical_flower",
  322.             "minecraft:fire_coral",
  323.             "spectrum:clover",
  324.             "minecraft:bread",
  325.             "minecraft:pumpkin",
  326.             "minecraft:string",
  327.         }
  328.     },
  329.     ["Twigs"] = {
  330.         location={x=243,y=71,z=-676},
  331.         heading=1,
  332.         storedItems = {
  333.             "twigs:twig"
  334.         }
  335.     },
  336.     ["Seeds"] = {
  337.         location={x=242,y=71,z=-676},
  338.         heading=1,
  339.         storedItems = {
  340.             "minecraft:wheat_seeds"
  341.         }
  342.     },
  343.     ["Tools"] = {
  344.         location={x=239,y=70,z=-676},
  345.         heading=3,
  346.         storedItems = {
  347.             "minecraft:wooden_shovel",
  348.             "minecraft:stone_shovel",
  349.             "minecraft:iron_shovel",
  350.             "minecraft:golden_shovel",
  351.             "minecraft:diamond_shovel",
  352.             "minecraft:wooden_pickaxe",
  353.             "minecraft:stone_pickaxe",
  354.             "minecraft:iron_pickaxe",
  355.             "minecraft:golden_pickaxe",
  356.             "minecraft:diamond_pickaxe",
  357.             "minecraft:wooden_hoe",
  358.             "minecraft:stone_hoe",
  359.             "minecraft:iron_hoe",
  360.             "minecraft:golden_hoe",
  361.             "minecraft:diamond_hoe",
  362.             "minecraft:wooden_axe",
  363.             "minecraft:stone_axe",
  364.             "minecraft:iron_axe",
  365.             "minecraft:golden_axe",
  366.             "minecraft:diamond_axe",
  367.             "minecraft:flint_and_steel",
  368.             "minecraft:bucket",
  369.             "minecraft:water_bucket",
  370.             "minecraft:lava_bucket",
  371.             "minecraft:torch",
  372.             "minecraft:diamond_sword",
  373.             "minecraft:diamond_boots",
  374.             "minecraft:diamond_leggings",
  375.             "minecraft:diamond_chestplate",
  376.             "minecraft:diamond_helmet",
  377.             "minecraft:golden_apple",
  378.             "minecraft:enchanted_golden_apple",
  379.             "minecraft:saddle",
  380.             "minecraft:iron_horse_armor",
  381.             "minecraft:golden_horse_armor",
  382.             "minecraft:diamond_horse_armor",
  383.             "minecraft:tipped_arrow",
  384.             "minecraft:enchanted_book",
  385.             "minecraft:supplementaries:bomb",
  386.             "minecraft:shears"
  387.         }
  388.     }
  389. }
  390.  
  391. local function getItemQuantity(item)
  392.     local currentSlot = turtle.getSelectedSlot()
  393.    
  394.     local counter = 0
  395.     for i=3,16,1 do
  396.         turtle.select(i)
  397.         local currentItem = turtle.getItemDetail()
  398.         if currentItem and currentItem.name == item then
  399.             counter = counter + currentItem.count
  400.         end
  401.     end
  402.  
  403.     turtle.select(currentSlot)
  404.     return counter
  405. end
  406.  
  407. local function dropItemTillQuantity(item,count)
  408.     local currentSlot = turtle.getSelectedSlot()
  409.  
  410.     local counter = 0
  411.     for i=3,16,1 do
  412.         turtle.select(i)
  413.         local currentItem = turtle.getItemDetail()
  414.         if currentItem and currentItem.name == item then
  415.             counter = counter + currentItem.count
  416.             if counter > count then
  417.                 turtle.drop(counter-count)
  418.                 counter = count
  419.             end
  420.         end
  421.     end
  422.  
  423.     turtle.select(currentSlot)
  424. end
  425.  
  426. function getItem(name,count)
  427.     local foundCount = 0
  428.     print("Finding item",name,count)
  429.     for containerName,container in pairs(itemMap) do
  430.         for i,item in ipairs(container.storedItems) do
  431.             if item == name then
  432.                 local nextPos = container.location
  433.                 repeat
  434.                     print("Checking container",containerName)
  435.                     turtleLib.navigateToLocation(nextPos,container.heading, true)
  436.                     if turtle.detect() == false then
  437.                         print("Reached end of container!")
  438.                         break
  439.                     end
  440.                     turtle.select(2)
  441.                     turtle.placeUp()
  442.                     turtle.select(3)
  443.                     while true do
  444.                         if turtle.suck() == false then
  445.                             break
  446.                         end
  447.                         local curItem = turtle.getItemDetail()
  448.                         if curItem.name == name then
  449.                             foundCount = foundCount + turtle.getItemCount()
  450.                             local slot = 4;
  451.                             while turtle.getItemCount() > 0 do
  452.                                 turtle.transferTo(slot, 64)
  453.                                 slot = slot + 1
  454.                             end
  455.                             if foundCount > count then
  456.                                 break
  457.                             end
  458.                         else
  459.                             if turtle.dropUp() == false then
  460.                                 break
  461.                             end
  462.                         end
  463.                     end
  464.                     repeat
  465.                         turtle.drop()
  466.                     until turtle.suckUp() == false
  467.  
  468.                     dropItemTillQuantity(name, count);
  469.  
  470.                     turtle.select(2)
  471.                     turtle.digUp()
  472.                     if foundCount >= count then
  473.                         print("Found all!")
  474.                         break
  475.                     end
  476.                     nextPos.y = nextPos.y + 1
  477.                 until container.isStack == false
  478.             end
  479.         end
  480.     end
  481.     return foundCount >= count
  482. end
  483.  
  484. function putAwayItem()
  485.     local currentItem = turtle.getItemDetail()
  486.     for j,exclusion in ipairs(dumpList) do
  487.         if exclusion == currentItem.name then
  488.             print("Dumping item",exclusion)
  489.             turtleLib.navigateToLocation(dumpPosition, dumpHeading, true)
  490.             turtle.drop()
  491.             return    
  492.         end
  493.     end
  494.     local seenContainer = false
  495.     for containerName,container in pairs(itemMap) do
  496.         for i,item in ipairs(container.storedItems) do
  497.             if item == currentItem.name then
  498.                 if container.discardUnenchanted then
  499.                     if turtle.getItemDetail(turtle.getSelectedSlot(), true).enchantments == nil then
  500.                         print("Discarding unenchanted item")
  501.                         turtleLib.navigateToLocation(dumpPosition, dumpHeading, true)
  502.                         turtle.drop()
  503.                         return
  504.                     end
  505.                 end
  506.                 print("Going to container",containerName)
  507.                 turtleLib.navigateToLocation(container.location,container.heading,true)
  508.                 seenContainer = true
  509.                 if container.isStack == true then
  510.                     while turtle.detect() do
  511.                         turtle.drop()
  512.                         if turtle.getItemCount() == 0 then
  513.                             return
  514.                         end
  515.                         turtleLib.forceUp()
  516.                     end
  517.                 else
  518.                     turtle.drop()
  519.                     if turtle.getItemCount() == 0 then
  520.                         return
  521.                     end
  522.                 end
  523.             end
  524.         end
  525.     end
  526.    
  527.     if seenContainer == false then
  528.         print("Putting unknown item in overflow")
  529.         turtleLib.navigateToLocation(overflowPosition,overflowHeading,true)
  530.         turtle.drop()
  531.         if turtle.getItemCount() == 0 then
  532.             return
  533.         end
  534.     end
  535.    
  536.     print("Item overflowed, dumping")
  537.     turtleLib.navigateToLocation(dumpPosition,dumpHeading,true)
  538.     turtle.drop()
  539. end
  540.  
  541. function fillInventory()
  542.     turtle.select(1)
  543.     while turtle.suck() do
  544.     end
  545. end
  546.  
  547. function cycle()
  548.     turtleLib.navigateToLocation(pickupPosition, pickupHeading, true)
  549.     fillInventory()
  550.     for i=3,16,1 do
  551.         turtle.select(i)
  552.         if turtle.getItemCount() ~= 0 then
  553.             putAwayItem()
  554.         end
  555.     end
  556. end
  557.  
  558. function getOrCraftItem(name,count,clear)
  559.     local gotItems = false
  560.     while clear == true do
  561.         turtleLib.goHome(true)
  562.         turtleLib.forceDown()
  563.         turtle.select(3)
  564.         if not turtle.suckDown() then
  565.             if gotItems == true then
  566.                 cycle()
  567.             else
  568.                 break
  569.             end
  570.         else
  571.             gotItems = true
  572.         end
  573.     end
  574.     if getItem(name, count) then
  575.         turtleLib.goHome(true)
  576.         return true
  577.     end
  578.     local recipe = nil
  579.     for i,tempRecipe in pairs(craftingRecipes) do
  580.         print("Recipe",i)
  581.         if i == name then
  582.             recipe = tempRecipe
  583.         end
  584.     end
  585.     if recipe == nil then
  586.         print("No item or recipe found for",name,count)
  587.         return false
  588.     end
  589.  
  590.     turtleLib.goHome(true)
  591.  
  592.     local alreadyRetrieved = getItemQuantity(name);
  593.  
  594.     turtleLib.forceDown()
  595.  
  596.     for i=3,16,1 do
  597.         turtle.select(i)
  598.         turtle.dropDown()
  599.     end
  600.  
  601.     local recipeCount = math.ceil((count-alreadyRetrieved) / recipe.count)
  602.     local recipeRequirements = {}
  603.     -- For each craft slot
  604.     for i=1,9,1 do
  605.         if recipe[i] ~= nil then
  606.             if recipeRequirements[recipe[i]] == nil then
  607.                 recipeRequirements[recipe[i]] = recipeCount
  608.             else
  609.                 recipeRequirements[recipe[i]] = recipeRequirements[recipe[i]] + recipeCount
  610.             end
  611.         end
  612.     end
  613.  
  614.     -- For each requirement
  615.     for i,requirement in pairs(recipeRequirements) do
  616.         if getOrCraftItem(i, requirement, true) == false then
  617.             print("Craft / retrieve failed for",i, requirement)
  618.             return false
  619.         end
  620.         turtleLib.goHome(true)
  621.         turtleLib.forceDown()
  622.         for j=3,16,1 do
  623.             turtle.dropDown()
  624.         end
  625.         print("Requirement",i,requirement,"retrieved")
  626.     end
  627.  
  628.     -- Setup work chest
  629.     turtle.select(2)
  630.     turtle.placeUp()
  631.  
  632.     -- For each slot
  633.     turtle.select(15)
  634.     for i=1,9,1 do
  635.         if recipe[i] then
  636.             local lastCount = 0
  637.             -- Get enough items for slot
  638.             while lastCount < recipeCount do
  639.                 turtle.suckDown()
  640.                 local item = turtle.getItemDetail()
  641.                 if item.name ~= recipe[i] then
  642.                    turtle.dropUp()
  643.                 else
  644.                     turtle.transferTo(i, recipeCount - lastCount)
  645.                     lastCount = turtle.getItemCount(i)
  646.                     turtle.dropDown()
  647.                 end
  648.             end
  649.         end
  650.     end
  651.  
  652.     -- Finally we can craft!
  653.     -- Clear standard issue
  654.     turtleLib.turnLeft()
  655.     turtle.select(1)
  656.     turtle.dropUp()
  657.     turtle.select(2)
  658.     turtle.dropUp()
  659.     turtle.equipRight()
  660.     turtle.dropUp()
  661.     turtle.suck()
  662.     turtle.equipRight()
  663.  
  664.     -- Move all ingredients
  665.     for i=1,6,1 do
  666.         turtle.select(i + 2)
  667.         turtle.drop()
  668.         turtle.select(i+math.floor((i-1)/3))
  669.         turtle.suck()
  670.     end
  671.    
  672.     -- Craft damn it
  673.     turtle.select(15)
  674.     turtle.craft(recipeCount)
  675.    
  676.     -- Fix inventory immediately
  677.     turtle.select(1)
  678.     turtle.suckUp()
  679.     turtle.select(2)
  680.     turtle.suckUp()
  681.     turtle.select(3)
  682.     turtle.suckUp()
  683.     turtle.equipRight()
  684.     turtle.drop()
  685.    
  686.     turtle.select(2)
  687.     turtle.digUp()
  688.    
  689.     turtle.select(15)
  690.    
  691.     turtle.turnRight()
  692.    
  693.     return turtle.getItemCount() >= count
  694. end
  695.  
  696. function handleCraftDropoff()
  697.     turtleLib.goHome(true)
  698.     turtleLib.forceUp()
  699.     turtleLib.forceUp()
  700.     turtleLib.dropUp()
  701. end
  702.  
  703. function craftItem(item, count)
  704.     while count > 64 do
  705.         if getOrCraftItem(item, 64) == false then
  706.             return false
  707.         end
  708.  
  709.         handleCraftDropoff()
  710.     end
  711.     if getOrCraftItem(item, count) then
  712.         handleCraftDropoff()
  713.         print("Completed job for",item,count)
  714.         return true
  715.     end
  716.     return false
  717. end
  718.  
  719. while true do
  720.     for i=1,32,1 do
  721.         cycle()
  722.         craftItem("minecraft:chest", 8)
  723.     end
  724.     turtleLib.goHome(true)
  725.     turtleLib.goRefuel(true)
  726. end
  727.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement