fork 0k-charms
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.4 KiB

  1. # -*- mode: shell-script -*-
  2. MINECRAFT_PATH="$SERVICE_CONFIGSTORE/opt/apps/minecraft"
  3. MINECRAFT_DATA="/var/lib/minecraft"
  4. minecraft:init-binary() {
  5. local type="$1" version="$2"
  6. case "$type" in
  7. "legacy")
  8. MINECRAFT_URL="https://docker.0k.io/downloads/minecraft_server.${version}.jar"
  9. MINECRAFT_BINARY="minecraft_server.${version}.jar"
  10. ;;
  11. "paper")
  12. MINECRAFT_URL="https://api.papermc.io/v2/projects/paper/versions/${version%-*}/builds/${version##*-}/downloads/paper-${version}.jar"
  13. MINECRAFT_BINARY="paper.${version}.jar"
  14. ;;
  15. *)
  16. err "Unknown ${WHITE}type${NORMAL} '$type'. Please use on of: legacy, paper"
  17. exit 1
  18. ;;
  19. esac
  20. if ! [ -f "$MINECRAFT_BINARY" ]; then
  21. mkdir -p "${MINECRAFT_PATH}"
  22. wget "$MINECRAFT_URL" -O "$MINECRAFT_PATH/$MINECRAFT_BINARY" || exit 1
  23. fi
  24. init-config-add "
  25. $SERVICE_NAME:
  26. volumes:
  27. - \"$MINECRAFT_PATH/${MINECRAFT_BINARY}:${MINECRAFT_DATA}/server.jar\"
  28. "
  29. }
  30. minecraft:init-command() {
  31. local threads="${1:-4}" mem="${2:-2048M}"
  32. init-config-add "
  33. $SERVICE_NAME:
  34. command:
  35. - java
  36. - -Xmx${mem}
  37. - -Xms${mem}
  38. - -XX:ParallelGCThreads=${threads}
  39. - -jar
  40. - server.jar
  41. - nogui
  42. "
  43. echo "eula=TRUE" > "$SERVICE_DATASTORE/${MINECRAFT_DATA}/eula.txt"
  44. }
  45. minecraft:make-whitelist() {
  46. local whitelist="$1"
  47. {
  48. e "$whitelist" |
  49. yq -o=json \
  50. 'to_entries | map({"uuid": .key, "name": .value})'
  51. } > "$SERVICE_DATASTORE/${MINECRAFT_DATA}/whitelist.json"
  52. }
  53. minecraft:make-ops() {
  54. local ops="$1"
  55. {
  56. e "$ops" |
  57. yq -o=json \
  58. 'to_entries | map({
  59. "uuid": .value.uuid,
  60. "name": .key,
  61. "level": .value.level,
  62. "bypassesPlayerLimit": .value.bypassesPlayerLimit
  63. })'
  64. } > "$SERVICE_DATASTORE/${MINECRAFT_DATA}/ops.json"
  65. }
  66. minecraft:make-properties() {
  67. local properties="$1"
  68. {
  69. e "$properties" |
  70. yq 'to_entries | map(.key + "=" + .value) | .[]'
  71. } > "$SERVICE_DATASTORE/${MINECRAFT_DATA}/server.properties"
  72. }
  73. minecraft:config-hash() {
  74. local opts="$1"
  75. debug "Adding config hash to enable recreating upon config change."
  76. config_hash=$(e "$opts" | md5_compat) || exit 1
  77. init-config-add "
  78. $SERVICE_NAME:
  79. labels:
  80. - compose.config_hash=$config_hash
  81. "
  82. }