Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function startup()
- for k,v in pairs(peripheral.getNames()) do
- if peripheral.getType(peripheral.getNames()[k]) == "modem" then
- rednet.open(peripheral.getNames()[k])
- end
- end
- scrollmenuLen={}
- scrollmenuInfos={}
- whitelistedPocketComputers = {53}
- nonStackableItems = {}
- mainStorage = Storage:new({"create:item_vault"},"minecraft:barrel_1","minecraft:barrel_0")
- mainStorage:refreshStorageContent()
- Chaos_Cash = Player:new("player_interface_3")
- end
- function main()
- while true do
- info1,info2,info3,info4 = os.pullEvent()
- if info1 == "rednet_message" then
- for k,v in pairs(whitelistedPocketComputers) do
- if v == info2 then
- if type(info3) == "table" then
- if info3["action"] == "inputToPlayer" then
- if nonStackableItems[info3["itemName"]] then
- Chaos_Cash:inputFromStorage(mainStorage,info3["itemName"],1)
- else
- Chaos_Cash:inputFromStorage(mainStorage,info3["itemName"],info3["amount"])
- end
- rednet.send(info2,{["action"]="sendingStorageContent",["storageContent"]=mainStorage:getStorageContent()})
- end
- if info3["action"] == "inputToStorage" then
- Chaos_Cash:inputToStorage(mainStorage,info3["slot"],info3["itemName"],info3["amount"])
- rednet.send(info2,{["action"]="sendingPlayerInventory",["inventory"]=Chaos_Cash:getInventory()})
- end
- else
- if info3 == "ping" then
- rednet.send(info2,"pong")
- end
- if info3 == "requestingStorageContent" then
- rednet.send(info2,{["action"]="sendingStorageContent",["storageContent"]=mainStorage:getStorageContent()})
- end
- if info3 == "requestingPlayerInventory" then
- rednet.send(info2,{["action"]="sendingPlayerInventory",["inventory"]=Chaos_Cash:getInventory()})
- end
- end
- break
- end
- end
- end
- if info1 == "key" and info2 == keys.space then
- mainStorage:input()
- end
- if curEnv == homeMenu then
- scrollOut = checkScrollmenu("storageItems")
- if scrollOut then
- mainStorage:output(scrollmenuInfos["storageItems"]["content"][scrollOut],64)
- scrollContent = getItemNames()
- table.sort(scrollContent)
- createScrollmenu("storageItems",1,1,30,19,scrollContent,scrollmenuInfos["storageItems"]["position"])
- printScrollmenu("storageItems")
- end
- end
- end
- end
- function getItemNames()
- returnTable = {}
- for k,v in pairs(mainStorage["storageContent"]) do
- table.insert(returnTable,k)
- end
- return returnTable
- end
- function homeMenu()
- curEnv = homeMenu
- term.clear()
- scrollContent = getItemNames()
- table.sort(scrollContent)
- createScrollmenu("storageItems",1,1,30,19,scrollContent,0)
- printScrollmenu("storageItems")
- end
- function printScrollmenu(name)
- for i=1,math.min(scrollmenuLen[name],scrollmenuInfos[name]["y2"]-scrollmenuInfos[name]["y1"]+1) do
- if type(scrollmenuInfos[name]["previewText"][i+scrollmenuInfos[name]["position"]]) == "string" then
- if (i+scrollmenuInfos[name]["position"])%2==0 then
- term.setBackgroundColor(colors.gray)
- else
- term.setBackgroundColor(colors.black)
- end
- term.setCursorPos(scrollmenuInfos[name]["x1"],scrollmenuInfos[name]["y1"]+i-1)
- term.write(scrollmenuInfos[name]["previewText"][i+scrollmenuInfos[name]["position"]])
- for i2=1,scrollmenuInfos[name]["x2"]-scrollmenuInfos[name]["x1"]+1-string.len(scrollmenuInfos[name]["content"][i+scrollmenuInfos[name]["position"]]) do
- term.write(" ")
- end
- end
- end
- end
- function checkScrollmenu(name)
- if info1 == "mouse_click" and info3 > scrollmenuInfos[name]["x1"]-1 and info3 < scrollmenuInfos[name]["x2"]+1 and info4 > scrollmenuInfos[name]["y1"]-1 and info4 < scrollmenuInfos[name]["y2"]+1 then
- resetEvent()
- return info4-scrollmenuInfos[name]["y1"]+1+scrollmenuInfos[name]["position"]
- end
- if info1 == "mouse_scroll" and not (scrollmenuInfos[name]["position"]+info2 > scrollmenuLen[name]-(scrollmenuInfos[name]["y2"]-scrollmenuInfos[name]["y1"]+1)) and not (scrollmenuInfos[name]["position"]+info2 < 0) then
- scrollmenuInfos[name]["position"] = scrollmenuInfos[name]["position"] + info2
- printScrollmenu(name)
- end
- end
- function createScrollmenu(name,x1,y1,x2,y2,content,position)
- scrollmenuLen[name]=0
- scrollmenuInfos[name]={["x1"]=x1,["y1"]=y1,["x2"]=x2,["y2"]=y2,["content"]=content,["position"]=position,["previewText"]={}}
- for k,v in pairs(content) do
- scrollmenuLen[name]=scrollmenuLen[name]+1
- end
- for k,v in pairs(content) do
- if string.len(v) > x2 - x1 + 1 then
- scrollmenuInfos[name]["previewText"][k] = string.sub(v,1,x2-x1-2) .. "..."
- else
- scrollmenuInfos[name]["previewText"][k] = v
- end
- end
- end
- Storage = {
- new = function(self,storageNames,inputPeripheral,outputPeripheral)
- if type(storageNames) ~= "table" then
- error("bad argument #1 (storageNames is supposed to be a table)")
- elseif type(inputPeripheral) ~= "string" then
- error("bad argument #2 (inputPeripheral is supposed to be a string)")
- elseif type(outputPeripheral) ~= "string" then
- error("bad argument #3 (outputPeripheral is supposed to be a string)")
- end
- local returnTable = {["storageNames"]=storageNames,["inputPeripheralName"]=inputPeripheral,["inputPeripheral"]=peripheral.wrap(inputPeripheral),["outputPeripheralName"]=outputPeripheral,["outputPeripheral"]=peripheral.wrap(outputPeripheral),["storagePeripherals"] = {},["storageContent"]={},["storageInfo"]={}}
- for k,v in pairs(peripheral.getNames()) do
- for k2,v2 in pairs(storageNames) do
- if string.find(v,v2) then
- returnTable["storagePeripherals"][v] = peripheral.wrap(v)
- returnTable["storageInfo"][v] = {}
- end
- end
- end
- setmetatable(returnTable,self)
- self.__index = self
- return returnTable
- end
- ,
- output = function(self,itemName,amount)
- for k,v in pairs(self["storageContent"][itemName]) do
- for i = 1, self["outputPeripheral"].size() do
- if self["outputPeripheral"].getItemDetail(i) == nil then
- local outputtedAmount = v["storage"].pushItems(self["outputPeripheralName"],v["slot"],amount)
- if amount >= v["count"] then
- self["storageContent"][itemName][k] = nil
- end
- amount = amount - outputtedAmount
- end
- if amount < 1 or self["storageContent"][itemName][k] == nil then
- break
- end
- end
- if amount < 1 then
- break
- end
- end
- end
- ,
- input = function(self)
- for slot,item in pairs(self["inputPeripheral"].list()) do
- while item["count"] > 0 do
- if self["storageContent"][item["name"]] then
- for k,v in pairs(self["storageContent"][item["name"]]) do
- if v["storage"].getItemLimit(v["slot"])-v["count"] > 0 then
- local pushedItems = self["inputPeripheral"].pushItems(v["storageName"],slot,v["storage"].getItemLimit(v["slot"])-v["count"],v["slot"])
- item["count"] = item["count"] - pushedItems
- self["storageContent"][item["name"]][k]["count"] = self["storageContent"][item["name"]][k]["count"] + pushedItems
- if item["count"] < 1 then
- break
- end
- end
- end
- if item["count"] > 0 then
- local freeStorageName, freeSlot = self:getFreeSlot()
- local pushedItems = self["inputPeripheral"].pushItems(freeStorageName,slot,self["storagePeripherals"][freeStorageName].getItemLimit(freeSlot),freeSlot)
- table.insert(self["storageContent"][item["name"]],{["slot"]=freeSlot,["storageName"]=freeStorageName,["storage"]=self["storagePeripherals"][freeStorageName],["count"]=pushedItems})
- item["count"] = item["count"] - pushedItems
- table.remove(self["storageInfo"][freeStorageName]["freeSlots"],1)
- end
- else
- local freeStorageName, freeSlot = self:getFreeSlot()
- local pushedItems = self["inputPeripheral"].pushItems(freeStorageName,slot,self["storagePeripherals"][freeStorageName].getItemLimit(freeSlot),freeSlot)
- self["storageContent"][item["name"]] = {}
- table.insert(self["storageContent"][item["name"]],{["slot"]=freeSlot,["storageName"]=freeStorageName,["storage"]=self["storagePeripherals"][freeStorageName],["count"]=pushedItems})
- item["count"] = item["count"] - pushedItems
- table.remove(self["storageInfo"][freeStorageName]["freeSlots"],1)
- end
- end
- end
- end
- ,
- refreshStorageContent = function(self)
- self["storageContent"] = {}
- for storageName,storage in pairs(self["storagePeripherals"]) do
- for slot,item in pairs(storage.list()) do
- if item["count"] == 1 then
- if storage.getItemDetail(slot)["maxCount"] == 1 then
- stackable = false
- else
- stackable = true
- end
- else
- stackable = true
- end
- if type(self["storageContent"][item["name"]]) == "table" then
- table.insert(self["storageContent"][item["name"]],{["slot"]=slot,["storageName"]=storageName,["storage"]=storage,["count"]=item["count"],["stackable"]=stackable})
- else
- self["storageContent"][item["name"]] = {{["slot"]=slot,["storageName"]=storageName,["storage"]=storage,["count"]=item["count"],["stackable"]=stackable}}
- end
- end
- self["storageInfo"][storageName] = {["freeSlots"] = {}}
- local storageItems = storage.list()
- for i = 1, storage.size() do
- if not storageItems[i] then
- table.insert(self["storageInfo"][storageName]["freeSlots"],i)
- end
- end
- end
- end
- ,
- getFreeSlot = function(self)
- for storageName,storage in pairs(self["storagePeripherals"]) do
- for k2,slot in pairs(self["storageInfo"][storageName]["freeSlots"]) do
- return storageName, slot
- end
- end
- error("storage is full")
- end
- ,
- getStorageContent = function(self)
- return self["storageContent"]
- end
- }
- Player = {
- new = function(self,peripheralName)
- if type(peripheralName) ~= "string" then
- error("bad argument #1 (peripheralName is supposed to be a string)")
- end
- local returnTable = {["peripheralName"]=peripheralName,["peripheral"]=peripheral.wrap(peripheralName),["inventory"]={}}
- for k,v in pairs(returnTable["peripheral"].list()) do
- if k < 37 then
- returnTable["inventory"][k] = v
- end
- end
- setmetatable(returnTable,self)
- self.__index = self
- return returnTable
- end
- ,
- getInventory = function(self)
- self["inventory"] = {}
- for k,v in pairs(self["peripheral"].list()) do
- if k < 37 then
- self["inventory"][k] = v
- end
- end
- return self["inventory"]
- end
- ,
- inputFromStorage = function(self,storage,itemName,amount)
- if storage["storageContent"][itemName] then
- if table.maxn(storage["storageContent"][itemName]) ~= 0 then
- for k,item in pairs(storage["storageContent"][itemName]) do
- pulledAmount = self["peripheral"].pullItems(item["storageName"],item["slot"],amount)
- if pulledAmount == item["count"] then
- table.insert(mainStorage["storageInfo"][item["storageName"]]["freeSlots"],item["slot"])
- storage["storageContent"][itemName][k] = nil
- if table.maxn(storage["storageContent"][itemName]) == 0 then
- storage["storageContent"][itemName] = nil
- end
- end
- amount = amount - pulledAmount
- if amount < 1 then
- break
- end
- end
- else
- error("Item wasnt found in storage.")
- end
- else
- error("Item wasnt found in storage.")
- end
- end
- ,
- inputToStorage = function(self,storage,itemSlot,itemName,itemCount)
- if storage["storageContent"][itemName] then
- for k,v in pairs(storage["storageContent"][itemName]) do
- if v["storage"].getItemLimit(v["slot"])-v["count"] > 0 then
- local pushedItems = self["peripheral"].pushItems(v["storageName"],itemSlot,v["storage"].getItemLimit(v["slot"])-v["count"],v["slot"])
- if pushedItems then
- itemCount = itemCount - pushedItems
- storage["storageContent"][itemName][k]["count"] = storage["storageContent"][itemName][k]["count"] + pushedItems
- if itemCount < 1 then
- break
- end
- end
- end
- end
- if itemCount > 0 then
- local freeStorageName, freeSlot = storage:getFreeSlot()
- local pushedItems = self["peripheral"].pushItems(freeStorageName,itemSlot,storage["storagePeripherals"][freeStorageName].getItemLimit(freeSlot),freeSlot)
- table.insert(storage["storageContent"][itemName],{["slot"]=freeSlot,["storageName"]=freeStorageName,["storage"]=storage["storagePeripherals"][freeStorageName],["count"]=pushedItems})
- itemCount = itemCount - pushedItems
- table.remove(storage["storageInfo"][freeStorageName]["freeSlots"],1)
- end
- else
- local freeStorageName, freeSlot = storage:getFreeSlot()
- local pushedItems = self["peripheral"].pushItems(freeStorageName,itemSlot,storage["storagePeripherals"][freeStorageName].getItemLimit(freeSlot),freeSlot)
- storage["storageContent"][itemName] = {}
- table.insert(storage["storageContent"][itemName],{["slot"]=freeSlot,["storageName"]=freeStorageName,["storage"]=storage["storagePeripherals"][freeStorageName],["count"]=pushedItems})
- itemCount = itemCount - pushedItems
- table.remove(storage["storageInfo"][freeStorageName]["freeSlots"],1)
- end
- end
- }
- function resetEvent()
- info1 = nil
- end
- startup()
- homeMenu()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement