Search in sources :

Example 6 with RegionAssociable

use of com.sk89q.worldguard.protection.association.RegionAssociable in project WorldGuard by EngineHub.

the class RegionProtectionListener method onDamageEntity.

@EventHandler(ignoreCancelled = true)
public void onDamageEntity(DamageEntityEvent event) {
    // Don't care about events that have been pre-allowed
    if (event.getResult() == Result.ALLOW)
        return;
    // Region support disabled
    if (!isRegionSupportEnabled(event.getWorld()))
        return;
    // Whitelist check is below
    com.sk89q.worldedit.util.Location target = BukkitAdapter.adapt(event.getTarget());
    RegionAssociable associable = createRegionAssociable(event.getCause());
    RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery();
    Player playerAttacker = event.getCause().getFirstPlayer();
    boolean canDamage;
    String what;
    // Block PvP like normal even if the player has an override permission
    // because (1) this is a frequent source of confusion and
    // (2) some users want to block PvP even with the bypass permission
    boolean pvp = event.getEntity() instanceof Player && playerAttacker != null && !playerAttacker.equals(event.getEntity());
    if (isWhitelisted(event.getCause(), event.getWorld(), pvp)) {
        return;
    }
    /* Hostile / ambient mob override */
    if (Entities.isHostile(event.getEntity()) || Entities.isAmbient(event.getEntity()) || Entities.isVehicle(event.getEntity().getType())) {
        canDamage = event.getRelevantFlags().isEmpty() || query.queryState(target, associable, combine(event)) != State.DENY;
        what = "hit that";
    /* Paintings, item frames, etc. */
    } else if (Entities.isConsideredBuildingIfUsed(event.getEntity())) {
        canDamage = query.testBuild(target, associable, combine(event));
        what = "change that";
    /* PVP */
    } else if (pvp) {
        LocalPlayer localAttacker = WorldGuardPlugin.inst().wrapPlayer(playerAttacker);
        Player defender = (Player) event.getEntity();
        // if defender is an NPC
        if (Entities.isNPC(defender)) {
            return;
        }
        canDamage = query.testBuild(target, associable, combine(event, Flags.PVP)) && query.queryState(localAttacker.getLocation(), localAttacker, combine(event, Flags.PVP)) != State.DENY && query.queryState(target, localAttacker, combine(event, Flags.PVP)) != State.DENY;
        // Fire the disallow PVP event
        if (!canDamage && Events.fireAndTestCancel(new DisallowedPVPEvent(playerAttacker, defender, event.getOriginalEvent()))) {
            canDamage = true;
        }
        what = "PvP";
    /* Player damage not caused  by another player */
    } else if (event.getEntity() instanceof Player) {
        canDamage = event.getRelevantFlags().isEmpty() || query.queryState(target, associable, combine(event)) != State.DENY;
        what = "damage that";
    /* damage to non-hostile mobs (e.g. animals) */
    } else if (Entities.isNonHostile(event.getEntity())) {
        canDamage = query.testBuild(target, associable, combine(event, Flags.DAMAGE_ANIMALS));
        what = "harm that";
    /* Everything else */
    } else {
        canDamage = query.testBuild(target, associable, combine(event, Flags.INTERACT));
        what = "hit that";
    }
    if (!canDamage) {
        tellErrorMessage(event, event.getCause(), event.getTarget(), what);
        event.setCancelled(true);
    }
}
Also used : RegionAssociable(com.sk89q.worldguard.protection.association.RegionAssociable) Player(org.bukkit.entity.Player) LocalPlayer(com.sk89q.worldguard.LocalPlayer) LocalPlayer(com.sk89q.worldguard.LocalPlayer) RegionQuery(com.sk89q.worldguard.protection.regions.RegionQuery) DisallowedPVPEvent(com.sk89q.worldguard.bukkit.protection.events.DisallowedPVPEvent) EventHandler(org.bukkit.event.EventHandler)

