package com.zivilon.cindercore.coremod;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.tree.AnnotationNode;
import net.minecraft.launchwrapper.IClassTransformer;


public class ChunkMethodsTransformer implements IClassTransformer {
    @Override
    public byte[] transform(String name, String transformedName, byte[] classBytes) {
        if ("net.minecraft.world.chunk.Chunk".equals(transformedName)) {
            ClassReader reader = new ClassReader(classBytes);
            ClassNode classNode = new ClassNode();
            reader.accept(classNode, 0);

            for (Object methodObj : classNode.methods) {
                MethodNode method = (MethodNode) methodObj;
                if ((method.name.equals("func_76607_a") && method.desc.equals("([BIIZ)V")) || 
                    (method.name.equals("func_76590_a") && method.desc.equals("()V"))) {
                    if (method.visibleAnnotations != null) {
                        method.visibleAnnotations.removeIf(annotation -> 
                            ((AnnotationNode)annotation).desc.equals("Lcpw/mods/fml/relauncher/SideOnly;"));
                    }
                }
            }

            ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
            classNode.accept(writer);
            return writer.toByteArray();
        }

        return classBytes;
    }
}

