org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource Java Examples

The following examples show how to use org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource. 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: ModSupport.java    From EagleFactions with MIT License 6 votes vote down vote up
/**
 * Gets the attacking entity from the EntityDamageSource. EntityDamageSource must come from TechGuns.
 * @param entityDamageSource the source
 * @return attacking/source entity.
 */
public static Entity getAttackerFromTechGuns(final EntityDamageSource entityDamageSource)
{
    Object attacker = null;
    try
    {
        attacker = entityDamageSource.getClass().getField("attacker").get(entityDamageSource);
    }
    catch (IllegalAccessException | NoSuchFieldException e)
    {
        e.printStackTrace();
    }
    if (attacker instanceof Entity)
        return (Entity)attacker;
    else return null;
}
 
Example #2
Source File: SpongeDeathListener.java    From Plan with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Listener
public void onEntityDeath(DestructEntityEvent.Death event) {
    long time = System.currentTimeMillis();
    Living dead = event.getTargetEntity();

    if (dead instanceof Player) {
        // Process Death
        SessionCache.getCachedSession(dead.getUniqueId()).ifPresent(Session::died);
    }

    try {
        Optional<EntityDamageSource> optDamageSource = event.getCause().first(EntityDamageSource.class);
        if (optDamageSource.isPresent()) {
            EntityDamageSource damageSource = optDamageSource.get();
            Entity killerEntity = damageSource.getSource();
            handleKill(time, dead, killerEntity);
        }
    } catch (Exception e) {
        errorLogger.log(L.ERROR, e, ErrorContext.builder().related(event, dead).build());
    }
}
 
Example #3
Source File: PvpListener.java    From Nations with MIT License 6 votes vote down vote up
@Listener(order=Order.FIRST, beforeModifications = true)
public void onEntityDamagedByPlayer(DamageEntityEvent event, @All(ignoreEmpty=false) EntityDamageSource[] sources)
{
	if (!ConfigHandler.getNode("worlds").getNode(event.getTargetEntity().getWorld().getName()).getNode("enabled").getBoolean())
	{
		return;
	}
	Entity attacker = null;
	for (int i = 0; i < sources.length; i++)
	{
		if (sources[i].getSource().getType() == EntityTypes.PLAYER
				|| (sources[i] instanceof IndirectEntityDamageSource && ((IndirectEntityDamageSource) sources[i]).getIndirectSource().getType() == EntityTypes.PLAYER))
		{
			attacker = sources[i].getSource();
		}
	}
	if (attacker != null && event.getTargetEntity().getType() == EntityTypes.PLAYER)
	{
		if (!DataHandler.getFlag("pvp", attacker.getLocation()) || !DataHandler.getFlag("pvp", event.getTargetEntity().getLocation()))
		{
			event.setCancelled(true);
			return;
		}
	}
}
 
Example #4
Source File: PrismRecord.java    From Prism with MIT License 6 votes vote down vote up
/**
 * Set a cause based on a Cause chain.
 *
 * @param cause Cause of event.
 * @return The created EventBuilder instance
 */
public PrismRecord.EventBuilder source(Cause cause) {
    Player player = cause.first(Player.class).orElse(null);
    if (player != null) {
        return new PrismRecord.EventBuilder(player);
    }

    EntityDamageSource attacker = cause.first(EntityDamageSource.class).orElse(null);
    if (attacker != null) {
        return new PrismRecord.EventBuilder(attacker);
    }

    IndirectEntityDamageSource indirectAttacker = cause.first(IndirectEntityDamageSource.class).orElse(null);
    if (indirectAttacker != null) {
        return new PrismRecord.EventBuilder(indirectAttacker);
    }

    if (!cause.all().isEmpty()) {
        return new PrismRecord.EventBuilder(cause.all().get(0));
    }

    return new PrismRecord.EventBuilder(null);
}
 
Example #5
Source File: EntityEventHandler.java    From GriefDefender with MIT License 5 votes vote down vote up
@Listener(order = Order.POST)
public void onEntityDamageMonitor(DamageEntityEvent event) {
    if (!GriefDefenderPlugin.getInstance().claimsEnabledForWorld(event.getTargetEntity().getWorld().getUniqueId())) {
        return;
    }

    GDTimings.ENTITY_DAMAGE_MONITOR_EVENT.startTimingIfSync();
    //FEATURE: prevent players who very recently participated in pvp combat from hiding inventory to protect it from looting
    //FEATURE: prevent players who are in pvp combat from logging out to avoid being defeated

    if (event.getTargetEntity().getType() != EntityTypes.PLAYER || NMSUtil.getInstance().isEntityMonster(event.getTargetEntity())) {
        GDTimings.ENTITY_DAMAGE_MONITOR_EVENT.stopTimingIfSync();
        return;
    }

    Player defender = (Player) event.getTargetEntity();

    //only interested in entities damaging entities (ignoring environmental damage)
    // the rest is only interested in entities damaging entities (ignoring environmental damage)
    if (!(event.getCause().root() instanceof EntityDamageSource)) {
        GDTimings.ENTITY_DAMAGE_MONITOR_EVENT.stopTimingIfSync();
        return;
    }

    GDPlayerData playerData = GriefDefenderPlugin.getInstance().dataStore.getOrCreatePlayerData(defender.getWorld(), defender.getUniqueId());
    GDClaim claim = this.dataStore.getClaimAtPlayer(playerData, defender.getLocation());
    EntityDamageSource entityDamageSource = (EntityDamageSource) event.getCause().root();
    GDTimings.ENTITY_DAMAGE_MONITOR_EVENT.stopTimingIfSync();
}