update_minecraft_bedrock.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #!/bin/bash
  2. # LinuxGSM update_minecraft_bedrock.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: http://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of Minecraft Bedrock servers.
  7. functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. #random number for userAgent
  9. randnum=$((1 + RANDOM % 5000))
  10. fn_update_minecraft_dl() {
  11. fn_fetch_file "https://minecraft.azureedge.net/bin-linux/bedrock-server-${remotebuild}.zip" "" "" "" "${tmpdir}" "bedrock_server.${remotebuild}.zip"
  12. echo -e "Extracting to ${serverfiles}...\c"
  13. if [ "${firstcommandname}" == "INSTALL" ]; then
  14. unzip -oq "${tmpdir}/bedrock_server.${remotebuild}.zip" -x "server.properties" -d "${serverfiles}"
  15. else
  16. unzip -oq "${tmpdir}/bedrock_server.${remotebuild}.zip" -x "permissions.json" "server.properties" "allowlist.json" -d "${serverfiles}"
  17. fi
  18. local exitcode=$?
  19. if [ "${exitcode}" == "0" ]; then
  20. fn_print_ok_eol_nl
  21. fn_script_log_pass "Extracting to ${serverfiles}"
  22. chmod u+x "${serverfiles}/bedrock_server"
  23. fn_clear_tmp
  24. else
  25. fn_print_fail_eol_nl
  26. fn_script_log_fatal "Extracting to ${serverfiles}"
  27. fn_clear_tmp
  28. core_exit.sh
  29. fi
  30. }
  31. fn_update_minecraft_localbuild() {
  32. # Gets local build info.
  33. fn_print_dots "Checking local build: ${remotelocation}"
  34. # Uses log file to gather info.
  35. # Log is generated and cleared on startup but filled on shutdown.
  36. requirerestart=1
  37. localbuild=$(grep Version "${consolelogdir}"/* 2> /dev/null | tail -1 | sed 's/.*Version //')
  38. if [ -z "${localbuild}" ]; then
  39. fn_print_error "Checking local build: ${remotelocation}"
  40. fn_print_error_nl "Checking local build: ${remotelocation}: no log files containing version info"
  41. fn_print_info_nl "Checking local build: ${remotelocation}: forcing server restart"
  42. fn_script_log_error "No log files containing version info"
  43. fn_script_log_info "Forcing server restart"
  44. check_status.sh
  45. # If server stopped.
  46. if [ "${status}" == "0" ]; then
  47. exitbypass=1
  48. command_start.sh
  49. fn_firstcommand_reset
  50. sleep 3
  51. exitbypass=1
  52. command_stop.sh
  53. fn_firstcommand_reset
  54. # If server started.
  55. else
  56. exitbypass=1
  57. command_stop.sh
  58. fn_firstcommand_reset
  59. fi
  60. fi
  61. if [ -z "${localbuild}" ]; then
  62. localbuild=$(grep Version "$(ls -tr "${consolelogdir}"/* 2> /dev/null)" | tail -1 | sed 's/.*Version //')
  63. fi
  64. if [ -z "${localbuild}" ]; then
  65. localbuild="0"
  66. fn_print_error "Checking local build: ${remotelocation}: waiting for local build: missing local build info"
  67. fn_script_log_error "Missing local build info"
  68. fn_script_log_error "Set localbuild to 0"
  69. else
  70. fn_print_ok "Checking local build: ${remotelocation}"
  71. fn_script_log_pass "Checking local build"
  72. fi
  73. }
  74. fn_update_minecraft_remotebuild() {
  75. # Gets remote build info.
  76. if [ "${mcversion}" == "latest" ]; then
  77. remotebuild=$(curl -H "Accept-Encoding: identity" -H "Accept-Language: en" -Ls -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.${randnum}.212 Safari/537.36" "https://www.minecraft.net/en-us/download/server/bedrock/" | grep -o 'https://minecraft.azureedge.net/bin-linux/[^"]*' | sed 's/.*\///' | grep -Eo "[.0-9]+[0-9]")
  78. else
  79. remotebuild="${mcversion}"
  80. fi
  81. if [ "${firstcommandname}" != "INSTALL" ]; then
  82. fn_print_dots "Checking remote build: ${remotelocation}"
  83. # Checks if remotebuild variable has been set.
  84. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ]; then
  85. fn_print_fail "Checking remote build: ${remotelocation}"
  86. fn_script_log_fatal "Checking remote build"
  87. core_exit.sh
  88. else
  89. fn_print_ok "Checking remote build: ${remotelocation}"
  90. fn_script_log_pass "Checking remote build"
  91. fi
  92. else
  93. # Checks if remotebuild variable has been set.
  94. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ]; then
  95. fn_print_failure "Unable to get remote build"
  96. fn_script_log_fatal "Unable to get remote build"
  97. core_exit.sh
  98. fi
  99. fi
  100. }
  101. fn_update_minecraft_compare() {
  102. # Removes dots so if statement can compare version numbers.
  103. fn_print_dots "Checking for update: ${remotelocation}"
  104. localbuilddigit=$(echo -e "${localbuild}" | tr -cd '[:digit:]')
  105. remotebuilddigit=$(echo -e "${remotebuild}" | tr -cd '[:digit:]')
  106. if [ "${localbuilddigit}" -ne "${remotebuilddigit}" ] || [ "${forceupdate}" == "1" ]; then
  107. fn_print_ok_nl "Checking for update: ${remotelocation}"
  108. echo -en "\n"
  109. echo -e "Update available"
  110. echo -e "* Local build: ${red}${localbuild}${default}"
  111. echo -e "* Remote build: ${green}${remotebuild}${default}"
  112. echo -en "\n"
  113. fn_script_log_info "Update available"
  114. fn_script_log_info "Local build: ${localbuild}"
  115. fn_script_log_info "Remote build: ${remotebuild}"
  116. fn_script_log_info "${localbuild} > ${remotebuild}"
  117. unset updateonstart
  118. check_status.sh
  119. # If server stopped.
  120. if [ "${status}" == "0" ]; then
  121. exitbypass=1
  122. fn_update_minecraft_dl
  123. if [ "${requirerestart}" == "1" ]; then
  124. exitbypass=1
  125. command_start.sh
  126. fn_firstcommand_reset
  127. exitbypass=1
  128. command_stop.sh
  129. fn_firstcommand_reset
  130. fi
  131. # If server started.
  132. else
  133. fn_print_restart_warning
  134. exitbypass=1
  135. command_stop.sh
  136. fn_firstcommand_reset
  137. exitbypass=1
  138. fn_update_minecraft_dl
  139. exitbypass=1
  140. command_start.sh
  141. fn_firstcommand_reset
  142. fi
  143. unset exitbypass
  144. date +%s > "${lockdir}/lastupdate.lock"
  145. alert="update"
  146. alert.sh
  147. else
  148. fn_print_ok_nl "Checking for update: ${remotelocation}"
  149. echo -en "\n"
  150. echo -e "No update available"
  151. echo -e "* Local build: ${green}${localbuild}${default}"
  152. echo -e "* Remote build: ${green}${remotebuild}${default}"
  153. echo -en "\n"
  154. fn_script_log_info "No update available"
  155. fn_script_log_info "Local build: ${localbuild}"
  156. fn_script_log_info "Remote build: ${remotebuild}"
  157. fi
  158. }
  159. # The location where the builds are checked and downloaded.
  160. remotelocation="minecraft.net"
  161. if [ "${firstcommandname}" == "INSTALL" ]; then
  162. fn_update_minecraft_remotebuild
  163. fn_update_minecraft_dl
  164. else
  165. fn_print_dots "Checking for update"
  166. fn_print_dots "Checking for update: ${remotelocation}"
  167. fn_script_log_info "Checking for update: ${remotelocation}"
  168. fn_update_minecraft_localbuild
  169. fn_update_minecraft_remotebuild
  170. fn_update_minecraft_compare
  171. fi