Search in sources :

Example 1 with RegionAssociable

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

the class RegionProtectionListener method onUseEntity.

@EventHandler(ignoreCancelled = true)
public void onUseEntity(UseEntityEvent 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();
    RegionAssociable associable = createRegionAssociable(event.getCause());
    RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery();
    boolean canUse;
    String what;
    /* Hostile / ambient mob override */
    final Entity entity = event.getEntity();
    final EntityType type = entity.getType();
    if (Entities.isHostile(entity) || Entities.isAmbient(entity) || Entities.isNPC(entity) || entity instanceof Player) {
        canUse = event.getRelevantFlags().isEmpty() || query.queryState(BukkitAdapter.adapt(target), associable, combine(event)) != State.DENY;
        what = "use that";
    /* Paintings, item frames, etc. */
    } else if (Entities.isConsideredBuildingIfUsed(entity)) {
        if ((type == EntityType.ITEM_FRAME || type == EntityType.GLOW_ITEM_FRAME) && event.getCause().getFirstPlayer() != null && ((ItemFrame) entity).getItem().getType() != Material.AIR) {
            canUse = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.ITEM_FRAME_ROTATE));
            what = "change that";
        } else if (Entities.isMinecart(type)) {
            canUse = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.CHEST_ACCESS));
            what = "open that";
        } else {
            canUse = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event));
            what = "change that";
        }
    /* Ridden on use */
    } else if (Entities.isRiddenOnUse(entity)) {
        canUse = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.RIDE, Flags.INTERACT));
        what = "ride that";
    /* Everything else */
    } else {
        canUse = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.INTERACT));
        what = "use that";
    }
    if (!canUse) {
        tellErrorMessage(event, event.getCause(), target, what);
        event.setCancelled(true);
    }
}
Also used : RegionAssociable(com.sk89q.worldguard.protection.association.RegionAssociable) EntityType(org.bukkit.entity.EntityType) Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) LocalPlayer(com.sk89q.worldguard.LocalPlayer) RegionQuery(com.sk89q.worldguard.protection.regions.RegionQuery) ItemFrame(org.bukkit.entity.ItemFrame) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 2 with RegionAssociable

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

the class RegionProtectionListener method onSpawnEntity.

@EventHandler(ignoreCancelled = true)
public void onSpawnEntity(SpawnEntityEvent 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.getEffectiveType();
    RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery();
    RegionAssociable associable = createRegionAssociable(event.getCause());
    boolean canSpawn;
    String what;
    /* Vehicles */
    if (Entities.isVehicle(type)) {
        canSpawn = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.PLACE_VEHICLE));
        what = "place vehicles";
    /* Item pickup */
    } else if (event.getEntity() instanceof Item) {
        canSpawn = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.ITEM_DROP));
        what = "drop items";
    /* XP drops */
    } else if (type == EntityType.EXPERIENCE_ORB) {
        canSpawn = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.EXP_DROPS));
        what = "drop XP";
    } else if (Entities.isAoECloud(type)) {
        canSpawn = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.POTION_SPLASH));
        what = "use lingering potions";
    /* Everything else */
    } else {
        canSpawn = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event));
        what = "place things";
    }
    if (!canSpawn) {
        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) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 3 with RegionAssociable

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

the class RegionProtectionListener method onPlaceBlock.

@EventHandler(ignoreCancelled = true)
public void onPlaceBlock(final PlaceBlockEvent 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());
    // Don't check liquid flow unless it's enabled
    if (event.getCause().getRootCause() instanceof Block && Materials.isLiquid(type) && !getWorldConfig(event.getWorld()).checkLiquidFlow) {
        return;
    }
    event.filter((Predicate<Location>) target -> {
        boolean canPlace;
        String what;
        if (Materials.isFire(type)) {
            Block block = event.getCause().getFirstBlock();
            boolean fire = block != null && Materials.isFire(type);
            boolean lava = block != null && Materials.isLava(block.getType());
            List<StateFlag> flags = new ArrayList<>();
            flags.add(Flags.BLOCK_PLACE);
            flags.add(Flags.LIGHTER);
            if (fire)
                flags.add(Flags.FIRE_SPREAD);
            if (lava)
                flags.add(Flags.LAVA_FIRE);
            canPlace = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, flags.toArray(new StateFlag[flags.size()])));
            what = "place fire";
        } else if (type == Material.FROSTED_ICE) {
            event.setSilent(true);
            canPlace = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.BLOCK_PLACE, Flags.FROSTED_ICE_FORM));
            what = "use frostwalker";
        } else {
            canPlace = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.BLOCK_PLACE));
            what = "place that block";
        }
        if (!canPlace) {
            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) Block(org.bukkit.block.Block) Material(org.bukkit.Material) ArrayList(java.util.ArrayList) List(java.util.List) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 4 with RegionAssociable

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

the class WorldGuardBlockListener method onBlockForm.

/*
     * Called when a block is formed based on world conditions.
     */
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onBlockForm(BlockFormEvent event) {
    ConfigurationManager cfg = getConfig();
    if (cfg.activityHaltToggle) {
        event.setCancelled(true);
        return;
    }
    WorldConfiguration wcfg = getWorldConfig(event.getBlock().getWorld());
    Material type = event.getNewState().getType();
    if (event instanceof EntityBlockFormEvent) {
        if (((EntityBlockFormEvent) event).getEntity() instanceof Snowman) {
            if (wcfg.disableSnowmanTrails) {
                event.setCancelled(true);
                return;
            }
        }
        return;
    }
    if (type == Material.ICE) {
        if (wcfg.disableIceFormation) {
            event.setCancelled(true);
            return;
        }
        if (wcfg.useRegions && !StateFlag.test(WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery().queryState(BukkitAdapter.adapt(event.getBlock().getLocation()), (RegionAssociable) null, Flags.ICE_FORM))) {
            event.setCancelled(true);
            return;
        }
    }
    if (type == Material.SNOW) {
        if (wcfg.disableSnowFormation) {
            event.setCancelled(true);
            return;
        }
        if (!wcfg.allowedSnowFallOver.isEmpty()) {
            Material targetId = event.getBlock().getRelative(0, -1, 0).getType();
            if (!wcfg.allowedSnowFallOver.contains(BukkitAdapter.asBlockType(targetId).getId())) {
                event.setCancelled(true);
                return;
            }
        }
        if (wcfg.useRegions && !StateFlag.test(WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery().queryState(BukkitAdapter.adapt(event.getBlock().getLocation()), (RegionAssociable) null, Flags.SNOW_FALL))) {
            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) EntityBlockFormEvent(org.bukkit.event.block.EntityBlockFormEvent) Snowman(org.bukkit.entity.Snowman) Material(org.bukkit.Material) ConfigurationManager(com.sk89q.worldguard.config.ConfigurationManager) EventHandler(org.bukkit.event.EventHandler)

Example 5 with RegionAssociable

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

the class RegionProtectionListener method onBreakBlock.

@EventHandler(ignoreCancelled = true)
public void onBreakBlock(final BreakBlockEvent 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 RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery();
    if (!event.isCancelled()) {
        final RegionAssociable associable = createRegionAssociable(event.getCause());
        event.filter((Predicate<Location>) target -> {
            boolean canBreak;
            String what;
            if (event.getCause().find(EntityType.PRIMED_TNT, EntityType.MINECART_TNT) != null) {
                canBreak = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.BLOCK_BREAK, Flags.TNT));
                what = "use dynamite";
            } else {
                canBreak = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.BLOCK_BREAK));
                what = "break that block";
            }
            if (!canBreak) {
                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) Location(org.bukkit.Location) 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