0

This is probably basic java, but I'm new to the language, so please forgive me

I would like to extend the SetCustomName method of LivingEntity so that, in addition to it's normal steps, it will also throw a custom event/exception.

My attempts to do this result in errors regarding abstract, and can't have body.

In addition, I can't tell if this is a class or an interface, which means I'm not sure if I should use extends or implements.

Finally, I'm looking to add this functionality, rather then create a new object.

Can you please tell me if this is possible / steer me in the correct direction? Thank you very much for the help.

Drazisil
  • 3,070
  • 4
  • 33
  • 53
  • 1
    An `Interface` can't have a "body" - it's an interface, and is used to abstract something into a more "generic" object. You need to make a `class` that `implement`s your `interface` and then the class needs to implement the body of the method. Finally: when looking at the signature of a file, you'll either see `interface` before the name or `class` - this will tell you whether it's an interface or a class... – Darwind Mar 27 '14 at 18:08
  • @Darwind So I'm looking for something like public class PetGuardLivingEntity extends CraftLivingEntity implements LivingEntity ? – Drazisil Mar 27 '14 at 18:11
  • Yup that could definitely be a solution and then implement (actually `override`) the `setCustomName` method of the `CraftLivingEntity` creating your own implementation of the method in question. **Edit:** Excactly like @Emerson Farrugia's answer :-) – Darwind Mar 27 '14 at 18:14
  • Would that work on any instances of CraftLivingEntity, or only on PetGuardLivingEntity? The end desire is to add the functionality to CraftLivingEntity. – Drazisil Mar 27 '14 at 18:15
  • 1
    When `overriding` a method coming from a "super-class" it will only take effect on the `class` extending the "super-class", so yes, this will only take effect on the `PetGuardLivingEntity` class :-) **EDIT:** Unless you create another class extending your `PetGuardLivingEntity` class... but that's another story ;) – Darwind Mar 27 '14 at 18:17
  • Looks like the answer my issue is 'can't be done' then :( Thanks for the help. – Drazisil Mar 27 '14 at 18:18
  • Ah, you want to do something like `C#`s [partial class](http://msdn.microsoft.com/en-us/library/wa80x488.aspx). Nope that can't be done, unless you actually have access to the source code of the `CraftLivingEntity` class... – Darwind Mar 27 '14 at 18:21
  • 1
    Also I recommend reading [this](http://www.amazon.com/Thinking-Java-Edition-Bruce-Eckel/dp/0131872486) to learn a lot more about Java :-) – Darwind Mar 27 '14 at 18:23

2 Answers2

2

Looked at your file you gave from GitHub..

LivingEntity is an interface and so you would have to implement it into another class because it's methods aren't allowed to be defined.

Just follow Emerson's overridden method, as it's correct. Put your custom code under the call to super.setCustomName(name);.

the_constant
  • 681
  • 4
  • 11
1

LivingEntity is an interface, CraftLivingEntity is a class. You can only extend a class, you implement an interface. The interface itself has no "normal steps".

If you can control whether CraftLivingEntity or a different class is instantiated, extend CLE as follows

public class MyCraftLivingEntity extends CraftLivingEntity {

    @Override // just a marker annotation to help catch inheritance issues
    public void setCustomName(String name) {

         super.setCustomName(name);

         // do whatever else
    }
}

and instantiate your class instead.

Emerson Farrugia
  • 11,153
  • 5
  • 43
  • 51
  • I can't, as far as I know, CraftLivingEntity will always be called. Besides, even if it were possible, I believe that would completely break all of minecraft :) – Drazisil Mar 27 '14 at 18:13
  • I know nothing about minecraft or bukkit, so you'll need someone else to help with that. :S – Emerson Farrugia Mar 27 '14 at 18:15