org.spongepowered.api.event.cause.EventContextKeys Java Examples

The following examples show how to use org.spongepowered.api.event.cause.EventContextKeys. 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: EventRunner.java    From EagleFactions with MIT License 5 votes vote down vote up
/**
 * @return True if cancelled, false if not
 */
public static boolean runFactionLeaveEvent(final Player player, final Faction faction)
{
    final EventContext eventContext = EventContext.builder()
            .add(EventContextKeys.OWNER, player)
            .add(EventContextKeys.PLAYER, player)
            .add(EventContextKeys.CREATOR, player)
            .build();

    final Cause eventCause = Cause.of(eventContext, player, faction);
    final FactionLeaveEvent event = new FactionLeaveEventImpl(player, faction, eventCause);
    return Sponge.getEventManager().post(event);
}
 
Example #2
Source File: RecordingQueueManager.java    From Prism with MIT License 5 votes vote down vote up
@Override
public synchronized void run() {
    List<DataContainer> eventsSaveBatch = new ArrayList<>();

    // Assume we're iterating everything in the queue
    while (!RecordingQueue.getQueue().isEmpty()) {
        // Poll the next event, append to list
        PrismRecord record = RecordingQueue.getQueue().poll();

        if (record != null) {
            // Prepare PrismRecord for sending to a PrismRecordEvent
            PluginContainer plugin = Prism.getInstance().getPluginContainer();
            EventContext eventContext = EventContext.builder().add(EventContextKeys.PLUGIN, plugin).build();

            PrismRecordPreSaveEvent preSaveEvent = new PrismRecordPreSaveEvent(record,
                Cause.of(eventContext, plugin));

            // Tell Sponge that this PrismRecordEvent has occurred
            Sponge.getEventManager().post(preSaveEvent);

            if (!preSaveEvent.isCancelled()) {
                eventsSaveBatch.add(record.getDataContainer());
            }
        }
    }

    if (eventsSaveBatch.size() > 0) {
        try {
            Prism.getInstance().getStorageAdapter().records().write(eventsSaveBatch);
        } catch (Exception e) {
            // @todo handle failures
            e.printStackTrace();
        }
    }
}
 
Example #3
Source File: VersionHelper8.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Cause getCause(CommandSource p) {
    if (p instanceof Player)
        return Cause.of(EventContext.builder().add(EventContextKeys.PLAYER, (Player) p).build(), p);
    else
        return Cause.of(EventContext.builder().add(EventContextKeys.PLUGIN, RedProtect.get().container).build(), p);
}
 
Example #4
Source File: VersionHelper7.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Cause getCause(CommandSource p) {
    if (p instanceof Player)
        return Cause.of(EventContext.builder().add(EventContextKeys.PLAYER, (Player) p).build(), p);
    else
        return Cause.of(EventContext.builder().add(EventContextKeys.PLUGIN, RedProtect.get().container).build(), p);
}
 
Example #5
Source File: PlayerEventHandler.java    From GriefPrevention with MIT License 5 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onPlayerInteractInventoryOpen(InteractInventoryEvent.Open event, @First Player player) {
    if (!GPFlags.INTERACT_INVENTORY || !GriefPreventionPlugin.instance.claimsEnabledForWorld(player.getWorld().getProperties())) {
        return;
    }

    final Cause cause = event.getCause();
    final EventContext context = cause.getContext();
    final BlockSnapshot blockSnapshot = context.get(EventContextKeys.BLOCK_HIT).orElse(BlockSnapshot.NONE);
    if (blockSnapshot == BlockSnapshot.NONE) {
        return;
    }
    if (GriefPreventionPlugin.isTargetIdBlacklisted(ClaimFlag.INTERACT_INVENTORY.toString(), blockSnapshot, player.getWorld().getProperties())) {
        return;
    }

    GPTimings.PLAYER_INTERACT_INVENTORY_OPEN_EVENT.startTimingIfSync();
    final Location<World> location = blockSnapshot.getLocation().get();
    final GPClaim claim = this.dataStore.getClaimAt(location);
    final Tristate result = GPPermissionHandler.getClaimPermission(event, location, claim, GPPermissions.INVENTORY_OPEN, player, blockSnapshot, player, TrustType.CONTAINER, true);
    if (result == Tristate.FALSE) {
        Text message = GriefPreventionPlugin.instance.messageData.permissionInventoryOpen
                .apply(ImmutableMap.of(
                "owner", claim.getOwnerName(),
                "block", blockSnapshot.getState().getType().getId())).build();
        GriefPreventionPlugin.sendClaimDenyMessage(claim, player, message);
        ((EntityPlayerMP) player).closeScreen();
        event.setCancelled(true);
    }

    GPTimings.PLAYER_INTERACT_INVENTORY_OPEN_EVENT.stopTimingIfSync();
}
 
