Skip to content

Instantly share code, notes, and snippets.

@JSH32
Created August 2, 2023 18:26
Show Gist options
  • Save JSH32/ea7606b40b2e4e5bc603892bba00f647 to your computer and use it in GitHub Desktop.
Save JSH32/ea7606b40b2e4e5bc603892bba00f647 to your computer and use it in GitHub Desktop.
Dump classes at runtime minecraft server
java
package com.yourplugin;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.*;
import java.nio.file.*;
import java.lang.instrument.*;
import java.security.ProtectionDomain;
public class ClassDumper extends JavaPlugin {
@Override
public void onEnable() {
try {
dumpClasses();
} catch (Exception e) {
e.printStackTrace();
}
}
public void dumpClasses() throws Exception {
// Create a folder in plugins directory where you will save the classes.
Path savePath = Paths.get(getDataFolder().getAbsolutePath(), "/dumpedClasses/");
Files.createDirectories(savePath);
Instrumentation instrumentation = ByteBuddyAgent.install();
Class<?>[] classes = instrumentation.getAllLoadedClasses();
for (Class<?> c : classes) {
String classFileName = c.getName().replace('.', File.separatorChar) + ".class";
Path classFilePath = Paths.get(savePath.toString(), classFileName);
Files.createDirectories(classFilePath.getParent());
ClassFileTransformer transformer = new ClassFileTransformer() {
@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) {
return classfileBuffer;
}
};
try {
instrumentation.addTransformer(transformer, true);
instrumentation.retransformClasses(c);
} finally {
instrumentation.removeTransformer(transformer);
}
Files.write(classFilePath, transformer.transform(c.getClassLoader(), c.getName(), c, c.getProtectionDomain(), ClassPool.getDefault().get(c.getName()).toBytecode()));
}
}
}
<dependencies>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.12.6</version>
<!-- Other vars. -->
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>1.12.6</version>
<!-- Other vars. -->
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.27.0-GA</version>
</dependency>
</dependencies>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment