Skip to content

Instantly share code, notes, and snippets.

@Konicai
Created July 31, 2024 22:40
Show Gist options
  • Select an option

  • Save Konicai/b3dc6c044daae0a5ea9fc0880487dd29 to your computer and use it in GitHub Desktop.

Select an option

Save Konicai/b3dc6c044daae0a5ea9fc0880487dd29 to your computer and use it in GitHub Desktop.
Minecraft Decompilation & Deobfuscation with Vanilla Gradle
import org.spongepowered.gradle.vanilla.repository.MinecraftPlatform
import java.nio.file.Files
import java.nio.file.Path
import java.util.zip.ZipFile
plugins {
java
id("org.spongepowered.gradle.vanilla") version "0.2.1-SNAPSHOT"
}
group = "me.konicai"
version = "1.0-SNAPSHOT"
val minecraftVersion = "1.21"
val minecraftPlatform = MinecraftPlatform.JOINED
repositories {
mavenCentral()
}
minecraft {
version(minecraftVersion) // or: latestRelease() or latestSnapshot()
platform(minecraftPlatform)
}
tasks {
register<UnpackJarTask>("unpack") {
val platform = minecraftPlatform.name
val jarName = "$platform-$minecraftVersion-sources.jar"
val jar = File(System.getProperty("user.home")).resolve(".gradle/caches/VanillaGradle/v2/jars/net/minecraft/$platform/$minecraftVersion/$jarName")
sourceJar.set(jar)
destination.set(project.projectDir.resolve("src/main/java"))
ignoredEntries.set(setOf("data", "assets", "version.json", "pack.png", "flightrecorder-config.jfc"))
}
getByName("unpack").dependsOn(decompile)
}
abstract class UnpackJarTask : DefaultTask() {
@get:Optional
@get:Input
abstract val ignoredEntries: Property<Collection<String>>
@get:InputFile
abstract val sourceJar: Property<File>
@get:OutputDirectory
abstract val destination: Property<File>
@TaskAction
fun unpack() {
val jar: Path = sourceJar.get().toPath()
val dest: Path = destination.get().toPath()
project.delete(dest)
Files.createDirectories(dest)
extract(jar, dest, ignoredEntries.getOrElse(emptySet()))
println("Successfully extracted")
}
private fun extract(jar: Path, dest: Path, ignoredEntries: Collection<String>) {
ZipFile(jar.toFile()).use { archive ->
archive.stream().forEachOrdered { entry ->
val name = entry.name
for (ignored in ignoredEntries) {
if (name.startsWith(ignored)) {
return@forEachOrdered
}
}
println(name)
val entryDest = dest.resolve(name)
if (!Files.exists(entryDest.parent)) {
Files.createDirectories(entryDest.parent)
}
Files.copy(archive.getInputStream(entry), entryDest)
}
}
}
}
rootProject.name = "vg-client"
pluginManagement {
repositories {
maven("https://repo.spongepowered.org/repository/maven-public/")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment