Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if os.pullEvent == nil then os.pullEvent = nil end
- table.contains = function(self,value)
- for _, v in ipairs(self) do
- if v == value then
- return true
- end
- end
- return false
- end
- List = {
- new=function(self,t)
- local list = t or {}
- setmetatable(list,self)
- self.__index = self
- return list
- end,
- insert=table.insert,
- remove=table.remove,
- contains=table.contains
- }
- ItemList={
- new=function(self,data)
- local ob = {
- names={},
- tags={}
- }
- setmetatable(ob,self)
- self.__index = self
- if data ~= nil then
- if data.names ~= nil then
- for _,name in ipairs(data.names) do
- ob.names[name] = true
- end
- end
- if data.tags ~= nil then
- for _,tag in ipairs(data.tags) do
- ob.tags[tag] = true
- end
- end
- end
- return ob
- end,
- addName=function(self,name)
- if not self:hasName(name) then self.names[name] = true return true end
- return false
- end,
- addTag=function(self,tag)
- if not self:hasTag(tag) then self.tags[tag] = true return true end
- return false
- end,
- removeName=function(self,name)
- if self:hasName(name) then self.names[name] = nil return true end
- return false
- end,
- removeTag=function(self,tag)
- if self:hasTag(tag) then self.tags[tag] = nil return true end
- return false
- end,
- hasName=function(self,name)
- return self.names[name] ~= nil
- end,
- hasTag=function(self,tag)
- return self.tags[tag] ~= nil
- end,
- getSaveData=function(self)
- local ob = {
- names={},
- tags={}
- }
- for name,_ in pairs(self.names) do table.insert(ob.names,name) end
- for tag,_ in pairs(self.tags) do table.insert(ob.tags,tag) end
- return ob
- end
- }
- PlayerList={
- list={},
- addPlayer=function(name)
- if PlayerList.list[name] == nil then
- local p = Player:new(name)
- PlayerList.list[name] = p
- if p.stored == nil then
- print("stored is nil")
- os.pullEvent("key")
- end
- p:loadSaveData()
- end
- end,
- getNames=function()
- local names = {}
- for name,_ in pairs(PlayerList.list) do
- table.insert(names,name)
- end
- return names
- end,
- hasPlayer=function(name)
- return PlayerList.list[name] ~= nil
- end,
- getPlayer=function(name)
- return PlayerList.list[name]
- end
- }
- Player={
- new=function(self,name)
- local ob = {
- name=name,
- managingEnabled=true,
- enablingItem=nil,
- stored=ItemList:new(),
- trashed=ItemList:new()
- }
- setmetatable(ob,self)
- self.__index = self
- return ob
- end,
- getSaveFilename=function(self)
- return self.name..".config"
- end,
- writeSaveData=function(self)
- local data = textutils.serialize({
- enablingItem=self.enablingItem,
- stored=self.stored:getSaveData(),
- trashed=self.trashed:getSaveData()
- })
- local h = fs.open(self:getSaveFilename(),"w")
- h.write(data)
- h.close()
- end,
- loadSaveData=function(self)
- local fname = self:getSaveFilename()
- if fs.exists(fname) then
- local h = fs.open(fname,"r")
- local text = h.readAll()
- h.close()
- local data = textutils.unserialize(text)
- if data.enablingItem ~= nil then
- self.enablingItem = data.enablingItem
- else
- self.enablingItem = nil
- end
- if data.stored ~= nil then
- self.stored=ItemList:new(data.stored)
- else
- self.stored=ItemList:new()
- end
- if data.trashed ~= nil then
- self.trashed=ItemList:new(data.trashed)
- else
- self.trashed=ItemList:new()
- end
- else
- self.enablingItem = nil
- self.stored = ItemList:new()
- self.trashed = ItemList:new()
- end
- end,
- setEnablingItem=function(self,name)
- if self.enablingItem == nil or self.enablingItem ~= name then
- self.enablingItem = name
- self:writeSaveData()
- end
- end,
- addStoredName=function(self,name)
- if self.stored:addName(name) then self:writeSaveData() end
- end,
- addStoredTag=function(self,tag)
- if self.stored:addTag(tag) then self:writeSaveData() end
- end,
- addTrashedName=function(self,name)
- if self.trashed:addName(name) then self:writeSaveData() end
- end,
- addTrashedTag=function(self,tag)
- if self.trashed:addTag(tag) then self:writeSaveData() end
- end,
- removeStoredName=function(self,name)
- if self.stored:removeName(name) then self:writeSaveData() end
- end,
- removeStoredTag=function(self,tag)
- if self.stored:removeTag(tag) then self:writeSaveData() end
- end,
- removeTrashedName=function(self,name)
- if self.trashed:removeName(name) then self:writeSaveData() end
- end,
- removeTrashedTag=function(self,tag)
- if self.trashed:removeTag(tag) then self:writeSaveData() end
- end,
- removeStoredItem=function(self,item)
- local needsSave = self.stored:removeName(item.name)
- if item.tags ~= nil then
- for _,tag in ipairs(item.tags) do
- if self.stored:removeTag(tag) then needsSave = true end
- end
- end
- if needsSave then self:writeSaveData() end
- end,
- removeTrashedItem=function(self,item)
- local needsSave = self.trashed:removeName(item.name)
- if item.tags ~= nil then
- for _,tag in ipairs(item.tags) do
- if self.trashed:removeTag(tag) then needsSave = true end
- end
- end
- if needsSave then self:writeSaveData() end
- end,
- shouldStore=function(self,item)
- if self.stored:hasName(item.name) then return true end
- if item.tags ~= nil then
- for _,tag in ipairs(item.tags) do
- if self.stored:hasTag(tag) then return true end
- end
- end
- return false
- end,
- shouldTrash=function(self,item)
- if self.trashed:hasName(item.name) then return true end
- if item.tags ~= nil then
- for _,tag in ipairs(item.tags) do
- if self.trashed:hasTag(tag) then return true end
- end
- end
- return false
- end
- }
- InventoryList={
- list={},
- hasInventory=function(name)
- return InventoryList.list[name] ~= nil
- end,
- addInventory=function(name)
- if not InventoryList.hasInventory(name) then
- InventoryList.list[name] = Inventory:new(name)
- end
- end,
- getInventoryForPlayer=function(playerName)
- for _,inv in pairs(InventoryList.list) do
- if inv:safeGetOwner() == playerName then return inv end
- end
- return nil
- end
- }
- Inventory={
- new=function(self,name)
- local inv = peripheral.wrap(name)
- inv.name = name
- setmetatable(inv,self)
- self.__index = self
- return inv
- end,
- scan=function(self)
- local playerName = self:safeGetOwner()
- if playerName == nil then return end
- if not PlayerList.hasPlayer(playerName) then
- PlayerList.addPlayer(playerName)
- end
- local player = PlayerList.getPlayer(playerName)
- --prevent scanning if inventory's player does not have an enabling item set
- if player.enablingItem == nil then return end
- --prevent modifying inventory if player is currently being configured
- --allow scan to continue to look for whether or not the player is holding their enabling item
- local readonly = CommandLine.currentPlayer ~= nil and CommandLine.currentPlayer.name == player.name
- local items = nil
- if not pcall(function() items = self.getItems() end) then return end
- local foundEnabler = false
- for slot,item in pairs(items) do
- if item.name == player.enablingItem then
- player.managingEnabled = true
- foundEnabler = true
- end
- if player.managingEnabled and not readonly then
- if player:shouldStore(item) then
- self.removeItemFromPlayer("UP",item.count,slot,item.name)
- elseif player:shouldTrash(item) then
- self.removeItemFromPlayer("DOWN",item.count,slot,item.name)
- end
- end
- end
- if not foundEnabler then
- player.managingEnabled = false
- end
- end,
- safeGetOwner=function(self)
- local owner = nil
- pcall(function() owner = self.getOwner() end)
- return owner
- end
- }
- Peripherals={
- sideNames = {front=true,back=true,left=true,right=true,top=true,bottom=true},
- findInventories=function()
- for _,name in ipairs(peripheral.getNames()) do
- if Peripherals.sideNames[name] == nil then
- local peripType = peripheral.getType(name)
- if peripType == "inventoryManager" then
- if not InventoryList.hasInventory(name) then
- InventoryList.addInventory(name)
- end
- end
- end
- end
- end,
- findModem=function()
- for _,name in ipairs(peripheral.getNames()) do
- if Peripherals.sideNames[name] == nil then
- local peripType = peripheral.getType(name)
- if peripType == "modem" then
- local p = peripheral.wrap(name)
- if p.isWireless() then
- rednet.open(name)
- return
- end
- end
- end
- end
- end
- }
- CommandLine={
- instructionColor=colors.lightBlue,
- errorColor=colors.red,
- valueColor=colors.yellow,
- currentPlayer=nil,
- currentOperation=nil,
- setEnablingItemText = "Set My Enabler Item",
- addStoredItemText = "Add Stored Item",
- addTrashedItemText = "Add Trashed Item",
- removeStoredItemText = "Remove Stored Item",
- removeTrashedItemText = "Remove Trashed Item",
- switchPlayerText = "Log Out",
- clear=function(self)
- term.clear()
- term.setCursorPos(1,1)
- if self.currentPlayer ~= nil then
- term.write("Current User: ")
- term.setTextColor(colors.yellow)
- self:printValue(self.currentPlayer.name)
- term.setTextColor(colors.white)
- if self.currentOperation ~= nil then
- term.write("Current Operation: ")
- term.setTextColor(colors.yellow)
- self:printValue(self.currentOperation)
- term.setTextColor(colors.white)
- end
- print()
- end
- end,
- print=function(self,text)
- if type(text) == "table" then
- if text.color ~= nil then
- local originalColor = term.getTextColor()
- if text.color ~= nil then
- term.setTextColor(text.color)
- end
- for _,line in ipairs(text) do
- self:print(line)
- end
- term.setTextColor(originalColor)
- else
- for _,line in ipairs(text) do
- self:print(line)
- end
- end
- else
- print(text)
- end
- end,
- printValue=function(self,text)
- term.setTextColor(colors.yellow)
- self:print(text)
- term.setTextColor(colors.white)
- end,
- instruction=function(self,text)
- term.setTextColor(colors.lightBlue)
- self:print(text)
- term.setTextColor(colors.white)
- end,
- error=function(self,text)
- term.setTextColor(colors.red)
- self:print(text)
- term.setTextColor(colors.white)
- end,
- promptOperation=function(self)
- local actions = {
- self.setEnablingItemText,
- self.addStoredItemText,
- self.removeStoredItemText,
- self.addTrashedItemText,
- self.removeTrashedItemText,
- self.switchPlayerText
- }
- while true do
- self.currentOperation = nil
- if self.currentPlayer == nil then
- self:setPlayer()
- else
- local cancelled, selection = self:promptSelection("What would you like to do?",actions,false)
- local action = actions[selection]
- if action == self.switchPlayerText then
- self.currentPlayer = nil
- else
- self.currentOperation = action
- if action == self.setEnablingItemText then
- self:setEnablingItem()
- elseif action == self.addStoredItemText then
- self:addStoredItem()
- elseif action == self.removeStoredItemText then
- self:removeStoredItem()
- elseif action == self.addTrashedItemText then
- self:addTrashedItem()
- elseif action == self.removeTrashedItemText then
- self:removeTrashedItem()
- end
- end
- end
- end
- end,
- promptSelection=function(self,instructionText,options,canCancel)
- if canCancel == nil then canCancel = true end
- while true do
- self:clear()
- self:instruction(instructionText)
- for i,option in ipairs(options) do
- print(i..") "..option)
- end
- if canCancel then
- self:error((#options+1)..") Cancel")
- end
- print()
- local input = tonumber(read())
- if input ~= nil then
- input = math.floor(input)
- if canCancel and input == #options + 1 then
- return true, nil
- end
- if input > 0 and input <= #options then
- return false, input
- end
- end
- end
- end,
- promptYesNo=function(self,instructionText)
- while true do
- self:clear()
- self:instruction(instructionText)
- term.write("1) ")
- term.setTextColor(colors.lime)
- print("Yes")
- term.setTextColor(colors.white)
- term.write("2) ")
- term.setTextColor(colors.red)
- print("No")
- term.setTextColor(colors.white)
- print()
- local input = tonumber(read())
- if input ~= nil then
- if input == 1 then return true end
- if input == 2 then return false end
- end
- end
- end,
- promptCancelOrRetry=function(self,errorText)
- self:clear()
- self:error(errorText)
- print()
- self:instruction({"Press X to cancel","Press any other key to try again"})
- local _,key = os.pullEvent("key")
- if key == keys.x then return false end
- return true
- end,
- promptNameOrTags=function(self,heldItem,instruction)
- local options = {
- "Name \""..heldItem.name.."\""
- }
- for _,tag in ipairs(heldItem.tags) do
- table.insert(options,"Tag \""..tag.."\"")
- end
- local cancelled, selection = self:promptSelection(instruction,options)
- if cancelled then return nil, nil end
- if selection == 1 then return heldItem.name, nil end
- return nil, heldItem.tags[selection-1]
- end,
- confirmHeldItem=function(self,heldItem,question)
- return self:promptYesNo({
- {color=colors.white,"The current user is holding this item:"},
- {color=self.valueColor,self.getItemDisplayName(heldItem)},
- "",
- question
- })
- end,
- getHeldItem=function(self)
- if self.currentPlayer == nil then return nil end
- while true do
- local inv = InventoryList.getInventoryForPlayer(self.currentPlayer.name)
- if inv ~= nil then
- local heldItem = nil
- if not pcall(function() heldItem = inv.getItemInHand() end) then
- if not self:promptCancelOrRetry({"The inventory for the current user is not","available at this time"}) then
- return nil
- end
- elseif heldItem == nil or heldItem.name == nil or heldItem.count == 0 then
- if not self:promptCancelOrRetry({"You are not currently holding any items"}) then
- return nil
- end
- else
- return heldItem
- end
- else
- if not self:promptCancelOrRetry({"An inventory with a memory card for the current","user could not be found"}) then
- return nil
- end
- end
- end
- end,
- getItemDisplayName=function(item)
- return string.sub(item.displayName,2,string.len(item.displayName)-1)
- end,
- setPlayer=function(self)
- self.currentPlayer = nil
- local names = PlayerList.getNames()
- if #names == 0 then
- self:error("No player memory cards have been found")
- sleep(10)
- return
- end
- table.insert(names,"Refresh List")
- local cancelled, selection = self:promptSelection({
- "Please select a player to modify",
- {color=self.valueColor,
- "Note: Inventory management will be disabled for",
- " the selected user until they are logged out"
- }
- },names,false)
- if selection == #names then return end
- self.currentPlayer = PlayerList.getPlayer(names[selection])
- end,
- setEnablingItem=function(self)
- local heldItem = self:getHeldItem()
- if heldItem == nil then return end
- if self:confirmHeldItem(heldItem,"Do you want to set that item as the enabling item?") then
- self.currentPlayer:setEnablingItem(heldItem.name)
- end
- end,
- addStoredItem=function(self)
- local heldItem = self:getHeldItem()
- if heldItem == nil then return end
- if heldItem.tags == nil then
- if self:confirmHeldItem(heldItem,"Do you want to automatically store that item?") then
- self.currentPlayer:addStoredName(heldItem.name)
- end
- else
- local name, tag = self:promptNameOrTags(heldItem,"Store items with...")
- if name ~= nil then
- self.currentPlayer:addStoredName(name)
- elseif tag ~= nil then
- self.currentPlayer:addStoredTag(tag)
- end
- end
- end,
- addTrashedItem=function(self)
- local heldItem = self:getHeldItem()
- if heldItem == nil then return end
- if heldItem.tags == nil then
- if self:confirmHeldItem(heldItem,"Do you want to automatically trash that item?") then
- self.currentPlayer:addTrashedName(heldItem.name)
- end
- else
- local name, tag = self:promptNameOrTags(heldItem,"Trash items with...")
- if name ~= nil then
- self.currentPlayer:addTrashedName(name)
- elseif tag ~= nil then
- self.currentPlayer:addTrashedTag(tag)
- end
- end
- end,
- removeStoredItem=function(self)
- local heldItem = self:getHeldItem()
- if heldItem == nil then return end
- if self:confirmHeldItem(heldItem,"Do you want to stop storing that item?") then
- self.currentPlayer:removeStoredItem(heldItem)
- end
- end,
- removeTrashedItem=function(self)
- local heldItem = self:getHeldItem()
- if heldItem == nil then return end
- if self:confirmHeldItem(heldItem,"Do you want to stop storing that item?") then
- self.currentPlayer:removeTrashedItem(heldItem)
- end
- end
- }
- function run()
- parallel.waitForAny(function()
- --rednet
- Peripherals.findModem()
- while true do
- local senderId, message, protocol = rednet.receive()
- end
- end,function()
- --inventory managing function
- while true do
- Peripherals.findInventories()
- for _,inv in pairs(InventoryList.list) do
- inv:scan()
- end
- sleep(5)
- end
- end,function()
- --command line function
- CommandLine:promptOperation()
- end)
- end
- run()
Add Comment
Please, Sign In to add comment