package in.twizmwaz.cardinal.module.modules.friendlyFire;
import in.twizmwaz.cardinal.GameHandler;
import in.twizmwaz.cardinal.match.Match;
import in.twizmwaz.cardinal.module.Module;
import in.twizmwaz.cardinal.module.modules.scoreboard.ScoreboardModule;
import in.twizmwaz.cardinal.module.modules.team.TeamModule;
import in.twizmwaz.cardinal.util.TeamUtils;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityShootBowEvent;
import org.bukkit.event.entity.PotionSplashEvent;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
public class FriendlyFire implements Module {
private Match match;
private boolean arrowReturn;
protected FriendlyFire(Match match, boolean enabled, boolean arrowReturn) {
this.match = match;
this.arrowReturn = arrowReturn;
if (enabled) {
for (TeamModule team : TeamUtils.getTeams()) {
for (ScoreboardModule scoreboard : GameHandler.getGameHandler().getMatch().getModules().getModules(ScoreboardModule.class)) {
scoreboard.getScoreboard().getTeam(team.getId()).setAllowFriendlyFire(false);
}
}
}
}
@EventHandler
public void onBowShootEvent(EntityShootBowEvent event) {
if (event.getEntity() instanceof Player) {
event.getEntity().setMetadata("team", new FixedMetadataValue(GameHandler.getGameHandler().getPlugin(), TeamUtils.getTeamByPlayer(((Player) event.getEntity()).getPlayer()).getId()));
}
}
@EventHandler
public void onEntityDamageEvent(EntityDamageByEntityEvent event) {
if (event.getDamager() instanceof Arrow) {
if (((Arrow) event.getDamager()).getShooter() instanceof Player) {
if (event.getDamager().hasMetadata("team")) {
Player shooter = (Player) ((Arrow) event.getDamager()).getShooter();
if (TeamUtils.getTeamByPlayer(shooter) == TeamUtils.getTeamById(event.getDamager().getMetadata("team").get(0).toString())) {
event.setCancelled(true);
}
}
}
}
}
@EventHandler
public void onPotionSplash(PotionSplashEvent event) {
boolean proceed = false;
for (PotionEffect effect : event.getPotion().getEffects()) {
if (effect.getType().equals(PotionEffectType.POISON) || effect.getType().equals(PotionEffectType.BLINDNESS) ||
effect.getType().equals(PotionEffectType.CONFUSION) || effect.getType().equals(PotionEffectType.HARM) ||
effect.getType().equals(PotionEffectType.HUNGER) || effect.getType().equals(PotionEffectType.SLOW) ||
effect.getType().equals(PotionEffectType.SLOW_DIGGING) || effect.getType().equals(PotionEffectType.WITHER) ||
effect.getType().equals(PotionEffectType.WEAKNESS)) {
proceed = true;
}
}
if (proceed && event.getPotion().getShooter() instanceof Player && TeamUtils.getTeamByPlayer((Player) event.getPotion().getShooter()) != null) {
TeamModule team = TeamUtils.getTeamByPlayer((Player) event.getPotion().getShooter());
for (LivingEntity affected : event.getAffectedEntities()) {
if (affected instanceof Player && TeamUtils.getTeamByPlayer((Player) affected) != null && TeamUtils.getTeamByPlayer((Player) affected) == team && !affected.equals((Player) event.getPotion().getShooter())) {
event.setIntensity(affected, 0);
}
}
}
}
@Override
public void unload() {
HandlerList.unregisterAll(this);
}
}