Example 7 with RegionAssociable

use of com.sk89q.worldguard.protection.association.RegionAssociable in project WorldGuard by EngineHub.

the class RegionProtectionListener method onUseBlock.

@EventHandler(ignoreCancelled = true)
public void onUseBlock(final UseBlockEvent event) {
    // Don't care about events that have been pre-allowed
    if (event.getResult() == Result.ALLOW)
        return;
    // Region support disabled
    if (!isRegionSupportEnabled(event.getWorld()))
        return;
    // Whitelisted cause
    if (isWhitelisted(event.getCause(), event.getWorld(), false))
        return;
    final Material type = event.getEffectiveMaterial();
    final RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery();
    final RegionAssociable associable = createRegionAssociable(event.getCause());
    event.filter((Predicate<Location>) target -> {
        boolean canUse;
        String what;
        if (Materials.isConsideredBuildingIfUsed(type)) {
            canUse = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event));
            what = "use that";
        } else if (Materials.isInventoryBlock(type)) {
            canUse = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.CHEST_ACCESS));
            what = "open that";
        } else if (handleAsInventoryUsage(event.getOriginalEvent())) {
            canUse = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.CHEST_ACCESS));
            what = "take that";
        } else if (Materials.isAnvil(type)) {
            canUse = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.USE_ANVIL));
            what = "use that";
        } else if (Materials.isBed(type)) {
            canUse = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.INTERACT, Flags.SLEEP));
            what = "sleep";
        } else if (type == Material.RESPAWN_ANCHOR) {
            canUse = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.INTERACT, Flags.RESPAWN_ANCHORS));
            what = "use anchors";
        } else if (type == Material.TNT) {
            canUse = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.INTERACT, Flags.TNT));
            what = "use explosives";
        } else if (Materials.isUseFlagApplicable(type)) {
            canUse = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.INTERACT, Flags.USE));
            what = "use that";
        } else {
            canUse = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.INTERACT));
            what = "use that";
        }
        if (!canUse) {
            tellErrorMessage(event, event.getCause(), target, what);
            return false;
        }
        return true;
    });
}
Also used : RegionAssociable(com.sk89q.worldguard.protection.association.RegionAssociable) Arrays(java.util.Arrays) Item(org.bukkit.entity.Item) Cause(com.sk89q.worldguard.bukkit.cause.Cause) Materials(com.sk89q.worldguard.bukkit.util.Materials) Event(org.bukkit.event.Event) InteropUtils(com.sk89q.worldguard.bukkit.util.InteropUtils) Player(org.bukkit.entity.Player) WGMetadata(com.sk89q.worldguard.bukkit.internal.WGMetadata) CommandUtils(com.sk89q.worldguard.commands.CommandUtils) ArrayList(java.util.ArrayList) EventHandler(org.bukkit.event.EventHandler) Block(org.bukkit.block.Block) Location(org.bukkit.Location) BreakBlockEvent(com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent) ItemFrame(org.bukkit.entity.ItemFrame) World(org.bukkit.World) WorldGuard(com.sk89q.worldguard.WorldGuard) StateFlag(com.sk89q.worldguard.protection.flags.StateFlag) UseBlockEvent(com.sk89q.worldguard.bukkit.event.block.UseBlockEvent) Material(org.bukkit.Material) UseEntityEvent(com.sk89q.worldguard.bukkit.event.entity.UseEntityEvent) Entities(com.sk89q.worldguard.bukkit.util.Entities) RegionAssociable(com.sk89q.worldguard.protection.association.RegionAssociable) Entity(org.bukkit.entity.Entity) State(com.sk89q.worldguard.protection.flags.StateFlag.State) DestroyEntityEvent(com.sk89q.worldguard.bukkit.event.entity.DestroyEntityEvent) WorldGuardPlugin(com.sk89q.worldguard.bukkit.WorldGuardPlugin) Flags(com.sk89q.worldguard.protection.flags.Flags) LocalPlayer(com.sk89q.worldguard.LocalPlayer) Events(com.sk89q.worldguard.bukkit.util.Events) RegionQuery(com.sk89q.worldguard.protection.regions.RegionQuery) EntityType(org.bukkit.entity.EntityType) Result(org.bukkit.event.Event.Result) PlayerTakeLecternBookEvent(org.bukkit.event.player.PlayerTakeLecternBookEvent) DelegateEvent(com.sk89q.worldguard.bukkit.event.DelegateEvent) DamageEntityEvent(com.sk89q.worldguard.bukkit.event.entity.DamageEntityEvent) List(java.util.List) WorldConfiguration(com.sk89q.worldguard.config.WorldConfiguration) ExperienceOrb(org.bukkit.entity.ExperienceOrb) Predicate(com.google.common.base.Predicate) PlaceBlockEvent(com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent) BukkitAdapter(com.sk89q.worldedit.bukkit.BukkitAdapter) DisallowedPVPEvent(com.sk89q.worldguard.bukkit.protection.events.DisallowedPVPEvent) VehicleExitEvent(org.bukkit.event.vehicle.VehicleExitEvent) ChatColor(org.bukkit.ChatColor) SpawnEntityEvent(com.sk89q.worldguard.bukkit.event.entity.SpawnEntityEvent) Tameable(org.bukkit.entity.Tameable) RegionQuery(com.sk89q.worldguard.protection.regions.RegionQuery) Material(org.bukkit.Material) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 8 with RegionAssociable

