plugins/tools/PlanetOreTransition.java
unimatrixgit b54c277568
Some checks failed
Planet Ore Profiles Build / planet-ore-profiles (push) Successful in 42s
DuneRealms Resourcepack CI / resourcepack (push) Failing after 11s
DuneRealms Plugin CI / plugins (push) Successful in 2m59s
Use vanilla height profiles for existing planet ores
2026-12-02 07:08:09 +01:00

65 lines
3.5 KiB
Java

import java.lang.classfile.ClassFile;
import java.lang.classfile.ClassTransform;
import java.lang.classfile.CodeBuilder;
import java.lang.classfile.CodeElement;
import java.lang.classfile.CodeTransform;
import java.lang.classfile.instruction.CharacterRange;
import java.lang.classfile.instruction.InvokeInstruction;
import java.lang.classfile.instruction.LocalVariable;
import java.lang.classfile.instruction.LocalVariableType;
import java.lang.constant.ClassDesc;
import java.lang.constant.MethodTypeDesc;
import java.nio.file.Files;
import java.nio.file.Path;
/** Preserve the five retained generators, replacing the private ore-placement tail only. */
class PlanetOreTransition {
private static final String PACKAGE = "com.example.duneterrain.";
private static final String GENERATOR = PACKAGE + "DuneTerrain$PlanetChunkGenerator";
private static final String CHUNK = "Lorg/bukkit/generator/ChunkGenerator$ChunkData;";
private static final String PARAMETERS = CHUNK + "Ljava/util/Random;Ljava/lang/String;";
private static final String DESCRIPTOR = "(" + PARAMETERS + "II)V";
private static final String SECTION = "Lorg/bukkit/configuration/ConfigurationSection;";
static byte[] patch(byte[] bytes) {
var format = ClassFile.of();
var original = format.parse(bytes);
if (!original.thisClass().asInternalName().equals(GENERATOR.replace('.', '/')))
throw new IllegalArgumentException("Expected retained planet generator");
var splice = new OreTail();
var updated = format.transformClass(original, ClassTransform.transformingMethodBodies(
method -> method.methodName().equalsString("placeOres") && method.methodType().equalsString(DESCRIPTOR),
splice));
if (splice.configLookups != 1) throw new IllegalArgumentException("Unexpected retained ore routine");
return updated;
}
private static final class OreTail implements CodeTransform {
private int configLookups;
@Override public void accept(CodeBuilder out, CodeElement element) {
if (element instanceof InvokeInstruction call
&& call.owner().asInternalName().equals("org/bukkit/configuration/file/FileConfiguration")
&& call.name().equalsString("getConfigurationSection")
&& call.type().equalsString("(Ljava/lang/String;)" + SECTION)) {
if (++configLookups != 1) throw new IllegalArgumentException("Multiple ore configuration lookups");
out.with(element);
out.aload(1).aload(2).aload(3).aload(0)
.getfield(ClassDesc.of(GENERATOR), "planet", ClassDesc.of("java.lang.String"))
.iload(4).iload(5)
.invokestatic(ClassDesc.of(PACKAGE + "OreHeights"), "place",
MethodTypeDesc.ofDescriptor("(" + SECTION + PARAMETERS + "Ljava/lang/String;II)V"))
.return_();
} else if (configLookups == 0 && !(element instanceof LocalVariable
|| element instanceof LocalVariableType || element instanceof CharacterRange)) {
out.with(element);
}
}
}
public static void main(String[] args) throws Exception {
if (args.length != 2) throw new IllegalArgumentException("Expected input and output class paths");
Files.write(Path.of(args[1]), patch(Files.readAllBytes(Path.of(args[0]))),
java.nio.file.StandardOpenOption.CREATE_NEW);
}
}