#!/bin/bash
cd papermc
urlPrefix=https://fill.papermc.io/v3/projects/paper
if [ ${MC_VERSION} = latest ]; then
  MC_VERSION=$(curl -s $urlPrefix | jq -r '.versions | to_entries[0] | .value[0]')
fi
urlPrefix=${urlPrefix}/versions/${MC_VERSION}
DOWNLOAD_URL=$(
  curl -s "$urlPrefix/builds" |
  jq -r 'first(.[] | select(.channel == "STABLE") | .downloads."server:default".url) // "null"')
rm -f *.jar
curl -sO "$DOWNLOAD_URL"
if [ ! -e eula.txt ]; then
 java -jar paper-*.jar
 sed -i 's/false/true/g' eula.txt
fi

#exec java -server -Xms${MC_RAM} -Xmx${MC_RAM} ${JAVA_OPTS} -jar ${JAR_NAME} nogui
exec java -Xms${MC_RAM} -Xmx${MC_RAM} -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 \
  ${JAVA_OPTS} -jar paper-*.jar nogui
