var Opcodes = Java.type('org.objectweb.asm.Opcodes')
var VarInsnNode = Java.type('org.objectweb.asm.tree.VarInsnNode')
var MethodInsnNode = Java.type('org.objectweb.asm.tree.MethodInsnNode')

function initializeCoreMod() {
    return {
        'Patch brush item': {
            'target': {
                "type":"METHOD",
                "class":"net.minecraft.world.item.BrushItem",
                "methodName":"m_5929_",
                "methodDesc":"(Lnet/minecraft/world/level/Level;Lnet/minecraft/world/entity/LivingEntity;Lnet/minecraft/world/item/ItemStack;I)V"
            },
            'transformer': function(node) {
                for (i = 0; i < node.instructions.size(); i++) {
                    var insn = node.instructions.get(i)

                    if (insn.getOpcode() == Opcodes.BIPUSH) {
                        node.instructions.insert(insn, new MethodInsnNode(
                            Opcodes.INVOKESTATIC,
                            'ru/dbotthepony/mc/otm/android/feature/LimbOverclockingFeature',
                            'getBrushCooldown',
                            '(Lnet/minecraft/world/entity/LivingEntity;)I'
                        ))
                        node.instructions.set(insn, new VarInsnNode(Opcodes.ALOAD, 2))

                        i += 1
                        continue
                    }

                    if (insn.getOpcode() == Opcodes.ICONST_5) {
                        node.instructions.insert(insn, new MethodInsnNode(
                            Opcodes.INVOKESTATIC,
                            'ru/dbotthepony/mc/otm/android/feature/LimbOverclockingFeature',
                            'getBrushTick',
                            '(Lnet/minecraft/world/entity/LivingEntity;)I'
                        ))
                        node.instructions.set(insn, new VarInsnNode(Opcodes.ALOAD, 2))

                        i += 1
                        continue
                    }
                }

                return node
            }
        },
        'Patch brushable block': {
            'target': {
                "type":"METHOD",
                "class":"net.minecraft.world.level.block.entity.BrushableBlockEntity",
                "methodName":"m_276923_",
                "methodDesc":"(JLnet/minecraft/world/entity/player/Player;Lnet/minecraft/core/Direction;)Z"
            },
            'transformer': function(node) {
                for (i = 0; i < node.instructions.size(); i++) {
                    var insn = node.instructions.get(i)

                    if (insn.getOpcode() == Opcodes.LDC && insn.cst == 10) {
                        node.instructions.insert(insn, new MethodInsnNode(
                            Opcodes.INVOKESTATIC,
                            'ru/dbotthepony/mc/otm/android/feature/LimbOverclockingFeature',
                            'getBrushableBlockCooldown',
                            '(Lnet/minecraft/world/entity/player/Player;)J'
                        ))
                        node.instructions.set(insn, new VarInsnNode(Opcodes.ALOAD, 3))

                        break
                    }
                }

                return node
            }
        },
    }
}