View difference between Paste ID: Y0n7aM90 and r9ZVLBqu
SHOW: | | - or go back to the newest paste.
1
crafting = false
2-
altar = peripheral.wrap("container_pedestal_0")
2+
altar = peripheral.wrap("crystal_1")
3
currItem = nil
4-
currItemnb = nil
4+
5
infusionRequester = peripheral.wrap("tileinterface_3")
6-
essentiaRequester = peripheral.wrap("tileinterface_2")
6+
7-
infusionRequester = peripheral.wrap("tileinterface_0")
7+
8-
pedestalRequester = peripheral.wrap("tileinterface_1")
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-
essentiaChest = peripheral.wrap("ender_chest_3")
63+
64-
pedestalChest = peripheral.wrap("crystal_2")
64+
65-
infusionItemChest = peripheral.wrap("ender_chest_1")
65+
66
67
function requestItems(requester, itemArr, requestCrafts)
68
  complete = true
69-
  clearTo(essentiaChest, "up")
69+
70-
  clearTo(pedestalChest, "up")
70+
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-
 pedestal, pJobs = requestItems(pedestalRequester, recipe.pedestalItems, requestCrafts)            
105+
106-
 fullRecipe = infusion and pedestal
106+
107
    return false
108
 else
109
	crafting = true
110
   return true
111
   
112
 end
113-
      recipe.pedestalJobs = pJobs
113+
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-
  return jobsDone(recipe.infusionJobs) and jobsDone(recipe.pedestalJobs)
131+
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-
inputChest = peripheral.wrap("ender_chest_4")
158+
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-
        sleep(0.5)
174+
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
193
      print("Processed recipe which infuses "..r.infusionItems[1].display_name.." from queue")
194
      enableInfusion()
195
      done = true
196-
    if not done and requestQueuedRecipe(inputChest, r, i) then
196+
197
   end   
198
 end
199
if not crafting and isAltarFull() then
200
   print("clearing up" .. altarItem())
201
   removeFromAltar()
202
   crafting = false
203
   disableInfusion()
204-
 if crafting and not isAltarFull() then
204+
205-
   error("Altar empty during craft, aborting")
205+
206
 
207
 
208-
 if not crafting and isAltarFull() then
208+
 if crafting and isAltarFull() then
209-
   sleep(4)
209+
210-
   crafting = true
210+
211-
   currItem = altarItem()
211+
212-
   currItemnb = altar.getStackInSlot(1).nbt_hash
212+
213-
   print("Infusing a " .. currItem)
213+
214
 end
215
 sleep(2) 
216-
 if crafting and altarItem() ~= currItem then
216+
217
end
218
219
parallel.waitForAll(craftItems, listenForCrafts)