use of com.sk89q.worldguard.protection.association.RegionAssociable in project WorldGuard by EngineHub.

the class RegionProtectionListener method onDestroyEntity.

@EventHandler(ignoreCancelled = true)
public void onDestroyEntity(DestroyEntityEvent event) {
    // Don't care about events that have been pre-allowed
    if (event.getResult() == Result.ALLOW)
        return;
    // Region support disabled
    if (!isRegionSupportEnabled(event.getWorld()))
        return;
    // Whitelisted cause
    if (isWhitelisted(event.getCause(), event.getWorld(), false))
        return;
    Location target = event.getTarget();
    EntityType type = event.getEntity().getType();
    RegionAssociable associable = createRegionAssociable(event.getCause());
    RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery();
    boolean canDestroy;
    String what;
    /* Vehicles */
    if (Entities.isVehicle(type)) {
        canDestroy = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.DESTROY_VEHICLE));
        what = "break vehicles";
    /* Item pickup */
    } else if (event.getEntity() instanceof Item || event.getEntity() instanceof ExperienceOrb) {
        canDestroy = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.ITEM_PICKUP));
        what = "pick up items";
    /* Everything else */
    } else {
        canDestroy = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event));
        what = "break things";
    }
    if (!canDestroy) {
        tellErrorMessage(event, event.getCause(), target, what);
        event.setCancelled(true);
    }
}
Also used : EntityType(org.bukkit.entity.EntityType) RegionAssociable(com.sk89q.worldguard.protection.association.RegionAssociable) Item(org.bukkit.entity.Item) RegionQuery(com.sk89q.worldguard.protection.regions.RegionQuery) ExperienceOrb(org.bukkit.entity.ExperienceOrb) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 9 with RegionAssociable

use of com.sk89q.worldguard.protection.association.RegionAssociable in project WorldGuard by EngineHub.

the class WorldGuardBlockListener method onBlockFromTo.

/*
     * Called when fluids flow.
     */
