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