package fr.aumgn.diamondrush.stage.listeners;

import java.util.Set;

import org.bukkit.ChatColor;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerChatEvent;

import fr.aumgn.bukkitutils.util.Vector;
import fr.aumgn.diamondrush.DiamondRush;
import fr.aumgn.diamondrush.game.Game;
import fr.aumgn.diamondrush.game.Team;
import fr.aumgn.diamondrush.stage.DevelopmentStage;

public class DevelopmentListener implements Listener {

    public DevelopmentStage stage;

    public DevelopmentListener(DevelopmentStage stage) {
        this.stage = stage;
    }

    @EventHandler(priority = EventPriority.MONITOR)
    public void onDamage(EntityDamageByEntityEvent event) {
        Entity targetEntity = event.getEntity();
        Entity damagerEntity = event.getDamager();

        if (damagerEntity instanceof Projectile) {
            damagerEntity = ((Projectile) damagerEntity).getShooter();
        }

        if (!(targetEntity instanceof Player)
                || !(damagerEntity instanceof Player)) {
            return;
        }

        Player target = (Player) targetEntity;
        Player damager = (Player) damagerEntity;
        Game game = stage.getGame();
        if (!game.contains(damager) || !game.contains(target)) {
            return;
        }

        Team damagerTeam = game.getTeam(damager);
        if (game.getTeam(target) == damagerTeam) {
            return;
        }

        if (!target.getWorld().equals(game.getWorld())) {
            return;
        }

        Vector targetPos = new Vector(damager.getLocation());
        int totemDistanceSq = damagerTeam.getTotem().
                getMiddle().distanceSq(targetPos);
        if (totemDistanceSq < DiamondRush.getConfig().getSpottedTotemDistance()) {
            handleSpottedPlayer(damager, target);
            return;
        }

        int spawnDistanceSq = damagerTeam.getSpawn().
                getMiddle().distanceSq(targetPos);
        if (spawnDistanceSq < DiamondRush.getConfig().getSpottedSpawnDistance()) {
            handleSpottedPlayer(damager, target);
        }
    }

    private void handleSpottedPlayer(Player player, Player target) {
        Game game = stage.getGame();
        Team team = game.getTeam(target);
        target.teleport(team.getSpawn().getTeleportLocation(game.getWorld(), game.getSpawn()));
        game.sendMessage(target.getDisplayName() + ChatColor.YELLOW +
                " s'est fait voir par " + ChatColor.RESET + player.getDisplayName());
    }

    @EventHandler(priority = EventPriority.HIGH)
    public void onChat(PlayerChatEvent event) {
        Player player = event.getPlayer();
        Game game = stage.getGame();
        if (game.contains(player)) {
            event.setMessage(ChatColor.ITALIC + event.getMessage());
            Set<Player> receivers = event.getRecipients();
            receivers.clear();
            Team team = game.getTeam(player);
            receivers.addAll(team.getPlayers());
            receivers.addAll(game.getSpectators().asCollection());
        }
    }
}

@@ -2939,32 +2939,206 @@
 ains(player)) %7B%0A
+            String message = event.getMessage();%0A            if (message.charAt(0) == '!') %7B%0A                event.setMessage(message.substring(1));%0A            %7D else %7B%0A    
             even
@@ -3186,24 +3186,28 @@
 Message());%0A
+    
             
@@ -3249,24 +3249,28 @@
 cipients();%0A
+    
             
@@ -3292,32 +3292,36 @@
 ();%0A            
+    
 Team team = game
@@ -3342,32 +3342,36 @@
 r);%0A            
+   
+ 
 receivers.addAll
@@ -3387,24 +3387,28 @@
 Players());%0A
+    
             
@@ -3454,32 +3454,46 @@
 sCollection());%0A
+            %7D%0A
         %7D%0A    %7D%0A

Add '!' prefix for global messages