So I’m trying to spawn an entity, and that’s all fine and dandy (except for the fact that not all my precious Keys work yet, but that’s another problem)… The entity spawns perfectly too. The problem I have, is I also need to prevent entity spawning, but not the ones I spawn, so I pass a Cause to the entity when I spawn it:
public boolean spawn() {
Optional<Entity> entityOptional = this.location.getExtent().createEntity(EntityTypes.ARMOR_STAND, this.location.getPosition());
if(entityOptional.isPresent()) {
this.kill();
this.entity = (ArmorStand) entityOptional.get();
this.entity.getOrCreate(DisplayNameData.class).get().set(Keys.DISPLAY_NAME, this.name).set(Keys.SHOWS_DISPLAY_NAME, true);
this.entity.getOrCreate(InvisibilityData.class).get().set(Keys.INVISIBLE, true);
this.entity.setGravity(false);
this.entity.setSmall(true);
StarMain.getOperatingInstance().getGame().getEventManager().registerListeners(StarMain.getOperatingInstance(), new Hologram.Listener(this));
this.location.getExtent().spawnEntity(entity, Cause.of(this));
return true;
} else {
return false;
}
}
As you can see, I use the Cause of ‘this,’ or the class that is wrapping the entity. And then my listener checks:
@Listener
public void onSpawn(SpawnEntityEvent ev) {
if(!ev.getCause().any(Hologram.class) && !ev.getCause().any(UnmovingNonPlayableCharacter.class) && this.lobby.is(ev.getTargetEntity().getWorld())){
StarMain.getOperatingInstance().getLogger().info("We should prevent spawning of a '".concat(ev.getTargetEntity().getClass().getName()).concat("' here"));
}
}
The spawn method is in the Hologram class, and as such ‘this’ refers to the Hologram class, however, this listener prints out that we should prevent spawning of the armor stands created by my hologram, and I don’t know why. Any help is much appreciated.