Java Code Examples for org.spongepowered.api.util.Tristate#FALSE

The following examples show how to use org.spongepowered.api.util.Tristate#FALSE . 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: FactionKickListener.java    From EagleFactions with MIT License 6 votes vote down vote up
@Listener(order = Order.POST)
@IsCancelled(value = Tristate.FALSE)
public void onPlayerFactionKick(final FactionKickEvent event)
{
    final Faction faction = event.getFaction();
    final FactionPlayer kickedPlayer = event.getKickedPlayer();

    final List<Player> onlineFactionPlayers = super.getPlugin().getFactionLogic().getOnlinePlayers(faction);
    for(final Player player : onlineFactionPlayers)
    {
        if (player.equals(event.getCreator()))
            continue;

        if(player.getName().equals(kickedPlayer.getName()))
            continue;

        player.sendMessage(Text.of(PluginInfo.PLUGIN_PREFIX, "Player " + kickedPlayer.getName() + " has been kicked from the faction."));
    }
}
 
Example 2
Source File: PlayerEventHandler.java    From GriefPrevention with MIT License 6 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onPlayerUseItem(UseItemStackEvent.Start event, @First Player player) {
    if (!GPFlags.ITEM_USE || !GriefPreventionPlugin.instance.claimsEnabledForWorld(player.getWorld().getProperties())) {
        return;
    }
    if (GriefPreventionPlugin.isTargetIdBlacklisted(ClaimFlag.ITEM_USE.toString(), event.getItemStackInUse().getType(), player.getWorld().getProperties())) {
        return;
    }

    GPTimings.PLAYER_USE_ITEM_EVENT.startTimingIfSync();
    Location<World> location = player.getLocation();
    GPPlayerData playerData = this.dataStore.getOrCreatePlayerData(location.getExtent(), player.getUniqueId());
    GPClaim claim = this.dataStore.getClaimAtPlayer(playerData, location);

    final Tristate result = GPPermissionHandler.getClaimPermission(event, location, claim, GPPermissions.ITEM_USE, player, event.getItemStackInUse().getType(), player, TrustType.ACCESSOR, true);
    if (result == Tristate.FALSE) {
        final Text message = GriefPreventionPlugin.instance.messageData.permissionItemUse
                .apply(ImmutableMap.of(
                "item", event.getItemStackInUse().getType().getId())).build();
        GriefPreventionPlugin.sendClaimDenyMessage(claim, player,  message);
        event.setCancelled(true);
    }
    GPTimings.PLAYER_USE_ITEM_EVENT.stopTimingIfSync();
}
 
Example 3
Source File: CommandHelper.java    From GriefPrevention with MIT License 6 votes vote down vote up
public static Consumer<CommandSource> createFlagConsumer(CommandSource src, Subject subject, String subjectName, Set<Context> contexts, GPClaim claim, String flagPermission, Tristate flagValue, String source) {
    return consumer -> {
        String target = flagPermission.replace(GPPermissions.FLAG_BASE + ".", "");
        if (target.isEmpty()) {
            target = "any";
        }
        Tristate newValue = Tristate.UNDEFINED;
        if (flagValue == Tristate.TRUE) {
            newValue = Tristate.FALSE;
        } else if (flagValue == Tristate.UNDEFINED) {
            newValue = Tristate.TRUE;
        }

        CommandHelper.applyFlagPermission(src, subject, subjectName, claim, flagPermission, source, target, newValue, null, FlagType.GROUP);
    };
}
 
Example 4
Source File: PlayerEventHandler.java    From GriefPrevention with MIT License 6 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onPlayerInteractInventoryClose(InteractInventoryEvent.Close event, @Root Player player) {
    final ItemStackSnapshot cursor = event.getCursorTransaction().getOriginal();
    if (cursor == ItemStackSnapshot.NONE || !GPFlags.ITEM_DROP || !GriefPreventionPlugin.instance.claimsEnabledForWorld(player.getWorld().getProperties())) {
        return;
    }
    if (GriefPreventionPlugin.isTargetIdBlacklisted(ClaimFlag.ITEM_DROP.toString(), cursor, player.getWorld().getProperties())) {
        return;
    }

    GPTimings.PLAYER_INTERACT_INVENTORY_CLOSE_EVENT.startTimingIfSync();
    final Location<World> location = player.getLocation();
    final GPClaim claim = this.dataStore.getClaimAt(location);
    if (GPPermissionHandler.getClaimPermission(event, location, claim, GPPermissions.ITEM_DROP, player, cursor, player, TrustType.ACCESSOR, true) == Tristate.FALSE) {
        Text message = GriefPreventionPlugin.instance.messageData.permissionItemDrop
                .apply(ImmutableMap.of(
                "owner", claim.getOwnerName(),
                "item", cursor.getType().getId())).build();
        GriefPreventionPlugin.sendClaimDenyMessage(claim, player, message);
        event.setCancelled(true);
    }

    GPTimings.PLAYER_INTERACT_INVENTORY_CLOSE_EVENT.stopTimingIfSync();
}
 