Example #6
Source File: LPSubjectDataUpdateEvent.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public @NonNull Cause getCause() {
    EventContext eventContext = EventContext.builder()
            .add(EventContextKeys.PLUGIN, this.plugin.getBootstrap().getPluginContainer())
            .build();

    return Cause.builder()
            .append(this.plugin.getService())
            .append(this.plugin.getService().sponge())
            .append(this.plugin.getBootstrap().getPluginContainer())
            .build(eventContext);
}
 
Example #7
Source File: PlayerInteractListener.java    From EagleFactions with MIT License 5 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onItemUse(final InteractItemEvent event, @Root final Player player)
{
    if (event.getItemStack() == ItemStackSnapshot.NONE)
        return;

    final Vector3d interactionPoint = event.getInteractionPoint().orElse(player.getLocation().getPosition());
    Location<World> location = new Location<>(player.getWorld(), interactionPoint);

    //Handle hitting entities
    boolean hasHitEntity = event.getContext().containsKey(EventContextKeys.ENTITY_HIT);
    if(hasHitEntity)
    {
        final Entity hitEntity = event.getContext().get(EventContextKeys.ENTITY_HIT).get();
        if (hitEntity instanceof Living && !(hitEntity instanceof ArmorStand))
            return;

        location = hitEntity.getLocation();
    }

    final ProtectionResult protectionResult = super.getPlugin().getProtectionManager().canUseItem(location, player, event.getItemStack(), true);
    if (!protectionResult.hasAccess())
    {
        event.setCancelled(true);
        return;
    }
}
 
Example #8
Source File: EventRunner.java    From EagleFactions with MIT License 5 votes vote down vote up
public static boolean runFactionDisbandEvent(final Player player, final Faction playerFaction)
{
    final EventContext eventContext = EventContext.builder()
            .add(EventContextKeys.OWNER, player)
            .add(EventContextKeys.PLAYER, player)
            .add(EventContextKeys.CREATOR, player)
            .build();

    final Cause cause = Cause.of(eventContext, player, playerFaction);
    final FactionDisbandEvent event = new FactionAreaEnterEventImp(player, playerFaction, cause);
    return Sponge.getEventManager().post(event);
}
 
Example #9
Source File: EventRunner.java    From EagleFactions with MIT License 5 votes vote down vote up
/**
    * @return True if cancelled, false if not
    */
public static boolean runFactionAreaEnterEvent(final MoveEntityEvent moveEntityEvent, final Player player, final Optional<Faction> enteredFaction, final Optional<Faction> leftFaction)
{
    final EventContext eventContext = EventContext.builder()
           .add(EventContextKeys.OWNER, player)
           .add(EventContextKeys.PLAYER, player)
           .add(EventContextKeys.CREATOR, player)
           .build();

       final Cause eventCause = Cause.of(eventContext, player, enteredFaction, leftFaction);
       final FactionAreaEnterEvent event = new FactionAreaEnterEventImpl(moveEntityEvent, player, enteredFaction, leftFaction, eventCause);
       return Sponge.getEventManager().post(event);
}
 
Example #10
Source File: EventRunner.java    From EagleFactions with MIT License 5 votes vote down vote up
/**
 * @return True if cancelled, false if not
 */
public static boolean runFactionUnclaimEvent(final Player player, final Faction faction, final World world, final Vector3i chunkPosition)
{
    final EventContext eventContext = EventContext.builder()
            .add(EventContextKeys.OWNER, player)
            .add(EventContextKeys.PLAYER, player)
            .add(EventContextKeys.CREATOR, player)
            .build();

    final Cause eventCause = Cause.of(eventContext, player, faction);
    final FactionClaimEvent.Unclaim event = new FactionUnclaimEventImpl(player, faction, world, chunkPosition, eventCause);
    return Sponge.getEventManager().post(event);
}
 
Example #11
Source File: EventRunner.java    From EagleFactions with MIT License 5 votes vote down vote up
/**
 * @return True if cancelled, false if not
 */
public static boolean runFactionKickEvent(final FactionPlayer kickedPlayer, final Player kickedBy, final Faction faction)
{
    final EventContext eventContext = EventContext.builder()
            .add(EventContextKeys.OWNER, kickedBy)
            .add(EventContextKeys.PLAYER, kickedBy)
            .add(EventContextKeys.CREATOR, kickedBy)
            .build();

    final Cause eventCause = Cause.of(eventContext, kickedBy, faction);
    final FactionKickEvent event = new FactionKickEventImpl(kickedPlayer, kickedBy, faction, eventCause);
    return Sponge.getEventManager().post(event);
}
 
Example #12
Source File: EventRunner.java    From EagleFactions with MIT License 5 votes vote down vote up
/**
 * @return True if cancelled, false if not
 */
public static boolean runFactionCreateEvent(final Player player, final Faction faction)
{
    final EventContext eventContext = EventContext.builder()
            .add(EventContextKeys.OWNER, player)
            .add(EventContextKeys.PLAYER, player)
            .add(EventContextKeys.CREATOR, player)
            .build();

    final Cause eventCause = Cause.of(eventContext, player, faction);
    final FactionCreateEvent event = new FactionCreateEventImpl(player, faction, eventCause);
    return Sponge.getEventManager().post(event);
}
 
Example #13
Source File: EventRunner.java    From EagleFactions with MIT License 5 votes vote down vote up
/**
 * @return True if cancelled, false if not
 */
public static boolean runFactionClaimEvent(final Player player, final Faction faction, final World world, final Vector3i chunkPosition)
{
    final EventContext eventContext = EventContext.builder()
            .add(EventContextKeys.OWNER, player)
            .add(EventContextKeys.PLAYER, player)
            .add(EventContextKeys.CREATOR, player)
            .build();

    final Cause eventCause = Cause.of(eventContext, player, faction);
    final FactionClaimEvent event = new FactionClaimEventImpl(player, faction, world, chunkPosition, eventCause);
    return Sponge.getEventManager().post(event);
}
 
Example #14
Source File: EventRunner.java    From EagleFactions with MIT License 5 votes vote down vote up
/**
 * @return True if cancelled, false if not
 */
public static boolean runFactionChestEvent(final Player player, final Faction faction)
{
    final EventContext eventContext = EventContext.builder()
            .add(EventContextKeys.OWNER, player)
            .add(EventContextKeys.PLAYER, player)
            .add(EventContextKeys.CREATOR, player)
            .build();

    final Cause eventCause = Cause.of(eventContext, player, faction);
    final FactionChestEvent event = new FactionChestEventImpl(player, faction, eventCause);
    return Sponge.getEventManager().post(event);
}
 
Example #15
Source File: EventRunner.java    From EagleFactions with MIT License 5 votes vote down vote up
public static boolean runFactionJoinEvent(final Player player, final Faction faction)
{
    final EventContext eventContext = EventContext.builder()
        .add(EventContextKeys.OWNER, player)
        .add(EventContextKeys.PLAYER, player)
        .add(EventContextKeys.CREATOR, player)
        .build();

    final Cause eventCause = Cause.of(eventContext, player, faction);
    final FactionJoinEvent event = new FactionJoinEventImpl(player, faction, eventCause);
    return Sponge.getEventManager().post(event);
}
 
Example #16
Source File: PlayerEventHandler.java    From GriefDefender with MIT License 5 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onPlayerInteractInventoryOpen(InteractInventoryEvent.Open event, @First Player player) {
    if (!GDFlags.INTERACT_INVENTORY || !GriefDefenderPlugin.getInstance().claimsEnabledForWorld(player.getWorld().getUniqueId())) {
        return;
    }

    final Cause cause = event.getCause();
    final EventContext context = cause.getContext();
    final BlockSnapshot blockSnapshot = context.get(EventContextKeys.BLOCK_HIT).orElse(BlockSnapshot.NONE);
    if (blockSnapshot == BlockSnapshot.NONE) {
        return;
    }
    if (GriefDefenderPlugin.isTargetIdBlacklisted(Flags.INTERACT_INVENTORY.getName(), blockSnapshot, player.getWorld().getProperties())) {
        return;
    }

    GDTimings.PLAYER_INTERACT_INVENTORY_OPEN_EVENT.startTimingIfSync();
    final Location<World> location = blockSnapshot.getLocation().get();
    final GDClaim claim = this.dataStore.getClaimAt(location);
    final Tristate result = GDPermissionManager.getInstance().getFinalPermission(event, location, claim, Flags.INTERACT_INVENTORY, player, blockSnapshot, player, TrustTypes.CONTAINER, true);
    if (result == Tristate.FALSE) {
        final Component message = GriefDefenderPlugin.getInstance().messageData.getMessage(MessageStorage.PERMISSION_INVENTORY_OPEN,
                ImmutableMap.of(
                "player", claim.getOwnerName(),
                "block", blockSnapshot.getState().getType().getId()));
        GriefDefenderPlugin.sendClaimDenyMessage(claim, player, message);
        NMSUtil.getInstance().closePlayerScreen(player);
        event.setCancelled(true);
    }

    GDTimings.PLAYER_INTERACT_INVENTORY_OPEN_EVENT.stopTimingIfSync();
}
 
