Skip to content

Instantly share code, notes, and snippets.

@loopyd
Last active June 29, 2019 02:09
Show Gist options
  • Save loopyd/b4a8f8aee22cc9ede66497301a3e928c to your computer and use it in GitHub Desktop.
Save loopyd/b4a8f8aee22cc9ede66497301a3e928c to your computer and use it in GitHub Desktop.
Basic minecraft server startup script for Windows with a bunch of configurable command-line arguments and easy JVM auto-optimization. Debug mode argument set fully compatible with IDE of your choice for plugin / server development.
@ECHO OFF
setlocal enableDelayedExpansion enableExtensions
REM @filename start_mc.cmd
REM @author loopyd &lt;<a href="mailto:loopyd@lupinedream.com">loopyd@lupinedream.com</a>&gt; LupineDream
REM @link <a href="https://gist.github.com/loopyd">gist.github.com/loopyd</a>
REM @description Use is always free, with credit! \
REM \
REM You may use this script at your leasure for testing purposes. \
REM \
REM You are not to remove these comments from this file if you so choose to use \
REM my code, please give credit where it is do by abiding by this simple request! \
REM Thank you~!
REM Changelog:
REM 1.0.0 initial copypasta
REM 1.0.1 fixed typos
REM 1.0.2 fix argument shifting
REM 1.0.3 fix argument validation
REM 1.1.0 added usage manual
REM 1.1.1 fix -oa flag
REM ***** Parse command line options
set "options=-jdk:"%PROGRAMFILES%\AdoptOpenJDK\jdk-8.0.212.04-hotspot\bin" -mcjar:"server.jar" -appdir:"%~dp0" -xms:"1G" -xmx:"2G" -debug: -normal: -loop: -ogc: -ot: -oc: -oa: -oof: -lru:"10000" -ccs:"2048m" -gct:"10" -gcmax:"150" -gcmin:"15" -tmax:"15" -tmin:"8" -ttarget:"90" -help: "
for %%O in (%options%) do for /f "tokens=1,* delims=:" %%A in ("%%O") do set "%%A=%%~B"
:loop
if not "%~1"=="" (
set "test=!options:*%~1:=! "
if "!test!"=="!options! " (
echo. Error: Invalid option %~1
) else if "!test:~0,1!"==" " (
set "%~1=t"
) else (
setlocal disableDelayedExpansion
set "val=%~2"
call :escapeVal
setlocal enableDelayedExpansion
for /f delims^=^ eol^= %%A in ("!val!") do endlocal&endlocal&set "%~1=%%A" !
shift
)
shift
goto :loop
)
goto :endArgs
:escapeVal
set "val=%val:^=^^%"
set "val=%val:!=^!%"
exit /b
:endArgs
REM ***** Validate command line options
IF DEFINED -help (
goto :displayuse
)
IF DEFINED -debug (
IF DEFINED -normal (
echo. Incompatible options: -debug -normal
set "errorflag=t"
)
IF DEFINED -oc (
echo. Incompatible option: -oc
echo. Cannot apply optimizations in debug run mode.
set "errorflag=t"
)
IF DEFINED -ot (
echo. Incompatible option: -ot
echo. Cannot apply optimizations in debug run mode.
set "errorflag=t"
)
IF DEFINED -ogc (
echo. Incompatible option: -ogc
echo. Cannot apply optimizations in debug run mode.
set "errorflag=t"
)
IF DEFINED -oa (
echo. Incompatible option: -oa
echo. Cannot apply optimizations in debug run mode.
set "errorflag=t"
)
IF DEFINED -oof (
echo. Incompatible option: -oof
echo. Cannot apply optimizations in debug run mode.
set "errorflag=t"
)
)
IF DEFINED -oof (
IF DEFINED -oc (
echo. Incompatible option: -oc
echo. Redundant option -oc conflicts with option -oof
set "errorflag=t"
)
IF DEFINED -ot (
echo. Incompatible option: -ot
echo. Redundant option -ot conflicts with option -oof
set "errorflag=t"
)
IF DEFINED -ogc (
echo. Incompatible option: -ogc
echo. Redundant option -ogc conflicts with option -oof
set "errorflag=t"
)
IF DEFINED -oa (
echo. Incompatible option: -oa
echo. Redundant option -oa conflicts with option -oof
set "errorflag=t"
)
IF NOT DEFINED errorflag (
SET "-ogc=t"
SET "-ot=t"
SET "-oc=t"
SET "-oa=t"
)
)
IF NOT DEFINED -oc (
IF NOT "!-lru!"=="10000" (
echo. Incompatible option: -lru
echo. Option -lru requires: -oc OR -oof
set "errorflag=t"
)
IF NOT "!-ccs!"=="2048m" (
echo. Incompatible option: -ccs
echo. Option -ccs requires: -oc OR -oof
set "errorflag=t"
)
)
IF NOT DEFINED -ogc (
IF NOT "!-gct!"=="10" (
echo. Incompatible option: -gct
echo. Option -gct requires: -ogc OR -oof
set "errorflag=t"
)
IF NOT "!-gcmax!"=="150" (
echo. Incompatible option: -gcmax
echo. Option -gcmax requires: -ogc OR -oof
set "errorflag=t"
)
IF NOT "!-gcmin!"=="15" (
echo. Incompatible option: -gcmin
echo. Option -gcmin requires: -ogc OR -oof
set "errorflag=t"
)
)
IF NOT DEFINED -ot (
IF NOT "!-tmin!"=="8" (
echo. Incompatible option: -tmin
echo. Option -tmin requires: -ot OR -oof
set "errorflag=t"
)
IF NOT "!-tmax!"=="15" (
echo. Incompatible option: -tmax
echo. Option -tmax requires: -ot OR -oof
set "errorflag=t"
)
IF NOT "!-ttarget!"=="90" (
echo. Incompatible option: -ttarget
echo. Option -tmin requires: -ot OR -oof
set "errorflag=t"
)
)
IF "!errorflag!"=="t" (
echo.
echo. FATAL: Cannot continue, displaying help
echo.
goto :displayuse
)
REM ***** Set JVM params
set "B_JAVA_HOME=!JAVA_HOME!"
set "B_APP_HOME=!APP_HOME!"
set "JAVA_HOME=!-jdk!"
set "APP_HOME=!-appdir!"
REM ***** Main
:serverloop
echo.
echo. ---------------------------------------------------------------------------
echo. JDK Home Directory: !-jdk!
echo. Running in directory: !-appdir!
IF DEFINED -debug (
echo. Mode: Debug
) ELSE (
echo. Mode: Normal
)
echo. ---------------------------------------------------------------------------
call :runserver
echo.
IF DEFINED -loop (
echo. -----------------------------------------------------------------------
echo. Loop mode active, the server will restart in 5 seconds.
echo. Press Q to cancel this operation and quit the script.
echo. -----------------------------------------------------------------------
choice /N /T 5 /C QC /D C >nul
IF !ERRORLEVEL! EQU 1 GOTO :serverdone
IF !ERRORLEVEL! EQU 2 GOTO :serverloop
) else (
echo. JVM process exited
echo. -----------------------------------------------------------------------
)
:serverdone
set "JAVA_HOME=!B_JAVA_HOME!"
set "APP_HOME=!B_APP_HOME!"
echo.
echo. Thanks for using start_mc.cmd !
echo.
goto :eof
:runserver
set "runstr="%JAVA_HOME%\java.exe""
IF DEFINED -debug (
set "runstr=!runstr! -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"
set "runstr_str=echo. Starting Minecraft JVM with remote debugger socket (IntelliJ, atom, etc)..."
) ELSE IF DEFINED -normal (
set "runstr=!runstr! -server"
set "runstr_str=echo. Starting Minecraft JVM in server/production mode..."
) ELSE (
set "runstr=!runstr! -server"
set "runstr_str=echo. Starting Minecraft JVM in default mode..."
)
set "runstr=!runstr! -Xms!-xms! -Xmx!-xmx!"
IF DEFINED -ogc (
set "runstr=!runstr! -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:MaxGCPauseMillis=!-gcmin! -XX:GCPauseIntervalMillis=!-gcmax! -XX:+UseAdaptiveGCBoundary -XX:-UseGCOverheadLimit -XX:ParallelGCThreads=!-gct!"
)
IF DEFINED -oc (
set "runstr=!runstr! -XX:ReservedCodeCacheSize=!-ccs! -XX:+UseCodeCacheFlushing -XX:SoftRefLRUPolicyMSPerMB=!-lru!"
)
IF DEFINED -ot (
set "runstr=!runstr! -XX:+UseNUMA -XX:+CMSParallelRemarkEnabled -XX:SurvivorRatio=!-tmin! -XX:TargetSurvivorRatio=!-ttarget! -XX:MaxTenuringThreshold=!-tmax!"
)
IF DEFINED -oa (
set "runstr=!runstr! -XX:+UseBiasedLocking -XX:+UseFastAccessorMethods -XX:+UseCompressedOops -XX:+OptimizeStringConcat -XX:+AggressiveOpts -Dfml.ignorePatchDiscrepancies=true -Dfml.ignoreInvalidMinecraftCertificates=true"
)
set "runstr=!runstr! -jar "!APP_HOME!\!-mcjar!" nogui"
!runstr_str!
!runstr!
set "runstr="
set "runstr_str="
exit /b
:displayuse
echo. start_mc - Easily start your minecraft server!
echo.
echo. version: 1.1.1
echo. author: loopyd ^<loopyd@lupinedream.com^>
echo.
echo. Usage: start_mc.cmd { Start options } { Path options } { Memory allocation options }
echo. { Optimization options } { ...Tweakers paradise... }
echo.
echo. Start options:
echo.
echo. -debug Starts the server with a JVM remote debugger attached for your favorite IDE.
echo. Cannot be used with -normal
echo. -normal Starts the server with a normal set of production arguments
echo. Cannot be used with -debug
echo. -loop Forces the server into a loop, at the end of the loop, the user gains the option
echo. to cancel out of the loop within 5 seconds by pressing CTRL+C.
echo.
echo. Path options:
echo.
echo. -jdk:"path" Specify a path to your jdk runtime executable using this option.
echo. Default: "^%PROGRAMFILES^%\AdoptOpenJDK\jdk-8.0.212.04-hotspot\bin"
echo. -mcjar:"file.jar" Specify the name of your minecraft server jar
echo. Default: "server.jar"
echo. -appdir:"path" Specify the directory where your server's files are stored.
echo. The default is the current directory.
echo.
echo. Memory allocation options:
echo.
echo. -xms:"value" How much memory the JVM should allocate at the minimum.
echo. Default: "1G"
echo. -xmx:"value" How much memory the JVM should allocate at the maximum.
echo. Default: "2G"
echo.
echo. Optimization options:
echo. -ogc Will start Minecraft with optimized garbage collector.
echo. -ot Will start Minecraft with NUMA Tenuring optimizations.
echo. -oc Will start Minecraft with caching optimizations.
echo. -oa Will start Minecraft with some additional JVM optimizations (misc.)
echo. -oof Will start Minecraft with all optimizations!
echo. Has the same effect as "-ogc -ot -oc -oa".
echo.
echo. ************************************** TWEAKERS PARADISE ******************************************
echo.
echo. These options have conveniently set defaults. However, if you're a tweaker, I've got you
echo. covered with overrides. Let those endless forum arguments continue !
echo.
echo. Garbage collector optimization tweaks: ( -ogc option )
echo. -gct:"value" Garbage collector threads
echo. Default: "10"
echo. -gcmin:"value" Minimum interval (in milliseconds) between garbage collector checks.
echo. Default: "15"
echo. -gcmax:"value" Maxmimum interval (in milliseconds) between forced cleanups.
echo. Default: "150"
echo.
echo. Cache optimization tweaks: ( -oc option )
echo. -lru:"value" Soft-reference LRU policy
echo. Default: "10000"
echo. -ccs:"value" Code cache prerequisate size
echo. Default: "2048m"
echo.
echo. Tenuring optimization tweaks: ( -ot option )
echo. -tmin:"value" Minimum tenuring survival ratio
echo. Default: "8"
echo. -tmax:"value" Maximum tenuring survival ratio
echo. Default: "15"
echo. -ttarget:"value" Target tenuring survival slice (percentage)
echo. Default: "90"
echo.
echo. *************************************************************************************************
echo.
echo. This script was designed in a hurry! Please forgive any bugs. Its intent is to provide an easy
echo. solution for developers and homebrew Minecraft server admins. It has not been tested in a
echo. production environment, but probably provides everything you'd damn well need to.
echo.
echo. I am not responsible for any damages that may incur *emotional or otherwise :^^)* for the use
echo. of this script. Please keep your diamond swords out of my nether portal, its not kosher!
echo.
echo. Luv,
echo. ~HeavyPaws
echo.
echo. Usage manual End of file.
echo.
goto :eof
:eof
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment