modified paper mc scripts to work for arbitrary servers

This commit is contained in:
Patrick Kuhs 2025-04-05 20:35:44 +02:00
parent 9ec50b69b7
commit fa1936f647
6 changed files with 210 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
server

28
Dockerfile Normal file
View File

@ -0,0 +1,28 @@
FROM alpine:latest
ENV MINECRAFT_VERSION= \
PAPER_BUILD="latest" \
EULA="false" \
MC_RAM=""
RUN apk update && \
apk add openjdk21-jre && \
apk add bash && \
apk add curl && \
apk add jq
RUN addgroup -S servergrp
RUN adduser -S server -G servergrp -D -u 1000
USER server
RUN mkdir /home/server/instance
#RUN chown -hR server:servergrp /home/server/instance
COPY setup.sh /home/server/
CMD ["bash", "/home/server/setup.sh"]
EXPOSE 25565
VOLUME /home/server/instance

9
LICENSE Normal file
View File

@ -0,0 +1,9 @@
MIT License
Copyright (c) 2025 Patrick Kuhs
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

17
Makefile Normal file
View File

@ -0,0 +1,17 @@
build:
docker build -t mc-env-docker .
run: build
docker run -it --name mc-env mc-env-docker
test-env: build
docker run -it -e RUNSCRIPT -e EULA="true" -e WHITELIST="true" -e SEED="123" -e MAX_PALYERS="1" -e DIFFICULTY="normal" -e GAMEMODE="survival" -e "MOTD"="Local Docker Test" -p 25565:25565 -p 8100:8100 --volume ./server:/home/server/instance:z --name mc-env mc-env-docker
clean:
docker rm mc-env
docker image rm mc-env-docker
echo "Holzhakker" > ./server/whitelist.prep
echo "Holzhakker" > ./server/op.prep
debug:
docker run --rm -it --entrypoint bash --name mc-env-debug mc-env-docker

22
docker-compose.yml Normal file
View File

@ -0,0 +1,22 @@
version: "3"
services:
server:
image: papermc-docker
container_name: YOUR_CONTAINER_NAME
restart: always
volumes:
- "YOUR_SERVER_LOCATION":/home/server/instance:z
ports:
- "YOUR_PORT:25565"
environment:
SERVER_MEMORY: "2G"
EULA: "true"
SEED: "abc"
# provide a txt file with the usernames of the players to be whitelisted in the server folder to automatically add them to the whitelist
WHITELIST: "true"
MAX_PLAYERS: "20"
DIFFICULTY: "easy"
GAMEMODE: "survival"
MOTD: "A Dockerized Minecraft Server"
ENFORCE_SECURE_PROFILE: "false"

133
setup.sh Executable file
View File

