use util for harmful effects list

This commit is contained in:
2023-06-22 19:06:15 +02:00
parent d859fd0aaa
commit c11c455c53
@@ -4,6 +4,7 @@ import me.youhavetrouble.preventstabby.PreventStabby;
import me.youhavetrouble.preventstabby.players.PlayerManager; import me.youhavetrouble.preventstabby.players.PlayerManager;
import me.youhavetrouble.preventstabby.util.CombatTimer; import me.youhavetrouble.preventstabby.util.CombatTimer;
import me.youhavetrouble.preventstabby.util.PreventStabbyListener; import me.youhavetrouble.preventstabby.util.PreventStabbyListener;
import me.youhavetrouble.preventstabby.util.Util;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.Tameable; import org.bukkit.entity.Tameable;
@@ -11,6 +12,7 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.entity.AreaEffectCloudApplyEvent; import org.bukkit.event.entity.AreaEffectCloudApplyEvent;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import java.util.Iterator; import java.util.Iterator;
import java.util.UUID; import java.util.UUID;
@@ -20,55 +22,39 @@ public class AreaEffectCloudApplyListener implements Listener {
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void onCloudEffects(AreaEffectCloudApplyEvent event) { public void onCloudEffects(AreaEffectCloudApplyEvent event) {
if (!(event.getEntity().getSource() instanceof Player)) return;
PotionEffectType potionEffectType = event.getEntity().getBasePotionData().getType().getEffectType(); PotionEffectType potionEffectType = event.getEntity().getBasePotionData().getType().getEffectType();
if (potionEffectType == null) return;
if (potionEffectType == null) if (!Util.isPotionEffectHarmful(potionEffectType)) return;
return;
if(event.getEntity().getSource() instanceof Player) { PlayerManager playerManager = PreventStabby.getPlugin().getPlayerManager();
if (potionEffectType.equals(PotionEffectType.BLINDNESS) ||
potionEffectType.equals(PotionEffectType.CONFUSION) ||
potionEffectType.equals(PotionEffectType.HARM) ||
potionEffectType.equals(PotionEffectType.HUNGER) ||
potionEffectType.equals(PotionEffectType.POISON) ||
potionEffectType.equals(PotionEffectType.SLOW_DIGGING) ||
potionEffectType.equals(PotionEffectType.WEAKNESS) ||
potionEffectType.equals(PotionEffectType.SLOW) ||
potionEffectType.equals(PotionEffectType.WITHER)) {
PlayerManager playerManager = PreventStabby.getPlugin().getPlayerManager(); Iterator<LivingEntity> it = event.getAffectedEntities().iterator();
UUID damager = ((Player) event.getEntity().getSource()).getUniqueId();
while (it.hasNext()) {
LivingEntity entity = it.next();
if (entity instanceof Player) {
UUID victim = entity.getUniqueId();
if (damager == victim) continue;
Iterator<LivingEntity> it = event.getAffectedEntities().iterator(); if (playerManager.canDamage(damager, victim, true))
UUID damager = ((Player) event.getEntity().getSource()).getUniqueId(); CombatTimer.refreshPlayersCombatTime(damager, victim);
while(it.hasNext()) { else
LivingEntity entity = it.next(); it.remove();
if(entity instanceof Player) {
UUID victim = entity.getUniqueId(); } else if (entity instanceof Tameable) {
if (damager == victim) Tameable tameable = (Tameable) entity;
continue;
if (playerManager.canDamage(damager, victim, true)) if (tameable.getOwner() == null) continue;
CombatTimer.refreshPlayersCombatTime(damager, victim);
else
it.remove();
} else if (entity instanceof Tameable) { UUID victim = tameable.getOwner().getUniqueId();
Tameable tameable = (Tameable) entity; if (victim == damager) continue;
if (tameable.getOwner() == null) if (playerManager.canDamage(damager, victim, true, false))
continue; CombatTimer.refreshPlayersCombatTime(damager, victim);
else
UUID victim = tameable.getOwner().getUniqueId(); it.remove();
if (victim == damager)
continue;
if (playerManager.canDamage(damager, victim, true, false))
CombatTimer.refreshPlayersCombatTime(damager, victim);
else
it.remove();
}
}
} }
} }
} }