Example 5
Source File: FactionLeaveListener.java    From EagleFactions with MIT License 5 votes vote down vote up
@Listener(order = Order.POST)
@IsCancelled(value = Tristate.FALSE)
public void onFactionLeave(final FactionLeaveEvent event, @Root final Player player)
{
    //Notify other faction members about someone leaving the faction
    final Faction faction = event.getFaction();
    final List<Player> factionPlayers = super.getPlugin().getFactionLogic().getOnlinePlayers(faction);
    for (final Player factionPlayer : factionPlayers)
    {
        if (factionPlayer.getName().equals(player.getName()))
            continue;
        factionPlayer.sendMessage(Text.of(PluginInfo.PLUGIN_PREFIX, TextColors.GOLD, player.getName(), TextColors.AQUA, " left your faction."));
    }
}
 
Example 6
Source File: BlockEventHandler.java    From GriefPrevention with MIT License 5 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onProjectileImpactBlock(CollideBlockEvent.Impact event) {
    if (!GPFlags.PROJECTILE_IMPACT_BLOCK || !(event.getSource() instanceof Entity)) {
        return;
    }

    final Entity source = (Entity) event.getSource();
    if (GriefPreventionPlugin.isSourceIdBlacklisted(ClaimFlag.PROJECTILE_IMPACT_BLOCK.toString(), source.getType().getId(), source.getWorld().getProperties())) {
        return;
    }

    final User user = CauseContextHelper.getEventUser(event);
    if (user == null) {
        return;
    }

    if (!GriefPreventionPlugin.instance.claimsEnabledForWorld(event.getImpactPoint().getExtent().getProperties())) {
        return;
    }

    GPTimings.PROJECTILE_IMPACT_BLOCK_EVENT.startTimingIfSync();
    Location<World> impactPoint = event.getImpactPoint();
    GPClaim targetClaim = null;
    GPPlayerData playerData = null;
    if (user instanceof Player) {
        playerData = GriefPreventionPlugin.instance.dataStore.getOrCreatePlayerData(event.getTargetLocation().getExtent(), user.getUniqueId());
        targetClaim = this.dataStore.getClaimAtPlayer(playerData, impactPoint);
    } else {
        targetClaim = this.dataStore.getClaimAt(impactPoint);
    }

    Tristate result = GPPermissionHandler.getClaimPermission(event, impactPoint, targetClaim, GPPermissions.PROJECTILE_IMPACT_BLOCK, source, event.getTargetBlock(), user, TrustType.ACCESSOR, true);
    if (result == Tristate.FALSE) {
        event.setCancelled(true);
        GPTimings.PROJECTILE_IMPACT_BLOCK_EVENT.stopTimingIfSync();
        return;
    }

    GPTimings.PROJECTILE_IMPACT_BLOCK_EVENT.stopTimingIfSync();
}
 
