Skip to content

Instantly share code, notes, and snippets.

@dentolos19
Last active August 24, 2023 23:24
Show Gist options
  • Save dentolos19/1986b781b28ed1c4be76c69d2208955e to your computer and use it in GitHub Desktop.
Save dentolos19/1986b781b28ed1c4be76c69d2208955e to your computer and use it in GitHub Desktop.
A simple script for automatically starting and updating a minecraft server.

📜 Minecraft Server

A simple script for automatically starting a minecraft server.

This script will automatically download the latest build for the respective server version that you have configured. It will also check and auto updates to the latest build every time you run the script.

⚒️ Usage

  1. Install the required dependencies globally.
    • py -m pip install requests
  2. Run the script.
  3. Done!
import hashlib
import os
import subprocess
import requests # py -m pip install requests
SERVER_FILE = "server.jar"
SERVER_VERSION = "1.20.1"
ALLOCATED_MEMORY = 4096 # mb
def main():
print("Getting version information...")
response = requests.get(
f"https://api.papermc.io/v2/projects/paper/versions/{SERVER_VERSION}"
)
if response.status_code != 200:
print("Error: Failed to get version information.")
exit(1)
latest_build = response.json()["builds"][-1]
print("Checking for updates...")
response = requests.get(
f"https://api.papermc.io/v2/projects/paper/versions/{SERVER_VERSION}/builds/{latest_build}"
)
if response.status_code != 200:
print("Error: Failed to get latest build information.")
exit(1)
application_info = response.json()["downloads"]["application"]
if (
not os.path.exists(SERVER_FILE)
or hash_file(SERVER_FILE) != application_info["sha256"]
):
print("Downloading latest build...")
response = requests.get(
f"https://api.papermc.io/v2/projects/paper/versions/{SERVER_VERSION}/builds/{latest_build}/downloads/{application_info['name']}",
stream=True,
)
if response.status_code != 200:
print("Error: Failed to download latest build.")
exit(1)
with open(SERVER_FILE, "wb") as file:
for chunk in response.iter_content(chunk_size=1024):
file.write(chunk)
print("Starting server...")
print()
subprocess.run(
[
"java",
f"-Xms{ALLOCATED_MEMORY}M",
f"-Xmx{ALLOCATED_MEMORY}M",
"-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",
"-Dusing.aikars.flags=https://mcflags.emc.gs",
"-Daikars.new.flags=true",
"-jar",
SERVER_FILE,
"--nogui",
]
)
print()
input("Press enter to exit...")
exit(0)
def hash_file(file_name):
buffer_size = 65536
hash = hashlib.sha256()
with open(file_name, "rb") as file:
while True:
data = file.read(buffer_size)
if not data:
break
hash.update(data)
return hash.hexdigest()
if __name__ == "__main__":
os.chdir(os.path.dirname(os.path.abspath(__file__)))
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment