VOOZH about

URL: https://minecraft.wiki/w/Tutorial:See_Minecraft's_code

โ‡ฑ Tutorial: See Minecraft's code โ€“ Minecraft Wiki


Tutorial: See Minecraft's code

From Minecraft Wiki
Jump to navigation Jump to search
Script warning
 
The Minecraft Wiki is a website that can be edited by anyone. Although edits are usually checked by other wiki editors, it is possible for a malicious user to modify scripts and programs to behave maliciously. It is suggested that you should check the integrity of any script or program you run on your system.
The contents of this page are not supported by Mojang Studios or the Minecraft Wiki.
 
This tutorial is exclusive to Java Edition.
 

The main game executable of Minecraft: Java Edition is a client.jar file containing Java bytecode designed to be ran by Java Virtual Machine (JVM). Because of how Java is compiled, there is enough information stored in the bytecode which makes it possible to turn it back into source code. This process is called decompilation (as oppose to compilation). However, the resulting decompiled code may be obfuscated, which contains cryptic class or method names that are meaningless. To fix this, it must undergo deobfuscation through JAR remapping, which is a process for the JAR file that replaces obfuscated class names and member names with their readable (deobfuscated) names provided from an obfuscation map. This process is usually done before decompiling the JAR file to produce a human-readable Java source code.

Historically, the game has been reverse-engineered and well understood by the community, that custom obfuscation maps are typically provided for use in developing mods.[1] However, since the release of Java Edition 19w36a, Mojang Studios have released official obfuscation maps for versions of Minecraft since Java Edition 1.14.4. It's safe to assume these obfuscation maps contain the names written during the development. When these maps are used in tandem with decompilation, the game's bytecode can be successfully reconstructed into human-readable Java source code. Since Java Edition 26.1 (Snapshot 1), the game has been deobfuscated by default, but will still need to be decompiled in order to see the readable source code.[2]

There are many benefits of seeing the game's source code, one of them is that you can analyze how the game's various mechanics work directly by seeing the code, which contains instructions and rules making them up. The source code of the game is also heavily involved and used in developing mods, an unofficial extension of the game typically made by the community that adds new content, progression, and mechanics.

Legal disclaimer

[edit | edit source]

If you use the official obfuscation mapping from Mojang Studios, note that there is additional license and terms you must follow. According to its license, decompiling Minecraft is allowed, but it is recommended that one does not release decompiled code. Furthermore, a Twitter post from Dinnerbone suggests usage of the mappings inside of projects by the community, such as modding (using them to decompile Minecraft for usage with Forge, NeoForge, Fabric, Quilt and other mod loaders) or for usage in server software with plugins (such as Spigot, Bukkit, Paper, and others) is okay, and an intended use. It should also be noted however that this post is not legally withstanding, so while it is probably fine to use these mappings for such purposes, you do so at your own risk. This can be extended to decompiling and accessing Minecraft's code as a whole; the only things you should not do under any circumstances are releasing the code or releasing something that contains an exact copy of the code.

Decompiling JAR

[edit | edit source]
Script warning
 
The Minecraft Wiki is a website that can be edited by anyone. Although edits are usually checked by other wiki editors, it is possible for a malicious user to modify scripts and programs to behave maliciously. It is suggested that you should check the integrity of any script or program you run on your system.

Although the game has been deobfuscated for modern versions (and the official obfuscation map provided for older versions), Mojang Studios does not provide an official documentation nor tutorial to decompile the game and actually see the source code itself. Instead, you would need to use a community tools designed to process the obfuscation map (remapping) and/or decompile the game into a human-readable Java source code. There are numerous ways to do this, all of them vary on the technical levels.

