DaikiKaminari

player_detector_daiki

Aug 16th, 2021 (edited)
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. --- GLOBAL VARIABLES ---
  2. local json -- json API
  3. local config -- variable where the config will be loaded
  4. local defaultConfig = { -- default config, feel free to change it
  5. ["version"] = 1.1,
  6. ["serverIP"] = "infinity.mineaurion.com",
  7. ["sides"] = {"back", "front", "left", "right", "bottom", "top"}
  8. }
  9.  
  10. --- INIT ---
  11. local function init()
  12. -- load json API
  13. if not fs.exists("json") or fs.exists("json.lua") or fs.exists("rom/modules/main/json.lua") or fs.exists("rom/modules/main/json.lua") then
  14. local f = fs.open("json.lua", "w")
  15. local str = http.get("https://raw.githubusercontent.com/DaikiKaminari/CC-Libs/master/ObjectJSON/json.lua").readAll()
  16. f.write(str)
  17. f.close()
  18. end
  19. json = require("json")
  20. -- Check if config file exists, otherwise create it
  21. if not fs.exists("config.json") then
  22. config = defaultConfig
  23. -- Reset display
  24. term.clear()
  25. term.setCursorPos(1, 1)
  26. -- Input
  27. config["name"] = {}
  28. print("Entrez les pseudos a detecter (appuyer sur entrer pour l'ajout) :")
  29. local input
  30. repeat
  31. input = io.read()
  32. if input ~= "" then
  33. table.insert(config["name"], input)
  34. end
  35. until input==""
  36. -- Write
  37. local str = json.encodePretty(config)
  38. assert(str and str ~= "", "Encoding of the config went wrong.")
  39. local file = fs.open("/config.json", "w")
  40. file.write(str)
  41. file.close()
  42. end
  43. -- Read config file and check version
  44. config = json.decodeFromFile("/config.json")
  45. if config["version"] < defaultConfig["version"] then
  46. shell.run("rm config.json")
  47. os.reboot()
  48. end
  49. print("\nDetecte les joueurs : " .. textutils.serialise(config["name"]))
  50. print("Sur le serveur : " .. config["serverIP"])
  51. print("\nEmet de la redstone quand le joueur est connecte, sinon non.")
  52. print("Pour couper un spawner il faut inverser le signal.")
  53. end
  54.  
  55.  
  56. --- UTILS ---
  57. local function has_value(tab, val)
  58. for index,value in pairs(tab) do
  59. if value == val then
  60. return true
  61. end
  62. end
  63. return false
  64. end
  65.  
  66.  
  67. --- FUNCTIONS ---
  68. -- Returns true if the player is connected, false otherwise
  69. local function arePlayersConnected(players, serverID)
  70. local str = http.get("http://api.mineaurion.com/v1/serveurs/" .. config["serverIP"]).readAll()
  71. local obj = json.decode(str)
  72. local joueurs = obj["joueurs"]
  73. if not joueurs or not next(joueurs) then
  74. return false
  75. end
  76. for _,player in pairs(players) do
  77. if has_value(joueurs, player) then
  78. return true
  79. end
  80. end
  81. return false
  82. end
  83.  
  84. -- Send or cut the redstone signal on all defined sides
  85. local function actualizeRedstone(boolean_signal)
  86. if boolean_signal then
  87. for _,side in pairs(config["sides"]) do
  88. rs.setOutput(side, true)
  89. end
  90. else
  91. for _,side in pairs(config["sides"]) do
  92. rs.setOutput(side, false)
  93. end
  94. end
  95. end
  96.  
  97.  
  98. --- MAIN ---
  99. local function main()
  100. init()
  101. while true do
  102. actualizeRedstone(arePlayersConnected(config["name"], config["serverIP"]))
  103. sleep(30)
  104. end
  105. end
  106.  
  107. main()
Add Comment
Please, Sign In to add comment