Java Code Examples for org.spongepowered.api.event.world.ExplosionEvent#Detonate

The following examples show how to use org.spongepowered.api.event.world.ExplosionEvent#Detonate . 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: EntityListener.java    From RedProtect with GNU General Public License v3.0 6 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onBlockExplode(ExplosionEvent.Detonate e) {
    RedProtect.get().logger.debug(LogLevel.DEFAULT, "Is BlockListener - BlockExplodeEvent event");

    for (Location<World> bex : e.getAffectedLocations()) {
        Region r = RedProtect.get().rm.getTopRegion(bex, this.getClass().getName());
        if (!cont.canWorldBreak(bex.createSnapshot())) {
            e.setCancelled(true);
            return;
        }
        if (r != null && !r.canFire()) {
            e.setCancelled(true);
            return;
        }
    }
}
 
Example 2
Source File: EntityEventHandler.java    From GriefPrevention with MIT License 5 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onEntityExplosionDetonate(ExplosionEvent.Detonate event) {
    if (!GPFlags.EXPLOSION || !GriefPreventionPlugin.instance.claimsEnabledForWorld(event.getTargetWorld().getProperties())) {
        return;
    }
    if (GriefPreventionPlugin.isSourceIdBlacklisted(ClaimFlag.EXPLOSION.toString(), event.getSource(), event.getTargetWorld().getProperties())) {
        return;
    }

    GPTimings.ENTITY_EXPLOSION_DETONATE_EVENT.startTimingIfSync();
    final User user = CauseContextHelper.getEventUser(event);
    Iterator<Entity> iterator = event.getEntities().iterator();
    GPClaim targetClaim = null;
    Object source = event.getSource();
    if (source instanceof Explosion) {
        final Explosion explosion = (Explosion) source;
        if (explosion.getSourceExplosive().isPresent()) {
            source = explosion.getSourceExplosive().get();
        } else {
            Entity exploder = event.getCause().first(Entity.class).orElse(null);
            if (exploder != null) {
                source = exploder;
            }
        }
    }

    while (iterator.hasNext()) {
        Entity entity = iterator.next();
        targetClaim =  GriefPreventionPlugin.instance.dataStore.getClaimAt(entity.getLocation(), targetClaim);
        if (GPPermissionHandler.getClaimPermission(event, entity.getLocation(), targetClaim, GPPermissions.ENTITY_DAMAGE, source, entity, user) == Tristate.FALSE) {
            iterator.remove();
        }
    }
    GPTimings.ENTITY_EXPLOSION_DETONATE_EVENT.stopTimingIfSync();
}
 
Example 3
Source File: GlobalListener.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onEntityExplode(ExplosionEvent.Detonate e) {

    World w = e.getTargetWorld();
    for (Location<World> b : e.getAffectedLocations()) {
        Region r = RedProtect.get().rm.getTopRegion(b, this.getClass().getName());
        if (r == null && !RedProtect.get().config.globalFlagsRoot().worlds.get(w.getName()).entity_block_damage) {
            e.setCancelled(true);
            return;
        }
    }
}
 
Example 4
Source File: EntityEventHandler.java    From GriefDefender with MIT License 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onEntityExplosionDetonate(ExplosionEvent.Detonate event) {
    if (!GDFlags.EXPLOSION_ENTITY || !GriefDefenderPlugin.getInstance().claimsEnabledForWorld(event.getTargetWorld().getUniqueId())) {
        return;
    }
    if (GriefDefenderPlugin.isSourceIdBlacklisted(Flags.EXPLOSION_ENTITY.getName(), event.getSource(), event.getTargetWorld().getProperties())) {
        return;
    }

    GDTimings.ENTITY_EXPLOSION_DETONATE_EVENT.startTimingIfSync();
    final User user = CauseContextHelper.getEventUser(event);
    Iterator<Entity> iterator = event.getEntities().iterator();
    GDClaim targetClaim = null;
    Object source = event.getSource();
    if (source instanceof Explosion) {
        final Explosion explosion = (Explosion) source;
        if (explosion.getSourceExplosive().isPresent()) {
            source = explosion.getSourceExplosive().get();
        } else {
            Entity exploder = event.getCause().first(Entity.class).orElse(null);
            if (exploder != null) {
                source = exploder;
            }
        }
    }

    final String sourceId = GDPermissionManager.getInstance().getPermissionIdentifier(source);
    boolean denySurfaceExplosion = GriefDefenderPlugin.getActiveConfig(event.getTargetWorld().getUniqueId()).getConfig().claim.explosionEntitySurfaceBlacklist.contains(sourceId);
    if (!denySurfaceExplosion) {
        denySurfaceExplosion = GriefDefenderPlugin.getActiveConfig(event.getTargetWorld().getUniqueId()).getConfig().claim.explosionEntitySurfaceBlacklist.contains("any");
    }

    while (iterator.hasNext()) {
        Entity entity = iterator.next();
        final Location<World> location = entity.getLocation();
        targetClaim =  GriefDefenderPlugin.getInstance().dataStore.getClaimAt(entity.getLocation(), targetClaim);
        if (denySurfaceExplosion && location.getExtent().getDimension().getType() != DimensionTypes.NETHER && location.getBlockY() >= location.getExtent().getSeaLevel()) {
            iterator.remove();
            GDPermissionManager.getInstance().processEventLog(event, location, targetClaim, Flags.EXPLOSION_ENTITY.getPermission(), source, entity, user, "explosion-surface", Tristate.FALSE);
            continue;
        }
        if (GDPermissionManager.getInstance().getFinalPermission(event, entity.getLocation(), targetClaim, Flags.EXPLOSION_ENTITY, source, entity, user) == Tristate.FALSE) {
            iterator.remove();
        } else if (GDPermissionManager.getInstance().getFinalPermission(event, entity.getLocation(), targetClaim, Flags.ENTITY_DAMAGE, source, entity, user) == Tristate.FALSE) {
            iterator.remove();
        }
    }
    GDTimings.ENTITY_EXPLOSION_DETONATE_EVENT.stopTimingIfSync();
}
 
