· 4 years ago · Jun 12, 2021, 04:52 PM
1--Creator AleksWinS
2--Поддержать меня - подпишись twitch - alekcwins ; youtube - AlekcWinS
3local VERSION = "1.0.0"
4
5-----------------Info for developers--
6---Максимальное количество позиций внутри паттерна 512 через метод setInterfacePatternInput, output - 512
7--------------------------------------
8
9--------------------------------------
10local component = require "component"
11local sides = require("sides")
12local inventory = component.inventory_controller
13local interface = component.me_interface
14--------------------------------------
15
16
17
18
19-----------------Instructions---------
20--Минимальный контейнер с 27 слотами, если меньше - ошибка
21--Необходим только 1 адаптер с контроллером инвентаря и 1 адаптер с базой данных, к которому подключён рабочий МЭ интерфейс
22--К адаптеру с контроллером инвентаря подключается 2 сундука, cторона входного сундука указывается в SETTINGS, сторона выходного находиться автоматически
23--Шаблон, с которым будет работать программа, должен быть уже создан, например (из 1 булыжника получается 1 булыжник)
24--При записи в шаблон всё должно быть указано в 1 слотах, иначе эти предметы могут остаться после выполнения программы (кривая Api у OpenComputers)
25
26
27-------------Slots in Terminal------------------
28---из чего-- что на выходе---
29-- O X X ----- X
30-- X X X ----- X
31-- X X X ----- O
32
33------------------------
34-- O - Это Первый слот, который нужно заполнить, другие не трогайте
35------------------------
36
37
38
39-----------------SETTINGS-------------
40
41--Заменить при необходимости chest_InputSide - сторона сундука с предметами для крафта( bottom - не указывать) , вторая сторона находиться автоматически
42--Верх = sides.top
43--Задняя сторона(north) = sides.back
44--Передняя сторона(south) = sides.front
45--Правая сторона запад (west) = sides.right
46--Левая сторона восток (east) = sides.left
47local chest_InputSide = sides.top
48--------------------------------------
49
50function findDatabaseGuidOrError()
51 local guid = nil
52 for k,v in component.list() do
53 if v=="database" then
54 guid=k
55 break
56 end
57 end
58 if guid ~= nil then
59 return guid
60 else
61 print("Database not found")
62 error("Database not found")
63 end
64end
65
66function getDatabaseSize(database)
67 local isComplete, _out = pcall(database.get, 81)
68 if isComplete then
69 return 81
70 else
71 isComplete, _out = pcall(database.get, 25)
72 if isComplete then
73 return 25
74 else
75 isComplete, _out = pcall(database.get, 9)
76 if isComplete then
77 return 9
78 else
79 print("Error get Database Size")
80 error("Error get Database Size")
81 end
82 end
83 end
84end
85
86function clearDatabase(database,databaseSize)
87 for i=1,databaseSize do
88 database.clear(i)
89 end
90end
91
92function getClearDatabaseAndSize()
93 local guid = findDatabaseGuidOrError()
94 local database = component.proxy(guid)
95 local databaseSize = getDatabaseSize(database)
96 clearDatabase(database,databaseSize)
97 return database,databaseSize
98end
99
100function checkInputSideValue(chestInputSide)
101 if chestInputSide == nil or chestInputSide < 0 or chestInputSide > 5 then
102 print("Please specify the correct side of the discount")
103 error("Please specify the correct side of the discount")
104 elseif chestInputSide == 0 then
105 print("Can't use the bottom side to interact with the chest")
106 error("Can't use the bottom side to interact with the chest")
107 else
108 local inventorySize = inventory.getInventorySize(chestInputSide)
109 if inventorySize == nil then
110 print("The selected side of the input chest is incorrect")
111 error("The selected side of the input chest is incorrect")
112 elseif inventorySize < 27 then
113 print("Chest Inventory Less than 27 slots use Other chest")
114 error("Chest Inventory Less than 27 slots use Other chest")
115 end
116 end
117end
118
119function findOutputSideAndCheck(chestInputSide)
120 for i= 1,5 do
121 if i ~= chestInputSide then
122 local inventorySize = inventory.getInventorySize(i)
123 if inventorySize ~= nil and inventorySize >= 27 then
124 return i
125 end
126 end
127 end
128 print("Chest Inventory output not found it must be a chest with more than 26 inventory slots (default chest 27 clots)")
129 error("Chest Inventory output not found it must be a chest with more than 26 inventory slots (default chest 27 clots)")
130end
131
132function getMeInterface()
133 local guid = nil
134 for k,v in component.list() do
135 if v=="me_interface" then
136 guid=k
137 end
138 end
139 if guid ~= nil then
140 --TODO check energy in me system
141 return component.proxy(guid)
142 else
143 print("Me_interface not found")
144 error("Me_interface not found")
145 end
146
147end
148
149function getAllItemsInChestAndSaveDatabase(sideNumber,databaseAdr,databaseSize)
150 local allItemsInChest ={}
151 local indexDB = 1
152 for i = 1, inventory.getInventorySize(sideNumber) do
153 local currentItem = inventory.getStackInSlot(sideNumber,i)
154 if currentItem ~= nil then
155 if databaseSize >= indexDB then
156 inventory.store(sideNumber,i,databaseAdr,indexDB)
157 allItemsInChest[i] = { item = currentItem , Chestindex = i, databaseIndex =indexDB}
158 indexDB = indexDB + 1
159 else
160 print("database is too small")
161 error("database is too small")
162 break
163 end
164 end
165 end
166 return allItemsInChest
167end
168
169function findSlotPatternNumber(interface)
170 for i=1, 9 do
171 if interface.getInterfacePattern(i) ~= nil then
172 return i
173 end
174 end
175 print("pattern not found")
176 error("pattern not found")
177end
178
179--Mode - "output" , or "input"
180function saveItemsInPattern(itemsToSave,interface, mode, patternSlotNumber,databaseAdr)
181 local index = 1
182 for k,v in pairs(itemsToSave) do
183
184
185 if mode =="output" then
186 interface.setInterfacePatternOutput(patternSlotNumber, databaseAdr,v.databaseIndex, v.item.size, index )
187 index=index+1
188 elseif mode =="input" then
189 interface.setInterfacePatternInput(patternSlotNumber, databaseAdr, v.databaseIndex, v.item.size, index )
190 index=index+1
191 else
192 print("incorrect mode in saveItemsOutput function")
193 error("incorrect mode in saveItemsOutput function")
194 end
195 end
196end
197function printGoose()
198g= {{226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,132,226,150,128,226,150,128,226,150,128,226,150,132,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145},
199{226,150,132,226,150,136,226,150,136,226,150,136,226,150,128,226,150,145,226,151,144,226,150,145,226,150,145,226,150,145,226,150,140,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145},
200{226,150,145,226,150,145,226,150,145,226,150,145,226,150,140,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,144,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145},
201{226,150,145,226,150,145,226,150,145,226,150,145,226,150,144,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,144,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145},
202{226,150,145,226,150,145,226,150,145,226,150,145,226,150,140,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,144,226,150,132,226,150,132,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145},
203{226,150,145,226,150,145,226,150,145,226,150,145,226,150,140,226,150,145,226,150,145,226,150,145,226,150,145,226,150,132,226,150,128,226,150,146,226,150,146,226,150,128,226,150,128,226,150,128,226,150,128,226,150,132},
204{226,150,145,226,150,145,226,150,145,226,150,144,226,150,145,226,150,145,226,150,145,226,150,145,226,150,144,226,150,146,226,150,146,226,150,146,226,150,146,226,150,146,226,150,146,226,150,146,226,150,146,226,150,128,226,150,128,226,150,132},
205{226,150,145,226,150,145,226,150,145,226,150,144,226,150,145,226,150,145,226,150,145,226,150,145,226,150,144,226,150,132,226,150,146,226,150,146,226,150,146,226,150,146,226,150,146,226,150,146,226,150,146,226,150,146,226,150,146,226,150,146,226,150,128,226,150,132},
206{226,150,145,226,150,145,226,150,145,226,150,145,226,150,128,226,150,132,226,150,145,226,150,145,226,150,145,226,150,145,226,150,128,226,150,132,226,150,146,226,150,146,226,150,146,226,150,146,226,150,146,226,150,146,226,150,146,226,150,146,226,150,146,226,150,146,226,150,128,226,150,132},
207{226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,128,226,150,132,226,150,132,226,150,132,226,150,132,226,150,132,226,150,136,226,150,132,226,150,132,226,150,132,226,150,132,226,150,132,226,150,132,226,150,132,226,150,132,226,150,132,226,150,132,226,150,132,226,150,128,226,150,132},
208{226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,140,226,150,140,226,150,145,226,150,140,226,150,140,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145},
209{226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,140,226,150,140,226,150,145,226,150,140,226,150,140,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145},
210{226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145,226,150,132,226,150,132,226,150,140,226,150,140,226,150,132,226,150,140,226,150,140,226,150,145,226,150,145,226,150,145,226,150,145,226,150,145}
211}
212
213 for i,j in pairs(g) do
214 local outputRow = ""
215 for k,v in pairs(j) do
216 outputRow=outputRow..string.char(v)
217 end
218 print(outputRow)
219 end
220end
221
222--{ item = currentItem , Chestindex = i, databaseIndex =indexDB}
223function deNormolizeItems(items)
224 local outputItems = {}
225 for k,v in pairs(items) do
226 local key = v.item.name..v.item.label
227 if(outputItems [key]~=nil)then
228 local editedItem = outputItems [key].item
229 editedItem.size = editedItem.size + v.item.size
230 outputItems [key] ={ item = editedItem , Chestindex = outputItems [key].Chestindex, databaseIndex =outputItems [key].databaseIndex}
231 else
232 outputItems [key] = v
233 end
234 end
235 return outputItems
236end
237
238function itemCopy(item )
239 local out={}
240 for k,v in pairs(item) do
241 out[k]=v
242 end
243 return out
244end
245
246function normalizeItems(items)
247 local outputItems = {}
248 for k,v in pairs(items) do
249 local countItem = v.item.size
250 while countItem~=0 do
251 local currentItem = itemCopy(v.item)
252 if(countItem // v.item.maxSize==0) then
253 currentItem.size = countItem
254 table.insert(outputItems, { item = currentItem , Chestindex = v.Chestindex, databaseIndex =v.databaseIndex})
255 break
256 else
257 currentItem.size= v.item.maxSize
258 table.insert(outputItems, { item = currentItem , Chestindex = v.Chestindex, databaseIndex =v.databaseIndex})
259 countItem = countItem - v.item.maxSize
260 end
261 end
262 end
263 return outputItems
264end
265
266function changeItems(items)
267 items =deNormolizeItems(items)
268 for k,v in pairs(items) do
269 print(v.item.name .. " ".. v.item.label.. " current count: ".. v.item.size)
270 print("Press ENTER to continue or number for change")
271 local EditedCount = tonumber(io.read())
272 if EditedCount==0 then
273 print("0 items will destroy the world, do you want to delete this save?")
274 io.read()
275 printGoose()
276 error("unfortunately could not be destroyed, don't be a beast =)")
277 elseif EditedCount~= nil then
278 v.item.size=EditedCount
279 end
280 end
281 local outputItems = normalizeItems(items)
282
283
284
285 return outputItems
286end
287
288function main(chestInputSide)
289 local database, databaseSize = getClearDatabaseAndSize()
290 checkInputSideValue(chestInputSide)
291 local outputChestSide = findOutputSideAndCheck(chestInputSide)
292 local interface = getMeInterface()
293 local slotPatternNumber = findSlotPatternNumber(interface)
294
295 local inputItems = getAllItemsInChestAndSaveDatabase(chestInputSide,database.address,databaseSize)
296 local outputItems = getAllItemsInChestAndSaveDatabase(outputChestSide,database.address,databaseSize)
297
298 print("if you want to change the count of items press E or other for to continue")
299 local inputMode = string.lower(io.read())
300
301 if inputMode=="e" or inputMode =="у" then
302 print("input items - press E or other for to continue")
303 inputMode = string.lower(io.read())
304 if inputMode=="e" or inputMode =="у" then
305 inputItems = changeItems(inputItems)
306 end
307 print("input output - press E or other for to continue")
308 inputMode = string.lower(io.read())
309 if inputMode=="e" or inputMode =="у" then
310 outputItems = changeItems(outputItems)
311 end
312 end
313
314 saveItemsInPattern(inputItems,interface,"input",slotPatternNumber, database.address)
315 saveItemsInPattern(outputItems,interface,"output",slotPatternNumber,database.address)
316 print("Successful")
317end
318
319main(chest_InputSide)