SHOW:
|
|
- or go back to the newest paste.
| 1 | crafting = false | |
| 2 | altar = peripheral.wrap("crystal_1")
| |
| 3 | currItem = nil | |
| 4 | ||
| 5 | infusionRequester = peripheral.wrap("tileinterface_3")
| |
| 6 | ||
| 7 | redstone.setAnalogOutput("bottom",0)
| |
| 8 | ||
| 9 | function isAltarFull() | |
| 10 | return altar.getStackInSlot(1) ~= nil | |
| 11 | end | |
| 12 | ||
| 13 | function altarItem() | |
| 14 | return altar.getStackInSlot(1)["display_name"] | |
| 15 | end | |
| 16 | ||
| 17 | function enableInfusion() | |
| 18 | redstone.setAnalogOutput("bottom",15)
| |
| 19 | end | |
| 20 | ||
| 21 | function disableInfusion() | |
| 22 | redstone.setAnalogOutput("bottom", 0)
| |
| 23 | end | |
| 24 | ||
| 25 | function removeFromAltar() | |
| 26 | altar.pushItem("down",1)
| |
| 27 | end | |
| 28 | ||
| 29 | os.loadAPI("json")
| |
| 30 | ||
| 31 | function loadRecipes() | |
| 32 | recipes = {}
| |
| 33 | for i,path in ipairs(fs.list("recipes")) do
| |
| 34 | f = fs.open("recipes/"..path, "r")
| |
| 35 | j = f.readAll() | |
| 36 | f.close() | |
| 37 | recipe = json.decode(j) | |
| 38 | table.insert(recipes,recipe) | |
| 39 | end | |
| 40 | return recipes | |
| 41 | end | |
| 42 | ||
| 43 | craftsWaiting = {}
| |
| 44 | craftingJobs = {}
| |
| 45 | ||
| 46 | function dump(o) | |
| 47 | print("Dumping ")
| |
| 48 | if type(o) == 'table' then | |
| 49 | local s = '{ '
| |
| 50 | for k,v in pairs(o) do | |
| 51 | if type(k) ~= 'number' then k = '"'..k..'"' end | |
| 52 | s = s .. '['..k..'] = ' .. dump(v) .. ',' | |
| 53 | end | |
| 54 | return s .. '} ' | |
| 55 | else | |
| 56 | return tostring(o) | |
| 57 | end | |
| 58 | end | |
| 59 | ||
| 60 | infusionItemChest = peripheral.wrap("diamond_2")
| |
| 61 | ||
| 62 | ||
| 63 | function revert() | |
| 64 | clearTo(infusionItemChest, "up") | |
| 65 | end | |
| 66 | ||
| 67 | function requestItems(requester, itemArr, requestCrafts) | |
| 68 | complete = true | |
| 69 | jobs = {}
| |
| 70 | for i,item in ipairs(itemArr) do | |
| 71 | success, exported = pcall(requester.exportItem, item, "down", item.qty) | |
| 72 | if not success or exported == nil then | |
| 73 | exported = {size = 0}
| |
| 74 | end | |
| 75 | if exported.size < item.qty then | |
| 76 | if requestCrafts then | |
| 77 | amount = item.qty - exported.size | |
| 78 | print("Requesting crafting of "..amount.." "..item.display_name)
| |
| 79 | requester.requestCrafting(item, amount) | |
| 80 | jobObj = {id = jobId(amount, item.id, item.dmg, item.nbtHash), done = false}
| |
| 81 | table.insert(jobs, jobObj) | |
| 82 | table.insert(craftingJobs, jobObj) | |
| 83 | end | |
| 84 | complete = false | |
| 85 | end | |
| 86 | end | |
| 87 | return complete, jobs | |
| 88 | end | |
| 89 | ||
| 90 | function clearTo(chest, dir) | |
| 91 | for slot,item in ipairs(chest.getAllStacks()) do | |
| 92 | chest.pushItem(dir, slot) | |
| 93 | end | |
| 94 | end | |
| 95 | ||
| 96 | function tryToCraft(chest, recipe, requestCrafts) | |
| 97 | infusion, iJobs = requestItems(infusionRequester, recipe.infusionItems, requestCrafts) | |
| 98 | fullRecipe = infusion | |
| 99 | if not fullRecipe then | |
| 100 | revert() | |
| 101 | if requestCrafts then | |
| 102 | print("Adding recipe which infuses "..recipe.infusionItems[1].display_name.." to queue")
| |
| 103 | table.insert(craftsWaiting, recipe) | |
| 104 | print(craftswaiting) | |
| 105 | recipe.infusionJobs = iJobs | |
| 106 | end | |
| 107 | return false | |
| 108 | else | |
| 109 | crafting = true | |
| 110 | return true | |
| 111 | ||
| 112 | end | |
| 113 | end | |
| 114 | ||
| 115 | function jobsDone(jobs) | |
| 116 | for i,job in ipairs(jobs) do | |
| 117 | if not job.done then | |
| 118 | return false | |
| 119 | end | |
| 120 | end | |
| 121 | return true | |
| 122 | end | |
| 123 | ||
| 124 | function isDoneCrafting(recipe) | |
| 125 | return jobsDone(recipe.infusionJobs) | |
| 126 | end | |
| 127 | ||
| 128 | ||
| 129 | function requestRecipeByTrigger(chest, recipe) | |
| 130 | stacks = chest.getAllStacks() | |
| 131 | for slot,item in ipairs(stacks) do | |
| 132 | if item.all().display_name == recipe.triggerItems[1].display_name then | |
| 133 | print("Found trigger "..item.all().display_name)
| |
| 134 | crafted = tryToCraft(chest, recipe, true) | |
| 135 | chest.pushItem("north", slot,1)
| |
| 136 | chest.condenseItems() | |
| 137 | return crafted | |
| 138 | end | |
| 139 | end | |
| 140 | end | |
| 141 | ||
| 142 | ||
| 143 | function requestQueuedRecipe(chest, recipe, recipeSlot) | |
| 144 | if isDoneCrafting(recipe) then | |
| 145 | table.remove(craftsWaiting, recipeSlot) | |
| 146 | crafted = tryToCraft(chest, recipe, true) | |
| 147 | chest.condenseItems() | |
| 148 | return crafted | |
| 149 | end | |
| 150 | end | |
| 151 | ||
| 152 | inputChest = peripheral.wrap("crystal_4")
| |
| 153 | ||
| 154 | function jobId(qty,id,dmg,nbt) | |
| 155 | if nbt == nil then | |
| 156 | nbt = "nil" | |
| 157 | end | |
| 158 | return qty..":"..id..":"..dmg..":"..nbt | |
| 159 | end | |
| 160 | ||
| 161 | function listenForCrafts() | |
| 162 | while true do | |
| 163 | key, stack, state = os.pullEvent("crafting_state")
| |
| 164 | jobid = jobId(stack.size, stack.fingerprint.id, stack.fingerprint.dmg, stack.fingerprint.nbtHash) | |
| 165 | for i,job in ipairs(craftingJobs) do | |
| 166 | if job.id == jobid then | |
| 167 | table.remove(craftingJobs, i) | |
| 168 | sleep(3) | |
| 169 | job.done = true | |
| 170 | print("Job "..job.id.." is done")
| |
| 171 | end | |
| 172 | end | |
| 173 | end | |
| 174 | end | |
| 175 | ||
| 176 | function craftItems() | |
| 177 | revert() | |
| 178 | while true do | |
| 179 | ||
| 180 | if not crafting and not isAltarFull() then | |
| 181 | done = false | |
| 182 | recipes = loadRecipes() | |
| 183 | for i,r in ipairs(recipes) do | |
| 184 | if not done and requestRecipeByTrigger(inputChest, r) then | |
| 185 | enableInfusion() | |
| 186 | done = true | |
| 187 | end | |
| 188 | end | |
| 189 | for i,r in ipairs(craftsWaiting) do | |
| 190 | print(i) | |
| 191 | print(r) | |
| 192 | - | if not done and rcequestQueuedRecipe(inputChest, r, i) then |
| 192 | + | print(inputchest) |
| 193 | if not done and rcequestQueuedRecipe(inputChest, 1, 1) then | |
| 194 | print("Processed recipe which infuses "..r.infusionItems[1].display_name.." from queue")
| |
| 195 | enableInfusion() | |
| 196 | done = true | |
| 197 | end | |
| 198 | end | |
| 199 | end | |
| 200 | if not crafting and isAltarFull() then | |
| 201 | print("clearing up" .. altarItem())
| |
| 202 | removeFromAltar() | |
| 203 | crafting = false | |
| 204 | disableInfusion() | |
| 205 | sleep(1) | |
| 206 | end | |
| 207 | ||
| 208 | ||
| 209 | if crafting and isAltarFull() then | |
| 210 | print("Crafted a " .. altarItem())
| |
| 211 | removeFromAltar() | |
| 212 | crafting = false | |
| 213 | disableInfusion() | |
| 214 | sleep(1) | |
| 215 | end | |
| 216 | sleep(2) | |
| 217 | end | |
| 218 | end | |
| 219 | ||
| 220 | parallel.waitForAll(craftItems, listenForCrafts) |