1.15.2 Phantom does not extend Monster

Discussion in 'Spigot Plugin Development' started by Swedz, Mar 23, 2020.

  1. Pretty simple question. Why does the interface, Phantom, not extend Monster? It only extends Flying it seems. I realized this when checking if an entity is an instance of a Monster and preventing it to spawn in a certain area, but Phantoms still spawn.
  2. Phantom extends Mob. As to why not Monster, you'd have to ask the spigot team. I assume because they don't spawn around the world, but just under specific circumstances?
    Anyways, you should be able to use event.getEntity() instanceof Phantom, idk why you would have to check for flying instead?
  3. The Bukkit entity interface structure is the same as the NMS class-structure. So like in Bukkit, the PhantomEntity extends FlyingEntity. The (somewhat logical) thing here would be to make PhantomEntity extend both FlyingEntity as well as MonsterEntity. However, Java does not allow that kind of inheritance (due to a lot of reasons, look up the "Diamond problem" for example). Due to this fact, it can only inherit from one super-class. The decision a designer has to make is whether this superclass should be MonsterEntity or FlyingEntity. I assume due to the fact that the flying logic is nearly the same as for all other flying entities, but the monster-logic differs quite heavily (just a vague guess here), the decision was made to make Phantom inherit from flying.
    But this is a good example of why inheritance can sometimes be disadvantageous. Some languages (like C++) allow for multiple inheritances, however, that does come with a side effect. Other languages (like Go) do not allow for inheritance (thou you could, of course, model it) and provide other features to model such dependencies (like interfaces that are implemented implicitly).

    Edit: You are also not able to include FlyingEntity into the monster-path or vice versa. What I mean by that is that you could not make FlyingEntity extend from MonsterEntity, since that would mean that all flying entities, like parrots or bats, are monsters. Vice-versa, you can also not make MonsterEntity inherit from FlyingEntity since that would mean that all monsters are also flying entities (which is for example not true for something like a zombie).

    Edit 2: What you could do (but this involves NMS, so you should look for another way) is to check the following:
    Code (Java):
    final Entity nmsEntity = ((CraftEntity) entity).getHandle(); // the NSM entity
    if (entity instanceof IMonster) {
        // is true for all monsters, including phantoms
    #3 Schottky, Mar 23, 2020
    Last edited: Mar 23, 2020
  4. It extends Flying which extends Mob, so it does extend Mob.