PageRenderTime 96ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/sk89q/worldguard/bukkit/WorldGuardEntityListener.java

https://github.com/talmor/worldguard
Java | 887 lines | 648 code | 131 blank | 108 comment | 211 complexity | 881e9d6aa5ec90b4361c4bfccff04b3a MD5 | raw file
  1. // $Id$
  2. /*
  3. * WorldGuard
  4. * Copyright (C) 2010 sk89q <http://www.sk89q.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.sk89q.worldguard.bukkit;
  20. import static com.sk89q.worldguard.bukkit.BukkitUtil.toVector;
  21. import java.util.Set;
  22. import java.util.logging.Logger;
  23. import org.bukkit.ChatColor;
  24. import org.bukkit.Location;
  25. import org.bukkit.Material;
  26. import org.bukkit.World;
  27. import org.bukkit.block.Block;
  28. import org.bukkit.entity.Creature;
  29. import org.bukkit.entity.CreatureType;
  30. import org.bukkit.entity.Creeper;
  31. import org.bukkit.entity.Entity;
  32. import org.bukkit.entity.Fireball;
  33. import org.bukkit.entity.LivingEntity;
  34. import org.bukkit.entity.Painting;
  35. import org.bukkit.entity.Player;
  36. import org.bukkit.entity.Projectile;
  37. import org.bukkit.entity.Skeleton;
  38. import org.bukkit.entity.TNTPrimed;
  39. import org.bukkit.entity.Tameable;
  40. import org.bukkit.entity.Wolf;
  41. import org.bukkit.event.Event;
  42. import org.bukkit.event.Event.Priority;
  43. import org.bukkit.event.entity.CreatureSpawnEvent;
  44. import org.bukkit.event.entity.CreeperPowerEvent;
  45. import org.bukkit.event.entity.EndermanPickupEvent;
  46. import org.bukkit.event.entity.EndermanPlaceEvent;
  47. import org.bukkit.event.entity.EntityCombustEvent;
  48. import org.bukkit.event.entity.EntityDamageByBlockEvent;
  49. import org.bukkit.event.entity.EntityDamageByEntityEvent;
  50. import org.bukkit.event.entity.EntityDamageEvent;
  51. import org.bukkit.event.entity.EntityDeathEvent;
  52. import org.bukkit.event.entity.EntityExplodeEvent;
  53. import org.bukkit.event.entity.EntityInteractEvent;
  54. import org.bukkit.event.entity.EntityListener;
  55. import org.bukkit.event.entity.EntityRegainHealthEvent;
  56. import org.bukkit.event.entity.ExplosionPrimeEvent;
  57. import org.bukkit.event.entity.PigZapEvent;
  58. import org.bukkit.event.entity.PlayerDeathEvent;
  59. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
  60. import org.bukkit.event.painting.PaintingBreakByEntityEvent;
  61. import org.bukkit.event.painting.PaintingBreakEvent;
  62. import org.bukkit.event.painting.PaintingPlaceEvent;
  63. import org.bukkit.inventory.ItemStack;
  64. import org.bukkit.plugin.PluginManager;
  65. import com.sk89q.worldedit.Vector;
  66. import com.sk89q.worldguard.blacklist.events.BlockBreakBlacklistEvent;
  67. import com.sk89q.worldguard.blacklist.events.ItemUseBlacklistEvent;
  68. import com.sk89q.worldguard.protection.ApplicableRegionSet;
  69. import com.sk89q.worldguard.protection.flags.DefaultFlag;
  70. import com.sk89q.worldguard.protection.managers.RegionManager;
  71. /**
  72. * Listener for entity related events.
  73. *
  74. * @author sk89q
  75. */
  76. public class WorldGuardEntityListener extends EntityListener {
  77. /**
  78. * Logger for messages.
  79. */
  80. private static final Logger logger = Logger.getLogger("Minecraft.WorldGuard");
  81. private WorldGuardPlugin plugin;
  82. /**
  83. * Construct the object;
  84. *
  85. * @param plugin
  86. */
  87. public WorldGuardEntityListener(WorldGuardPlugin plugin) {
  88. this.plugin = plugin;
  89. }
  90. /**
  91. * Register events.
  92. */
  93. public void registerEvents() {
  94. // PluginManager pm = plugin.getServer().getPluginManager();
  95. registerEvent("ENTITY_DAMAGE", Priority.High);
  96. registerEvent("ENTITY_COMBUST", Priority.High);
  97. registerEvent("ENTITY_EXPLODE", Priority.High);
  98. registerEvent("EXPLOSION_PRIME", Priority.High);
  99. registerEvent("CREATURE_SPAWN", Priority.High);
  100. registerEvent("ENTITY_INTERACT", Priority.High);
  101. registerEvent("CREEPER_POWER", Priority.High);
  102. registerEvent("PIG_ZAP", Priority.High);
  103. registerEvent("PAINTING_BREAK", Priority.High);
  104. registerEvent("PAINTING_PLACE", Priority.High);
  105. registerEvent("ENTITY_REGAIN_HEALTH", Priority.High);
  106. registerEvent("ENDERMAN_PICKUP", Priority.High);
  107. registerEvent("ENDERMAN_PLACE", Priority.High);
  108. registerEvent("ENTITY_DEATH", Priority.High);
  109. }
  110. /**
  111. * Register an event, but not failing if the event is not implemented.
  112. *
  113. * @param typeName
  114. * @param priority
  115. */
  116. private void registerEvent(String typeName, Priority priority) {
  117. try {
  118. Event.Type type = Event.Type.valueOf(typeName);
  119. PluginManager pm = plugin.getServer().getPluginManager();
  120. pm.registerEvent(type, this, priority, plugin);
  121. } catch (IllegalArgumentException e) {
  122. logger.info("WorldGuard: Unable to register missing event type " + typeName);
  123. }
  124. }
  125. /**
  126. * Called when an entity interacts with another object.
  127. */
  128. @Override
  129. public void onEntityInteract(EntityInteractEvent event) {
  130. Entity entity = event.getEntity();
  131. Block block = event.getBlock();
  132. ConfigurationManager cfg = plugin.getGlobalStateManager();
  133. WorldConfiguration wcfg = cfg.get(entity.getWorld());
  134. if (block.getType() == Material.SOIL) {
  135. if (entity instanceof Creature && wcfg.disableCreatureCropTrampling) {
  136. event.setCancelled(true);
  137. }
  138. }
  139. }
  140. /**
  141. * Called when an entity dies.
  142. */
  143. @Override
  144. public void onEntityDeath(EntityDeathEvent event) {
  145. WorldConfiguration wcfg = plugin.getGlobalStateManager().get(event.getEntity().getWorld());
  146. if (wcfg.disableExpDrops) {
  147. event.setDroppedExp(0);
  148. }
  149. try {
  150. if (event instanceof PlayerDeathEvent && wcfg.disableDeathMessages) {
  151. ((PlayerDeathEvent) event).setDeathMessage("");
  152. }
  153. } catch (Throwable t) {
  154. // old CraftBukkit, eat it
  155. }
  156. }
  157. /**
  158. * Called on entity damage by a block.
  159. *
  160. * @param event
  161. */
  162. private void onEntityDamageByBlock(EntityDamageByBlockEvent event) {
  163. Entity defender = event.getEntity();
  164. DamageCause type = event.getCause();
  165. ConfigurationManager cfg = plugin.getGlobalStateManager();
  166. WorldConfiguration wcfg = cfg.get(defender.getWorld());
  167. if (defender instanceof Wolf && ((Wolf) defender).isTamed()) {
  168. if (wcfg.antiWolfDumbness && !(type == DamageCause.VOID)) {
  169. event.setCancelled(true);
  170. return;
  171. }
  172. } else if (defender instanceof Player) {
  173. Player player = (Player) defender;
  174. if (isInvincible(player)) {
  175. event.setCancelled(true);
  176. return;
  177. }
  178. if (wcfg.disableLavaDamage && type == DamageCause.LAVA) {
  179. event.setCancelled(true);
  180. if (cfg.hasGodMode(player)) player.setFireTicks(0);
  181. return;
  182. }
  183. if (wcfg.disableContactDamage && type == DamageCause.CONTACT) {
  184. event.setCancelled(true);
  185. return;
  186. }
  187. if (wcfg.teleportOnVoid && type == DamageCause.VOID) {
  188. BukkitUtil.findFreePosition(player);
  189. event.setCancelled(true);
  190. return;
  191. }
  192. if (wcfg.disableVoidDamage && type == DamageCause.VOID) {
  193. event.setCancelled(true);
  194. return;
  195. }
  196. if (wcfg.disableExplosionDamage && type == DamageCause.BLOCK_EXPLOSION) {
  197. event.setCancelled(true);
  198. return;
  199. }
  200. }
  201. }
  202. /**
  203. * Called on entity damage by an entity.
  204. *
  205. * @param event
  206. */
  207. private void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
  208. if (event.getCause() == DamageCause.PROJECTILE) {
  209. onEntityDamageByProjectile(event);
  210. return;
  211. }
  212. Entity attacker = event.getDamager();
  213. Entity defender = event.getEntity();
  214. if (attacker instanceof Player) {
  215. Player player = (Player) attacker;
  216. ConfigurationManager cfg = plugin.getGlobalStateManager();
  217. WorldConfiguration wcfg = cfg.get(player.getWorld());
  218. ItemStack held = player.getInventory().getItemInHand();
  219. if (held != null) {
  220. if (wcfg.getBlacklist() != null) {
  221. if (!wcfg.getBlacklist().check(
  222. new ItemUseBlacklistEvent(plugin.wrapPlayer(player),
  223. toVector(player.getLocation()), held.getTypeId()), false, false)) {
  224. event.setCancelled(true);
  225. return;
  226. }
  227. }
  228. }
  229. }
  230. if (defender instanceof Player) {
  231. Player player = (Player) defender;
  232. ConfigurationManager cfg = plugin.getGlobalStateManager();
  233. WorldConfiguration wcfg = cfg.get(player.getWorld());
  234. if (isInvincible(player)) {
  235. if (wcfg.regionInvinciblityRemovesMobs
  236. && attacker instanceof LivingEntity && !(attacker instanceof Player)
  237. && !(attacker instanceof Tameable && ((Tameable) attacker).isTamed())) {
  238. attacker.remove();
  239. }
  240. event.setCancelled(true);
  241. return;
  242. }
  243. if (wcfg.disableLightningDamage && event.getCause() == DamageCause.LIGHTNING) {
  244. event.setCancelled(true);
  245. return;
  246. }
  247. if (wcfg.disableExplosionDamage && event.getCause() == DamageCause.ENTITY_EXPLOSION) {
  248. event.setCancelled(true);
  249. return;
  250. }
  251. if (attacker != null && attacker instanceof Player) {
  252. if (wcfg.useRegions) {
  253. Vector pt = toVector(defender.getLocation());
  254. RegionManager mgr = plugin.getGlobalRegionManager().get(player.getWorld());
  255. if (!mgr.getApplicableRegions(pt).allows(DefaultFlag.PVP)) {
  256. ((Player) attacker).sendMessage(ChatColor.DARK_RED + "You are in a no-PvP area.");
  257. event.setCancelled(true);
  258. return;
  259. }
  260. }
  261. }
  262. if (attacker != null && attacker instanceof TNTPrimed) {
  263. if (wcfg.blockTNTExplosions) {
  264. event.setCancelled(true);
  265. return;
  266. }
  267. if (wcfg.useRegions) {
  268. Vector pt = toVector(defender.getLocation());
  269. RegionManager mgr = plugin.getGlobalRegionManager().get(player.getWorld());
  270. ApplicableRegionSet set = mgr.getApplicableRegions(pt);
  271. if (!set.allows(DefaultFlag.TNT)) {
  272. event.setCancelled(true);
  273. return;
  274. }
  275. }
  276. }
  277. if (attacker != null && attacker instanceof Fireball) {
  278. if (wcfg.blockFireballExplosions) {
  279. event.setCancelled(true);
  280. return;
  281. }
  282. if (wcfg.useRegions) {
  283. Vector pt = toVector(defender.getLocation());
  284. RegionManager mgr = plugin.getGlobalRegionManager().get(player.getWorld());
  285. ApplicableRegionSet set = mgr.getApplicableRegions(pt);
  286. if (!set.allows(DefaultFlag.GHAST_FIREBALL)) {
  287. event.setCancelled(true);
  288. return;
  289. }
  290. }
  291. }
  292. if (attacker != null && attacker instanceof LivingEntity
  293. && !(attacker instanceof Player)) {
  294. if (attacker instanceof Creeper && wcfg.blockCreeperExplosions) {
  295. event.setCancelled(true);
  296. return;
  297. }
  298. if (wcfg.disableMobDamage) {
  299. event.setCancelled(true);
  300. return;
  301. }
  302. if (wcfg.useRegions) {
  303. Vector pt = toVector(defender.getLocation());
  304. RegionManager mgr = plugin.getGlobalRegionManager().get(player.getWorld());
  305. ApplicableRegionSet set = mgr.getApplicableRegions(pt);
  306. if (!set.allows(DefaultFlag.MOB_DAMAGE)) {
  307. event.setCancelled(true);
  308. return;
  309. }
  310. if (attacker instanceof Creeper) {
  311. if (!set.allows(DefaultFlag.CREEPER_EXPLOSION)) {
  312. event.setCancelled(true);
  313. return;
  314. }
  315. }
  316. }
  317. }
  318. }
  319. }
  320. /**
  321. * Called on entity damage by a projectile.
  322. *
  323. * @param event
  324. */
  325. private void onEntityDamageByProjectile(EntityDamageByEntityEvent event) {
  326. Entity defender = event.getEntity();
  327. Entity attacker = ((Projectile) event.getDamager()).getShooter();
  328. if (defender instanceof Player) {
  329. Player player = (Player) defender;
  330. ConfigurationManager cfg = plugin.getGlobalStateManager();
  331. WorldConfiguration wcfg = cfg.get(player.getWorld());
  332. if (isInvincible(player)) {
  333. event.setCancelled(true);
  334. return;
  335. }
  336. if (attacker != null && attacker instanceof Player) {
  337. if (wcfg.useRegions) {
  338. Vector pt = toVector(defender.getLocation());
  339. RegionManager mgr = plugin.getGlobalRegionManager().get(player.getWorld());
  340. if (!mgr.getApplicableRegions(pt).allows(DefaultFlag.PVP)) {
  341. ((Player) attacker).sendMessage(ChatColor.DARK_RED + "You are in a no-PvP area.");
  342. event.setCancelled(true);
  343. return;
  344. }
  345. }
  346. }
  347. if (attacker != null && attacker instanceof Skeleton) {
  348. if (wcfg.disableMobDamage) {
  349. event.setCancelled(true);
  350. return;
  351. }
  352. if (wcfg.useRegions) {
  353. Vector pt = toVector(defender.getLocation());
  354. RegionManager mgr = plugin.getGlobalRegionManager().get(player.getWorld());
  355. if (!mgr.getApplicableRegions(pt).allows(DefaultFlag.MOB_DAMAGE)) {
  356. event.setCancelled(true);
  357. return;
  358. }
  359. }
  360. }
  361. }
  362. }
  363. /**
  364. * Called on entity damage.
  365. */
  366. @Override
  367. public void onEntityDamage(EntityDamageEvent event) {
  368. if (event.isCancelled()) {
  369. return;
  370. }
  371. if (event instanceof EntityDamageByEntityEvent) {
  372. this.onEntityDamageByEntity((EntityDamageByEntityEvent) event);
  373. return;
  374. } else if (event instanceof EntityDamageByBlockEvent) {
  375. this.onEntityDamageByBlock((EntityDamageByBlockEvent) event);
  376. return;
  377. }
  378. Entity defender = event.getEntity();
  379. DamageCause type = event.getCause();
  380. ConfigurationManager cfg = plugin.getGlobalStateManager();
  381. WorldConfiguration wcfg = cfg.get(defender.getWorld());
  382. if (defender instanceof Wolf && ((Wolf) defender).isTamed()) {
  383. if (wcfg.antiWolfDumbness) {
  384. event.setCancelled(true);
  385. return;
  386. }
  387. } else if (defender instanceof Player) {
  388. Player player = (Player) defender;
  389. if (isInvincible(player)) {
  390. event.setCancelled(true);
  391. player.setFireTicks(0);
  392. return;
  393. }
  394. if (type == DamageCause.DROWNING && cfg.hasAmphibiousMode(player)) {
  395. player.setRemainingAir(player.getMaximumAir());
  396. event.setCancelled(true);
  397. return;
  398. }
  399. if (type == DamageCause.DROWNING && wcfg.pumpkinScuba
  400. && (player.getInventory().getHelmet().getType() == Material.PUMPKIN
  401. || player.getInventory().getHelmet().getType() == Material.JACK_O_LANTERN)) {
  402. player.setRemainingAir(player.getMaximumAir());
  403. event.setCancelled(true);
  404. return;
  405. }
  406. if (wcfg.disableFallDamage && type == DamageCause.FALL) {
  407. event.setCancelled(true);
  408. return;
  409. }
  410. if (wcfg.disableFireDamage && (type == DamageCause.FIRE
  411. || type == DamageCause.FIRE_TICK)) {
  412. event.setCancelled(true);
  413. return;
  414. }
  415. if (wcfg.disableDrowningDamage && type == DamageCause.DROWNING) {
  416. player.setRemainingAir(player.getMaximumAir());
  417. event.setCancelled(true);
  418. return;
  419. }
  420. if (wcfg.teleportOnSuffocation && type == DamageCause.SUFFOCATION) {
  421. BukkitUtil.findFreePosition(player);
  422. event.setCancelled(true);
  423. return;
  424. }
  425. if (wcfg.disableSuffocationDamage && type == DamageCause.SUFFOCATION) {
  426. event.setCancelled(true);
  427. return;
  428. }
  429. }
  430. }
  431. /**
  432. * Called on entity combust.
  433. */
  434. @Override
  435. public void onEntityCombust(EntityCombustEvent event) {
  436. if (event.isCancelled()) {
  437. return;
  438. }
  439. Entity entity = event.getEntity();
  440. ConfigurationManager cfg = plugin.getGlobalStateManager();
  441. WorldConfiguration wcfg = cfg.get(entity.getWorld());
  442. if (entity instanceof Player) {
  443. Player player = (Player) entity;
  444. if (cfg.hasGodMode(player) || (wcfg.useRegions && RegionQueryUtil.isInvincible(plugin, player))) {
  445. event.setCancelled(true);
  446. return;
  447. }
  448. }
  449. }
  450. /**
  451. * Called on entity explode.
  452. */
  453. @Override
  454. public void onEntityExplode(EntityExplodeEvent event) {
  455. if (event.isCancelled()) {
  456. return;
  457. }
  458. ConfigurationManager cfg = plugin.getGlobalStateManager();
  459. Location l = event.getLocation();
  460. World world = l.getWorld();
  461. WorldConfiguration wcfg = cfg.get(world);
  462. Entity ent = event.getEntity();
  463. if (cfg.activityHaltToggle) {
  464. ent.remove();
  465. event.setCancelled(true);
  466. return;
  467. }
  468. if (ent instanceof LivingEntity) {
  469. if (wcfg.blockCreeperBlockDamage || wcfg.blockCreeperExplosions) {
  470. event.setCancelled(true);
  471. return;
  472. }
  473. if (wcfg.useRegions) {
  474. if (wcfg.useRegions) {
  475. RegionManager mgr = plugin.getGlobalRegionManager().get(world);
  476. for (Block block : event.blockList()) {
  477. if (!mgr.getApplicableRegions(toVector(block)).allows(DefaultFlag.CREEPER_EXPLOSION)) {
  478. event.setCancelled(true);
  479. return;
  480. }
  481. }
  482. }
  483. }
  484. } else if (ent instanceof TNTPrimed) {
  485. if (wcfg.blockTNTBlockDamage || wcfg.blockTNTExplosions) {
  486. event.setCancelled(true);
  487. return;
  488. }
  489. if (wcfg.useRegions) {
  490. RegionManager mgr = plugin.getGlobalRegionManager().get(world);
  491. for (Block block : event.blockList()) {
  492. if (!mgr.getApplicableRegions(toVector(block)).allows(DefaultFlag.TNT)) {
  493. event.setCancelled(true);
  494. return;
  495. }
  496. }
  497. }
  498. } else if (ent instanceof Fireball) {
  499. if (wcfg.blockFireballBlockDamage || wcfg.blockFireballExplosions) {
  500. event.setCancelled(true);
  501. return;
  502. }
  503. if (wcfg.useRegions) {
  504. RegionManager mgr = plugin.getGlobalRegionManager().get(world);
  505. for (Block block : event.blockList()) {
  506. if (!mgr.getApplicableRegions(toVector(block)).allows(DefaultFlag.GHAST_FIREBALL)) {
  507. event.setCancelled(true);
  508. return;
  509. }
  510. }
  511. }
  512. }
  513. if (wcfg.signChestProtection) {
  514. for (Block block : event.blockList()) {
  515. if (wcfg.isChestProtected(block)) {
  516. event.setCancelled(true);
  517. return;
  518. }
  519. }
  520. }
  521. }
  522. /**
  523. * Called on explosion prime
  524. */
  525. @Override
  526. public void onExplosionPrime(ExplosionPrimeEvent event) {
  527. if (event.isCancelled()) {
  528. return;
  529. }
  530. ConfigurationManager cfg = plugin.getGlobalStateManager();
  531. Entity ent = event.getEntity();
  532. if (cfg.activityHaltToggle) {
  533. ent.remove();
  534. event.setCancelled(true);
  535. return;
  536. }
  537. }
  538. /**
  539. * Called on creature spawn.
  540. */
  541. @Override
  542. public void onCreatureSpawn(CreatureSpawnEvent event) {
  543. if (event.isCancelled()) {
  544. return;
  545. }
  546. ConfigurationManager cfg = plugin.getGlobalStateManager();
  547. if (cfg.activityHaltToggle) {
  548. event.setCancelled(true);
  549. return;
  550. }
  551. WorldConfiguration wcfg = cfg.get(event.getEntity().getWorld());
  552. CreatureType creaType = event.getCreatureType();
  553. if (wcfg.blockCreatureSpawn.contains(creaType)) {
  554. event.setCancelled(true);
  555. return;
  556. }
  557. Location eventLoc = event.getLocation();
  558. if (wcfg.useRegions) {
  559. Vector pt = toVector(eventLoc);
  560. RegionManager mgr = plugin.getGlobalRegionManager().get(eventLoc.getWorld());
  561. // @TODO get victims' stacktraces and find out why it's null anyway
  562. if (mgr == null) return;
  563. ApplicableRegionSet set = mgr.getApplicableRegions(pt);
  564. if (!set.allows(DefaultFlag.MOB_SPAWNING)) {
  565. event.setCancelled(true);
  566. return;
  567. }
  568. Set<CreatureType> creatureTypes = set.getFlag(DefaultFlag.DENY_SPAWN);
  569. if (creatureTypes != null && creatureTypes.contains(creaType)) {
  570. event.setCancelled(true);
  571. return;
  572. }
  573. }
  574. }
  575. /**
  576. * Called on pig zap.
  577. */
  578. @Override
  579. public void onPigZap(PigZapEvent event) {
  580. if (event.isCancelled()) {
  581. return;
  582. }
  583. ConfigurationManager cfg = plugin.getGlobalStateManager();
  584. WorldConfiguration wcfg = cfg.get(event.getEntity().getWorld());
  585. if (wcfg.disablePigZap) {
  586. event.setCancelled(true);
  587. }
  588. }
  589. /**
  590. * Called on creeper power.
  591. */
  592. @Override
  593. public void onCreeperPower(CreeperPowerEvent event) {
  594. if (event.isCancelled()) {
  595. return;
  596. }
  597. ConfigurationManager cfg = plugin.getGlobalStateManager();
  598. WorldConfiguration wcfg = cfg.get(event.getEntity().getWorld());
  599. if (wcfg.disableCreeperPower) {
  600. event.setCancelled(true);
  601. }
  602. }
  603. /**
  604. * Called when a painting is removed.
  605. */
  606. @Override
  607. public void onPaintingBreak(PaintingBreakEvent breakEvent) {
  608. if (breakEvent.isCancelled()) {
  609. return;
  610. }
  611. if (!(breakEvent instanceof PaintingBreakByEntityEvent)) {
  612. return;
  613. }
  614. PaintingBreakByEntityEvent event = (PaintingBreakByEntityEvent) breakEvent;
  615. if (!(event.getRemover() instanceof Player)) {
  616. return;
  617. }
  618. Painting painting= event.getPainting();
  619. Player player = (Player) event.getRemover();
  620. World world = painting.getWorld();
  621. ConfigurationManager cfg = plugin.getGlobalStateManager();
  622. WorldConfiguration wcfg = cfg.get(world);
  623. if (wcfg.getBlacklist() != null) {
  624. if (!wcfg.getBlacklist().check(
  625. new BlockBreakBlacklistEvent(plugin.wrapPlayer(player),
  626. toVector(player.getLocation()), 321), false, false)) {
  627. event.setCancelled(true);
  628. return;
  629. }
  630. }
  631. if (wcfg.useRegions) {
  632. if (!plugin.getGlobalRegionManager().canBuild(player, painting.getLocation())) {
  633. player.sendMessage(ChatColor.DARK_RED + "You don't have permission for this area.");
  634. event.setCancelled(true);
  635. return;
  636. }
  637. }
  638. }
  639. /**
  640. * Called on painting place.
  641. */
  642. @Override
  643. public void onPaintingPlace(PaintingPlaceEvent event) {
  644. if (event.isCancelled()) {
  645. return;
  646. }
  647. Block placedOn = event.getBlock();
  648. Player player = event.getPlayer();
  649. World world = placedOn.getWorld();
  650. ConfigurationManager cfg = plugin.getGlobalStateManager();
  651. WorldConfiguration wcfg = cfg.get(world);
  652. if (wcfg.getBlacklist() != null) {
  653. if (!wcfg.getBlacklist().check(
  654. new ItemUseBlacklistEvent(plugin.wrapPlayer(player),
  655. toVector(player.getLocation()), 321), false, false)) {
  656. event.setCancelled(true);
  657. return;
  658. }
  659. }
  660. if (wcfg.useRegions) {
  661. if (!plugin.getGlobalRegionManager().canBuild(player, placedOn.getLocation())) {
  662. player.sendMessage(ChatColor.DARK_RED + "You don't have permission for this area.");
  663. event.setCancelled(true);
  664. return;
  665. }
  666. }
  667. }
  668. /**
  669. * Called on entity health regain.
  670. */
  671. public void onEntityRegainHealth(EntityRegainHealthEvent event) {
  672. if (event.isCancelled()) {
  673. return;
  674. }
  675. Entity ent = event.getEntity();
  676. World world = ent.getWorld();
  677. ConfigurationManager cfg = plugin.getGlobalStateManager();
  678. WorldConfiguration wcfg = cfg.get(world);
  679. if (wcfg.disableHealthRegain) {
  680. event.setCancelled(true);
  681. return;
  682. }
  683. }
  684. /**
  685. * Called when an enderman picks a block up.
  686. */
  687. @Override
  688. public void onEndermanPickup(EndermanPickupEvent event) {
  689. if (event.isCancelled()) {
  690. return;
  691. }
  692. Entity ent = event.getEntity();
  693. Block block = event.getBlock();
  694. ConfigurationManager cfg = plugin.getGlobalStateManager();
  695. WorldConfiguration wcfg = cfg.get(ent.getWorld());
  696. if (wcfg.disableEndermanGriefing) {
  697. event.setCancelled(true);
  698. return;
  699. }
  700. if (wcfg.useRegions) {
  701. if (!plugin.getGlobalRegionManager().allows(DefaultFlag.ENDER_BUILD, block.getLocation())) {
  702. event.setCancelled(true);
  703. return;
  704. }
  705. }
  706. }
  707. /**
  708. * Called when an enderman places a block.
  709. */
  710. @Override
  711. public void onEndermanPlace(EndermanPlaceEvent event) {
  712. if (event.isCancelled()) {
  713. return;
  714. }
  715. Entity ent = event.getEntity();
  716. Location loc = event.getLocation();
  717. ConfigurationManager cfg = plugin.getGlobalStateManager();
  718. WorldConfiguration wcfg = cfg.get(ent.getWorld());
  719. if (wcfg.disableEndermanGriefing) {
  720. event.setCancelled(true);
  721. return;
  722. }
  723. if (wcfg.useRegions) {
  724. if (!plugin.getGlobalRegionManager().allows(DefaultFlag.ENDER_BUILD, loc)) {
  725. event.setCancelled(true);
  726. return;
  727. }
  728. }
  729. }
  730. /**
  731. * Check if a player is invincible, via either god mode or region flag. If
  732. * the region denies invincibility, the player must have an extra permission
  733. * to override it. (worldguard.god.override-regions)
  734. *
  735. * @param player
  736. * @return
  737. */
  738. private boolean isInvincible(Player player) {
  739. ConfigurationManager cfg = plugin.getGlobalStateManager();
  740. WorldConfiguration wcfg = cfg.get(player.getWorld());
  741. boolean god = cfg.hasGodMode(player);
  742. if (wcfg.useRegions) {
  743. Boolean flag = RegionQueryUtil.isAllowedInvinciblity(plugin, player);
  744. boolean allowed = flag == null || flag == true;
  745. boolean invincible = RegionQueryUtil.isInvincible(plugin, player);
  746. if (allowed) {
  747. return god || invincible;
  748. } else {
  749. return (god && plugin.hasPermission(player, "worldguard.god.override-regions"))
  750. || invincible;
  751. }
  752. } else {
  753. return god;
  754. }
  755. }
  756. }