Example 5
Source File: BlockEventHandler.java    From GriefDefender with MIT License 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onExplosionDetonate(ExplosionEvent.Detonate event) {
    final World world = event.getExplosion().getWorld();
    if (!GDFlags.EXPLOSION_BLOCK || !GriefDefenderPlugin.getInstance().claimsEnabledForWorld(world.getUniqueId())) {
        return;
    }

    Object source = event.getSource();
    if (source instanceof Explosion) {
        final Explosion explosion = (Explosion) source;
        if (explosion.getSourceExplosive().isPresent()) {
            source = explosion.getSourceExplosive().get();
        } else {
            Entity exploder = event.getCause().first(Entity.class).orElse(null);
            if (exploder != null) {
                source = exploder;
            }
        }
    }
    if (GriefDefenderPlugin.isSourceIdBlacklisted(Flags.EXPLOSION_BLOCK.getName(), source, event.getExplosion().getWorld().getProperties())) {
        return;
    }

    GDTimings.EXPLOSION_EVENT.startTimingIfSync();
    final User user = CauseContextHelper.getEventUser(event);
    GDClaim targetClaim = null;
    final List<Location<World>> filteredLocations = new ArrayList<>();
    final String sourceId = GDPermissionManager.getInstance().getPermissionIdentifier(source);
    final int cancelBlockLimit = GriefDefenderPlugin.getGlobalConfig().getConfig().claim.explosionCancelBlockLimit;
    boolean denySurfaceExplosion = GriefDefenderPlugin.getActiveConfig(world.getUniqueId()).getConfig().claim.explosionBlockSurfaceBlacklist.contains(sourceId);
    if (!denySurfaceExplosion) {
        denySurfaceExplosion = !GriefDefenderPlugin.getActiveConfig(world.getUniqueId()).getConfig().claim.explosionBlockSurfaceBlacklist.contains("any");
    }
    for (Location<World> location : event.getAffectedLocations()) {
        targetClaim =  GriefDefenderPlugin.getInstance().dataStore.getClaimAt(location, targetClaim);
        if (denySurfaceExplosion && world.getDimension().getType() != DimensionTypes.NETHER && location.getBlockY() >= world.getSeaLevel()) {
            filteredLocations.add(location);
            GDPermissionManager.getInstance().processEventLog(event, location, targetClaim, Flags.EXPLOSION_BLOCK.getPermission(), source, location.getBlock(), user, "explosion-surface", Tristate.FALSE);
            continue;
        }

        Tristate result = GDPermissionManager.getInstance().getFinalPermission(event, location, targetClaim, Flags.EXPLOSION_BLOCK, source, location.getBlock(), user, true);

        if (result == Tristate.FALSE) {
            // Avoid lagging server from large explosions.
            if (event.getAffectedLocations().size() > cancelBlockLimit) {
                event.setCancelled(true);
                break;
            }
            filteredLocations.add(location);
        }
    }
    // Workaround for SpongeForge bug
    if (event.isCancelled()) {
        event.getAffectedLocations().clear();
    } else if (!filteredLocations.isEmpty()) {
        event.getAffectedLocations().removeAll(filteredLocations);
    }
    GDTimings.EXPLOSION_EVENT.stopTimingIfSync();
}
 
