mc-server-query.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python3
  2. # WARNING: Only on GNU/Linux you see the colors of this script,
  3. # because it is using the standard bash shell color formatting
  4. # import modules
  5. from mcipc.query import Client
  6. from os import name as os_name, system
  7. from time import sleep
  8. from sys import argv
  9. # define "replace_all" function
  10. def replace_all(text, dic):
  11. for i, j in dic.items():
  12. text = text.replace(i, j)
  13. return text
  14. # define sleep time
  15. if len(argv)<2:
  16. seconds, stop_cmd = 1, False
  17. elif argv[1]=="0":
  18. stop_cmd = True
  19. else:
  20. seconds, stop_cmd = int(argv[1]), False
  21. # Define colors for user OS
  22. if os_name=="nt":
  23. minecraft_to_terminal_colors = {}
  24. else:
  25. minecraft_to_terminal_colors = { "§0": "\\e[30m", "§1": "\\e[34m", "§2":"\\e[32m", "§2":"\\e[36m", "§4":"\\e[31m", "§5":"\\e[35m", "§6":"\\e[33m",
  26. "§7":"\\e[37m", "§8":"\\e[90m","§9":"\\e[94m", "§a":"\\e[92m", "§b":"\\e[96m", "§c":"\\e[91m", "§d":"\\e[95m", "§e":"\\e[93m", "§f":"\\e[97m",
  27. "§r":""} # WARNING: "§r" gets removed
  28. # first cleanup if sleep timer given
  29. if not(stop_cmd):
  30. system("clear")
  31. while True:
  32. # get query
  33. with Client('127.0.0.1', 25565) as client:
  34. stats = client.full_stats
  35. # print motd
  36. motd="echo -e \""+replace_all(stats[2], minecraft_to_terminal_colors)+"\\e[39m\""
  37. system(motd)
  38. # print server type
  39. cache_text = str(stats[6])[str(stats[6]).find('\'')+1:]
  40. system("echo -e \"\n\\e[97mServer: \\e[39m"+stats[4]+" "+cache_text[:cache_text.find('\'')]+"\"")
  41. # list all players
  42. player_list = ""
  43. for i in stats[12]:
  44. player_list += i+", "
  45. player_list = player_list[:-2]
  46. system("echo -e \"\\e[97mPlayers ["+str(stats[8])+"/"+str(stats[9])+"]: \\e[39m"+player_list+"\"")
  47. # reload after timer or stop completely
  48. if stop_cmd:
  49. break
  50. else:
  51. sleep(seconds)
  52. system("clear")