Example 7
Source File: CommandHelper.java    From GriefPrevention with MIT License 5 votes vote down vote up
public static Consumer<CommandSource> createFlagConsumer(CommandSource src, Subject subject, String subjectName, Set<Context> contexts, String flagPermission, Tristate flagValue, FlagType flagType) {
    return consumer -> {
        Tristate newValue = Tristate.UNDEFINED;
        if (flagValue == Tristate.TRUE) {
            newValue = Tristate.FALSE;
        } else if (flagValue == Tristate.UNDEFINED) {
            newValue = Tristate.TRUE;
        }

        Text flagTypeText = Text.of();
        if (flagType == FlagType.OVERRIDE) {
            flagTypeText = Text.of(TextColors.RED, "OVERRIDE");
        } else if (flagType == FlagType.DEFAULT) {
            flagTypeText = Text.of(TextColors.LIGHT_PURPLE, "DEFAULT");
        } else if (flagType == FlagType.CLAIM) {
            flagTypeText = Text.of(TextColors.GOLD, "CLAIM");
        }
        String target = flagPermission.replace(GPPermissions.FLAG_BASE + ".",  "");
        Set<Context> newContexts = new HashSet<>(contexts);
        subject.getSubjectData().setPermission(newContexts, flagPermission, newValue);
        src.sendMessage(Text.of(
                TextColors.GREEN, "Set ", flagTypeText, " permission ", 
                TextColors.AQUA, target, 
                TextColors.GREEN, "\n to ", 
                TextColors.LIGHT_PURPLE, getClickableText(src, subject, subjectName, newContexts, flagPermission, newValue, flagType), 
                TextColors.GREEN, " for ", 
                TextColors.GOLD, subjectName));
    };
}
 
Example 8
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 9
Source File: EntityEventHandler.java    From GriefPrevention with MIT License 5 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onProjectileImpactEntity(CollideEntityEvent.Impact event) {
    if (!GPFlags.PROJECTILE_IMPACT_ENTITY) {
        return;
    }
    if (GriefPreventionPlugin.isSourceIdBlacklisted(ClaimFlag.PROJECTILE_IMPACT_ENTITY.toString(), event.getSource(), event.getImpactPoint().getExtent().getProperties())) {
        return;
    }

    final User user = CauseContextHelper.getEventUser(event);
    if (user == null || !GriefPreventionPlugin.instance.claimsEnabledForWorld(event.getImpactPoint().getExtent().getProperties())) {
        return;
    }

    GPTimings.PROJECTILE_IMPACT_ENTITY_EVENT.startTimingIfSync();
    Object source = event.getCause().root();
    Location<World> impactPoint = event.getImpactPoint();
    GPClaim targetClaim = null;
    for (Entity entity : event.getEntities()) {
        if (GriefPreventionPlugin.isTargetIdBlacklisted(ClaimFlag.PROJECTILE_IMPACT_ENTITY.toString(), entity, event.getImpactPoint().getExtent().getProperties())) {
            return;
        }
        targetClaim = this.dataStore.getClaimAt(impactPoint, targetClaim);
        final Tristate result = GPPermissionHandler.getClaimPermission(event, impactPoint, targetClaim, GPPermissions.PROJECTILE_IMPACT_ENTITY, source, entity, user, TrustType.ACCESSOR, true);
        if (result == Tristate.FALSE) {
            if (GPPermissionHandler.getClaimPermission(event, impactPoint, targetClaim, GPPermissions.PROJECTILE_IMPACT_ENTITY, source, entity, user) == Tristate.TRUE) {
                GPTimings.PROJECTILE_IMPACT_ENTITY_EVENT.stopTimingIfSync();
                return;
            }

            event.setCancelled(true);
        }
    }
    GPTimings.PROJECTILE_IMPACT_ENTITY_EVENT.stopTimingIfSync();
}
 
Example 10
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 11
Source File: FallbackPermActions.java    From ChatUI with MIT License 5 votes vote down vote up
@Override
public CompletableFuture<Boolean> setPermission(Player player, Subject subject, Set<Context> contexts, String permission, Tristate value) {
    Boolean perm = subject.getSubjectData().getPermissions(contexts).get(permission);
    if ((perm == null && value == Tristate.UNDEFINED)
            || (perm == Boolean.TRUE && value == Tristate.TRUE)
            || (perm == Boolean.FALSE && value == Tristate.FALSE)) {
        return CompletableFuture.completedFuture(true);
    }
    return subject.getSubjectData().setPermission(contexts, permission, value);
}
 
