Created
October 10, 2022 00:56
-
-
Save tinytengu/df1a5636a3780d4c65924c3007d59e25 to your computer and use it in GitHub Desktop.
Python script to launch Minecraft on linux from terminal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
import subprocess | |
DIRNAME = os.path.join(os.path.dirname(__file__), ".minecraft") | |
LIBRARIES = os.path.join(DIRNAME, "libraries") | |
VERSIONS = os.path.join(DIRNAME, "versions") | |
VERSION = "1.12.2" | |
def get_os_name() -> str: | |
match os.name: | |
case "nt": | |
return "windows" | |
case "posix": | |
return "linux" | |
case _: | |
return "osx" | |
def splitlib(libname: str) -> list[str]: | |
tokens = libname.split(":") | |
if len(tokens) == 0: | |
return [] | |
if "." in tokens[0]: | |
return tokens[0].split(".") + tokens[1:] | |
return tokens | |
def get_lib_path(tokens: list[str], native_prefix: str = None) -> str: | |
return "%s/%s%s.jar" % ( | |
"/".join(tokens), | |
"-".join(tokens[-2:]), | |
(f"-{native_prefix}" if native_prefix else ""), | |
) | |
# | |
def get_libs(filepath: str, os_name: str) -> list[str]: | |
libs = [] | |
with open(filepath) as file: | |
schema = json.load(file) | |
for lib in schema["libraries"]: | |
libname = lib["name"] | |
native_prefix = ( | |
lib["natives"][os_name] | |
if "natives" in lib and os_name in lib["natives"] | |
else "" | |
) | |
libpath = os.path.join( | |
LIBRARIES, get_lib_path(splitlib(libname), native_prefix=native_prefix) | |
) | |
libs.append(libpath) | |
return libs | |
def main(): | |
os_name = get_os_name() | |
libs = [] | |
libs.extend( | |
get_libs(os.path.join(VERSIONS, f"{VERSION}/{VERSION}.json"), os_name=os_name) | |
) | |
# Extra from 1.12.2-forge1.12.2-14.23.5.2838 | |
libs.extend( | |
get_libs( | |
os.path.join( | |
VERSIONS, | |
"1.12.2-forge1.12.2-14.23.5.2838/1.12.2-forge1.12.2-14.23.5.2838.json", | |
), | |
os_name=os_name, | |
) | |
) | |
libs.append(os.path.join(VERSIONS, VERSION, f"{VERSION}.jar")) | |
args = [ | |
"java", | |
"-Xmx512M", | |
"-Xmx1G", | |
f"-Djava.library.path={DIRNAME}/versions/{VERSION}/natives", | |
"-cp", | |
":".join(libs), | |
f"-Dminecraft.applet.TargetDirectory={DIRNAME}", | |
"-XX:+UseConcMarkSweepGC", | |
"-Dfml.ignoreInvalidMinecraftCertificates=true", | |
"-Dfml.ignorePatchDiscrepancies=true", | |
"net.minecraft.launchwrapper.Launch", | |
"--username", | |
"tinytengu", | |
"--version", | |
"1.12.2", | |
"--gameDir", | |
DIRNAME, | |
"--assetsDir", | |
f"{DIRNAME}/assets", | |
"--assetIndex", | |
"1.12", | |
"--uuid", | |
"17d6b4af8-481a-11ed-ad98-0538d0a6bd5c", | |
"--userType", | |
"legacy", | |
"--accessToken", | |
"accessToken", | |
"--tweakClass", | |
"net.minecraftforge.fml.common.launcher.FMLTweaker", | |
"--versionType", | |
"Forge", | |
"--width", | |
"800", | |
"--height", | |
"600", | |
] | |
print(" ".join(args)) | |
subprocess.run(args) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment