Download MCFA — Minecraft Mods — MetaMods

Downloads

0

Last update

6 months ago
Client
Libraries

MCFA

A small helper mod that provides other modifications with access to a powerful tool - the Java 24 Bytecode API. This library enables other mods to directly make changes to the program code of Java classes during their loading process.

How It Works?

The main functionality of MCFA is providing a convenient mechanism for working with low-level bytecode. Developers of other mods can register their transformers that are automatically applied to specified classes when they load into the game.

Here's a practical implementation example:

public class Example implements McfaInitializer {
    @Override
    public void initializeTransformers(@NotNull Mcfa registry) {
        registry.registerTransformer(
            "net.minecraft.class_310",
            ClassTransform.transformingMethods(
                MethodTransform.transformingCode(
                    (builder, element) -> {
                        var result = switch(element) {
                            case ConstantInstruction instruction -> switch(instruction.typeKind()) {
                                case REFERENCE -> {
                                    var value = instruction.constantValue();
                                    // Example of replacing a text constant
                                    if(value.equals("Setting user: {}")) {
                                        builder.ldc("This text was changed via the Bytecode API :-) {}");
                                        yield true;
                                    }

                                    yield false;
                                }

                                default -> false;
                            };

                            default -> false;
                        };
                        if(result) {
                            return;
                        }

                        builder.with(element);
                    }
                )
            )
        );
    }
}

This example demonstrates how you can replace a text constant in bytecode, changing the actual text display in the game to a custom version.

Project members
gudenau

gudenau

Created: 30 Mar 2025

ID: 97426