Example 12
Source File: PlayerEventHandler.java    From GriefPrevention with MIT License 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onPlayerDispenseItem(DropItemEvent.Dispense event, @Root Entity spawncause) {
    if (!GPFlags.ITEM_DROP || !(spawncause instanceof User)) {
        return;
    }

    final User user = (User) spawncause;
    final World world = spawncause.getWorld();
    if (!GriefPreventionPlugin.instance.claimsEnabledForWorld(world.getProperties())) {
        return;
    }

    // in creative worlds, dropping items is blocked
    if (GriefPreventionPlugin.instance.claimModeIsActive(world.getProperties(), ClaimsMode.Creative)) {
        event.setCancelled(true);
        return;
    }

    GPTimings.PLAYER_DISPENSE_ITEM_EVENT.startTimingIfSync();
    Player player = user instanceof Player ? (Player) user : null;
    GPPlayerData playerData = this.dataStore.getOrCreatePlayerData(world, user.getUniqueId());

    // FEATURE: players in PvP combat, can't throw items on the ground to hide
    // them or give them away to other players before they are defeated

    // if in combat, don't let him drop it
    if (player != null && !GriefPreventionPlugin.getActiveConfig(world.getProperties()).getConfig().pvp.allowCombatItemDrops && playerData.inPvpCombat(player.getWorld())) {
        GriefPreventionPlugin.sendMessage(player, GriefPreventionPlugin.instance.messageData.pvpNoItemDrop.toText());
        event.setCancelled(true);
        GPTimings.PLAYER_DISPENSE_ITEM_EVENT.stopTimingIfSync();
        return;
    }

    for (Entity entityItem : event.getEntities()) {
        if (GriefPreventionPlugin.isTargetIdBlacklisted(ClaimFlag.ITEM_DROP.toString(), entityItem, world.getProperties())) {
            continue;
        }

        Location<World> location = entityItem.getLocation();
        GPClaim claim = this.dataStore.getClaimAtPlayer(playerData, location);
        if (claim != null) {
            final Tristate override = GPPermissionHandler.getFlagOverride(event, location, claim, GPPermissions.ITEM_DROP,  user, entityItem, user, playerData, true);
            if (override != Tristate.UNDEFINED) {
                if (override == Tristate.TRUE) {
                    GPTimings.PLAYER_DISPENSE_ITEM_EVENT.stopTimingIfSync();
                    return;
                }

                event.setCancelled(true);
                GPTimings.PLAYER_DISPENSE_ITEM_EVENT.stopTimingIfSync();
                return;
            }

            // allow trusted users
            if (claim.isUserTrusted(user, TrustType.ACCESSOR)) {
                GPTimings.PLAYER_DISPENSE_ITEM_EVENT.stopTimingIfSync();
                return;
            }

            Tristate perm = GPPermissionHandler.getClaimPermission(event, location, claim, GPPermissions.ITEM_DROP, user, entityItem, user);
            if (perm == Tristate.TRUE) {
                GPTimings.PLAYER_DISPENSE_ITEM_EVENT.stopTimingIfSync();
                return;
            } else if (perm == Tristate.FALSE) {
                event.setCancelled(true);
                if (spawncause instanceof Player) {
                    final Text message = GriefPreventionPlugin.instance.messageData.permissionItemDrop
                            .apply(ImmutableMap.of(
                            "owner", claim.getOwnerName(),
                            "item", entityItem.getType().getId())).build();
                    GriefPreventionPlugin.sendClaimDenyMessage(claim, player, message);
                }
                GPTimings.PLAYER_DISPENSE_ITEM_EVENT.stopTimingIfSync();
                return;
            }
        }
    }
    GPTimings.PLAYER_DISPENSE_ITEM_EVENT.stopTimingIfSync();
}
 
Example 13
Source File: EntityEventHandler.java    From GriefPrevention with MIT License 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onEntityExplosionPre(ExplosionEvent.Pre 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_PRE_EVENT.startTimingIfSync();
    Location<World> location = event.getExplosion().getLocation();
    GPClaim claim =  GriefPreventionPlugin.instance.dataStore.getClaimAt(location);
    User user = CauseContextHelper.getEventUser(event);
    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;
            }
        }
    }

    Tristate result = Tristate.UNDEFINED;
    if (GPFlags.EXPLOSION_SURFACE && location.getPosition().getY() > ((net.minecraft.world.World) location.getExtent()).getSeaLevel()) {
        result = GPPermissionHandler.getClaimPermission(event, location, claim, GPPermissions.EXPLOSION_SURFACE, source, location.getBlock(), user, true);
    } else {
        result = GPPermissionHandler.getClaimPermission(event, location, claim, GPPermissions.EXPLOSION, source, location.getBlock(), user, true);
    }

    if(result == Tristate.FALSE) {
        event.setCancelled(true);
        GPTimings.ENTITY_EXPLOSION_PRE_EVENT.stopTimingIfSync();
        return;
    }

    GPTimings.ENTITY_EXPLOSION_PRE_EVENT.stopTimingIfSync();
}
 
