Jump to content

Inject EntityPlayerSP?


Alex_Wells

Recommended Posts

Hey there! I'm trying to hook a method of EntityPlayerSP using coremod, but for some reason

public byte[] transform(String name, String transformedName, byte[] bytes)

is not getting called with needed class name (net.minecraft.client.entity.EntityPlayerSP), but is getting called with all other class names (also modifying ItemPotion's method, and it works great). Here's the full code, if needed:

public class ClassTransformer implements IClassTransformer
{
    private Map<String, List<AbstractMethodTransformer>> methodTransformers = new HashMap<>();

    public ClassTransformer() {
        addMethodTransformer(new HasEffectMethodTransformer());
        addMethodTransformer(new ReachInfoClassTransformer());
    }

    public void addMethodTransformer(AbstractMethodTransformer transformer) {
        String className = transformer.getClassName();

        addMethodTransformerByName(className, transformer);
        addMethodTransformerByName(Obfuscations.get(className), transformer);
    }

    private void addMethodTransformerByName(String name, AbstractMethodTransformer transformer) {
        methodTransformers.computeIfAbsent(name, s -> new ArrayList<>()).add(transformer);
    }

    public byte[] transform(String name, String transformedName, byte[] bytes)
    {
        // DEBUG
        if(name.contains("Player") || transformedName.contains("Player"))
            System.out.println(name + " --- " + transformedName);


        if(! methodTransformers.containsKey(transformedName)) {
            return bytes;
        }

        System.out.println("Contains key: " + name + ", " + transformedName);

        List<AbstractMethodTransformer> transformers = methodTransformers.get(transformedName);

        ClassReader classReader = new ClassReader(bytes);
        ClassNode classNode = new ClassNode();
        ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
        classReader.accept(classNode, 0);

        for (MethodNode method : classNode.methods)
        {
            for(AbstractMethodTransformer transformer : transformers) {
                String mappedMethodName = FMLDeobfuscatingRemapper.INSTANCE.mapMethodName(classNode.name, method.name, method.desc);

                if(! Obfuscations.check(transformer.getMethodName(), mappedMethodName)) {
                    continue;
                }

                System.out.println("Found method: " + transformer.getMethodName() + " " + mappedMethodName);

                MethodReplacer methodReplacer = new MethodReplacer(classWriter, method.name, method.desc, transformer);
                classReader.accept(methodReplacer, ClassReader.EXPAND_FRAMES);
                break;
            }
        }

        return classWriter.toByteArray();
    }
}


When looking for EntityPlayerSP in the console I find nothing. What can be the problem and is there any workaround?

Here's a link to logs: https://pastebin.com/aEKyucjE

Edited by Alex_Wells
Link to comment
Share on other sites

Just now, diesieben07 said:

That depends on why you want to replace them. What effect are you trying to achieve?

Default hasEffect of ItemPotion implementation decides if glinting enchanment effect is rendered on potions or not. I want to set it to always return false.

Default attackEntityFrom doesn't emit any Forge events. In EntityLivingBase's attackEntityFrom, livingAttackEvent is emitted. I want this event to be also emitted for EntityPlayerSP, so that after this I'm able to track when player gets damage from other players.

Link to comment
Share on other sites

Quote

You can overwrite the potion item using the forge registries. Just register your own implementation extending ItemPotion under the same name.

Yes, but this will break any compatibility with other mods. Am I wrong?

 

Quote

EntityPlayerSP::attackEntityFrom emits LivingAttackEvent already. However this won't be of much use, since the client has no idea about what caused a given damage, that is only possible on the server.

On Forge 1.8.9 latest it does not. What about damage amount and damage source, it doesn't really matter for me. The purpose of this is to send a chat message about other players reach distance on each their hit. I know it will never be precise, that it highly depends on ping, that it's not gonna always send true data. But again, I don't really care about all those ;)

And sadly, I can't update my MC version. I would, but PVP was broken in 1.9 and most servers still use 1.8 (and even 1.7) as their main versions. Therefore, I would go for any trick to make it work :)


UPD: Yes, it's fixed only in 1.11 version

Edited by Alex_Wells
Fixed quotes
Link to comment
Share on other sites

42 minutes ago, diesieben07 said:

So will making a coremod.

 

The solution is to update.

Update isn't real. Until Minecraft rolls out 1.14/1.15, with new PVP changes, none of the servers neither their players will update. Therefore, I just can't use 1.12.

Unfortunately, I will have to overwrite EntityLivingBase::handleStatusUpdate in order to make it work. That's the only solution I see for 1.8.9.

Anyways, thanks for your help.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.