iconmaster

elementalgems.lua

Jan 9th, 2022 (edited)
728
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.19 KB | None | 0 0
  1. local function logMsg(text) print("[" .. os.date("%T") .. "] " .. text) end
  2.  
  3. local function selectItem(id)
  4.     -- match exact
  5.     for i = 1, 16 do
  6.         local data = turtle.getItemDetail(i)
  7.         if data and data.name == id then
  8.             turtle.select(i)
  9.             return true
  10.         end
  11.     end
  12.     -- match by tags
  13.     for i = 1, 16 do
  14.         local data = turtle.getItemDetail(i, true)
  15.         if data then
  16.             for tag, _ in pairs(data.tags) do
  17.                 if tag == id then
  18.                     turtle.select(i)
  19.                     return true
  20.                 end
  21.             end
  22.         end
  23.     end
  24.     -- no match
  25.     return false
  26. end
  27.  
  28. local function nKeys(t)
  29.     local i = 0
  30.     for _, _ in pairs(t) do i = i + 1 end
  31.     return i
  32. end
  33.  
  34. local crystallizer
  35.  
  36. local QUALITY_NONE = 0
  37. local QUALITY_CRUDE = 1
  38. local QUALITY_FINE = 2
  39. local QUALITY_PRISTINE = 3
  40.  
  41. local qualityEnumToName = {
  42.     [0] = "NONE",
  43.     [1] = "CRUDE",
  44.     [2] = "FINE",
  45.     [3] = "PRISTINE"
  46. }
  47.  
  48. local function getCrystalState()
  49.     for _, item in pairs(crystallizer.list()) do
  50.         if item and item.name then
  51.             if item.name:find("pristine") then
  52.                 return QUALITY_PRISTINE
  53.             elseif item.name:find("fine") then
  54.                 return QUALITY_FINE
  55.             elseif item.name:find("crude") then
  56.                 return QUALITY_CRUDE
  57.             end
  58.         end
  59.     end
  60.     return QUALITY_NONE
  61. end
  62.  
  63. local function isCrystalDone() return nKeys(crystallizer.list()) == 1 end
  64.  
  65. local function processCrystal()
  66.     if not selectItem("elementalcraft:shards") then
  67.         turtle.suckUp() -- shards above on a pedestal
  68.     end
  69.     for shard = 1, 10 do
  70.         while not selectItem("elementalcraft:shards") do
  71.             turtle.suckUp() -- shards above on a pedestal
  72.             os.sleep(1)
  73.         end
  74.         turtle.drop(1) -- crystallizer in front
  75.     end
  76.     logMsg("Shards placed!")
  77.  
  78.     if not selectItem("elementalcraft:crystals") then
  79.         turtle.suckDown(1) -- crystals below on a pedestal
  80.     end
  81.     while not selectItem("elementalcraft:crystals") do
  82.         turtle.suckDown(1) -- crystals below on a pedestal
  83.         os.sleep(1)
  84.     end
  85.     turtle.drop(1) -- crystallizer in front
  86.     logMsg("Crystal placed!")
  87.  
  88.     while not isCrystalDone() do os.sleep(1) end
  89.     local quality = getCrystalState()
  90.     logMsg("Got gem of quality " .. qualityEnumToName[quality] .. "!")
  91.     if quality < QUALITY_PRISTINE then
  92.         logMsg("Gem not quality enough!")
  93.         return processCrystal()
  94.     else
  95.         logMsg("Gem is correct quality!")
  96.         turtle.suck() -- gem will be imported from turtle inventory
  97.         return nil
  98.     end
  99. end
  100.  
  101. local function mainLoop()
  102.     crystallizer = peripheral.find("elementalcraft:crystallizer")
  103.  
  104.     while selectItem("minecraft:diamond") do
  105.         logMsg("Gem requested!")
  106.         turtle.drop(1) -- crystallizer in front
  107.         processCrystal()
  108.     end
  109.  
  110.     os.sleep(1)
  111. end
  112.  
  113. local function errorHandler(error) logMsg("internal error: " .. error) end
  114.  
  115. logMsg("Elemental gem crafter started up.")
  116. while true do
  117.     -- xpcall(mainLoop, errorHandler)
  118.     mainLoop() -- for debugging
  119. end
  120.  
Advertisement
Add Comment
Please, Sign In to add comment