package in.twizmwaz.cardinal.module.modules.scorebox;

import in.twizmwaz.cardinal.GameHandler;
import in.twizmwaz.cardinal.chat.ChatConstant;
import in.twizmwaz.cardinal.chat.LocalizedChatMessage;
import in.twizmwaz.cardinal.chat.UnlocalizedChatMessage;
import in.twizmwaz.cardinal.event.ScoreUpdateEvent;
import in.twizmwaz.cardinal.module.Module;
import in.twizmwaz.cardinal.module.modules.filter.FilterModule;
import in.twizmwaz.cardinal.module.modules.filter.FilterState;
import in.twizmwaz.cardinal.module.modules.regions.RegionModule;
import in.twizmwaz.cardinal.module.modules.regions.type.BlockRegion;
import in.twizmwaz.cardinal.module.modules.score.ScoreModule;
import in.twizmwaz.cardinal.module.modules.team.TeamModule;
import in.twizmwaz.cardinal.util.ChatUtils;
import in.twizmwaz.cardinal.util.TeamUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.inventory.ItemStack;

import java.util.HashMap;

public class Scorebox implements Module {

    private final RegionModule region;
    private final int points;
    private final FilterModule filter;
    private final HashMap<ItemStack, Integer> redeemables;

    protected Scorebox(final RegionModule region, final int points, final FilterModule filter, final HashMap<ItemStack, Integer> redeemables) {
        this.region = region;
        this.points = points;
        this.filter = filter;
        this.redeemables = redeemables;
    }

    @Override
    public void unload() {
        HandlerList.unregisterAll(this);
    }

    @EventHandler(priority = EventPriority.HIGH)
    public void onPlayerMove(PlayerMoveEvent event) {
        if (GameHandler.getGameHandler().getMatch().isRunning()) {
            if (region.contains(new BlockRegion(null, event.getTo().toVector()))) {
                if (filter == null || filter.evaluate(event.getPlayer()).equals(FilterState.ALLOW)) {
                    int points = 0;
                    if (redeemables.size() > 0) {
                        for (ItemStack item : redeemables.keySet()) {
                            if (event.getPlayer().getInventory().contains(item)) {
                                for (ScoreModule score : GameHandler.getGameHandler().getMatch().getModules().getModules(ScoreModule.class)) {
                                    TeamModule playerTeam = TeamUtils.getTeamByPlayer(event.getPlayer());
                                    if (playerTeam != null && score.getTeam() == playerTeam) {
                                        event.getPlayer().getInventory().remove(item);
                                        points += redeemables.get(item);
                                    }
                                }
                            }
                        }
                    }
                    points += this.points;
                    if (points != 0) {
                        TeamModule playerTeam = TeamUtils.getTeamByPlayer(event.getPlayer());
                        if (playerTeam != null) {
                            for (ScoreModule score : GameHandler.getGameHandler().getMatch().getModules().getModules(ScoreModule.class)) {
                                if (score.getTeam() == playerTeam) {
                                    score.setScore(score.getScore() + points);
                                    Bukkit.getServer().getPluginManager().callEvent(new ScoreUpdateEvent(score));
                                    ChatUtils.getGlobalChannel().sendLocalizedMessage(new UnlocalizedChatMessage(ChatColor.GRAY + "{0}", new LocalizedChatMessage(ChatConstant.UI_SCORED_FOR, new UnlocalizedChatMessage(playerTeam.getColor() + event.getPlayer().getName() + ChatColor.GRAY), new UnlocalizedChatMessage(ChatColor.DARK_AQUA + "{0}" + ChatColor.GRAY, points == 1 ? new LocalizedChatMessage(ChatConstant.UI_ONE_POINT) : new LocalizedChatMessage(ChatConstant.UI_POINTS, points + "" + ChatColor.GRAY)), new UnlocalizedChatMessage(playerTeam.getCompleteName()))));
                                }
                            }
                        }
                    }
                }
            }
        }
    }

}

@@ -2031,24 +2031,89 @@
 e.ALLOW)) %7B%0A
+                    if (event.getPlayer().getHealth() %3E 0) %7B%0A    
             
@@ -2136,16 +2136,20 @@
 ts = 0;%0A
+    
         
@@ -2210,24 +2210,28 @@
             
+   
+ 
 for (ItemSta
@@ -2260,24 +2260,28 @@
 keySet()) %7B%0A
+    
             
@@ -2347,24 +2347,28 @@
 ns(item)) %7B%0A
+    
             
@@ -2526,32 +2526,36 @@
                 
+   
+ 
 TeamModule playe
@@ -2600,32 +2600,36 @@
 t.getPlayer());%0A
+    
                 
@@ -2747,16 +2747,20 @@
         
+    
 event.ge
@@ -2830,32 +2830,36 @@
                 
+   
+ 
 points += redeem
@@ -2867,32 +2867,74 @@
 bles.get(item);%0A
+                                        %7D%0A
                 
@@ -3048,35 +3048,16 @@
    %7D%0A   
-                 %7D%0A
         
@@ -3060,32 +3060,33 @@
                 
+ 
 points += this.p
@@ -3108,24 +3108,28 @@
             
+    
 if (points !
@@ -3131,24 +3131,28 @@
 nts != 0) %7B%0A
+    
             
@@ -3249,32 +3249,36 @@
                 
+   
+ 
 if (playerTeam !
@@ -3283,24 +3283,28 @@
  != null) %7B%0A
+    
             
@@ -3458,24 +3458,28 @@
             
+    
 if (score.ge
@@ -3539,16 +3539,20 @@
         
+   
+ 
 score.se
@@ -3582,24 +3582,28 @@
  + points);%0A
+    
             
@@ -3700,24 +3700,28 @@
 nt(score));%0A
+    
             
@@ -4275,24 +4275,62 @@
 Name()))));%0A
+                                    %7D%0A
             

Make sure a player is alive when scoring, fixes #489