PageRenderTime 96ms CodeModel.GetById 73ms app.highlight 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

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