Advertisement
Miki_Tellurium

StripMiningExceptions

Apr 20th, 2024
645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.92 KB | Gaming | 0 0
  1. local stringhelperLib = require("stringhelper")
  2. local exceptionsFile = "stripminingexceptions.txt"
  3. local exceptions = {}
  4. local args = { ... }
  5.  
  6. --Write the exception file
  7. local function serialize()
  8.     local string = textutils.serialiseJSON(exceptions)
  9.     local file = fs.open(exceptionsFile, "w")
  10.     file.write(string)
  11.     file.close()
  12. end
  13. --Read the exceptions file and load
  14. local function unserialize()
  15.     local file = fs.open(exceptionsFile, "r")
  16.     local json = file.readAll()
  17.     exceptions = textutils.unserialiseJSON(json)
  18.     file.close()
  19. end
  20. --Append a new string to the exception table
  21. local function appendToTable(table, string)
  22.     table[#table + 1] = string
  23. end
  24. --Write user help
  25. local function writeHelp()
  26.     term.setTextColor(colors.white)
  27.     print("To modify the list of exceptions use:")
  28.     term.write("StripMiningExceptions ")
  29.     term.setTextColor(colors.orange)
  30.     term.write("add ")
  31.     term.setTextColor(colors.lightBlue)
  32.     print("block_id1 block_id2 ...")
  33.     term.setTextColor(colors.white)
  34.     term.write("StripMiningExceptions ")
  35.     term.setTextColor(colors.orange)
  36.     term.write("remove ")
  37.     term.setTextColor(colors.lightBlue)
  38.     print("block_id1 block_id2 ...")
  39. end
  40.  
  41. local file = fs.open(exceptionsFile, "r")
  42. if not file or stringhelper.isBlank(file.readAll()) then --Created the file if it doesn't exists or its empty
  43.     local file = fs.open(exceptionsFile, "w")
  44.     file.write("{}")
  45.     file.close()
  46. end
  47. unserialize()
  48.  
  49. local command = args[1]
  50. if command == "add" then
  51.     for i = 2, #args do
  52.         local string = args[i]
  53.         if not stringhelper.isInTable(string, exceptions) then
  54.             appendToTable(exceptions, string)
  55.             term.setTextColor(colors.lightBlue)
  56.             textutils.pagedPrint("Added " .. string)
  57.         else
  58.             term.setTextColor(colors.orange)
  59.             textutils.pagedPrint(string .. " already present")
  60.         end
  61.     end
  62.     serialize()
  63. elseif command == "remove" then
  64.     local removedStrings = {} --Hold string to be removed
  65.     for i = 2, #args do --Get the string to remove
  66.         local string = args[i]
  67.         if stringhelper.isInTable(string, exceptions) then
  68.             appendToTable(removedStrings, string)
  69.             term.setTextColor(colors.lightBlue)
  70.             textutils.pagedPrint("Removed " .. string)
  71.         else
  72.             term.setTextColor(colors.orange)
  73.             textutils.pagedPrint(string .. " is not present")
  74.         end
  75.     end
  76.  
  77.     local newExceptions = {}
  78.     for _, string in ipairs(exceptions) do --Make a new table without removed strings
  79.         if not stringhelper.isInTable(string, removedStrings) then
  80.             appendToTable(newExceptions, string)
  81.         end
  82.     end
  83.     exceptions = newExceptions
  84.     serialize()
  85. elseif command == "help" then
  86.     writeHelp()
  87. else
  88.     term.setTextColor(colors.red)
  89.     print("Invalid command.")
  90.     writeHelp()
  91. end
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement