{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "ZktjRJuZp1h6" }, "source": [ "\n", "```\n", "███╗ ███╗██╗███╗ ██╗███████╗ ██████╗ ██████╗ ██╗ █████╗ ██████╗ \n", "████╗ ████║██║████╗ ██║██╔════╝██╔════╝██╔═══██╗██║ ██╔══██╗██╔══██╗\n", "██╔████╔██║██║██╔██╗ ██║█████╗ ██║ ██║ ██║██║ ███████║██████╔╝\n", "██║╚██╔╝██║██║██║╚██╗██║██╔══╝ ██║ ██║ ██║██║ ██╔══██║██╔══██╗\n", "██║ ╚═╝ ██║██║██║ ╚████║███████╗╚██████╗╚██████╔╝███████╗██║ ██║██████╔╝\n", "╚═╝ ╚═╝╚═╝╚═╝ ╚═══╝╚══════╝ ╚═════╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝╚═════╝ \n", "```\n", "**Run a Minecraft Server on Google Colab!**\n", "\n", "---\n", "\n", "The script below will run your server. You'll have to create a server first to be able to use it - don't worry, the scripts below will do the majority of the work for you. You might also want to change the default region to your region, check below." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "lqtMoYg0dUmu" }, "outputs": [], "source": [ "import os\n", "import re\n", "import json\n", "\n", "# Update the package lists\n", "!sudo apt update &>/dev/null && echo \"apt cache successfully updated\" || echo \"apt cache update failed, you might receive stale packages\"\n", "# Install OpenJDK 17\n", "# !wget -qO - https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public | sudo apt-key add -\n", "# !sudo add-apt-repository --yes https://adoptopenjdk.jfrog.io/adoptopenjdk/deb/ &>/dev/null || echo \"Failed to add repo. Still can be ignored if openjdk17 gets installed.\"\n", "!sudo apt-get install openjdk-17-jre-headless &>/dev/null && echo \"Yay! Openjdk17 has been successfully installed.\" || echo \"Failed to install OpenJdk17.\"\n", "#Perform java version check\n", "java_ver = !java -version 2>&1 | awk -F[\\\"\\.] -v OFS=. 'NR==1{print $2}'\n", "if java_ver[0] == \"17\" :\n", " print(\"Openjdk17 is working correctly, you are good to go.\")\n", "else:\n", " print(\"Openjdk17 doesn't seems to be installed or isn't working, falling back to java\", java_ver[0], \". You might experience reduced performance. Minecraft 1.17 and above might fail to launch.\")\n", "\n", "# Mount Google Drive\n", "from google.colab import drive\n", "drive.mount('/content/drive')\n", "# Change directory to the Minecraft server folder on Google Drive\n", "%cd \"/content/drive/My Drive/Minecraft-server\"\n", "!ls #list the directory contents (to verify that working directory was changed)\n", "\n", "# Import config file.\n", "if os.path.isfile(\"colabconfig.json\"):\n", " colabconfig = json.load(open(\"colabconfig.json\"))\n", "else:\n", " colabconfig = {\"server_type\": \"generic\"} # using default, if config doesn't exists.\n", " json.dump(colabconfig, open(\"colabconfig.json\",'w'))\n", "\n", "# Server jar names.\n", "jar_list = {'paper': 'server.jar', 'fabric': 'fabric-server-launch.jar', 'generic': 'server.jar', 'forge': 'server.jar'}\n", "jar_name = jar_list[colabconfig[\"server_type\"]]\n", "\n", "# Java arguments.\n", "if colabconfig[\"server_type\"] == \"paper\":\n", " server_flags = \"-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\"\n", "else:\n", " server_flags = \"\" # aiker's flags might negatively impact performance on non-paper servers.\n", "memory_allocation = \"-Xmx6144M -Xms6144M\"\n", "\n", "# Chose the tunnle service you want to use\n", "# Available options: ngrok, argo, playit\n", "tunnel_service = \"argo\"\n", "print(\"Procedding to use\", tunnel_service)\n", " \n", "\n", "if tunnel_service == \"ngrok\":\n", " !pip -q install pyngrok\n", " from pyngrok import conf, ngrok\n", " \n", " # Ask for the ngrok authtoken\n", " print(\"Get your authtoken from https://dashboard.ngrok.com/auth\")\n", " import getpass\n", " authtoken = getpass.getpass() # input your Ngrok auth token everytime you run the cell or simply replace \"getpass.getpass()\" with your token in \"double quotes\"\n", " ! ngrok authtoken $authtoken # login to ngrok\n", "\n", " # Sets default ngrok region\n", " conf.get_default().region = 'us' # Change this to whichever region you want\n", "\n", " # Connect to ngrok\n", " url = ngrok.connect(25565, 'tcp')\n", " print('Your server address is ' + ((str(url).split('\"')[1::2])[0]).replace('tcp://', ''))\n", "\n", " print('Starting server...')\n", " !java $memory_allocation $server_flags -jar $jar_name nogui\n", "\n", "elif tunnel_service == \"playit\":\n", " ! curl -SsL https://playit-cloud.github.io/ppa/key.gpg | sudo apt-key add -\n", " ! sudo curl -SsL -o /etc/apt/sources.list.d/playit-cloud.list https://playit-cloud.github.io/ppa/playit-cloud.list\n", " ! sudo apt update &>/dev/null && sudo apt install playit &>/dev/null && echo \"Playit.gg installed\" || echo \"Failed to install playit\"\n", " print('Starting server...')\n", " ! playit & java $memory_allocation $server_flags -jar $jar_name nogui\n", "\n", "else:\n", " # Download & make argo executable\n", " !wget https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64\n", " !chmod +x cloudflared-linux-amd64\n", " print('Starting server...')\n", " !./cloudflared-linux-amd64 tunnel --url tcp://127.0.0.1:25565 & java $memory_allocation $server_flags -jar $jar_name nogui\n" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "_4IowSDVUZp8" }, "source": [ "# Make-a-Server\n", "\n", "The code below will download a server for you and accept the EULA. After running these scripts, your server will be ready to run." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "7wrNfinQOlWV" }, "source": [ "**Download the Minecraft server**\n", "\n", "The code below will download Paper, a high-performance fork of the Vanilla server.\n", "Other server platforms can be used by placing the server.jar in the Drive folder manually." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Fy5-YjJMPV3S" }, "outputs": [], "source": [ "# Replace \"1.19.2\" with your desired server version.\n", "# Available versions:\n", "# - 1.19.2\n", "# - 1.19.1\n", "# - 1.19\n", "# - 1.18.2\n", "# - 1.18.1\n", "# - 1.18\n", "# - 1.17.1\n", "# - 1.17\n", "# - 1.16.4\n", "# - 1.16.3\n", "# - 1.15.2\n", "# - 1.14.4\n", "# - 1.13.2\n", "# - 1.12.2\n", "# - 1.11.2\n", "# - 1.10.2\n", "# - 1.9.4\n", "# - 1.8.8\n", "# Newer versions might work too, however this isn't guaranteed.\n", "version = '1.19.2'\n", "forge_version = \"43.2.8\" # If you are going to use forge \n", "\n", "#Chose server type. Currently available versions: fabric, paper, forge\n", "server_type = 'paper'\n", "\n", "from google.colab import drive\n", "import requests\n", "import json\n", "\n", "\n", "drive.mount('/content/drive')\n", "\n", "! mkdir \"/content/drive/My Drive/Minecraft-server\"\n", "%cd \"/content/drive/My Drive/Minecraft-server\"\n", "\n", "if server_type == \"paper\":\n", " a = requests.get(\"https://papermc.io/api/v2/projects/paper/versions/\" + version)\n", " b = requests.get(\"https://papermc.io/api/v2/projects/paper/versions/\" + version + \"/builds/\" + str(a.json()[\"builds\"][-1]))\n", " print(\"https://papermc.io/api/v2/projects/paper/versions/\" + version + \"/builds/\" + str(a.json()[\"builds\"][-1]) + \"/downloads/\" + b.json()[\"downloads\"][\"application\"][\"name\"])\n", "\n", " url = \"https://papermc.io/api/v2/projects/paper/versions/\" + version + \"/builds/\" + str(a.json()[\"builds\"][-1]) + \"/downloads/\" + b.json()[\"downloads\"][\"application\"][\"name\"]\n", "\n", " jar_name = \"server.jar\"\n", "print('Downloading to Google Drive...')\n", "elif server_type == \"fabric\":\n", " url = \"https://maven.fabricmc.net/net/fabricmc/fabric-installer/0.7.4/fabric-installer-0.7.4.jar\"\n", " jar_name = \"fabric-installer.jar\"\n", "elif server_type == \"forge\":\n", " url = f\"https://maven.minecraftforge.net/net/minecraftforge/forge/{version}-{forge_version}/forge-{version}-{forge_version}-installer.jar\"\n", " jar_name = \"server.jar\"\n", "\n", "\n", "r = requests.get(url)\n", "\n", "if r.status_code is 200:\n", " with open('/content/drive/My Drive/Minecraft-server/' + jar_name, 'wb') as f:\n", " f.write(r.content)\n", "else:\n", " print('Error '+ str(r.status_code) + '! Most likely you entered a unsupported version. Try running the code again if you think that shouldn\\'t have happened.')\n", "\n", "# Running specific install path.\n", "if server_type == \"fabric\":\n", " !java -jar fabric-installer.jar server -mcversion $version -downloadMinecraft\n", "elif server_type == \"forge\":\n", " !java -jar server.jar --installServer\n", "\n", "colabconfig = {\"server_type\": server_type}\n", "json.dump(colabconfig, open(\"colabconfig.json\",'w'))\n", "\n", "print('Done!')\n" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "vhHYrg6FlbcQ" }, "source": [ "**Automatically accept the EULA**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "aefCB6cFlle8" }, "outputs": [], "source": [ "# Please read the file stored in your server folder before running this command. \n", "# Also, go to https://www.minecraft.net/en-us/eula to read Minecraft's EULA.\n", "\n", "# Make sure Drive is mounted\n", "from google.colab import drive\n", "drive.mount('/content/drive')\n", "\n", "%cd \"/content/drive/My Drive/Minecraft-server\"\n", "!echo \"eula=true\" >> eula.txt" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "FO5S4OHVdV5O" }, "source": [ "# Debug\n", "SSH access to host OS - Thanks to [colab-ssh](https://github.com/WassimBenzarti/colab-ssh).\n", "\n", "
YOU MIGHT GET BANNED
" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "cM7gKp6Yi8Q2" }, "outputs": [], "source": [ "#@title Colab-ssh tunnel\n", "#@markdown Execute this cell to open the ssh tunnel. Check [colab-ssh documentation](https://github.com/WassimBenzarti/colab-ssh) for more details.\n", "\n", "# Install colab_ssh on google colab\n", "!pip install colab_ssh --upgrade\n", "\n", "from colab_ssh import launch_ssh_cloudflared, init_git_cloudflared\n", "ssh_tunnel_password = \"