Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Display Help Function
- local function displayHelp()
- if term.isColor() then term.setTextColor(colors.lightBlue) end
- print(" -- Help/Usage -- ")
- print("Counter v1.0 by 1lann")
- print(" ")
- print("help: Displays help")
- print("?: Displays help")
- print("exit: Exits the program")
- print("<object>: Lists the number of the object")
- print("<num> <object>: Adds a number of object to")
- print(" the database. Ex: 24 copper")
- print("purge <object>: Removes the object completely")
- print(" Out of the database")
- print("list: Lists the number of objects you have")
- term.setTextColor(colors.white)
- end
- local function calcNumber(str)
- counterCaptureVariableThing = nil
- loadstring("counterCaptureVariableThing = " .. tostring(str))()
- return tonumber(counterCaptureVariableThing)
- end
- local tableData = {}
- local function isObject(data)
- for k,v in pairs(tableData) do
- if k == data then
- return v
- end
- end
- return false
- end
- -- Load Data
- if fs.exists("/oreData") then
- local f = io.open("/oreData", "r")
- tableData = textutils.unserialize(f:read("*l"))
- if not tableData then
- tableData = {}
- end
- end
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.black)
- term.setTextColour(colors.white)
- term.clear()
- -- Main Input Loop
- while true do
- if term.isColor() then
- term.setTextColor(colors.yellow)
- end
- write("Input> ")
- term.setTextColor(colors.white)
- local objects = read():lower()
- objects = objects:gsub("^%s*(.-)%s*$", "%1") -- Removes trailing whitespace
- if objects == "help" or objects == "?" then
- displayHelp()
- elseif objects == "exit" then
- term.clear()
- term.setCursorPos(1,1)
- break
- elseif objects == "list" then
- if term.isColor() then term.setTextColor(colors.lime) end
- print("There are:")
- local _, y = term.getSize()
- local loopNum = 0
- for k,v in pairs(tableData) do
- print(tostring(v) .. " " .. k)
- loopNum = loopNum+1
- if loopNum >= y-1 then
- local _, pY = term.getCursorPos()
- write("Press any key to continue...")
- os.pullEvent("key")
- term.clearLine()
- term.setCursorPos(1, pY)
- end
- end
- elseif isObject(objects) then
- if term.isColor() then term.setTextColor(colors.lime) end
- print("There are: " .. tableData[objects] .. " " .. objects)
- elseif #{objects:find(" ")} == 2 then
- local firstArg = objects:sub(1, objects:find(" ")-1)
- local secondArg = objects:sub(objects:find(" ")+1, -1)
- if firstArg == "purge" then
- if tableData[secondArg] then
- if term.isColor() then term.setTextColor(colors.lime) end
- print("Deleted Entry: " .. secondArg)
- print("Which had: " .. tostring(tableData[secondArg]) .. " items")
- tableData[secondArg] = nil
- local f = io.open("/oreData", "w")
- f:write(textutils.serialize(tableData))
- f:close()
- else
- if term.isColor() then term.setTextColor(colors.red) end
- print("Invalid Use!")
- displayHelp()
- end
- else
- num = calcNumber(firstArg)
- if num then
- if term.isColor() then term.setTextColor(colors.lime) end
- if tableData[secondArg] then
- print("Before there were " .. tableData[secondArg] .. " " .. secondArg)
- if math.floor(num) ~= num then
- print("Number is not an integer, rounding to " .. math.ceil(num-0.5))
- num = math.ceil(num-0.5)
- end
- if tostring(num):sub(1,1) == "-" then
- print("Subtracting " .. tostring(num*-1))
- else
- print("Adding " .. tostring(num))
- end
- if tableData[secondArg] then
- tableData[secondArg] = tableData[secondArg] + num
- end
- else
- tableData[secondArg] = num
- end
- print("Now there are " .. tableData[secondArg] .. " " .. secondArg)
- local f = io.open("/oreData", "w")
- f:write(textutils.serialize(tableData))
- f:close()
- else
- if term.isColor() then term.setTextColor(colors.red) end
- print("Invalid Use!")
- displayHelp()
- end
- end
- else
- if term.isColor() then term.setTextColor(colors.red) end
- print("Unknown Command")
- displayHelp()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement