package com.hyperfresh.mchyperchat.bukkit;

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

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<Player, User> userCache = new HashMap<>();

	/**
	 * Bukkit's console object.
	 */
	private User console = new BukkitConsole(Bukkit.getConsoleSender(), null);

	@Override
	public void onEnable()
	{
		HyperChat.onEnable();
	}

	@Override
	public void onDisable()
	{
		HyperChat.onDisable();
	}

	@Override
	public User getPlayer(UUID id)
	{
		Player player = Bukkit.getPlayer(id);

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

	public User getPlayer(Player player)
	{
		return userCache.computeIfAbsent(player, p -> new BukkitUser(p, null));
	}

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

@@ -133,16 +133,58 @@
 Plugin;%0A
+import com.hyperfresh.mchyperchat.Player;%0A
 import c
@@ -293,41 +293,8 @@
 er;%0A
-import org.bukkit.entity.Player;%0A
 impo
@@ -595,16 +595,34 @@
 Map%3C
+org.bukkit.entity.
 Player, 
 User
@@ -617,18 +617,20 @@
 Player, 
-Us
+Play
 er%3E user
@@ -916,34 +916,36 @@
 verride%0A%09public 
-Us
+Play
 er getPlayer(UUI
@@ -955,16 +955,34 @@
 d)%0A%09%7B%0A%09%09
+org.bukkit.entity.
 Player p
@@ -1110,26 +1110,28 @@
 %09%7D%0A%0A%09public 
-Us
+Play
 er getPlayer
@@ -1131,16 +1131,34 @@
 tPlayer(
+org.bukkit.entity.
 Player p

return correct type for getPlayer(UUID) in BukkitPlugin