Last active
September 1, 2024 14:18
-
-
Save runekaagaard/e8acc8cc9299f0a2eea9008f89ba7589 to your computer and use it in GitHub Desktop.
Linux script to start java minecraft with a custom username directly without running the launcher
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
#!/usr/bin/env bash | |
# Function to display help message | |
show_help() { | |
cat << EOF | |
Usage: minecraft-start [options] <username> | |
Launch Minecraft with the specified username. | |
Arguments: | |
username The Minecraft username to use | |
Options: | |
-h, --help Show this help message and exit | |
Requirements: | |
- jq must be installed (sudo apt-get install jq) | |
- Minecraft must be installed in the default location (~/.minecraft) | |
Example: | |
minecraft-start MyMinecraftUser | |
This script launches Minecraft using the latest installed release version. | |
It automatically detects the appropriate Java runtime and natives directory. | |
EOF | |
} | |
# Check if jq is installed | |
if ! command -v jq &> /dev/null; then | |
echo "Error: jq is not installed. Please install it (e.g., sudo apt-get install jq) and try again." >&2 | |
exit 1 | |
fi | |
# Initialize variables | |
USERNAME="" | |
SHOW_HELP=false | |
# Parse arguments | |
while [[ $# -gt 0 ]]; do | |
case $1 in | |
-h|--help) | |
SHOW_HELP=true | |
shift | |
;; | |
*) | |
if [ -z "$USERNAME" ]; then | |
USERNAME="$1" | |
else | |
echo "Error: Unexpected argument '$1'" >&2 | |
echo "Use 'minecraft-start --help' for more information." >&2 | |
exit 1 | |
fi | |
shift | |
;; | |
esac | |
done | |
# Show help if requested or if no username provided | |
if $SHOW_HELP; then | |
show_help | |
exit 0 | |
fi | |
if [ -z "$USERNAME" ]; then | |
echo "Error: Please provide a username as an argument." >&2 | |
echo "Use 'minecraft-start --help' for more information." >&2 | |
exit 1 | |
fi | |
MINECRAFT_DIR="$HOME/.minecraft" | |
# Function to find the latest release Minecraft version | |
find_latest_version() { | |
local manifest_file="$MINECRAFT_DIR/versions/version_manifest_v2.json" | |
if [ ! -f "$manifest_file" ]; then | |
echo "Error: version_manifest_v2.json not found!" >&2 | |
exit 1 | |
fi | |
jq -r '.latest.release' "$manifest_file" | |
} | |
# Get the latest release version | |
VERSION=$(find_latest_version) | |
echo "Using Minecraft version: $VERSION" | |
# Create the custom log4j2 configuration file | |
cat > "${MINECRAFT_DIR}/custom_log4j2.xml" << EOL | |
<?xml version="1.0" encoding="UTF-8"?> | |
<Configuration status="WARN"> | |
<Appenders> | |
<Console name="Console" target="SYSTEM_OUT"> | |
<PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/> | |
</Console> | |
<File name="File" fileName="minecraft_custom.log"> | |
<PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/> | |
</File> | |
</Appenders> | |
<Loggers> | |
<Root level="info"> | |
<AppenderRef ref="Console"/> | |
<AppenderRef ref="File"/> | |
</Root> | |
</Loggers> | |
</Configuration> | |
EOL | |
# Dynamically build the classpath | |
CP=$(find "${MINECRAFT_DIR}/libraries" "${MINECRAFT_DIR}/versions/${VERSION}" -name "*.jar" | tr '\n' ':') | |
# Remove the trailing colon | |
CP=${CP%:} | |
# Find the Java runtime | |
JAVA_RUNTIME=$(find "$MINECRAFT_DIR/runtime" -name "java" | head -n 1) | |
if [ -z "$JAVA_RUNTIME" ]; then | |
echo "Java runtime not found in Minecraft directory. Using system Java." | |
JAVA_RUNTIME="java" | |
fi | |
# Find the natives directory | |
NATIVES_DIR=$(find "${MINECRAFT_DIR}/bin" -type d -exec sh -c 'ls -1 "$0"/*.so 2>/dev/null | grep -q .' {} \; -print | head -n 1) | |
if [ -z "$NATIVES_DIR" ]; then | |
echo "Warning: Natives directory not found. The game may not work correctly." >&2 | |
NATIVES_DIR="${MINECRAFT_DIR}/bin" | |
fi | |
echo "Using natives directory: ${NATIVES_DIR}" | |
"$JAVA_RUNTIME" \ | |
-Xmx2G -Xms2G \ | |
-Djava.library.path="${NATIVES_DIR}" \ | |
-Dlog4j.configurationFile="${MINECRAFT_DIR}/custom_log4j2.xml" \ | |
-cp "${CP}" \ | |
net.minecraft.client.main.Main \ | |
--username "${USERNAME}" \ | |
--version ${VERSION} \ | |
--gameDir ${MINECRAFT_DIR} \ | |
--assetsDir ${MINECRAFT_DIR}/assets \ | |
--assetIndex 17 \ | |
--accessToken 0 \ | |
--userProperties {} \ | |
--userType legacy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This bash script provides a flexible and user-friendly way to launch Minecraft from the command line. It automatically detects the latest installed Minecraft release version and sets up the necessary environment for launching the game.
Key features:
Requirements:
Usage:
./minecraft-start [options]
Example:
./minecraft-start MyMinecraftUser