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);
}
}
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;
});
}
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);
}
}
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;
}
}
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);
}
Aggregations