@EventHandler(ignoreCancelled = true)
public void onBlockFromTo(BlockFromToEvent event) {
    World world = event.getBlock().getWorld();
    Block blockFrom = event.getBlock();
    Block blockTo = event.getToBlock();
    ConfigurationManager cfg = getConfig();
    if (cfg.activityHaltToggle) {
        event.setCancelled(true);
        return;
    }
    Material fromType = blockFrom.getType();
    boolean isWater = Materials.isWater(fromType);
    boolean isLava = fromType == Material.LAVA;
    boolean isAir = fromType == Material.AIR;
    WorldConfiguration wcfg = getWorldConfig(world);
    if (wcfg.simulateSponge && isWater) {
        int ox = blockTo.getX();
        int oy = blockTo.getY();
        int oz = blockTo.getZ();
        for (int cx = -wcfg.spongeRadius; cx <= wcfg.spongeRadius; cx++) {
            for (int cy = -wcfg.spongeRadius; cy <= wcfg.spongeRadius; cy++) {
                for (int cz = -wcfg.spongeRadius; cz <= wcfg.spongeRadius; cz++) {
                    Block sponge = world.getBlockAt(ox + cx, oy + cy, oz + cz);
                    if (sponge.getType() == Material.SPONGE && (!wcfg.redstoneSponges || !sponge.isBlockIndirectlyPowered())) {
                        event.setCancelled(true);
                        return;
                    }
                }
            }
        }
    }
    // If so and the target block is protected, cancel the event
    if (!wcfg.preventWaterDamage.isEmpty()) {
        Material targetId = blockTo.getType();
        if ((isAir || isWater) && wcfg.preventWaterDamage.contains(BukkitAdapter.asBlockType(targetId).getId())) {
            event.setCancelled(true);
            return;
        }
    }
    if (!wcfg.allowedLavaSpreadOver.isEmpty() && isLava) {
        Material targetId = blockTo.getRelative(0, -1, 0).getType();
        if (!wcfg.allowedLavaSpreadOver.contains(BukkitAdapter.asBlockType(targetId).getId())) {
            event.setCancelled(true);
            return;
        }
    }
    if (wcfg.highFreqFlags && (isWater || blockFrom.getBlockData() instanceof Waterlogged) && WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery().queryState(BukkitAdapter.adapt(blockFrom.getLocation()), (RegionAssociable) null, Flags.WATER_FLOW) == StateFlag.State.DENY) {
        event.setCancelled(true);
        return;
    }
    if (wcfg.highFreqFlags && isLava && !StateFlag.test(WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery().queryState(BukkitAdapter.adapt(blockFrom.getLocation()), (RegionAssociable) null, Flags.LAVA_FLOW))) {
        event.setCancelled(true);
        return;
    }
}
Also used : RegionAssociable(com.sk89q.worldguard.protection.association.RegionAssociable) BukkitWorldConfiguration(com.sk89q.worldguard.bukkit.BukkitWorldConfiguration) WorldConfiguration(com.sk89q.worldguard.config.WorldConfiguration) Block(org.bukkit.block.Block) Material(org.bukkit.Material) Waterlogged(org.bukkit.block.data.Waterlogged) World(org.bukkit.World) ConfigurationManager(com.sk89q.worldguard.config.ConfigurationManager) EventHandler(org.bukkit.event.EventHandler)

Example 10 with RegionAssociable

use of com.sk89q.worldguard.protection.association.RegionAssociable in project WorldGuard by EngineHub.

the class WorldGuardBlockListener method onBlockSpread.

