Java Code Examples for org.bukkit.entity.Entity#isValid()

The following examples show how to use org.bukkit.entity.Entity#isValid() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EffKill.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void execute(final Event e) {
	for (Entity entity : entities.getArray(e)) {

		if (entity instanceof EnderDragonPart) {
			entity = ((EnderDragonPart) entity).getParent();
		}

		if (entity instanceof Damageable) {
			final boolean creative = entity instanceof Player && ((Player) entity).getGameMode() == GameMode.CREATIVE;
			if (creative) // Set player to survival before applying damage
				((Player) entity).setGameMode(GameMode.SURVIVAL);

			HealthUtils.damage((Damageable) entity, HealthUtils.getMaxHealth((Damageable) entity) * 100); // just to make sure that it really dies >:)

			if (creative) // Set creative player back to creative
				((Player) entity).setGameMode(GameMode.CREATIVE);
		}

		// if everything done so far has failed to kill this thing
		// We also don't want to remove a player as this would remove the player's data from the server.
		if (entity.isValid() && !(entity instanceof Player))
			entity.remove();
		
	}
}
 
Example 2
Source File: SitListener.java    From NyaaUtils with MIT License 6 votes vote down vote up
@EventHandler
public void onDismount(EntityDismountEvent event) {
    if (event.getDismounted() instanceof ArmorStand) {
        ArmorStand armorStand = (ArmorStand) event.getDismounted();
        if (armorStand.hasMetadata(metadata_key)) {
            for (Entity p : armorStand.getPassengers()) {
                if (p.isValid()) {
                    Location loc = safeLocations.get(p.getUniqueId());
                    safeLocations.remove(p.getUniqueId());
                    new BukkitRunnable() {
                        @Override
                        public void run() {
                            if (p.isValid() && loc != null) {
                                p.teleport(loc);
                            }
                        }
                    }.runTask(plugin);
                }
            }
        }
        armorStand.remove();
    }
}
 
Example 3
Source File: CraftTNTPrimed.java    From Thermos with GNU General Public License v3.0 5 votes vote down vote up
public Entity getSource() {
    net.minecraft.entity.EntityLivingBase source = getHandle().getTntPlacedBy();

    if (source != null) {
        Entity bukkitEntity = source.getBukkitEntity();

        if (bukkitEntity.isValid()) {
            return bukkitEntity;
        }
    }

    return null;
}
 
Example 4
Source File: AutoBreeder.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
private boolean canBreed(Entity n) {
    if (n.isValid() && n instanceof Animals) {
        Animals animal = (Animals) n;

        return animal.isAdult() && animal.canBreed() && !animal.isLoveMode();
    }

    return false;
}
 
Example 5
Source File: InfusedHopper.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
private boolean isValidItem(Location l, Entity entity) {
    if (entity instanceof Item && entity.isValid()) {
        Item item = (Item) entity;
        return !SlimefunUtils.hasNoPickupFlag(item) && item.getLocation().distanceSquared(l) > 0.25;
    }

    return false;
}
 
Example 6
Source File: EliteMobScanner.java    From EliteMobs with GNU General Public License v3.0 4 votes vote down vote up
public static void scanValidAggressiveLivingEntity(LivingEntity livingEntity) {

                if (!EntityTracker.isNaturalEntity(livingEntity) &&
                        !ConfigValues.mobCombatSettingsConfig.getBoolean(MobCombatSettingsConfig.STACK_AGGRESSIVE_SPAWNER_MOBS))
                    return;
                if (EntityTracker.isNaturalEntity(livingEntity) &&
                        !ConfigValues.mobCombatSettingsConfig.getBoolean(MobCombatSettingsConfig.STACK_AGGRESSIVE_NATURAL_MOBS))
                    return;

                for (Entity secondEntity : livingEntity.getNearbyEntities(aggressiveRange, aggressiveRange, aggressiveRange)) {

                    if (!secondEntity.getType().equals(livingEntity.getType())) continue;
                    if (!livingEntity.isValid() || !secondEntity.isValid()) continue;

                    if (!EntityTracker.isNaturalEntity(secondEntity) &&
                            !ConfigValues.mobCombatSettingsConfig.getBoolean(MobCombatSettingsConfig.STACK_AGGRESSIVE_SPAWNER_MOBS))
                        return;
                    if (EntityTracker.isNaturalEntity(secondEntity) &&
                            !ConfigValues.mobCombatSettingsConfig.getBoolean(MobCombatSettingsConfig.STACK_AGGRESSIVE_NATURAL_MOBS))
                        return;


                    EliteMobEntity eliteMobEntity1 = null;
                    EliteMobEntity eliteMobEntity2 = null;

                    int level1 = 1;
                    int level2 = 1;

                    if (EntityTracker.isEliteMob(livingEntity)) {
                        eliteMobEntity1 = EntityTracker.getEliteMobEntity(livingEntity);
                        if (!eliteMobEntity1.canStack()) continue;
                        level1 = eliteMobEntity1.getLevel();
                    }

                    if (EntityTracker.isEliteMob(secondEntity)) {
                        eliteMobEntity2 = EntityTracker.getEliteMobEntity(secondEntity);
                        if (!eliteMobEntity2.canStack()) continue;
                        level2 = eliteMobEntity2.getLevel();
                    }

                    int newLevel = level1 + level2;

                    if (newLevel > ConfigValues.mobCombatSettingsConfig.getInt(MobCombatSettingsConfig.ELITEMOB_STACKING_CAP))
                        return;

                    double healthPercentage = (livingEntity.getHealth() / livingEntity.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue() +
                            ((LivingEntity) secondEntity).getHealth() / ((LivingEntity) secondEntity).getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()) / 2;

                    /*
                    This is a bit of a dirty hack
                    It won't register the entity twice, but it will overwrite the existing method
                    */
                    eliteMobEntity1 = new EliteMobEntity(livingEntity, newLevel, healthPercentage, CreatureSpawnEvent.SpawnReason.CUSTOM);
                    secondEntity.remove();

                }

    }
 
Example 7
Source File: DynamicLocation.java    From EffectLib with MIT License 4 votes vote down vote up
public boolean hasValidEntity() {
    Entity entity = this.getEntity();
    return entity != null && entity.isValid();
}