SHOW:
|
|
- or go back to the newest paste.
1 | - | nextDirection = 2 |
1 | + | nextDirection = 3 |
2 | barrelDirection = 0 | |
3 | chestDirection = 5 | |
4 | craftingDirection = 4 | |
5 | sorterDirection = "bottom" | |
6 | modemDirection = "back" | |
7 | ||
8 | sorter = peripheral.wrap(sorterDirection) | |
9 | rednet.open(modemDirection) | |
10 | ||
11 | ||
12 | items = {} --UUID, Amount | |
13 | ||
14 | function split(pString, pPattern) --("A_Small_Dog", "_") | |
15 | local Table = {} | |
16 | local fpat = "(.-)" .. pPattern | |
17 | local last_end = 1 | |
18 | local s, e, cap = pString:find(fpat, 1) | |
19 | while s do | |
20 | if s ~= 1 or cap ~= "" then | |
21 | table.insert(Table, cap) | |
22 | end | |
23 | last_end = e + 1 | |
24 | s, e, cap = pString:find(fpat, last_end) | |
25 | end | |
26 | if last_end <= #pString then | |
27 | cap = pString:sub(last_end) | |
28 | table.insert(Table, cap) | |
29 | end | |
30 | return Table | |
31 | end | |
32 | ||
33 | function save(table, name) | |
34 | local file = fs.open(name, "w") | |
35 | file.write(textutils.serialize(table)) | |
36 | file.close() | |
37 | end | |
38 | ||
39 | function load(name) | |
40 | local file = fs.open(name, "r") | |
41 | if file ~= nil then | |
42 | local data = file.readAll() | |
43 | file.close() | |
44 | return textutils.unserialize(data) | |
45 | else | |
46 | return nil | |
47 | end | |
48 | end | |
49 | ||
50 | function reload() | |
51 | t = load("itemsData") | |
52 | if t == nil then | |
53 | for uuid, amount in pairs(sorter.list(barrelDirection)) do | |
54 | t = {uuid, 1} | |
55 | end | |
56 | end | |
57 | items = t | |
58 | end | |
59 | ||
60 | function barrelContains(uuid) | |
61 | if items[1] == uuid then | |
62 | return true | |
63 | else | |
64 | return false | |
65 | end | |
66 | end | |
67 | ||
68 | term.setCursorPos(1,1) | |
69 | term.clear() | |
70 | term.setCursorPos(1,1) | |
71 | ||
72 | reload() | |
73 | ||
74 | while true do | |
75 | event, param1, param2, param3 = os.pullEvent() | |
76 | if event == "isort_item" then | |
77 | if barrelContains(param1) then | |
78 | sorter.sort(barrelDirection) | |
79 | items[2] = items[2] + param2 | |
80 | save(items, "itemsData") | |
81 | else | |
82 | -- Check if sorter next to is empty else sleep(1) | |
83 | sorter.sort(nextDirection) | |
84 | end | |
85 | elseif event == "rednet_message" then | |
86 | print("Received Message") | |
87 | message = split(param2, " ") | |
88 | if message[1] == "inv" then | |
89 | print("Message Contains Inv") | |
90 | if message [2] == "update" then | |
91 | reload() | |
92 | elseif message[2] == "getAmount" then | |
93 | print("Message Wants Amount") | |
94 | print("In Barrel" ..items[1]) | |
95 | if tonumber(message[3]) == tonumber(items[1]) then | |
96 | print("Message Has Same Item") | |
97 | rMsg = "inv replyAmount " ..message[3] .." " ..items[2] | |
98 | rednet.send(param1, rMsg) | |
99 | end | |
100 | elseif message[2] == "withdraw" then | |
101 | if tonumber(message[3]) == tonumber(items[1]) then | |
102 | amount = tonumber(message[4]) | |
103 | if amount < items[2] + 1 then | |
104 | sorter.extract(barrelDirection, tonumber(message[3]), chestDirection, amount) | |
105 | items[2] = items[2] - amount | |
106 | save(items, "itemsData") | |
107 | -- Handle no enough items reply | |
108 | end | |
109 | end | |
110 | elseif message[2] == "sendToCrafter" then | |
111 | if barrelContains(tonumber(message[3])) then | |
112 | sorter.extract(0, tonumber(message[3]), craftingDirection, tonumber(message[4])) | |
113 | items[2] = items[2] - tonumber(message[4]) | |
114 | save(items, "itemsData") | |
115 | rednet.broadcast("inv crafterExtract " ..message[3]) | |
116 | end | |
117 | end | |
118 | end | |
119 | end | |
120 | end |