-
-
Notifications
You must be signed in to change notification settings - Fork 17
When writing scripts, you will probably face the problem, such as what the specific entity's type is. One way to do it is like this: IF entity.getType().name() == "ZOMBIE"
This use the Enum to check and process only if the entity
is a zombie. But in some cases, you might want to use IF statement to filter all the creatures, not only a zombie. What would be the solution? you could use IF statement and add all possible monster types like this: IF entity.getType().name() == "ZOMBIE" || entity.getType().name() == "CREEPER" || blahblah...
but this is way too tedious. And as developer ourselves, we want to find better way to solve it. And the solution is using IS statement.
In order to understand how IS statement work, you need to know at least some portion of how Java's Class hierarchy. Zombie(Click it to open the Javadoc), for example, you can see in the All Superinterfaces:
list that one of the superclasses of Zombie is Monster. If you click on the Monster, in the All Known Subinterfaces:
list, you probably will notice that the subclass of Monster is list of entities that can damage players, and that's what we are looking for.
- Superclass: superclass is the mother class. For example, if there is a class Animal, you can have subclasses of Animal, such as Pig, Dog, Cow, etc. It's some kind of circle that groups multiple classes together. In the above example, the Monster class was grouping all the entities that can harm players.
- Subclass: Like already said, it's the child of the mother class. Zombie is subclass of Monster.
Let's bring the example at the top again. We want to filter entities only if they are a monster. But putting every single monster type in IF statement is tedious, and in the future version of Minecraft, there might be more monsters are introduced. You wouldn't want to add those monster every time when a new version of Minecraft come out.
So, to go around, use IS statement:
IMPORT org.bukkit.entity.Monster
IF entity IS Monster
do something if a monster...
ENDIF
IMPORT org.bukkit.entity.Player
IF entity IS Player
do something if a player...
ENDIF
Notice that you have to IMPORT the class you want to use for IS statement. This is because you want to compare the type of entity directly with the exact class.
Plus, more the general the mother class is, the more IF statement will catch. For example, Monster is also a subclass of Entity(open and check it yourself), so if you filter it like IF entity IS Entity
, it is same as not using the IF statement as entity is always an Entity anyway.