/*
 * Copyright (c) 2015, Mazen Kotb <email@mazenmc.io>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
package io.minecloud.bukkit;

import io.minecloud.models.player.PlayerData;
import io.minecloud.models.server.Server;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.List;

public class PlayerTracker implements Listener {
    private MineCloudPlugin plugin;

    PlayerTracker() {
        this.plugin = (MineCloudPlugin) JavaPlugin.getProvidingPlugin(PlayerTracker.class);
    }

    private void updatePlayer(PlayerData data) {
        Server server = plugin.server();
        List<PlayerData> onlinePlayers = server.onlinePlayers();

        onlinePlayers.set(onlinePlayers.lastIndexOf(data), data);
        server.setOnlinePlayers(onlinePlayers);

        plugin.mongo().repositoryBy(Server.class).save(server);
    }

    @EventHandler
    public void playerJoin(PlayerJoinEvent event) {
        Server server = plugin.server();
        List<PlayerData> onlinePlayers = server.onlinePlayers();
        Player player = event.getPlayer();
        PlayerData data = new PlayerData();

        data.setHealth(player.getHealth());
        data.setMaxHealth(player.getMaxHealth());
        data.setName(player.getName());
        data.setId(player.getUniqueId().toString());

        onlinePlayers.add(data);

        server.setOnlinePlayers(onlinePlayers);
        plugin.mongo().repositoryBy(Server.class).save(server);
    }

    @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
    public void playerHit(EntityDamageEvent event) {
        if (event.getEntityType() != EntityType.PLAYER)
            return;

        Player player = (Player) event.getEntity();
        PlayerData data = plugin.server().playerBy(player.getUniqueId());

        if (data == null)
            return;

        data.setHealth(player.getHealth());
        data.setMaxHealth(player.getMaxHealth());

        updatePlayer(data);
    }

    @EventHandler
    public void playerQuit(PlayerQuitEvent event) {
        Server server = plugin.server();

        server.removePlayer(event.getPlayer().getUniqueId());
        plugin.mongo().repositoryBy(Server.class).save(server);
    }
}

@@ -1534,16 +1534,31 @@
 ePlayer(
+Server server, 
 PlayerDa
@@ -1571,49 +1571,8 @@
 a) %7B
-%0A        Server server = plugin.server();
 %0A   
@@ -2678,24 +2678,65 @@
 etEntity();%0A
+        Server server = plugin.server();%0A
         Play
@@ -2749,31 +2749,22 @@
  data = 
-plugin.
 server
-()
 .playerB
@@ -2952,16 +2952,24 @@
 ePlayer(
+server, 
 data);%0A 

Fix bug