start-minecraft.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/bin/bash
  2. cd /home/minecraft
  3. # https://github.com/itzg/dockerfiles/blob/master/minecraft-server/start-minecraft.sh
  4. if [ ! -e eula.txt ]; then
  5. if [ "$EULA" != "" ]; then
  6. echo "# Generated via Docker at $(date)" > eula.txt
  7. echo "eula=TRUE" >> eula.txt
  8. else
  9. echo "Accept the Minecraft EULA at"
  10. echo " https://account.mojang.com/documents/minecraft_eula"
  11. echo "by adding -e EULA=TRUE"
  12. exit 1
  13. fi
  14. fi
  15. SERVER_PROPERTIES=server.properties
  16. function setProp {
  17. local prop=$1
  18. local var=$2
  19. if [ -n "$var" ]; then
  20. echo "Setting $prop to $var"
  21. sed -i "/$prop\s*=/ c $prop=$var" server.properties
  22. fi
  23. }
  24. if [ ! -e server.properties ]; then
  25. echo "Creating server.properties"
  26. cp base.properties server.properties
  27. if [ -n "$WHITELIST" ]; then
  28. echo "Whitelist enable. You need to create the whitelist.json maunally, or disable and add users with /whitelist nick"
  29. fi
  30. setProp "motd" "$MOTD"
  31. setProp "allow-nether" "$ALLOW_NETHER"
  32. setProp "announce-player-achievements" "$ANNOUNCE_PLAYER_ACHIEVEMENTS"
  33. setProp "enable-command-block" "$ENABLE_COMMAND_BLOCK"
  34. setProp "spawn-animals" "$SPAWN_ANIMAILS"
  35. setProp "spawn-monsters" "$SPAWN_MONSTERS"
  36. setProp "spawn-npcs" "$SPAWN_NPCS"
  37. setProp "generate-structures" "$GENERATE_STRUCTURES"
  38. setProp "spawn-npcs" "$SPAWN_NPCS"
  39. setProp "view-distance" "$VIEW_DISTANCE"
  40. setProp "hardcore" "$HARDCORE"
  41. setProp "max-build-height" "$MAX_BUILD_HEIGHT"
  42. setProp "force-gamemode" "$FORCE_GAMEMODE"
  43. setProp "hardmax-tick-timecore" "$MAX_TICK_TIME"
  44. setProp "enable-query" "$ENABLE_QUERY"
  45. setProp "query.port" "$QUERY_PORT"
  46. setProp "enable-rcon" "$ENABLE_RCON"
  47. setProp "rcon.password" "$RCON_PASSWORD"
  48. setProp "rcon.port" "$RCON_PORT"
  49. setProp "max-players" "$MAX_PLAYERS"
  50. setProp "max-world-size" "$MAX_WORLD_SIZE"
  51. setProp "level-name" "$LEVEL"
  52. setProp "level-seed" "$SEED"
  53. setProp "pvp" "$PVP"
  54. setProp "generator-settings" "$GENERATOR_SETTINGS"
  55. setProp "online-mode" "$ONLINE_MODE"
  56. if [ -n "$LEVEL_TYPE" ]; then
  57. # uppercase
  58. LEVEL_TYPE=$( echo ${LEVEL_TYPE} | tr '[:lower:]' '[:upper:]' )
  59. echo "Setting level type to $LEVEL_TYPE"
  60. case $LEVEL_TYPE in
  61. DEFAULT|FLAT|LARGEBIOMES|AMPLIFIED|CUSTOMIZED|BIOMESOP)
  62. setProp "level-type" "$LEVEL_TYPE"
  63. ;;
  64. *)
  65. echo "Invalid LEVEL_TYPE: $LEVEL_TYPE"
  66. echo "Try: DEFAULT FLAT LARGEBIOMES AMPLIFIED CUSTOMZED or BIOMESOP"
  67. exit 1
  68. ;;
  69. esac
  70. fi
  71. if [ -n "$DIFFICULTY" ]; then
  72. case $DIFFICULTY in
  73. peaceful|0)
  74. DIFFICULTY=0
  75. ;;
  76. easy|1)
  77. DIFFICULTY=1
  78. ;;
  79. normal|2)
  80. DIFFICULTY=2
  81. ;;
  82. hard|3)
  83. DIFFICULTY=3
  84. ;;
  85. *)
  86. echo "DIFFICULTY must be peaceful, easy, normal, or hard."
  87. exit 1
  88. ;;
  89. esac
  90. echo "Setting difficulty to $DIFFICULTY"
  91. setProp "difficulty" "$DIFFICULTY"
  92. fi
  93. if [ -n "$MODE" ]; then
  94. echo "Setting mode"
  95. MODE_LC=$( echo $MODE | tr '[:upper:]' '[:lower:]' )
  96. case $MODE_LC in
  97. 0|1|2|3)
  98. ;;
  99. su*)
  100. MODE=0
  101. ;;
  102. c*)
  103. MODE=1
  104. ;;
  105. a*)
  106. MODE=2
  107. ;;
  108. sp*)
  109. MODE=3
  110. ;;
  111. *)
  112. echo "ERROR: Invalid game mode: $MODE"
  113. exit 1
  114. ;;
  115. esac
  116. setProp "gamemode" "$MODE"
  117. fi
  118. fi
  119. if [ -n "$OPS" -a ! -e ops.txt.converted ]; then
  120. echo "Setting ops"
  121. echo $OPS | awk -v RS=, '{print}' >> ops.txt
  122. fi
  123. # There whitelist code https://github.com/itzg/dockerfiles/blob/master/minecraft-server/start-minecraft.sh#L540
  124. # but it doesn't work. See https://www.beastnode.com/portal/knowledgebase/6/Setting-Up-a-Whitelist-or-Make-Your-Server-Private.html
  125. # on using /whitelist add username (to actually setup and use the whitelist.json file) :P
  126. if [ ! -e banned-players.json ]; then
  127. echo '[]' > banned-players.json
  128. fi
  129. if [ ! -e banned-ips.json ]; then
  130. echo '[]' > banned-ips.json
  131. fi
  132. EXTRA_ARGS=""
  133. # Optional disable console
  134. if [[ ${CONSOLE} = false || ${CONSOLE} = FALSE ]]; then
  135. EXTRA_ARGS+="--noconsole"
  136. fi
  137. # Optional disable GUI for headless servers
  138. if [[ ${GUI} = false || ${GUI} = FALSE ]]; then
  139. EXTRA_ARGS="${EXTRA_ARGS} nogui"
  140. fi
  141. # put these prior JVM_OPTS at the end to give any memory settings there higher precedence
  142. echo "Setting initial memory to ${INIT_MEMORY:-${MEMORY}} and max to ${MAX_MEMORY:-${MEMORY}}"
  143. JVM_OPTS="-Xms${INIT_MEMORY:-${MEMORY}} -Xmx${MAX_MEMORY:-${MEMORY}} ${JVM_OPTS}"
  144. exec java $JVM_XX_OPTS $JVM_OPTS -jar $SERVER "$@" $EXTRA_ARGS