@ -0,0 +1,133 @@
#!/bin/bash
SERVER_DIR=/home/server/instance
cd $SERVER_DIR
: ${MINECRAFT_VERSION:='latest'}
MINECRAFT_VERSION="${MINECRAFT_VERSION,,}"
PROJECT="paper"
# prepare performance opts for java
JAVA_OPTS=" -XX:+UseG1GC -XX:+ParallelRefProcEnabled \
-XX:MaxGCPauseMillis=200 \
-XX:+UnlockExperimentalVMOptions \
-XX:+DisableExplicitGC \
-XX:+AlwaysPreTouch \
-XX:G1NewSizePercent=30 \
-XX:G1MaxNewSizePercent=40 \
-XX:G1HeapRegionSize=8M \
-XX:G1ReservePercent=20 \
-XX:G1HeapWastePercent=5 \
-XX:G1MixedGCCountTarget=4 \
-XX:InitiatingHeapOccupancyPercent=15 \
-XX:G1MixedGCLiveThresholdPercent=90 \
-XX:G1RSetUpdatingPauseTimePercent=5 \
-XX:SurvivorRatio=32 \
-XX:+PerfDisableSharedMem \
-XX:MaxTenuringThreshold=1 \
-Daikars.new.flags=true \
-Dusing.aikars.flags=https://mcflags.emc.gs"
echo "eula=${EULA:-false}" > eula.txt
if [[ -n $SERVER_MEMORY ]]
then
JAVA_OPTS="-Xms${SERVER_MEMORY} -Xmx${SERVER_MEMORY} $JAVA_OPTS"
fi
SERVER_PROPERTIES=$SERVER_DIR/server.properties
SERVER_WHITELIST=$SERVER_DIR/whitelist.json
SERVER_OPLIST=$SERVER_DIR/ops.json
WHITELIST_PREP=$SERVER_DIR/whitelist.prep
OPLIST_PREP=$SERVER_DIR/op.prep
if [ -f $SERVER_PROPERTIES ]; then
mv $SERVER_PROPERTIES $SERVER_PROPERTIES.bkp
rm $SERVER_PROPERTIES
touch $SERVER_PROPERTIES
fi
if [ -f $WHITELIST_PREP ]; then
echo -n "[" > $SERVER_WHITELIST
cat $WHITELIST_PREP | while read u
do
echo "Adding $u to whitelist"
# get uuid of user
uuid=$(curl -s https://api.mojang.com/users/profiles/minecraft/$u | jq -r '.id')
uuid=$(echo "${uuid:0:8}-${uuid:8:4}-${uuid:12:4}-${uuid:16:4}-${uuid:20:12}")
if [[ $uuid == "null" ]]; then
echo "User $u not found. Skipping."
continue
fi
# write to whitelist.json
echo -n "{\"uuid\": \"$uuid\",\"name\": \"$u\"}" >> $SERVER_WHITELIST;
done
echo "]" >> $SERVER_WHITELIST
sed --in-place -e 's/}{/}, {/g' $SERVER_WHITELIST
fi
if [ -f $OPLIST_PREP ]; then
echo -n "[" > $SERVER_OPLIST
cat $OPLIST_PREP | while read u
do
echo "Adding $u to ops"
# get uuid of user
uuid=$(curl -s https://api.mojang.com/users/profiles/minecraft/$u | jq -r '.id')
uuid=$(echo "${uuid:0:8}-${uuid:8:4}-${uuid:12:4}-${uuid:16:4}-${uuid:20:12}")
if [[ $uuid == "null" ]]; then
echo "User $u not found. Skipping."
continue
fi
# write to whitelist.json
echo -n "{\"uuid\": \"$uuid\",\"name\": \"$u\",\"level\": 4,\"bypassesPlayerLimit\": false}" >> $SERVER_OPLIST;
done
echo "]" >> $SERVER_OPLIST
sed --in-place -e 's/}{/}, {/g' $SERVER_OPLIST
fi
if [[ -n $WHITELIST ]]; then
echo "white-list=$WHITELIST" >> $SERVER_PROPERTIES
fi
if [[ -n $SEED ]]; then
echo "seed=$SEED" >> $SERVER_PROPERTIES
fi
if [[ -n $MAX_PLAYERS ]]; then
echo "max-players=$MAX_PLAYERS" >> $SERVER_PROPERTIES
fi
if [[ -n $DIFFICULTY ]]; then
echo "difficulty=$DIFFICULTY" >> $SERVER_PROPERTIES
fi
if [[ -n $GAMEMODE ]]; then
echo "gamemode=$GAMEMODE" >> $SERVER_PROPERTIES
fi
if [[ -n $MOTD ]]; then
echo "motd=$MOTD" >> $SERVER_PROPERTIES
fi
if [[ -n $ENFORCE_SECURE_PROFILE ]]; then
echo "enforce-secure-profile=$ENFORCE_SECURE_PROFILE" >> $SERVER_PROPERTIES
fi
# Start server
if [[ -n $RUNSCRIPT ]]; then
exec java $JAVA_OPTS -jar "server.jar" nogui
else
exec bash /home/server/instance/run.sh
fi