0

I'm trying some alterations in minecraft src. I'm trying to override a method in a class so I don't have to edit the original class.

In the regular class I want to alter this method:

public void sendChatMessage(String par1Str)
{
    this.sendQueue.addToSendQueue(new Packet3Chat(par1Str));
}

So in my subclass I have this code:

package cobalt.gui;

import cobalt.hacks.*;
import net.minecraft.client.Minecraft;
import net.minecraft.src.EntityClientPlayerMP;
import net.minecraft.src.NetClientHandler;
import net.minecraft.src.Session;
import net.minecraft.src.World;



public class Console extends EntityClientPlayerMP {

    public Console(Minecraft par1Minecraft, World par2World,
            Session par3Session, NetClientHandler par4NetClientHandler) {
        super(par1Minecraft, par2World, par3Session, par4NetClientHandler);
    }

    @Override
    public void sendChatMessage(String par1Str) {

        if (par1Str.startsWith(".help")) {
                    //Do stuff
            return;
        }
    }
}

From my understanding, anytime a method is called, it should be "redirected" for the subclass to handle? (Tell me if I'm wrong ha)

The if statement does work correctly if I modify the original class.

Thank you very much!

2
  • >>From my understanding, anytime a method is called, it should be "redirected" for the subclass to handle? No, Only if you are calling the method of the subclass
    – rajesh
    Commented Dec 27, 2012 at 5:30
  • Depending on with which class's object you are calling the method, control will be "redirected" accordingly. For ex. if you are calling method on base class's object base class version will be called. Commented Dec 27, 2012 at 5:33

2 Answers 2

2

This would only work if somehow the rest of the minecraft code starts using your class, Console, where it meant to use EntityClientPlayerMP. Without that, your function will not be called.

If you want to change the behavior of the game, the easiest way would be to change EntityClientPlayerMP itself. If you want to use the modified class Console elsewhere in the code, then what you have done is fine.

0
0

It depends on the actual object type. If the object is of type Console e.g. EntityClientPlayerMP obj = new Console(..) and obj.sendChatMessage(..) it'll work. But if the object itself is of type EntityClientPlayerMP like new EntityClientPlayerMP(..) It won't work

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.