Skip to content

Commit 32168d8

Browse files
committed
Fix Java version issue
Added option to run with specific Java version or auto-decided Java for to fix #2
1 parent 7468696 commit 32168d8

File tree

3 files changed

+73
-9
lines changed

3 files changed

+73
-9
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ Many thanks to both projects for making this one possible.
147147
- 1.3.1
148148
- Fixed memory allocation issue which was causing servers to always be limited to 1G memory
149149
- Setting memory within `JVM_OPTS` using the `-Xmx` and `-Xms` flags fails to set memory, using docker flags `MAX_MEMORY` and `INIT_MEMORY` works properly
150-
- Within the start-minecraftFinalSetup file from https://github.com/itgz/docker-minecraft-server/ (line 155 as of March 13, 2021), the setup overrides the `-Xmx` and `-Xms` JVM flags using the `MAX_MEMORY` and `INIT_MEMORY` variables
150+
- Within the start-minecraftFinalSetup file from https://github.com/itzg/docker-minecraft-server/ (line 155 as of March 13, 2021), the setup overrides the `-Xmx` and `-Xms` JVM flags using the `MAX_MEMORY` and `INIT_MEMORY` variables
151151
- 1.4
152152
- Updated template-vars file with some additional settings and better comments on settings
153153
- Added WORLD and SPAWN_PROTECTION settings
@@ -165,4 +165,9 @@ Many thanks to both projects for making this one possible.
165165
- Added `print-environment` and `print-var` commands to script to print out all variables or a specific variable respectively
166166
- Removed copying server icon from file, as it was not working without changing write permissions within server directory
167167
- Removed `follow-log` command, functionality moved to `log --follow`
168-
- General reworking of script
168+
- General reworking of script
169+
- 1.5.1
170+
- Adjusted `print-environment` output
171+
- Added `--version` option to output version of script
172+
- Added `JAVA_VERSION` option to variables file
173+
- Reworked start function to utilize `JAVA_VERSION` option

