From fa1936f647e50e5cd6ab2c666be6107c2c50e526 Mon Sep 17 00:00:00 2001 From: Patrick Kuhs Date: Sat, 5 Apr 2025 20:35:44 +0200 Subject: [PATCH] modified paper mc scripts to work for arbitrary servers --- .gitignore | 1 + Dockerfile | 28 ++++++++++ LICENSE | 9 +++ Makefile | 17 ++++++ docker-compose.yml | 22 ++++++++ setup.sh | 133 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 210 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 docker-compose.yml create mode 100755 setup.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..13cd1fa --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +server \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..7ae44e2 --- /dev/null +++ b/Dockerfile @@ -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 + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..db22d3a --- /dev/null +++ b/LICENSE @@ -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. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6482aec --- /dev/null +++ b/Makefile @@ -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 \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..6b29d6e --- /dev/null +++ b/docker-compose.yml @@ -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" diff --git a/setup.sh b/setup.sh new file mode 100755 index 0000000..42898c2 --- /dev/null +++ b/setup.sh @@ -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 \ No newline at end of file