Example 14
Source File: PlayerEventHandler.java    From GriefPrevention with MIT License 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onPlayerInteractEntity(InteractEntityEvent.Secondary event, @First Player player) {
    if (!GPFlags.INTERACT_ENTITY_SECONDARY || !GriefPreventionPlugin.instance.claimsEnabledForWorld(player.getWorld().getProperties())) {
        return;
    }

    final Entity targetEntity = event.getTargetEntity();
    final HandType handType = event.getHandType();
    final ItemStack itemInHand = player.getItemInHand(handType).orElse(ItemStack.empty());
    final Object source = !itemInHand.isEmpty() ? itemInHand : player;
    if (GriefPreventionPlugin.isTargetIdBlacklisted(ClaimFlag.INTERACT_ENTITY_SECONDARY.toString(), targetEntity, player.getWorld().getProperties())) {
        return;
    }

    GPTimings.PLAYER_INTERACT_ENTITY_SECONDARY_EVENT.startTimingIfSync();
    Location<World> location = targetEntity.getLocation();
    GPClaim claim = this.dataStore.getClaimAt(location);
    GPPlayerData playerData = this.dataStore.getOrCreatePlayerData(player.getWorld(), player.getUniqueId());

    // if entity is living and has an owner, apply special rules
    if (targetEntity instanceof Living) {
        EntityBridge spongeEntity = (EntityBridge) targetEntity;
        Optional<User> owner = ((OwnershipTrackedBridge) spongeEntity).tracked$getOwnerReference();
        if (owner.isPresent()) {
            UUID ownerID = owner.get().getUniqueId();
            // if the player interacting is the owner or an admin in ignore claims mode, always allow
            if (player.getUniqueId().equals(ownerID) || playerData.canIgnoreClaim(claim)) {
                // if giving away pet, do that instead
                if (playerData.petGiveawayRecipient != null) {
                    SpongeEntityType spongeEntityType = (SpongeEntityType) ((Entity) spongeEntity).getType();
                    if (spongeEntityType.equals(EntityTypes.UNKNOWN) || !spongeEntityType.getModId().equalsIgnoreCase("minecraft")) {
                        final Text message = GriefPreventionPlugin.instance.messageData.commandPetInvalid
                                .apply(ImmutableMap.of(
                                "type", spongeEntityType.getId())).build();
                        GriefPreventionPlugin.sendMessage(player, message);
                        playerData.petGiveawayRecipient = null;
                        GPTimings.PLAYER_INTERACT_ENTITY_SECONDARY_EVENT.stopTimingIfSync();
                        return;
                    }
                    ((Entity) spongeEntity).setCreator(playerData.petGiveawayRecipient.getUniqueId());
                    if (targetEntity instanceof EntityTameable) {
                        EntityTameable tameable = (EntityTameable) targetEntity;
                        tameable.setOwnerId(playerData.petGiveawayRecipient.getUniqueId());
                    } else if (targetEntity instanceof EntityHorse) {
                        EntityHorse horse = (EntityHorse) targetEntity;
                        horse.setOwnerUniqueId(playerData.petGiveawayRecipient.getUniqueId());
                        horse.setHorseTamed(true);
                    }
                    playerData.petGiveawayRecipient = null;
                    GriefPreventionPlugin.sendMessage(player, GriefPreventionPlugin.instance.messageData.commandPetConfirmation.toText());
                    event.setCancelled(true);
                    this.sendInteractEntityDenyMessage(itemInHand, targetEntity, claim, player, handType);
                }
                GPTimings.PLAYER_INTERACT_ENTITY_SECONDARY_EVENT.stopTimingIfSync();
                return;
            }
        }
    }

    if (playerData.canIgnoreClaim(claim)) {
        GPTimings.PLAYER_INTERACT_ENTITY_SECONDARY_EVENT.stopTimingIfSync();
        return;
    }

    Tristate result = GPPermissionHandler.getClaimPermission(event, location, claim, GPPermissions.INTERACT_ENTITY_SECONDARY, source, targetEntity, player, TrustType.ACCESSOR, true);
    if (result == Tristate.TRUE && targetEntity instanceof ArmorStand) {
        result = GPPermissionHandler.getClaimPermission(event, location, claim, GPPermissions.INVENTORY_OPEN, source, targetEntity, player, TrustType.CONTAINER, false);
    }
    if (result == Tristate.FALSE) {
        event.setCancelled(true);
        this.sendInteractEntityDenyMessage(itemInHand, targetEntity, claim, player, handType);
        GPTimings.PLAYER_INTERACT_ENTITY_SECONDARY_EVENT.stopTimingIfSync();
    }
}
 