/*
     * Called when a block spreads based on world conditions.
     */
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onBlockSpread(BlockSpreadEvent event) {
    ConfigurationManager cfg = getConfig();
    if (cfg.activityHaltToggle) {
        event.setCancelled(true);
        return;
    }
    WorldConfiguration wcfg = getWorldConfig(event.getBlock().getWorld());
    // craftbukkit randomly gives AIR as event.getSource even if that block is not air
    Material newType = event.getNewState().getType();
    if (Materials.isMushroom(newType)) {
        if (wcfg.disableMushroomSpread) {
            event.setCancelled(true);
            return;
        }
        if (wcfg.useRegions && !StateFlag.test(WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery().queryState(BukkitAdapter.adapt(event.getBlock().getLocation()), (RegionAssociable) null, Flags.MUSHROOMS))) {
            event.setCancelled(true);
            return;
        }
    }
    if (newType == Material.GRASS_BLOCK) {
        if (wcfg.disableGrassGrowth) {
            event.setCancelled(true);
            return;
        }
        if (wcfg.useRegions && !StateFlag.test(WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery().queryState(BukkitAdapter.adapt(event.getBlock().getLocation()), (RegionAssociable) null, Flags.GRASS_SPREAD))) {
            event.setCancelled(true);
            return;
        }
    }
    if (newType == Material.MYCELIUM) {
        if (wcfg.disableMyceliumSpread) {
            event.setCancelled(true);
            return;
        }
        if (wcfg.useRegions && !StateFlag.test(WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery().queryState(BukkitAdapter.adapt(event.getBlock().getLocation()), (RegionAssociable) null, Flags.MYCELIUM_SPREAD))) {
            event.setCancelled(true);
            return;
        }
    }
    if (Materials.isVine(newType)) {
        if (wcfg.disableVineGrowth) {
            event.setCancelled(true);
            return;
        }
        if (wcfg.useRegions && !StateFlag.test(WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery().queryState(BukkitAdapter.adapt(event.getBlock().getLocation()), (RegionAssociable) null, Flags.VINE_GROWTH))) {
            event.setCancelled(true);
            return;
        }
    }
    if (newType == Material.BUDDING_AMETHYST || newType == Material.POINTED_DRIPSTONE) {
        if (wcfg.disableRockGrowth) {
            event.setCancelled(true);
            return;
        }
        if (wcfg.useRegions && !StateFlag.test(WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery().queryState(BukkitAdapter.adapt(event.getBlock().getLocation()), (RegionAssociable) null, Flags.ROCK_GROWTH))) {
            event.setCancelled(true);
            return;
        }
    }
    handleGrow(event, event.getBlock().getLocation(), newType);
}
Also used : RegionAssociable(com.sk89q.worldguard.protection.association.RegionAssociable) BukkitWorldConfiguration(com.sk89q.worldguard.bukkit.BukkitWorldConfiguration) WorldConfiguration(com.sk89q.worldguard.config.WorldConfiguration) Material(org.bukkit.Material) ConfigurationManager(com.sk89q.worldguard.config.ConfigurationManager) EventHandler(org.bukkit.event.EventHandler)

Aggregations

RegionAssociable (com.sk89q.worldguard.protection.association.RegionAssociable)12 EventHandler (org.bukkit.event.EventHandler)11 WorldConfiguration (com.sk89q.worldguard.config.WorldConfiguration)7 RegionQuery (com.sk89q.worldguard.protection.regions.RegionQuery)7 Location (org.bukkit.Location)6 Material (org.bukkit.Material)6 EntityType (org.bukkit.entity.EntityType)6 Player (org.bukkit.entity.Player)6 LocalPlayer (com.sk89q.worldguard.LocalPlayer)5 Entity (org.bukkit.entity.Entity)5 Item (org.bukkit.entity.Item)5 ItemFrame (org.bukkit.entity.ItemFrame)5 DisallowedPVPEvent (com.sk89q.worldguard.bukkit.protection.events.DisallowedPVPEvent)4 World (org.bukkit.World)4 Predicate (com.google.common.base.Predicate)3 BukkitAdapter (com.sk89q.worldedit.bukkit.BukkitAdapter)3 WorldGuard (com.sk89q.worldguard.WorldGuard)3 BukkitWorldConfiguration (com.sk89q.worldguard.bukkit.BukkitWorldConfiguration)3 WorldGuardPlugin (com.sk89q.worldguard.bukkit.WorldGuardPlugin)3 Cause (com.sk89q.worldguard.bukkit.cause.Cause)3