package com.hyperfresh.mchyperchat.bukkit;

import com.hyperfresh.mchyperchat.HyperChat;
import com.hyperfresh.mchyperchat.HyperChatPlugin;
import com.hyperfresh.mchyperchat.Player;
import com.hyperfresh.mchyperchat.User;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * The entry class for the Bukkit plugin.
 */
public class BukkitPlugin extends JavaPlugin implements HyperChatPlugin
{
	/**
	 * A cache of Users that wrap Players.
	 */
	private Map<UUID, Player> userCache = new HashMap<>();

	/**
	 * Bukkit's console object.
	 */
	private User console;

	private BukkitListener listener;

	private HyperChat hyperChat;

	@Override
	public void onEnable()
	{
		hyperChat = HyperChat.getInstance();
		listener = new BukkitListener(hyperChat, this);

		hyperChat.onEnable(this);

		this.console = new BukkitConsole(hyperChat, Bukkit.getConsoleSender());

		Bukkit.getOnlinePlayers().stream().forEach(
			player -> userCache.put(player.getUniqueId(), new BukkitPlayer(hyperChat, player))
		);

		Bukkit.getPluginManager().registerEvents(listener, this);
	}

	@Override
	public void onDisable()
	{
		hyperChat.onDisable();

		userCache.clear();
	}

	@Override
	public Player getPlayer(UUID id)
	{
		org.bukkit.entity.Player player = Bukkit.getPlayer(id);

		//return null if the player is null
		return player == null ? null : getPlayer(player);
	}

	@Override
	public Collection<Player> getPlayers()
	{
		return userCache.values();
	}

	public Player getPlayer(org.bukkit.entity.Player player)
	{
		return userCache.computeIfAbsent(player.getUniqueId(), id -> new BukkitPlayer(hyperChat, player));
	}

	public void addPlayer(org.bukkit.entity.Player player)
	{
		userCache.put(player.getUniqueId(), new BukkitPlayer(hyperChat, player));
	}

	public void removePlayer(org.bukkit.entity.Player player)
	{
		userCache.remove(player.getUniqueId());
	}

	@Override
	public User getConsole()
	{
		return console;
	}
}

@@ -383,16 +383,52 @@
 il.UUID;
+%0Aimport java.util.stream.Collectors;
 %0A%0A/**%0A *
@@ -1540,23 +1540,19 @@
 ion%3C
-Play
+Us
 er%3E get
-Play
+Us
 ers(
@@ -1562,33 +1562,165 @@
 %7B%0A%09%09
-return userCache.values()
+Collection%3CUser%3E users = userCache.values().stream()%0A%09%09%09.map(player -%3E (User)player)%0A%09%09%09.collect(Collectors.toList());%0A%0A%09%09users.add(console);%0A%0A%09%09return users
 ;%0A%09%7D
@@ -2136,69 +2136,7 @@
 ;%0A%09%7D
-%0A%0A%09@Override%0A%09public User getConsole()%0A%09%7B%0A%09%09return console;%0A%09%7D
 %0A%7D%0A

change BukkitPlugin for interface changes