src/minecraftctl.sh

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#!/bin/bash
22
# Names of variables that should have a non-null default value
3-
name_arr=("DOCKER" "PORT" "RCON_PORT" "RCON_PASSWORD" "MINHEAP" "MAXHEAP" "EULA" "TYPE" "VERSION" "DIFFICULTY" "MAX_PLAYERS" "MAX_WORLD_SIZE" "ALLOW_NETHER" "ANNOUNCE_PLAYER_ACHIEVEMENTS" "ENABLE_COMMAND_BLOCK" "FORCE_GAMEMODE" "GENERATE_STRUCTURES" "HARDCORE" "MAX_BUILD_HEIGHT" "MAX_TICK_TIME" "SPAWN_MONSTERS" "SPAWN_NPCS" "VIEW_DISTANCE" "MODE" "MOTD" "PVP" "LEVEL_TYPE" "LEVEL" "AUTOPAUSE" "TIMEOUT" "EPHEMERAL" "DATA_DIR" "BACKUP_DIR" "SPAWN_PROTECTION" "MINECRAFT_UID" "MINECRAFT_GID")
3+
name_arr=("DOCKER" "PORT" "RCON_PORT" "RCON_PASSWORD" "MINHEAP" "MAXHEAP" "EULA" "TYPE" "VERSION" "DIFFICULTY" "MAX_PLAYERS" "MAX_WORLD_SIZE" "ALLOW_NETHER" "ANNOUNCE_PLAYER_ACHIEVEMENTS" "ENABLE_COMMAND_BLOCK" "FORCE_GAMEMODE" "GENERATE_STRUCTURES" "HARDCORE" "MAX_BUILD_HEIGHT" "MAX_TICK_TIME" "SPAWN_MONSTERS" "SPAWN_NPCS" "VIEW_DISTANCE" "MODE" "MOTD" "PVP" "LEVEL_TYPE" "LEVEL" "AUTOPAUSE" "TIMEOUT" "EPHEMERAL" "DATA_DIR" "BACKUP_DIR" "SPAWN_PROTECTION" "MINECRAFT_UID" "MINECRAFT_GID" "JAVA_VERSION")
44
# Default values for variables in above array
5-
vals_arr=("$(which docker)" "25565" "25575" "minecraft" "512" "2048" "false" "vanilla" "LATEST" "normal" "10" "29999984" "true" "true" "false" "false" "true" "false" "256" "60000" "true" "true" "10" "survival" "A minecraft server" "true" "default" "world" "true" "0" "false" "/srv/minecraft" "./backups" "64" "$(id -u minecraft)" "$(id -g minecraft)")
5+
vals_arr=("$(which docker)" "25565" "25575" "minecraft" "512" "2048" "false" "vanilla" "LATEST" "normal" "10" "29999984" "true" "true" "false" "false" "true" "false" "256" "60000" "true" "true" "10" "survival" "A minecraft server" "true" "default" "world" "true" "0" "false" "/srv/minecraft" "./backups" "64" "$(id -u minecraft)" "$(id -g minecraft)" "DEFAULT")
6+
# Version string
7+
VERSION_STR="1.5.1"
68
# Setup flag variables for script
79
seteula=false
810
debug=false
@@ -18,6 +20,7 @@ function usage {
1820
echo " -e <TRUE|FALSE> | --eula <TRUE|FALSE>: Set Eula to a desired value, regardless of environment file. Useful for starting a default environment server that doesn't need an environment file just for the EULA option."
1921
echo " -f | --follow: Follows the log of the docker container when used with the 'log' command"
2022
echo " -v | --verbose: Enables verbosity messages about what is happening"
23+
echo " -V | --version: Prints version"
2124
echo
2225
echo "Actions:"
2326
echo " help | usage"
@@ -57,9 +60,26 @@ function usage {
5760
}
5861
# Print environment variables
5962
function printvars {
60-
for i in ${!name_arr[@]}; do
61-
echo "${name_arr[$i]} - Default: ${vals_arr[$i]} - Currently: ${!name_arr[$i]}"
63+
for i in ${!name_arr[@]}; do
64+
len=${#name_arr[$i]}
65+
echo -n "${name_arr[$i]}"
66+
for j in {0..28}; do
67+
# echo -n " "
68+
if [[ $j -gt $len ]]; then
69+
echo -n " "
70+
fi
71+
done
72+
echo -n " - Default: "
73+
len=${#vals_arr[$i]}
74+
echo -n "${vals_arr[$i]}"
75+
for j in {0..18}; do
76+
# echo -n " "
77+
if [[ $j -gt $len ]]; then
78+
echo -n " "
79+
fi
6280
done
81+
echo " - Current: ${!name_arr[$i]}"
82+
done
6383
}
6484
# Print a specific environment variable
6585
function printvar {
@@ -93,6 +113,7 @@ function status {
93113
if [[ "x$name" == "xall" ]]; then
94114
vmsg "Finding docker containers ..."
95115
servers=($(docker ps --filter ancestor=itzg/minecraft-server:java8 --format "{{.Names}}"))
116+
servers+=($(docker ps --filter ancestor=itzg/minecraft-server:java16 --format "{{.Names}}"))
96117
dmsg "Containers found: [ ${servers[@]} ]"
97118
for i in ${!servers[@]}; do
98119
vmsg "Testing container '${servers[$i]}'"
@@ -102,6 +123,7 @@ function status {
102123
echo "Server '${servers[$i]}' is stopped"
103124
fi
104125
done
126+
exit 0
105127
else
106128
vmsg "Testing container '$name' for running status ..."
107129
if running; then
@@ -137,7 +159,40 @@ function start {
137159
vol_mount=""
138160
echo "Ephemeral server, a restart will lose all world data."
139161
fi
162+
163+
vmsg "Deciding Java version to use ..."
164+
if [[ "$JAVA_VERSION" == "DEFAULT" ]]; then
165+
# Check for game version
166+
dmsg "Java version set to default. Checking game version ..."
167+
if [[ $VERSION == "LATEST" ]] || [[ $VERSION == "SNAPSHOT" ]]; then
168+
dmsg "Game version set to $VERSION. Setting Java version to latest."
169+
jvm_ver="latest"
170+
else
171+
dmsg "Game version is $VERSION. Splitting version string to check for usable Java version ..."
172+
if [[ ! -z $IFS ]]; then
173+
OLD_IFS=$IFS
174+
reset_ifs=true
175+
fi
176+
IFS="."
177+
# Split game version string from (example) 1.17.1 to an array as [ 1 17 1 ]
178+
readarray -t ver_arr <<< $VERSION
179+
ver_arr=($ver_arr)
180+
IFS=$OLD_IFS
181+
# Check if game is 1.17 or higher, as that is the point when Java16 is required
182+
if [[ ${ver_arr[1]} -ge 17 ]]; then
183+
dmsg "Game version is at least 1.17. Setting Java version to Java16."
184+
jvm_ver="java16"
185+
else
186+
dmsg "Game version is below 1.17. Setting Java version to Java8."
187+
jvm_ver="java8"
188+
fi
189+
fi
190+
else
191+
dmsg "Java version has been manually specified. Running with specification."
192+
jvm_ver="$JAVA_VERSION"
193+
fi
140194
vmsg "Calling '$DOCKER' to start server container '$name' ..."
195+
# Maybe remove that default port from ports? might not be needed
141196
$DOCKER run -d -i \
142197
--name "$name" \
143198
$vol_mount \
@@ -181,7 +236,7 @@ function start {
181236
-e "ENABLE_AUTOPAUSE=$AUTOPAUSE" \
182237
-e "TIMEOUT=$TIMEOUT" \
183238
-e "SPAWN_PROTECTION=$SPAWN_PROTECTION" \
184-
itzg/minecraft-server:java8
239+
itzg/minecraft-server:$jvm_ver
185240
echo "Started minecraft container '$name'"
186241
}
187242
# Send a command to the game server
@@ -305,7 +360,7 @@ function check_vars {
305360
done
306361
# Set the rcon command now that password and port are set either to user-defined or default values
307362
RCON_CMD="mcrcon -P ${RCON_PORT} -p ${RCON_PASSWORD}"
308-
dmsg "Setting RCON_CMD to '$RCON_CMD'"
363+
dmsg "Set RCON_CMD to '$RCON_CMD'"
309364
}
310365

311366
# Exit immediately if a command exits with non-zero status
@@ -314,6 +369,7 @@ set -e
314369
# Check valid command before other steps
315370
case $1 in
316371
status|start|stop|restart|backup|save|command|console|log|help|usage|print-environment|print-var) if [[ "$@" == *"-d"* ]] || [[ "$@" == *"--debug"* ]]; then echo "Command recognized as $1"; fi; cmd=$1; shift;;
372+
-V|--version) echo "$VERSION_STR"; exit 0;;
317373
*) usage;;
318374
esac
319375
# Command line options

src/template-vars

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
# Game version - can be set to LATEST, SNAPSHOT, or a specific version number
2828
#VERSION="LATEST"
2929

30+
# Java version to use - can be set to DEFAULT, latest, or a specific version, such as java8 or java16 - see https://github.com/itzg/docker-minecraft-server#running-minecraft-server-on-different-java-version for full list of supported versions
31+
#JAVA_VERSION="DEFAULT"
32+
3033
# Game difficulty
3134
#DIFFICULTY="normal"
3235

@@ -121,4 +124,4 @@
121124
#DATA_DIR="/srv/minecraft"
122125

123126
# Relative directory to $DATA_DIR for saving minecraft backups
124-
#BACKUP_DIR="./backups"
127+
#BACKUP_DIR="./backups"

0 commit comments

Comments
 (0)