Example #17
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 #18
Source File: ExplosionListener.java    From EagleFactions with MIT License 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onExplosionPre(final ExplosionEvent.Pre event)
{
    final EventContext context = event.getContext();
    final Cause cause = event.getCause();

    User user = null;
    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();
    }

    final Location<World> location = event.getExplosion().getLocation();
    if (user == null)
    {
        if(!super.getPlugin().getProtectionManager().canExplode(location).hasAccess())
        {
            event.setCancelled(true);
            return;
        }
    }
    else
    {
        if (!super.getPlugin().getProtectionManager().canExplode(location, user, false).hasAccess())
        {
            event.setCancelled(true);
            return;
        }
    }
}
 
Example #19
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 #20
Source File: PlayerEventHandler.java    From GriefPrevention with MIT License 4 votes vote down vote up
public InteractEvent handleItemInteract(InteractEvent event, Player player, World world, ItemStack itemInHand) {
    if (lastInteractItemSecondaryTick == Sponge.getServer().getRunningTimeTicks() || lastInteractItemPrimaryTick == Sponge.getServer().getRunningTimeTicks()) {
        // ignore
        return event;
    }

    if (event instanceof InteractItemEvent.Primary) {
        lastInteractItemPrimaryTick = Sponge.getServer().getRunningTimeTicks();
    } else {
        lastInteractItemSecondaryTick = Sponge.getServer().getRunningTimeTicks();
    }

    final ItemType itemType = itemInHand.getType();
    if (itemInHand.isEmpty() || itemType instanceof ItemFood) {
        return event;
    }

    final boolean primaryEvent = event instanceof InteractItemEvent.Primary || event instanceof InteractBlockEvent.Primary;
    if (!GPFlags.INTERACT_ITEM_PRIMARY && primaryEvent || !GPFlags.INTERACT_ITEM_SECONDARY && !primaryEvent || !GriefPreventionPlugin.instance.claimsEnabledForWorld(world.getProperties())) {
        return event;
    }

    if (primaryEvent && GriefPreventionPlugin.isTargetIdBlacklisted(ClaimFlag.INTERACT_ITEM_PRIMARY.toString(), itemInHand.getType(), world.getProperties())) {
        return event;
    }
    if (!primaryEvent && GriefPreventionPlugin.isTargetIdBlacklisted(ClaimFlag.INTERACT_ITEM_SECONDARY.toString(), itemInHand.getType(), world.getProperties())) {
        return event;
    }

    final Cause cause = event.getCause();
    final EventContext context = cause.getContext();
    final BlockSnapshot blockSnapshot = context.get(EventContextKeys.BLOCK_HIT).orElse(BlockSnapshot.NONE);
    final Vector3d interactPoint = event.getInteractionPoint().orElse(null);
    final Entity entity = context.get(EventContextKeys.ENTITY_HIT).orElse(null);
    final Location<World> location = entity != null ? entity.getLocation() 
            : blockSnapshot != BlockSnapshot.NONE ? blockSnapshot.getLocation().get() 
                    : interactPoint != null ? new Location<World>(world, interactPoint) 
                            : player.getLocation();

    final String ITEM_PERMISSION = primaryEvent ? GPPermissions.INTERACT_ITEM_PRIMARY : GPPermissions.INTERACT_ITEM_SECONDARY;
    final GPPlayerData playerData = this.dataStore.getOrCreatePlayerData(player.getWorld(), player.getUniqueId());
    if (!itemInHand.isEmpty() && (itemInHand.getType().equals(GriefPreventionPlugin.instance.modificationTool) ||
            itemInHand.getType().equals(GriefPreventionPlugin.instance.investigationTool))) {
        GPPermissionHandler.addEventLogEntry(event, location, itemInHand, blockSnapshot == null ? entity : blockSnapshot, player, ITEM_PERMISSION, null, Tristate.TRUE);
        if (investigateClaim(event, player, blockSnapshot, itemInHand)) {
            return event;
        }

        onPlayerHandleShovelAction(event, blockSnapshot, player,  ((HandInteractEvent) event).getHandType(), playerData);
        return event;
    }

    final GPClaim claim = this.dataStore.getClaimAtPlayer(location, playerData, true);
    if (GPPermissionHandler.getClaimPermission(event, location, claim, ITEM_PERMISSION, player, itemType, player, TrustType.ACCESSOR, true) == Tristate.FALSE) {
        Text message = GriefPreventionPlugin.instance.messageData.permissionInteractItem
                .apply(ImmutableMap.of(
                "owner", claim.getOwnerName(),
                "item", itemInHand.getType().getId())).build();
        GriefPreventionPlugin.sendClaimDenyMessage(claim, player, message);
        if (event instanceof InteractItemEvent) {
            if (!SpongeImplHooks.isFakePlayer(((EntityPlayerMP) player)) && itemType == ItemTypes.WRITABLE_BOOK) {
                ((EntityPlayerMP) player).closeScreen();
            }
        }
        event.setCancelled(true);
    }
    return event;
}
 
Example #21
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 #22
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;
                }
            }
        }
    }
}