Example 15
Source File: PlayerEventHandler.java    From GriefPrevention with MIT License 4 votes vote down vote up
@Listener(order = Order.LAST, beforeModifications = true)
public void onPlayerPickupItem(ChangeInventoryEvent.Pickup.Pre event, @Root Player player) {
    if (!GPFlags.ITEM_PICKUP || !GriefPreventionPlugin.instance.claimsEnabledForWorld(player.getWorld().getProperties())) {
        return;
    }
    if (GriefPreventionPlugin.isTargetIdBlacklisted(ClaimFlag.ITEM_PICKUP.toString(), event.getTargetEntity(), player.getWorld().getProperties())) {
        return;
    }

    GPTimings.PLAYER_PICKUP_ITEM_EVENT.startTimingIfSync();
    final World world = player.getWorld();
    GPPlayerData playerData = this.dataStore.getOrCreatePlayerData(world, player.getUniqueId());
    Location<World> location = player.getLocation();
    GPClaim claim = this.dataStore.getClaimAtPlayer(playerData, location);
    if (GPPermissionHandler.getClaimPermission(event, location, claim, GPPermissions.ITEM_PICKUP, player, event.getTargetEntity(), player, TrustType.ACCESSOR, true) == Tristate.FALSE) {
        event.setCancelled(true);
        GPTimings.PLAYER_PICKUP_ITEM_EVENT.stopTimingIfSync();
        return;
    }

    // the rest of this code is specific to pvp worlds
    if (claim.pvpRulesApply()) {
        // if we're preventing spawn camping and the player was previously empty handed...
        if (GriefPreventionPlugin.getActiveConfig(world.getProperties()).getConfig().pvp.protectFreshSpawns && PlayerUtils.hasItemInOneHand(player, ItemTypes.NONE)) {
            // if that player is currently immune to pvp
            if (playerData.pvpImmune) {
                // if it's been less than 10 seconds since the last time he spawned, don't pick up the item
                long now = Calendar.getInstance().getTimeInMillis();
                long elapsedSinceLastSpawn = now - playerData.lastSpawn;
                if (elapsedSinceLastSpawn < 10000) {
                    event.setCancelled(true);
                    GPTimings.PLAYER_PICKUP_ITEM_EVENT.stopTimingIfSync();
                    return;
                }

                // otherwise take away his immunity. he may be armed now. at least, he's worth killing for some loot
                playerData.pvpImmune = false;
                GriefPreventionPlugin.sendMessage(player, GriefPreventionPlugin.instance.messageData.pvpImmunityEnd.toText());
            }
        }
    }
    GPTimings.PLAYER_PICKUP_ITEM_EVENT.stopTimingIfSync();
}
 
Example 16
Source File: PEXActions.java    From ChatUI with MIT License 4 votes vote down vote up
private static int asInt(Tristate tristate) {
    return tristate == Tristate.TRUE ? 1 : tristate == Tristate.FALSE ? -1 : 0;
}
 