JAR-based tools, such as Vineflower and MinecraftDecompiler, require Java to run, see Tutorial:Update Java ยง How to download to set it up. They may also only be used as a command-line interface, which you need a terminal provided by your system (on Windows, this is Command Prompt or Windows Terminal; on macOS or Linux, it's typically provided by the same name, Terminal).

mcsrc.dev

[edit | edit source]

mcsrc.dev is a web-based tool made by FabricMC authors for viewing the Minecraft's game source code directly inside a web browser. It works by downloading the game's JAR file from Mojang's servers, and decompile the game directly inside the user's web browser using Vineflower. This results in the game's source code being easily accessible as a website.

The website lets you select Minecraft versions from the latest versions down to Java Edition 25w45a. For older versions, you may want to use other methods, such as manually decompiling the JAR using third-party software below.

Vineflower

[edit | edit source]
๐Ÿ‘ Image
The Vineflower project

Vineflower is a modern standalone Java decompiler made by Vineflower authors. It's used to decompile the game into a series of readable Java source code organized with their package structure intact. However, it cannot be used to deobfuscate, and when used on an obfuscated game version, it will produce Java source code with ambiguous cryptic names. It's recommended to use the deobfuscated game versions from Java Edition 26.1, and for older versions, you'd need to manually deobfuscate the game using a JAR remapper tool before decompiling. This process is automatically provided by MinecraftDecompiler, which deobfuscate and decompile with Vineflower, and other decompilers.

Vineflower is derived from Fernflower, the original decompiler made by Jetbrains, and well as numerous community forks, such as MinecraftForge's ForgeFlower, FabricMC's fork of Fernflower, and a direct continuation of Quiltflower. Compared to other Java decompilers, Vineflower often produce better result, even so close to the source code itself, see the output comparison.

To use Vineflower, you need to download the latest JAR file release from the Github release page. You will also need to retrieve the client.jar file (the game's main program) from the .minecraft directory under .minecraft/versions/<version>/<version>.jar. Typically, you would need to launch the specific game version before in order for the Minecraft Launcher to download the JAR file. The server.jar may also be used to generate the source code, however, it only contain server specific codes, excluding entity rendering, model resources, and other client features. Additionally, both JAR files are available on the wiki's version pages, see Category:Java Edition versions.

After doing the prerequisites, open the terminal and run the JAR file with the following command:

java-jarvineflower.jar26.2.jarout

This will produce out directory containing readable Java source code files. The vineflower.jar might be different depending on the version. Additionally, you can run java -jar vineflower.jar --help to see all available options, or read the usage documentation.

MinecraftDecompiler

[edit | edit source]

MinecraftDecompiler is a JAR tool made by MaxPixelStudios that can generate the game's source code from various obfuscation mappings and custom decompilers from the community, and the official Mojang mapping. Unlike a standalone decompiler (like Vineflower), this tool also processes obfuscation map to deobfuscate the game before decompiling. At the moment, it only supports obfuscated game versions with official Mojang mapping from Java Edition 1.14.4 up to Java Edition 1.21.11.[3]

To use MinecraftDecompiler, you need to download the latest JAR file release from the GitHub release page. After downloading, open a terminal in the same directory as the JAR file, and run the JAR file with Java by typing the following command:

java-jarMinecraftDecompiler.jar--version1.21.11--sideSERVER--decompile--output1.21.11-remapped.jar--decompiled-output1.21.11-decompiled

This will write a mapped version of the server.jar to 1.21.11-remapped.jar, as well as decompiling the game and writing the generated source files to 1.21.11-decompiled. The following arguments for the MinecraftDecompiler software are used:

--version <version>
This specifies which version of the game you want to decompile. MinecraftDecompiler will automatically fetch the game's executable files from the specified version.
--side <environment>
This specifies which environment you want to decompile. There are two environment you can specify: CLIENT and SERVER. The client environment contains the entire game you're playing, while the server only contains the resources necessary to host a multiplayer game. The client includes the entire server code, plus client specific, like rendering, user interface, etc.
--decompile [<decompiler>]
This specifies that you want to decompile the game, usually after deobfuscating the JAR file. Optionally, you can also specify which decompiler you want to use. By default, it uses vineflower, but you can use other decompilers: fernflower, forgeflower, cfr, or user-defined.
--output <mapped-jar-output>
This specifies the output file for the deobfuscated JAR file (or mapped JAR file) from the obfuscation map.
--decompiled-output <decompilation-directory>
This specifies the output directory for the decompilation files.

Additionally, you can run java -jar MinecraftDecompiler.jar --help to see all available options.

Using built-in IDE functions

[edit | edit source]

If you prefer to download as little additional software as possible, you can also view Minecraft's source code using built-in IDE functions from IntelliJ IDEA, Eclipse, or Visual Studio Code. They let you view an artifact, which contains the game's source code, generated from a mod project (i.e., creating a Fabric mod) for development purpose; such project has a build configuration (like one with Gradle) that specifically does this.

You can start by getting the template of a Fabric example mod project from GitHub, and opening it in your IDE of choice.

Next, you will have to ensure that the version you want to find the source for is selected. If you want to use the latest stable version, i.e. 26.2, you may skip this step, as the example mod will almost always be up-to-date. Otherwise, navigate here, select your chosen version in the first dropdown menu, and update your gradle.properties file accordingly.

Once you have completed this step, open a command line window. If this window is in the IDE, run gradlew gensources, or ./gradlew gensources if you are on Linux or Mac. However, if this window is separate from your IDE, remember to run cd path/to/mod_folder/ first.

Now, return to your IDE window and follow the steps below, depending on the IDE you use:

  • Visual Studio Code: /โ€ฏ+โ€ฏ, and begin typing possible class names with the # prefix
  • IntelliJ IDEA: โ€ฏ+โ€ฏ or โ€ฏ+โ€ฏ, and begin typing possible class names
  • Eclipse: /โ€ฏ+โ€ฏโ€ฏ+โ€ฏ, and begin typing possible class names

Legacy Yarn (pre-1.14.4)

[edit | edit source]
This section is a work in progress.
 
Please help expand and improve it. The talk page may contain suggestions.

Mojang Studios have only released official mapping from Java Edition 1.14.4 onward, any versions older require a community mapping in order to be deobfuscated. This tutorial will use Legacy Yarn by Legacy Fabric, which is a community mapping for versions from Java Edition 1.13.2 down to Java Edition 1.3 with several snapshots. It's used to remap the JAR files through Tiny Remapper with the intermediary mapping, and the resulting remapped JAR file is then decompiled with Vineflower to produce readable source code.

Prerequisites

[edit | edit source]

To use Legacy Yarn and decompile the game, you'll need the following resources:

  • the game's JAR file to be decompiled, typically client.jar or server.jar;
  • the Legacy Yarn mapping;
  • the JAR remapper tool, which is Tiny Remapper; and
  • the decompiler, which is Vineflower.

Obtaining

[edit | edit source]

Legacy Yarn provides many obfuscation maps for various legacy versions of the game, which are listed on the Legacy Fabric's Maven repository. Each directory is named like <version>+build.<build>, where version is the game's version, and the build is a unique build ID for the mapping. Open the directory with the target version, and the highest build number for the newest mapping provided.

In the directory, you'll need to find a file that ends with -mergedv2.jar (e.g., for 1.8.9 build 604, it's yarn-1.8.9+build.604-mergedv2.jar). This JAR archive file can be opened with any archival software/tool (such as 7-Zip), and contains the mapping file required for Tiny Remapper. It's stored inside them as mappings/mappings.tiny. As such, you'll need to extract them in the current directory, preferably next to Vineflower and Tiny Remapper software. We'll reference this mapping as mergedv2.tiny later on.

In order to get Tiny Remapper, you'll need to find a JAR file from FabricMC's Maven repository. The listing contains multiple versions for Tiny Remapper, you'll need the one with the latest release. Upon opening the directory, download the fat version (e.g., tiny-remapper-<x.y.z>-fat.jar).

The client.jar or server.jar, which contains the program and resources for the game, can be found on .minecraft/versions. This is the game's JAR file that gets to be remapped and decompiled later with Tiny Remapper and Vineflower. Older versions of the JAR file can typically be found on the wiki's version pages.

Remapping

[edit | edit source]

Legacy Yarn uses three namespaces for mapping:

  • official โ€“ the obfuscated names in the original game JAR file.
  • intermediary โ€“ a stable intermediate names from Legacy Fabric.
  • named โ€“ the readable Legacy Yarn names.

These namespaces are used to differentiate which names of the mapping come from, especially for intermediary and named. Before the official names get remapped into the named, it has to go to intermediary names first. This is because the named mapping are mapped from the intermediary names, which are preferably because they considered stable (unlikely to change) compared to the official (obfuscated) names, and can be referenced across multiple Minecraft versions.[4]

Luckily for our case, the mergedv2.tiny mapping already contain both intermediary and named mapping combined, so we don't need to manually remap to intermediary first. Otherwise, there is the non-merged version which requires to do so with the two mappings.

After setting up the tiny map file and tools, run the following command in the current directory:

java-jartiny-remapper-x.y.z-fat.jarclient.jarclient-named.jarmergedv2.tinyofficialnamed

Once the remapping step is complete, decompile client-named.jar or server-named.jar with Vineflower in the usual way.

References

[edit | edit source]

Navigation

[edit | edit source]
Retrieved from "https://minecraft.wiki/w/Tutorial:See_Minecraft%27s_code?oldid=3645873"

Navigation menu