From 7c2dacce7ca9c3eac1dcc564b703ede2b37f3e29 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Thu, 18 Apr 2013 17:12:46 +0200 Subject: [PATCH] Some more work to the potion damage checker. Moved stuff to MCore infact. --- src/com/massivecraft/factions/Const.java | 1 + .../listeners/TodoFactionsEntityListener.java | 55 ++++++------------- 2 files changed, 17 insertions(+), 39 deletions(-) diff --git a/src/com/massivecraft/factions/Const.java b/src/com/massivecraft/factions/Const.java index 18a663ec..727146fc 100644 --- a/src/com/massivecraft/factions/Const.java +++ b/src/com/massivecraft/factions/Const.java @@ -28,6 +28,7 @@ public class Const public static final char[] MAP_KEY_CHARS = "\\/#?$%=&^ABCDEFGHJKLMNOPQRSTUVWXYZ1234567890abcdeghjmnopqrsuvwxyz".toCharArray(); // Enumerations + public static final Set MATERIALS_EDIT_ON_INTERACT = MUtil.set( Material.DIODE_BLOCK_OFF, Material.DIODE_BLOCK_ON, diff --git a/src/com/massivecraft/factions/listeners/TodoFactionsEntityListener.java b/src/com/massivecraft/factions/listeners/TodoFactionsEntityListener.java index 79fa99cd..afacb400 100644 --- a/src/com/massivecraft/factions/listeners/TodoFactionsEntityListener.java +++ b/src/com/massivecraft/factions/listeners/TodoFactionsEntityListener.java @@ -1,10 +1,6 @@ package com.massivecraft.factions.listeners; import java.text.MessageFormat; -import java.util.Arrays; -import java.util.Iterator; -import java.util.LinkedHashSet; -import java.util.Set; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -20,8 +16,6 @@ import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.entity.PotionSplashEvent; -import org.bukkit.potion.PotionEffect; -import org.bukkit.potion.PotionEffectType; import com.massivecraft.factions.BoardColl; import com.massivecraft.factions.ConfServer; @@ -32,6 +26,7 @@ import com.massivecraft.factions.Faction; import com.massivecraft.factions.Rel; import com.massivecraft.factions.event.PowerLossEvent; import com.massivecraft.mcore.ps.PS; +import com.massivecraft.mcore.util.MUtil; public class TodoFactionsEntityListener implements Listener @@ -92,18 +87,16 @@ public class TodoFactionsEntityListener implements Listener // PVP STUFF?? // -------------------------------------------- // - - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onEntityDamage(EntityDamageEvent event) { - if (event instanceof EntityDamageByEntityEvent) + // TODO: Can't we just listen to the class type the sub is of? + if (!(event instanceof EntityDamageByEntityEvent)) return; + EntityDamageByEntityEvent sub = (EntityDamageByEntityEvent)event; + + if ( ! this.canDamagerHurtDamagee(sub, true)) { - EntityDamageByEntityEvent sub = (EntityDamageByEntityEvent)event; - if ( ! this.canDamagerHurtDamagee(sub, true)) - { - event.setCancelled(true); - } + event.setCancelled(true); } } @@ -118,38 +111,22 @@ public class TodoFactionsEntityListener implements Listener } } - private static final Set badPotionEffects = new LinkedHashSet(Arrays.asList( - PotionEffectType.BLINDNESS, PotionEffectType.CONFUSION, PotionEffectType.HARM, PotionEffectType.HUNGER, - PotionEffectType.POISON, PotionEffectType.SLOW, PotionEffectType.SLOW_DIGGING, PotionEffectType.WEAKNESS, - PotionEffectType.WITHER - )); - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onPotionSplashEvent(PotionSplashEvent event) { - // see if the potion has a harmful effect - boolean badjuju = false; - for (PotionEffect effect : event.getPotion().getEffects()) - { - if (badPotionEffects.contains(effect.getType())) - { - badjuju = true; - break; - } - } - if ( ! badjuju) return; - + // If a harmful potion is splashing ... + if (!MUtil.isHarmfulPotion(event.getPotion())) return; + Entity thrower = event.getPotion().getShooter(); // scan through affected entities to make sure they're all valid targets - Iterator iter = event.getAffectedEntities().iterator(); - while (iter.hasNext()) + for (LivingEntity affectedEntity : event.getAffectedEntities()) { - LivingEntity target = iter.next(); - EntityDamageByEntityEvent sub = new EntityDamageByEntityEvent(thrower, target, EntityDamageEvent.DamageCause.CUSTOM, 0); - if ( ! this.canDamagerHurtDamagee(sub, true)) - event.setIntensity(target, 0.0); // affected entity list doesn't accept modification (iter.remove() is a no-go), but this works - sub = null; + EntityDamageByEntityEvent sub = new EntityDamageByEntityEvent(thrower, affectedEntity, EntityDamageEvent.DamageCause.CUSTOM, 0); + if (this.canDamagerHurtDamagee(sub, true)) continue; + + // affected entity list doesn't accept modification (iter.remove() is a no-go), but this works + event.setIntensity(affectedEntity, 0.0); } }