Example 6
Source File: ExplosionListener.java    From EagleFactions with MIT License 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onExplosion(final ExplosionEvent.Detonate event)
{
    final List<Location<World>> locationList = new ArrayList<>(event.getAffectedLocations());
    final List<Entity> entityList = new ArrayList<>(event.getEntities());
    User user = null;
    final Cause cause = event.getCause();
    final EventContext context = event.getContext();
    if (cause.root() instanceof TileEntity) {
        user = context.get(EventContextKeys.OWNER)
                .orElse(context.get(EventContextKeys.NOTIFIER)
                        .orElse(context.get(EventContextKeys.CREATOR)
                                .orElse(null)));
    } else {
        user = context.get(EventContextKeys.NOTIFIER)
                .orElse(context.get(EventContextKeys.OWNER)
                        .orElse(context.get(EventContextKeys.CREATOR)
                                .orElse(null)));
    }

    if(event.getCause().containsType(Player.class))
    {
        user = event.getCause().first(Player.class).get();
    }
    else if(event.getCause().containsType(User.class))
    {
        user = event.getCause().first(User.class).get();
    }

    for(final Entity entity : entityList)
    {
        final Location<World> entityLocation = entity.getLocation();
        if(user != null)
        {
            if(!super.getPlugin().getProtectionManager().canExplode(entityLocation, user, false).hasAccess())
            {
                event.getEntities().remove(entity);
            }
        }
        else if(!super.getPlugin().getProtectionManager().canExplode(entityLocation).hasAccess())
        {
            event.getEntities().remove(entity);
        }
    }

    for(final Location<World> location : locationList)
    {
        if(user != null)
        {
            if(!super.getPlugin().getProtectionManager().canExplode(location, user, false).hasAccess())
            {
                event.getAffectedLocations().remove(location);
            }
        }
        else if(!super.getPlugin().getProtectionManager().canExplode(location).hasAccess())
        {
            event.getAffectedLocations().remove(location);
        }
    }
}
 
Example 7
Source File: BlockEventHandler.java    From GriefPrevention with MIT License 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onExplosionDetonate(ExplosionEvent.Detonate event) {
    final World world = event.getExplosion().getWorld();
    if (!GPFlags.EXPLOSION || !GriefPreventionPlugin.instance.claimsEnabledForWorld(world.getProperties())) {
        return;
    }

    Object source = event.getSource();
    if (source instanceof Explosion) {
        final Explosion explosion = (Explosion) source;
        if (explosion.getSourceExplosive().isPresent()) {
            source = explosion.getSourceExplosive().get();
        } else {
            Entity exploder = event.getCause().first(Entity.class).orElse(null);
            if (exploder != null) {
                source = exploder;
            }
        }
    }
    if (GriefPreventionPlugin.isSourceIdBlacklisted(ClaimFlag.EXPLOSION.toString(), source, event.getExplosion().getWorld().getProperties())) {
        return;
    }

    GPTimings.EXPLOSION_EVENT.startTimingIfSync();
    final User user = CauseContextHelper.getEventUser(event);
    GPClaim targetClaim = null;
    final List<Location<World>> filteredLocations = new ArrayList<>();
    for (Location<World> location : event.getAffectedLocations()) {
        targetClaim =  GriefPreventionPlugin.instance.dataStore.getClaimAt(location, targetClaim);
        Tristate result = Tristate.UNDEFINED;
        if (GPFlags.EXPLOSION_SURFACE && location.getPosition().getY() > ((net.minecraft.world.World) world).getSeaLevel()) {
            result = GPPermissionHandler.getClaimPermission(event, location, targetClaim, GPPermissions.EXPLOSION_SURFACE, source, location.getBlock(), user, true);
        } else {
            result = GPPermissionHandler.getClaimPermission(event, location, targetClaim, GPPermissions.EXPLOSION, source, location.getBlock(), user, true);
        }

        if (result == Tristate.FALSE) {
            // Avoid lagging server from large explosions.
            if (event.getAffectedLocations().size() > 100) {
                event.setCancelled(true);
                break;
            }
            filteredLocations.add(location);
        }
    }
    // Workaround for SpongeForge bug
    if (event.isCancelled()) {
        event.getAffectedLocations().clear();
    } else if (!filteredLocations.isEmpty()) {
        event.getAffectedLocations().removeAll(filteredLocations);
    }
    GPTimings.EXPLOSION_EVENT.stopTimingIfSync();
}