jubjub727

Untitled

Aug 30th, 2023
5
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.61 KB | None | 0 0
  1. local clients = peripheral.call("back", "getNamesRemote")
  2.  
  3. local chests = {}
  4.  
  5. local items = {}
  6. local itemCollection = {}
  7.  
  8. local usingCollection = false
  9.  
  10. local itemPage = 1
  11.  
  12. local searchTerm = ""
  13.  
  14. local function tprint (tbl, indent)
  15. if not indent then indent = 0 end
  16. for k, v in pairs(tbl) do
  17. formatting = string.rep(" ", indent) .. k .. ": "
  18. if type(v) == "table" then
  19. print(formatting)
  20. tprint(v, indent+1)
  21. elseif type(v) == 'boolean' then
  22. print(formatting .. tostring(v))
  23. else
  24. print(formatting .. v)
  25. end
  26. end
  27. end
  28.  
  29. local function split(inputstr, sep)
  30. if sep == nil then
  31. sep = "%s"
  32. end
  33. local t={}
  34. for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  35. table.insert(t, str)
  36. end
  37. return t
  38. end
  39.  
  40. local function DiscoverChests()
  41. for k,client in pairs(clients) do
  42. if (string.sub(peripheral.getType(client), 1, 19) == "minecraft:ironchest") then
  43. table.insert(chests, client)
  44. end
  45. end
  46. end
  47.  
  48. local function DiscoverItems()
  49. for _k,_chest in pairs(chests) do
  50. local chest = peripheral.wrap(_chest)
  51.  
  52. local list = chest.list()
  53.  
  54. local listNum = #list
  55. local chestNum = #chests
  56.  
  57. for k,_item in pairs(list) do
  58. term.clear()
  59. term.setCursorPos(1,1)
  60. print(tostring(_k).."/"..tostring(chestNum))
  61. print(tostring(k).."/"..tostring(listNum))
  62. --tprint(_item)
  63.  
  64. --local item = chest.getItem(k)
  65.  
  66. local itemTable = {}
  67.  
  68. --itemTable.metaData = item.getMetadata()
  69. --itemTable.displayName = itemTable.metaData.displayName
  70. itemTable.displayName = _item.name
  71. itemTable.name = _item.name
  72. itemTable.count = _item.count
  73. itemTable.chest = _chest
  74. itemTable.slot = k
  75.  
  76. table.insert(items, itemTable)
  77. end
  78. end
  79. end
  80.  
  81. local function Request(itemIndex, count)
  82. local item = items[itemIndex]
  83. local chest = peripheral.wrap(item.chest)
  84.  
  85. if (count == nil) then
  86. chest.pushItems("minecraft:chest_2", item.slot)
  87. else
  88. chest.pushItems("minecraft:chest_2", item.slot, count)
  89. end
  90. end
  91.  
  92. local function Deposit()
  93. local chest = peripheral.wrap("minecraft:chest_2")
  94. local depositChest = nil
  95.  
  96. for k, v in pairs(chests) do
  97. local depChest = peripheral.wrap(v)
  98.  
  99. if (#depChest.list() < depChest.size()) then
  100. depositChest = v
  101. end
  102. end
  103.  
  104. if (depositChest == nil) then
  105. print("No available chest space")
  106. read()
  107. end
  108.  
  109. for k, item in pairs(chest.list()) do
  110. chest.pushItems(depositChest, k)
  111. end
  112. end
  113.  
  114. local function DrawScreen(start)
  115. term.clear()
  116. term.setCursorPos(1,1)
  117.  
  118. print("Loaded "..#items.." Items")
  119.  
  120. for i=start,11+start do
  121. if (#items >= i) then
  122. print(i..": "..items[i].displayName.." - "..items[i].count)
  123. end
  124. end
  125.  
  126. print("\n")
  127.  
  128. print("Commands: dep (deposit items), req (request item <index> <count>), find (find items <text>), col\nPage Left - Left Arrow | Page Right - Right Arrow")
  129. end
  130.  
  131. local function DrawScreenCollection(start)
  132. term.clear()
  133. term.setCursorPos(1,1)
  134.  
  135. print("Loaded "..#itemCollection.." Items")
  136.  
  137. for i=start,11+start do
  138. if (#itemCollection >= i) then
  139. print(i..": "..itemCollection[i].displayName.." - "..itemCollection[i].count)
  140. end
  141. end
  142.  
  143. print("\n")
  144.  
  145. print("Commands: dep (deposit items), req (request item <index> <count>), find (find items <text>), col\nPage Left - Left Arrow | Page Right - Right Arrow")
  146. end
  147.  
  148. local function LoadItemCollection()
  149. for k,item in pairs(items) do
  150. local found = false
  151.  
  152. for _k,_v in pairs(itemCollection) do
  153. if (_v.name == item.name) then
  154. itemCollection[_k].count = itemCollection[_k].count + item.count
  155. table.insert(itemCollection[_k].slots, item)
  156. found = true
  157. end
  158. end
  159.  
  160. if (found == false) then
  161. local itemCollectionEntry = {}
  162. itemCollectionEntry.count = item.count
  163. itemCollectionEntry.slots = {}
  164. itemCollectionEntry.displayName = item.displayName
  165. itemCollectionEntry.name = item.name
  166.  
  167. table.insert(itemCollectionEntry.slots, item)
  168.  
  169. table.insert(itemCollection, itemCollectionEntry)
  170. end
  171. end
  172. end
  173.  
  174. local function Reload()
  175. chests = {}
  176. items = {}
  177. searchTerm = ""
  178.  
  179. DiscoverChests()
  180.  
  181. DiscoverItems()
  182.  
  183. LoadItemCollection()
  184. end
  185.  
  186. local function DrawSearch(search, start)
  187. local searchResults = {}
  188.  
  189. for k,item in pairs(items) do
  190. if (string.find(string.lower(item.displayName), search) or string.find(string.lower(item.name), search)) then
  191. item.index = k
  192. table.insert(searchResults, item)
  193. end
  194. end
  195.  
  196. if (start > #searchResults) then
  197. start = #searchResults
  198. end
  199.  
  200. term.clear()
  201. term.setCursorPos(1,1)
  202.  
  203. print("Found "..#searchResults.." Items")
  204.  
  205. if (#searchResults ~= 0) then
  206. for i=start,11+start do
  207. if (#searchResults >= i) then
  208. print(searchResults[i].index..": "..searchResults[i].displayName.." - "..searchResults[i].count)
  209. end
  210. end
  211. end
  212.  
  213. print("\n")
  214.  
  215. print("Commands: dep (deposit items), req (request item <index> <count>), find (find items <text>), col\nPage Left - Left Arrow | Page Right - Right Arrow")
  216. end
  217.  
  218. local function DrawSearchCollection(search, start)
  219. local searchResults = {}
  220.  
  221. for k,item in pairs(itemCollection) do
  222. if (string.find(string.lower(item.displayName), search) or string.find(string.lower(item.name), search)) then
  223. item.index = k
  224. table.insert(searchResults, item)
  225. end
  226. end
  227.  
  228. if (start > #searchResults) then
  229. start = #searchResults
  230. end
  231.  
  232. term.clear()
  233. term.setCursorPos(1,1)
  234.  
  235. print("Found "..#searchResults.." Items")
  236.  
  237. if (#searchResults ~= 0) then
  238. for i=start,11+start do
  239. if (#searchResults >= i) then
  240. print(searchResults[i].index..": "..searchResults[i].displayName.." - "..searchResults[i].count)
  241. end
  242. end
  243. end
  244.  
  245. print("\n")
  246.  
  247. print("Commands: dep (deposit items), req (request item <index> <count>), find (find items <text>), col\nPage Left - Left Arrow | Page Right - Right Arrow")
  248. end
  249.  
  250. local function main()
  251.  
  252. if (searchTerm == "" or searchTerm == nil) then
  253. if (usingCollection) then
  254. DrawScreenCollection(itemPage)
  255. else
  256. DrawScreen(itemPage)
  257. end
  258. else
  259. if (usingCollection) then
  260. DrawSearchCollection(searchTerm, itemPage)
  261. else
  262. DrawSearch(searchTerm, itemPage)
  263. end
  264. end
  265.  
  266. local event, key = os.pullEvent("key")
  267.  
  268. if (key == keys.left) then
  269. itemPage = itemPage - 12
  270. if (itemPage < 1) then
  271. itemPage = 1
  272. end
  273.  
  274. main()
  275. return
  276. end
  277.  
  278. if (key == keys.right) then
  279. itemPage = itemPage + 12
  280. if (not usingCollection) then
  281. if (itemPage > #items) then
  282. itemPage = #items
  283. end
  284. else
  285. if (itemPage > #itemCollection) then
  286. itemPage = #itemCollection
  287. end
  288. end
  289.  
  290. main()
  291. return
  292. end
  293.  
  294. local input = read()
  295.  
  296. if (string.lower(string.sub(input, 1, 3)) == "dep") then
  297. Deposit()
  298. Reload()
  299. end
  300.  
  301. if (string.lower(string.sub(input, 1, 3)) == "col") then
  302. usingCollection = not usingCollection
  303. itemPage = 1
  304. end
  305.  
  306. if (string.lower(string.sub(input, 1, 3)) == "req") then
  307. local inputs = split(input)
  308. local itemIndex = tonumber(inputs[2])
  309. local count = tonumber(inputs[3])
  310.  
  311. if (itemIndex == nil) then
  312. print("Invalid input - Press Enter to continue")
  313. read()
  314. else
  315. Request(itemIndex, count)
  316. Reload()
  317. end
  318. end
  319.  
  320. if (string.lower(string.sub(input, 1, 4)) == "find") then
  321. itemIndex = 1
  322. if (split(input)[2] ~= nil) then
  323. searchTerm = string.lower(split(input)[2])
  324. else
  325. searchTerm = ""
  326. end
  327. end
  328.  
  329. main()
  330. end
  331.  
  332. Reload()
  333. main()
Add Comment
Please, Sign In to add comment