Example 17
Source File: ClaimFlagBase.java    From GriefPrevention with MIT License 4 votes vote down vote up
private Consumer<CommandSource> createFlagConsumer(CommandSource src, GPClaim claim, String flagPermission, Tristate flagValue, String source, FlagType displayType, FlagType flagType, boolean toggleType) {
    return consumer -> {
        // Toggle DEFAULT type
        final String sourceId = GPPermissionHandler.getSourcePermission(flagPermission);
        final String targetId = GPPermissionHandler.getTargetPermission(flagPermission);
        final ClaimFlag claimFlag = GPPermissionHandler.getFlagFromPermission(flagPermission);
        if (claimFlag == null) {
            return;
        }
        Context claimContext = claim.getContext();
        Tristate newValue = Tristate.UNDEFINED;
        if (flagType == FlagType.DEFAULT) {
            if (toggleType) {
                if (flagValue == Tristate.TRUE) {
                    newValue = Tristate.FALSE;
                } else {
                    newValue = Tristate.TRUE;
                }
                ClaimType claimType = claim.getType();
                if (claimType == ClaimType.SUBDIVISION) {
                    claimType = ClaimType.BASIC;
                }
                final Boolean defaultValue = DataStore.CLAIM_FLAG_DEFAULTS.get(claimType).get(claimFlag.toString());
                if (defaultValue != null && defaultValue == newValue.asBoolean()) {
                    newValue = Tristate.UNDEFINED;
                }
            }
            claimContext = CommandHelper.validateCustomContext(src, claim, "default");
        // Toggle CLAIM type
        } else if (flagType == FlagType.CLAIM) {
            if (flagValue == Tristate.TRUE) {
                newValue = Tristate.FALSE;
            } else if (flagValue == Tristate.UNDEFINED) {
                newValue = Tristate.TRUE;
            }
        // Toggle OVERRIDE type
        } else if (flagType == FlagType.OVERRIDE) {
            if (flagValue == Tristate.TRUE) {
                newValue = Tristate.FALSE;
            } else if (flagValue == Tristate.UNDEFINED) {
                newValue = Tristate.TRUE;
            }
            claimContext = CommandHelper.validateCustomContext(src, claim, "override");
        }
        try (final CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
            Sponge.getCauseStackManager().pushCause(src);
            GPFlagClaimEvent.Set event = new GPFlagClaimEvent.Set(claim, this.subject, claimFlag, sourceId, targetId, toggleType ? newValue : flagValue, claimContext);
            Sponge.getEventManager().post(event);
            if (event.isCancelled()) {
                return;
            }
            FlagResult result = CommandHelper.applyFlagPermission(src, this.subject, "ALL", claim, flagPermission, source, "any", toggleType ? newValue : flagValue, claimContext, flagType, null, true);
            if (result.successful()) {
                showFlagPermissions(src, claim, displayType, source);
            }
        }
    };
}
 
Example 18
Source File: GlobalListener.java    From RedProtect with GNU General Public License v3.0 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
@IsCancelled(Tristate.FALSE)
public void onCreatureSpawn(SpawnEntityEvent event) {

    for (Entity e : event.getEntities()) {
        if (e == null || (RedProtect.get().rm.getTopRegion(e.getLocation(), this.getClass().getName()) != null &&
                RedProtect.get().config.globalFlagsRoot().worlds.get(e.getWorld().getName()).spawn_allow_on_regions)) {
            continue;
        }

        //blacklist
        List<String> blacklist = RedProtect.get().config.globalFlagsRoot().worlds.get(e.getWorld().getName()).spawn_blacklist;
        if (e instanceof Monster && blacklist.contains("MONSTERS")) {
            RedProtect.get().logger.debug(LogLevel.SPAWN, "GlobalListener - Cancelled spawn of BLACKLISTED Monster " + e.getType().getName());
            event.setCancelled(true);
            return;
        }
        if ((e instanceof Animal || e instanceof Villager || e instanceof Ambient || e instanceof Golem) && blacklist.contains("PASSIVES")) {
            RedProtect.get().logger.debug(LogLevel.SPAWN, "GlobalListener - Cancelled spawn of BLACKLISTED Animal " + e.getType().getName());
            event.setCancelled(true);
            return;
        }
        if (blacklist.stream().anyMatch(e.getType().getName()::matches)) {
            RedProtect.get().logger.debug(LogLevel.SPAWN, "GlobalListener - Cancelled spawn of BLACKLISTED " + e.getType().getName());
            event.setCancelled(true);
            return;
        }

        //whitelist
        List<String> wtl = RedProtect.get().config.globalFlagsRoot().worlds.get(e.getWorld().getName()).spawn_whitelist;
        if (!wtl.isEmpty()) {
            if (e instanceof Monster && !wtl.contains("MONSTERS")) {
                RedProtect.get().logger.debug(LogLevel.SPAWN, "GlobalListener - Cancelled spawn of NON WHITELISTED Monster " + e.getType().getName());
                event.setCancelled(true);
                return;
            }
            if ((e instanceof Animal || e instanceof Villager || e instanceof Ambient || e instanceof Golem) && !wtl.contains("PASSIVES")) {
                RedProtect.get().logger.debug(LogLevel.SPAWN, "GlobalListener - Cancelled spawn of NON WHITELISTED Animal " + e.getType().getName());
                event.setCancelled(true);
                return;
            }
            if (wtl.stream().noneMatch(e.getType().getName()::matches)) {
                RedProtect.get().logger.debug(LogLevel.SPAWN, "GlobalListener - Cancelled spawn of NON WHITELISTED " + e.getType().getName());
                event.setCancelled(true);
                return;
            }
        }
        RedProtect.get().logger.debug(LogLevel.SPAWN, "GlobalListener - Spawn mob " + e.getType().getName());
    }
}
 
