File size: 14,872 Bytes
1b97c1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
{
  "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",
        "<p style=\"color:red;\">YOU MIGHT GET BANNED</p>"
      ]
    },
    {
      "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 = \"<PUT_YOUR_PASSWORD_HERE>\" #@param {type: \"string\"}\n",
        "launch_ssh_cloudflared(password=ssh_tunnel_password)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "7kR6rsn7jyxt"
      },
      "outputs": [],
      "source": [
        "#Get public address (ngrok)\n",
        "! curl -s http://localhost:4040/api/tunnels | python3 -c \\\n",
        "    \"import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Z2rztsCQk0kh"
      },
      "outputs": [],
      "source": [
        "## For inspecting the minecraft server directory ##\n",
        "%cd \"/content/drive/My Drive/Minecraft-server\"\n",
        "!ls\n"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "collapsed_sections": [],
      "name": "MineColab",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3 (ipykernel)",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.4"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}