Call method of superclass superclass from subclass

Discussion in 'Plugin Development' started by kumpelblase2, Oct 31, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    kumpelblase2

    So the title might be a bit confusing, but this bothers me a lot lately.
    Imaging we have a setup like this:
    Code:
    class BaseClass
    {
        public void doThat()
        {
            //Does something
        }
    }
     
    class OtherClass extends BaseClass
    {
        @Override
        public void doThat()
        {
            //Do more
            super.doThat();
        }
    }
     
    class SubClass extends OtherClass
    {
        @Override
        public void doThat()
        {
            //Do something
            super.doThat();
        }
    }
    Now, when you do super.doThat() in the SubClass it'll execute OtherClass.doThat() and that'll call BaseClass.doThat(). My problem is now that I don't want that the OtherClass.doThat() method getting called but still the BaseClass.doThat(). Is that even possible with java or am I trying to achieve the impossible here?
     
  2. Offline

    Pandemoneus

  3. Offline

    kumpelblase2

    Because I can't, I'm inside the nms classes.
     
  4. Offline

    fireblast709

    then what are you trying to call? like, what function, and from where.

    (any code would be nice)
     
  5. Offline

    kumpelblase2

    EntityOcelote.c(EntityHuman).
    I need to change one method call in there ('this.e.f()' to my stuff).
    https://github.com/Bukkit/mc-dev/blob/master/net/minecraft/server/EntityOcelot.java#L113
    But I don't want to write all the subclass code again...

    To clarify: I'm extending EntityOcelote.
    So what I did until now is that I took the code from that method and changed that one method call. But now, I'd need to call the c(EntityHuman) from the subclass of entityOcelote without calling it from EntityOcelote.
     
  6. Offline

    ferrybig

    you can try to use reflection to get the methode "c(EntityHuman)" from the class that EntityOcelot extends, to call the moethode, but I am not 100% sure it work
     
  7. Offline

    nisovin

    You know that your SuperClass is actually a subclass? Not a superclass? BaseClass and OtherClass are superclasses, SuperClass is not. I know this doesn't help, but I just wanted to point out how confusing your title is when you have your terminology backwards.
     
  8. Offline

    kumpelblase2

    Oh yeah, it always confuses me which one is which.
     
  9. Offline

    Comphenix

    The problem with copying the subclass is that your class would no longer be a net.minecraft.server.EntityOcelot, which would break a lot of native code that makes this assumption.

    There's a reflection trick that allows you to to call the parent method in EntityAnimal, but it relies on making a temporary copy of the instance, which would increment the entity ID count and create a bunch of useless objects. Not that desirable, and I'm not sure if it would even work in this case.

    Instead, look at the line of code you're trying to modify:
    Code:
    } else if (this.e.f() && itemstack != null && itemstack.id == Item.RAW_FISH.id &&
                                  entityhuman.e(this) < 9.0D) 
    
    There are several ways you could change the execution if you're the caller. The best way is probably to replace the "e" instance to a class of your own (by extending PathfinderGoalTempt) and always return FALSE.

    That way, you can safely copy the content of the c-method and modify it to your hearts content.
     
    kumpelblase2 likes this.
  10. Offline

    kumpelblase2

    Thanks for the input. But since there's no other way for me to do certain things (e.g. ability to modify max health at runtime), I'm forced to override the nms classes already. However, I'll probably need a way to deal with those problems anyway.
     
  11. Offline

    Comphenix

    I think you might have misunderstood my suggestion. :p

    I'm saying you can do that, you just have to override the "e" field in EntityOcelot as well. That way, you can simply use the "e" method to call the super class of your super class (notice that it's calling super.e at the end).
    Code:
    private static class PathfinderGoalTemptEx extends PathfinderGoalTempt {
        public PathfinderGoalTemptEx(EntityCreature arg0, float arg1, int arg2, boolean arg3) {
            super(arg0, arg1, arg2, arg3);
        }
    
        @Override
        public boolean f() {
            return false;
        }
    }
     
  12. Offline

    kumpelblase2

    Well, I was more referring to your first statement (that a lot would break which relies on net.minecraft.server.EntityOcelote) and haven't covered all of your statements. I more or less ignored the part to replace the e variable, because I'm using my own Pathfinders/AI anyway. But when I think about it now, it's actually better than the way I imagined. I'll probably won't just return false, but that are some changes that I'll do locally.
    Thanks again for the suggestion(s).
     
    Comphenix likes this.
Thread Status:
Not open for further replies.

Share This Page