Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LDC constant helper methods #51

Merged

Conversation

Jonathing
Copy link
Member

@Jonathing Jonathing commented Oct 11, 2024

This PR aims to address the pains of dealing with LDC constant instructions.

Will remain as draft until I test these additions myself.

@Jonathing Jonathing marked this pull request as draft October 11, 2024 04:24
@Jonathing
Copy link
Member Author

I have tested this PR in Forge and it is ready for a proper review. Here's what I used to test this:

CoreMod:

var ASMAPI = Java.type('net.minecraftforge.coremod.api.ASMAPI');
var Opcodes = Java.type('org.objectweb.asm.Opcodes');

function initializeCoreMod() {
    return {
        'thunderHit_ldc_test': {
            'target': {
                'type': 'METHOD',
                'class': 'net.minecraft.world.entity.Entity',
                'methodName': 'thunderHit',
                'methodDesc': '(Lnet/minecraft/server/level/ServerLevel;Lnet/minecraft/world/entity/LightningBolt;)V'
            },
            'transformer': thunderHit
        }
    };
}

function thunderHit(method) {
    ASMAPI.log('INFO', 'HELLO! ENTERING LDC TEST');

    var insn = ASMAPI.findFirstInstruction(method, Opcodes.LDC);

    ASMAPI.log('INFO', 'LDC instruction object class: {}', ASMAPI.ldcInsnClassToString(insn));
    ASMAPI.log('INFO', 'LDC instruction object value: {}', insn.cst);

    ASMAPI.log('INFO', 'SETTING NEW LDC! SETTING VALUE TO 12.0F');

    var newInsn = ASMAPI.buildNumberLdcInsnNode(12.0, ASMAPI.LdcNumberType.FLOAT);
    method.instructions.set(insn, newInsn);

    ASMAPI.log('INFO', 'NEW LDC instruction object class: {}', ASMAPI.ldcInsnClassToString(newInsn));
    ASMAPI.log('INFO', 'NEW LDC instruction object value: {}', newInsn.cst);

    return method;
}

RESULTING LOG MESSAGES:

[11:03:29] [pool-5-thread-1/INFO] [ne.mi.co.Co.forge/COREMODLOG]: HELLO! ENTERING LDC TEST
[11:03:29] [pool-5-thread-1/INFO] [ne.mi.co.Co.forge/COREMODLOG]: LDC instruction object class: class java.lang.Float
[11:03:29] [pool-5-thread-1/INFO] [ne.mi.co.Co.forge/COREMODLOG]: LDC instruction object value: 8.0
[11:03:29] [pool-5-thread-1/INFO] [ne.mi.co.Co.forge/COREMODLOG]: SETTING NEW LDC! SETTING VALUE TO 12.0F
[11:03:29] [pool-5-thread-1/INFO] [ne.mi.co.Co.forge/COREMODLOG]: NEW LDC instruction object class: class java.lang.Float
[11:03:29] [pool-5-thread-1/INFO] [ne.mi.co.Co.forge/COREMODLOG]: NEW LDC instruction object value: 12.0

This example changes the time set in fire from thunder hit from 8 to 12.

@Jonathing Jonathing marked this pull request as ready for review October 13, 2024 15:05
@PaintNinja PaintNinja self-requested a review October 13, 2024 15:41
@LexManos LexManos merged commit deac494 into MinecraftForge:master Oct 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Would like a way to create LDC with a float value No way to check type of LDC?
3 participants