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

The following examples show how to use org.spongepowered.api.event.cause.entity.damage.source.IndirectEntityDamageSource. 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: 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 #2
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);
}