PageRenderTime 142ms CodeModel.GetById 38ms RepoModel.GetById 2ms app.codeStats 0ms

/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectEntityListener.java

https://github.com/tobias1222/Essentials
Java | 368 lines | 341 code | 22 blank | 5 comment | 154 complexity | ca7a6c7ebef90a5c2453dd988bce9891 MD5 | raw file
  1. package com.earth2me.essentials.protect;
  2. import com.earth2me.essentials.IEssentials;
  3. import com.earth2me.essentials.User;
  4. import java.util.HashSet;
  5. import java.util.Set;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. import net.minecraft.server.ChunkPosition;
  9. import net.minecraft.server.Packet60Explosion;
  10. import org.bukkit.Location;
  11. import org.bukkit.Material;
  12. import org.bukkit.block.Block;
  13. import org.bukkit.block.BlockFace;
  14. import org.bukkit.craftbukkit.CraftServer;
  15. import org.bukkit.craftbukkit.CraftWorld;
  16. import org.bukkit.entity.Creeper;
  17. import org.bukkit.entity.Entity;
  18. import org.bukkit.entity.Fireball;
  19. import org.bukkit.entity.LivingEntity;
  20. import org.bukkit.entity.Player;
  21. import org.bukkit.entity.Projectile;
  22. import org.bukkit.entity.TNTPrimed;
  23. import org.bukkit.event.entity.CreatureSpawnEvent;
  24. import org.bukkit.event.entity.EndermanPickupEvent;
  25. import org.bukkit.event.entity.EntityDamageByBlockEvent;
  26. import org.bukkit.event.entity.EntityDamageByEntityEvent;
  27. import org.bukkit.event.entity.EntityDamageEvent;
  28. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
  29. import org.bukkit.event.entity.EntityExplodeEvent;
  30. import org.bukkit.event.entity.EntityListener;
  31. import org.bukkit.event.entity.EntityTargetEvent;
  32. import org.bukkit.event.entity.EntityTargetEvent.TargetReason;
  33. import org.bukkit.event.entity.ExplosionPrimeEvent;
  34. public class EssentialsProtectEntityListener extends EntityListener
  35. {
  36. private final transient IProtect prot;
  37. private final transient IEssentials ess;
  38. public EssentialsProtectEntityListener(final IProtect prot)
  39. {
  40. this.prot = prot;
  41. this.ess = prot.getEssentials();
  42. }
  43. @Override
  44. public void onEntityDamage(final EntityDamageEvent event)
  45. {
  46. if (event.isCancelled())
  47. {
  48. return;
  49. }
  50. final Entity target = event.getEntity();
  51. final User user = ess.getUser(target);
  52. if (event instanceof EntityDamageByBlockEvent)
  53. {
  54. final DamageCause cause = event.getCause();
  55. if (prot.getSettingBool(ProtectConfig.disable_contactdmg)
  56. && cause == DamageCause.CONTACT
  57. && !(target instanceof Player
  58. && user.isAuthorized("essentials.protect.damage.contact")
  59. && !user.isAuthorized("essentials.protect.damage.disable")))
  60. {
  61. event.setCancelled(true);
  62. return;
  63. }
  64. if (prot.getSettingBool(ProtectConfig.disable_lavadmg)
  65. && cause == DamageCause.LAVA
  66. && !(target instanceof Player
  67. && user.isAuthorized("essentials.protect.damage.lava")
  68. && !user.isAuthorized("essentials.protect.damage.disable")))
  69. {
  70. event.setCancelled(true);
  71. return;
  72. }
  73. if (prot.getSettingBool(ProtectConfig.prevent_tnt_explosion)
  74. && cause == DamageCause.BLOCK_EXPLOSION
  75. && !(target instanceof Player
  76. && user.isAuthorized("essentials.protect.damage.tnt")
  77. && !user.isAuthorized("essentials.protect.damage.disable")))
  78. {
  79. event.setCancelled(true);
  80. return;
  81. }
  82. }
  83. if (event instanceof EntityDamageByEntityEvent)
  84. {
  85. final EntityDamageByEntityEvent edEvent = (EntityDamageByEntityEvent)event;
  86. final Entity eAttack = edEvent.getDamager();
  87. final User attacker = ess.getUser(eAttack);
  88. // PVP Settings
  89. if (target instanceof Player && eAttack instanceof Player
  90. && prot.getSettingBool(ProtectConfig.disable_pvp)
  91. && (!user.isAuthorized("essentials.protect.pvp") || !attacker.isAuthorized("essentials.protect.pvp")))
  92. {
  93. event.setCancelled(true);
  94. return;
  95. }
  96. //Creeper explode prevention
  97. if (eAttack instanceof Creeper && prot.getSettingBool(ProtectConfig.prevent_creeper_explosion)
  98. && !(target instanceof Player
  99. && user.isAuthorized("essentials.protect.damage.creeper")
  100. && !user.isAuthorized("essentials.protect.damage.disable")))
  101. {
  102. event.setCancelled(true);
  103. return;
  104. }
  105. if (eAttack instanceof Creeper && prot.getSettingBool(ProtectConfig.prevent_creeper_playerdmg)
  106. && !(target instanceof Player
  107. && user.isAuthorized("essentials.protect.damage.creeper")
  108. && !user.isAuthorized("essentials.protect.damage.disable")))
  109. {
  110. event.setCancelled(true);
  111. return;
  112. }
  113. if (eAttack instanceof Fireball && prot.getSettingBool(ProtectConfig.prevent_fireball_playerdmg)
  114. && !(target instanceof Player
  115. && user.isAuthorized("essentials.protect.damage.fireball")
  116. && !user.isAuthorized("essentials.protect.damage.disable")))
  117. {
  118. event.setCancelled(true);
  119. return;
  120. }
  121. if (eAttack instanceof TNTPrimed && prot.getSettingBool(ProtectConfig.prevent_tnt_playerdmg)
  122. && !(target instanceof Player
  123. && user.isAuthorized("essentials.protect.damage.tnt")
  124. && !user.isAuthorized("essentials.protect.damage.disable")))
  125. {
  126. event.setCancelled(true);
  127. return;
  128. }
  129. if (edEvent.getDamager() instanceof Projectile
  130. && target instanceof Player
  131. && ((prot.getSettingBool(ProtectConfig.disable_projectiles)
  132. && !(user.isAuthorized("essentials.protect.damage.projectiles")
  133. && !user.isAuthorized("essentials.protect.damage.disable")))
  134. || (((Projectile)edEvent.getDamager()).getShooter() instanceof Player
  135. && prot.getSettingBool(ProtectConfig.disable_pvp)
  136. && (!user.isAuthorized("essentials.protect.pvp")
  137. || !ess.getUser(((Projectile)edEvent.getDamager()).getShooter()).isAuthorized("essentials.protect.pvp")))))
  138. {
  139. event.setCancelled(true);
  140. return;
  141. }
  142. }
  143. final DamageCause cause = event.getCause();
  144. if (target instanceof Player)
  145. {
  146. if (cause == DamageCause.FALL
  147. && prot.getSettingBool(ProtectConfig.disable_fall)
  148. && !(user.isAuthorized("essentials.protect.damage.fall")
  149. && !user.isAuthorized("essentials.protect.damage.disable")))
  150. {
  151. event.setCancelled(true);
  152. return;
  153. }
  154. if (cause == DamageCause.SUFFOCATION
  155. && prot.getSettingBool(ProtectConfig.disable_suffocate)
  156. && !(user.isAuthorized("essentials.protect.damage.suffocation")
  157. && !user.isAuthorized("essentials.protect.damage.disable")))
  158. {
  159. event.setCancelled(true);
  160. return;
  161. }
  162. if ((cause == DamageCause.FIRE
  163. || cause == DamageCause.FIRE_TICK)
  164. && prot.getSettingBool(ProtectConfig.disable_firedmg)
  165. && !(user.isAuthorized("essentials.protect.damage.fire")
  166. && !user.isAuthorized("essentials.protect.damage.disable")))
  167. {
  168. event.setCancelled(true);
  169. return;
  170. }
  171. if (cause == DamageCause.DROWNING
  172. && prot.getSettingBool(ProtectConfig.disable_drown)
  173. && !(user.isAuthorized("essentials.protect.damage.drowning")
  174. && !user.isAuthorized("essentials.protect.damage.disable")))
  175. {
  176. event.setCancelled(true);
  177. return;
  178. }
  179. if (cause == DamageCause.LIGHTNING
  180. && prot.getSettingBool(ProtectConfig.disable_lightning)
  181. && !(user.isAuthorized("essentials.protect.damage.lightning")
  182. && !user.isAuthorized("essentials.protect.damage.disable")))
  183. {
  184. event.setCancelled(true);
  185. return;
  186. }
  187. }
  188. }
  189. @Override
  190. public void onEntityExplode(final EntityExplodeEvent event)
  191. {
  192. if (event.isCancelled())
  193. {
  194. return;
  195. }
  196. final int maxHeight = ess.getSettings().getProtectCreeperMaxHeight();
  197. //Nicccccccccce plaaacccccccccce..
  198. if (event.getEntity() instanceof LivingEntity
  199. && (prot.getSettingBool(ProtectConfig.prevent_creeper_explosion)
  200. || prot.getSettingBool(ProtectConfig.prevent_creeper_blockdmg)
  201. || (maxHeight >= 0 && event.getLocation().getBlockY() > maxHeight)))
  202. {
  203. try
  204. {
  205. final Set<ChunkPosition> set = new HashSet<ChunkPosition>(event.blockList().size());
  206. final Player[] players = ess.getServer().getOnlinePlayers();
  207. final Set<ChunkPosition> blocksUnderPlayers = new HashSet<ChunkPosition>(players.length);
  208. final Location loc = event.getLocation();
  209. for (Player player : players)
  210. {
  211. if (player.getWorld().equals(loc.getWorld()))
  212. {
  213. blocksUnderPlayers.add(
  214. new ChunkPosition(
  215. player.getLocation().getBlockX(),
  216. player.getLocation().getBlockY() - 1,
  217. player.getLocation().getBlockZ()));
  218. }
  219. }
  220. ChunkPosition cp;
  221. for (Block block : event.blockList())
  222. {
  223. cp = new ChunkPosition(block.getX(), block.getY(), block.getZ());
  224. if (!blocksUnderPlayers.contains(cp))
  225. {
  226. set.add(cp);
  227. }
  228. }
  229. ((CraftServer)ess.getServer()).getHandle().sendPacketNearby(loc.getX(), loc.getY(), loc.getZ(), 64.0D, ((CraftWorld)loc.getWorld()).getHandle().worldProvider.dimension,
  230. new Packet60Explosion(loc.getX(), loc.getY(), loc.getZ(), 3.0f, set));
  231. }
  232. catch (Throwable ex)
  233. {
  234. Logger.getLogger("Minecraft").log(Level.SEVERE, null, ex);
  235. }
  236. event.setCancelled(true);
  237. return;
  238. }
  239. else if (event.getEntity() instanceof TNTPrimed
  240. && prot.getSettingBool(ProtectConfig.prevent_tnt_explosion))
  241. {
  242. event.setCancelled(true);
  243. return;
  244. }
  245. else if (event.getEntity() instanceof Fireball
  246. && prot.getSettingBool(ProtectConfig.prevent_fireball_explosion))
  247. {
  248. event.setCancelled(true);
  249. return;
  250. }
  251. // This code will prevent explosions near protected rails, signs or protected chests
  252. // TODO: Use protect db instead of this code
  253. for (Block block : event.blockList())
  254. {
  255. if ((block.getRelative(BlockFace.UP).getType() == Material.RAILS
  256. || block.getType() == Material.RAILS
  257. || block.getRelative(BlockFace.UP).getType() == Material.POWERED_RAIL
  258. || block.getType() == Material.POWERED_RAIL
  259. || block.getRelative(BlockFace.UP).getType() == Material.DETECTOR_RAIL
  260. || block.getType() == Material.DETECTOR_RAIL)
  261. && prot.getSettingBool(ProtectConfig.protect_rails))
  262. {
  263. event.setCancelled(true);
  264. return;
  265. }
  266. if ((block.getType() == Material.WALL_SIGN
  267. || block.getRelative(BlockFace.NORTH).getType() == Material.WALL_SIGN
  268. || block.getRelative(BlockFace.EAST).getType() == Material.WALL_SIGN
  269. || block.getRelative(BlockFace.SOUTH).getType() == Material.WALL_SIGN
  270. || block.getRelative(BlockFace.WEST).getType() == Material.WALL_SIGN
  271. || block.getType() == Material.SIGN_POST
  272. || block.getRelative(BlockFace.UP).getType() == Material.SIGN_POST)
  273. && prot.getSettingBool(ProtectConfig.protect_signs))
  274. {
  275. event.setCancelled(true);
  276. return;
  277. }
  278. }
  279. }
  280. @Override
  281. public void onCreatureSpawn(final CreatureSpawnEvent event)
  282. {
  283. if (event.getEntity() instanceof Player)
  284. {
  285. return;
  286. }
  287. if (event.isCancelled())
  288. {
  289. return;
  290. }
  291. final String creatureName = event.getCreatureType().toString().toLowerCase();
  292. if (creatureName == null || creatureName.isEmpty())
  293. {
  294. return;
  295. }
  296. if (ess.getSettings().getProtectPreventSpawn(creatureName))
  297. {
  298. event.setCancelled(true);
  299. }
  300. }
  301. @Override
  302. public void onEntityTarget(final EntityTargetEvent event)
  303. {
  304. if (event.isCancelled())
  305. {
  306. return;
  307. }
  308. if (!(event.getTarget() instanceof Player))
  309. {
  310. return;
  311. }
  312. final User user = ess.getUser(event.getTarget());
  313. if ((event.getReason() == TargetReason.CLOSEST_PLAYER
  314. || event.getReason() == TargetReason.TARGET_ATTACKED_ENTITY
  315. || event.getReason() == TargetReason.PIG_ZOMBIE_TARGET
  316. || event.getReason() == TargetReason.RANDOM_TARGET
  317. || event.getReason() == TargetReason.TARGET_ATTACKED_OWNER
  318. || event.getReason() == TargetReason.OWNER_ATTACKED_TARGET)
  319. && prot.getSettingBool(ProtectConfig.prevent_entitytarget)
  320. && !user.isAuthorized("essentials.protect.entitytarget.bypass"))
  321. {
  322. event.setCancelled(true);
  323. return;
  324. }
  325. }
  326. @Override
  327. public void onExplosionPrime(ExplosionPrimeEvent event)
  328. {
  329. if (event.getEntity() instanceof Fireball
  330. && prot.getSettingBool(ProtectConfig.prevent_fireball_fire))
  331. {
  332. event.setFire(false);
  333. }
  334. }
  335. @Override
  336. public void onEndermanPickup(EndermanPickupEvent event)
  337. {
  338. if (event.isCancelled()) {
  339. return;
  340. }
  341. if (prot.getSettingBool(ProtectConfig.prevent_enderman_pickup)) {
  342. event.setCancelled(true);
  343. return;
  344. }
  345. }
  346. }