Example 19
Source File: BlockEventHandler.java    From GriefPrevention with MIT License 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onSignChanged(ChangeSignEvent event) {
    final User user = CauseContextHelper.getEventUser(event);
    if (user == null) {
        return;
    }

    if (!GriefPreventionPlugin.instance.claimsEnabledForWorld(event.getTargetTile().getLocation().getExtent().getProperties())) {
        return;
    }

    GPTimings.SIGN_CHANGE_EVENT.startTimingIfSync();
    Location<World> location = event.getTargetTile().getLocation();
    // Prevent users exploiting signs
    GPClaim claim = GriefPreventionPlugin.instance.dataStore.getClaimAt(location);
    if (GPPermissionHandler.getClaimPermission(event, location, claim, GPPermissions.INTERACT_BLOCK_SECONDARY, user, location.getBlock(), user, TrustType.ACCESSOR, true) == Tristate.FALSE) {
        if (user instanceof Player) {
            event.setCancelled(true);
            final Text message = GriefPreventionPlugin.instance.messageData.permissionAccess
                    .apply(ImmutableMap.of(
                    "player", Text.of(claim.getOwnerName()))).build();
            GriefPreventionPlugin.sendClaimDenyMessage(claim, (Player) user, message);
            return;
        }
    }

    // send sign content to online administrators
    if (!GriefPreventionPlugin.getActiveConfig(location.getExtent().getProperties())
            .getConfig().general.generalAdminSignNotifications) {
        GPTimings.SIGN_CHANGE_EVENT.stopTimingIfSync();
        return;
    }

    World world = location.getExtent();
    StringBuilder lines = new StringBuilder(" placed a sign @ " + GriefPreventionPlugin.getfriendlyLocationString(event.getTargetTile().getLocation()));
    boolean notEmpty = false;
    for (int i = 0; i < event.getText().lines().size(); i++) {
        String withoutSpaces = Text.of(event.getText().lines().get(i)).toPlain().replace(" ", "");
        if (!withoutSpaces.isEmpty()) {
            notEmpty = true;
            lines.append("\n  " + event.getText().lines().get(i));
        }
    }

    String signMessage = lines.toString();
    // prevent signs with blocked IP addresses
    if (!user.hasPermission(GPPermissions.OVERRIDE_SPAM) && GriefPreventionPlugin.instance.containsBlockedIP(signMessage)) {
        event.setCancelled(true);
        GPTimings.SIGN_CHANGE_EVENT.stopTimingIfSync();
        return;
    }

    // if not empty and wasn't the same as the last sign, log it and remember it for later
    GPPlayerData playerData = this.dataStore.getOrCreatePlayerData(world, user.getUniqueId());
    if (notEmpty && playerData.lastMessage != null && !playerData.lastMessage.equals(signMessage)) {
        GriefPreventionPlugin.addLogEntry(user.getName() + lines.toString().replace("\n  ", ";"), null);
        //PlayerEventHandler.makeSocialLogEntry(player.get().getName(), signMessage);
        playerData.lastMessage = signMessage;

        if (!user.hasPermission(GPPermissions.EAVES_DROP_SIGNS)) {
            Collection<Player> players = (Collection<Player>) Sponge.getGame().getServer().getOnlinePlayers();
            for (Player otherPlayer : players) {
                if (otherPlayer.hasPermission(GPPermissions.EAVES_DROP_SIGNS)) {
                    otherPlayer.sendMessage(Text.of(TextColors.GRAY, user.getName(), signMessage));
                }
            }
        }
    }
    GPTimings.SIGN_CHANGE_EVENT.stopTimingIfSync();
}
 
Example 20
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();
}