View difference between Paste ID: sBTwAePK and FeiUhiS5
SHOW: | | - or go back to the newest paste.
1
--[[
2
	Using: CC: Tweaked, Plethora
3-
	pastebin run FeiUhiS5
3+
	pastebin run sBTwAePK
4
	pastebin get sBTwAePK startup
5
]]--
6
7
if pcall(os.loadAPI, "jLib/jFuncs") then
8
else
9
	shell.run("pastebin get kqBFGcfV jLib/jFuncs")
10
	os.loadAPI("jLib/jFuncs")
11
end
12-
self = peripheral.wrap("left")
12+
if(fs.exists("jLib/ExternalCrafterRecipes")) then
13
	fs.delete("jLib/ExternalCrafterRecipes")
14-
local function toExportFormat(itemData)
14+
15-
	if(itemData == nil) then
15+
shell.run("pastebin get a2DA84Hg jLib/ExternalCrafterRecipes")
16-
		return nil
16+
17
18
local inputChest = peripheral.wrap("back")
19-
	ret = {}
19+
local self = peripheral.wrap("left").getInventory()
20-
	ret["name"] = itemData.name
20+
21-
	ret["damage"] = itemData.damage
21+
local file = fs.open("jLib/ExternalCrafterRecipes", "r")
22-
	ret["nbtHash"] = itemData.nbtHash
22+
local recipes = textutils.unserialize(file.readAll())
23
24-
	return ret
24+
local function containsRecipe(items, recipe)
25
    local needed = {}
26
    for _, material in pairs(recipe) do
27-
local inv = self.getInventory().list()
27+
        table.insert(needed, {["name"] = material.name, ["damage"] = material.damage, ["nbtHash"] = material.nbtHash, ["checked"] = false})
28-
local ret = {}
28+
    end
29-
ret["test"] = {}
29+
30-
ret["test"][1] = toExportFormat(inv[1])
30+
	local lItems = jFuncs.tableClone(items)
31-
ret["test"][2] = toExportFormat(inv[2])
31+
32-
ret["test"][3] = toExportFormat(inv[3])
32+
    local ret = {}
33
    for _, material in pairs(needed) do
34-
ret["test"][4] = toExportFormat(inv[5])
34+
        for i, item in pairs(lItems) do
35-
ret["test"][5] = toExportFormat(inv[6])
35+
            if(item.name == material.name and item.damage == material.damage and item.nbtHash == material.nbtHash and not material.checked and item.count > 0) then
36-
ret["test"][6] = toExportFormat(inv[7])
36+
                table.insert(ret, { ["slot"] = i })
37
                material.checked = true
38-
ret["test"][7] = toExportFormat(inv[9])
38+
				item.count = item.count - 1
39-
ret["test"][8] = toExportFormat(inv[10])
39+
            end
40-
ret["test"][9] = toExportFormat(inv[11])
40+
        end
41
    end
42
43
    for _, material in pairs(needed) do
44-
file = fs.open("tempUpload", "w")
44+
        if(not material.checked) then
45-
file.write(textutils.serialize(ret))
45+
            return nil
46-
file.close()
46+
        end
47-
shell.run("pastebin put tempUpload")
47+
    end
48
    
49
    return ret
50
end
51
52
local function firstValidRecipe(items)
53
    local valid = nil
54
    for i, recipe in pairs(recipes) do
55
        valid = containsRecipe(items, recipe)
56
        if(valid ~= nil) then
57
            return i
58
        end
59
    end
60
    return nil
61
end
62
63
local function getFirst(toGet) 
64
	local allItems = inputChest.list()
65
	for i, item in pairs(allItems) do
66
		if(item.name == toGet.name and item.damage == toGet.damage and item.nbtHash == toGet.nbtHash) then
67
			return i
68
		end
69
	end
70
	return nil
71
end
72
73
local function pullRecipe(recipe)
74
	local slot
75
	for i, material in pairs(recipe) do
76
		slot = getFirst(material)
77
		self.pullItems("back", slot, 1, math.floor(i + i/3 - 1/3))
78
	end
79
end
80
81
local function pushItems() 
82
	for i, _ in pairs(self.list()) do
83
		self.pushItems("top", i)
84
	end
85
end
86
87
88
while (true) do
89
    local allItems = inputChest.list()
90
    local recipeIndex = firstValidRecipe(allItems)
91
    if(recipeIndex ~= nil) then
92
		print("Pulling recipe: " .. recipeIndex)
93
        pullRecipe(recipes[recipeIndex])
94
		turtle.craft()
95
		pushItems() 
96
    else
97
        os.sleep(1)
98
    end 
99
end
100
101