Java Code Examples for org.spongepowered.api.event.cause.EventContext#containsKey()

The following examples show how to use org.spongepowered.api.event.cause.EventContext#containsKey() . 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: EntityDamageListener.java    From EagleFactions with MIT License 4 votes vote down vote up
@Listener
    public void onIgniteEntity(final IgniteEntityEvent event)
    {
        final EventContext eventContext = event.getContext();
        final Entity entity = event.getTargetEntity();
        final World world = event.getTargetEntity().getWorld();

        //Only if ignited entity is player
        if(!(entity instanceof Player))
            return;

        //Check safezone world
        if(this.protectionConfig.getSafeZoneWorldNames().contains(world.getName()))
        {
            event.setCancelled(true);
            return;
        }

        final Player ignitedPlayer = (Player) entity;

        //Check if location is safezone
        final Optional<Faction> optionalChunkFaction = super.getPlugin().getFactionLogic().getFactionByChunk(world.getUniqueId(), ignitedPlayer.getLocation().getChunkPosition());
        if(optionalChunkFaction.isPresent() && optionalChunkFaction.get().isSafeZone())
        {
            event.setCancelled(true);
            return;
        }

        if(!eventContext.containsKey(EventContextKeys.OWNER) || !(eventContext.get(EventContextKeys.OWNER).get() instanceof Player))
            return;

//        if(!cause.containsType(Player.class))
//            return;

        final Player igniterPlayer = (Player) eventContext.get(EventContextKeys.OWNER).get();
        final boolean isFactionFriendlyFireOn = this.factionsConfig.isFactionFriendlyFire();
        final boolean isAllianceFriendlyFireOn = this.factionsConfig.isAllianceFriendlyFire();
        final boolean isTruceFriendlyFireOn = this.factionsConfig.isTruceFriendlyFire();

        if(isFactionFriendlyFireOn && isAllianceFriendlyFireOn && isTruceFriendlyFireOn)
            return;

        final Optional<Faction> optionalIgnitedPlayerFaction = super.getPlugin().getFactionLogic().getFactionByPlayerUUID(ignitedPlayer.getUniqueId());
        final Optional<Faction> optionalIgniterPlayerFaction = super.getPlugin().getFactionLogic().getFactionByPlayerUUID(igniterPlayer.getUniqueId());

        if(optionalIgnitedPlayerFaction.isPresent() && optionalIgniterPlayerFaction.isPresent())
        {
            final Faction ignitedPlayerFaction = optionalIgnitedPlayerFaction.get();
            final Faction igniterPlayerFaction = optionalIgniterPlayerFaction.get();

            if(!isFactionFriendlyFireOn)
            {
                if(ignitedPlayerFaction.getName().equals(igniterPlayerFaction.getName()))
                {
                    event.setCancelled(true);
                    return;
                }
            }

            if(!isTruceFriendlyFireOn)
            {
                if(ignitedPlayerFaction.getTruces().contains(igniterPlayerFaction.getName()))
                {
                    event.setCancelled(true);
                    return;
                }
            }

            if(!isAllianceFriendlyFireOn)
            {
                if(ignitedPlayerFaction.getAlliances().contains(igniterPlayerFaction.getName()))
                {
                    event.setCancelled(true);
                    return;
                }
            }
        }
    }
 
Example 2
Source File: RPBlockListener7.java    From RedProtect with GNU General Public License v3.0 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onPiston(ChangeBlockEvent.Pre e) {
    RedProtect.get().logger.debug(LogLevel.BLOCKS, "RPBlockListener7 - Is onChangeBlock event");

    EventContext context = e.getContext();
    Cause cause = e.getCause();
    LocatableBlock sourceLoc = cause.first(LocatableBlock.class).orElse(null);

    if (sourceLoc != null) {
        RedProtect.get().logger.debug(LogLevel.BLOCKS, "sourceLoc");

        if (context.containsKey(EventContextKeys.PISTON_EXTEND) || context.containsKey(EventContextKeys.PISTON_RETRACT)) {
            if (RedProtect.get().config.configRoot().performance.disable_PistonEvent_handler) {
                return;
            }

            Region r = RedProtect.get().rm.getTopRegion(sourceLoc.getLocation(), this.getClass().getName());
            for (Location<World> pistonLoc : e.getLocations()) {
                Region targetr = RedProtect.get().rm.getTopRegion(pistonLoc, this.getClass().getName());

                boolean antih = RedProtect.get().config.configRoot().region_settings.anti_hopper;
                RedProtect.get().logger.debug(LogLevel.BLOCKS, "getLocations");

                if (targetr != null && (r == null || r != targetr)) {
                    if (cause.first(Player.class).isPresent() && targetr.canBuild(cause.first(Player.class).get())) {
                        continue;
                    }

                    if (antih) {
                        BlockSnapshot ib = e.getLocations().get(0).add(0, 1, 0).createSnapshot();
                        if (cont.canWorldBreak(ib) || cont.canWorldBreak(e.getLocations().get(0).createSnapshot())) {
                            continue;
                        }
                    }
                    e.setCancelled(true);
                    break;
                }
            }
        }
    }
}
 
Example 3
Source File: RPBlockListener8.java    From RedProtect with GNU General Public License v3.0 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onPiston(ChangeBlockEvent.Pre e) {
    RedProtect.get().logger.debug(LogLevel.BLOCKS, "RPBlockListener8 - Is onChangeBlock event");

    EventContext context = e.getContext();
    Cause cause = e.getCause();
    LocatableBlock sourceLoc = cause.first(LocatableBlock.class).orElse(null);

    if (sourceLoc != null) {
        RedProtect.get().logger.debug(LogLevel.BLOCKS, "sourceLoc");

        if (context.containsKey(EventContextKeys.PISTON_EXTEND) || context.containsKey(EventContextKeys.PISTON_RETRACT)) {
            if (RedProtect.get().config.configRoot().performance.disable_PistonEvent_handler) {
                return;
            }

            Region r = RedProtect.get().rm.getTopRegion(sourceLoc.getLocation(), this.getClass().getName());
            for (Location<World> pistonLoc : e.getLocations()) {
                Region targetr = RedProtect.get().rm.getTopRegion(pistonLoc, this.getClass().getName());

                boolean antih = RedProtect.get().config.configRoot().region_settings.anti_hopper;
                RedProtect.get().logger.debug(LogLevel.BLOCKS, "getLocations");

                if (targetr != null && (r == null || r != targetr)) {
                    if (cause.first(Player.class).isPresent() && targetr.canBuild(cause.first(Player.class).get())) {
                        continue;
                    }

                    if (antih) {
                        BlockSnapshot ib = e.getLocations().get(0).add(0, 1, 0).createSnapshot();
                        if (cont.canWorldBreak(ib) || cont.canWorldBreak(e.getLocations().get(0).createSnapshot())) {
                            continue;
                        }
                    }
                    e.setCancelled(true);
                    break;
                }
            }
        }
    }
}