package com.alecgorge.minecraft.jsonapi.util;

import java.util.UUID;

import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

import com.mojang.authlib.GameProfile;

//#ifdefined mcversion
//$import net.minecraft.server./*$mcversion$*/.EntityPlayer;
//$import net.minecraft.server./*$mcversion$*/.*;
//$import org.bukkit.craftbukkit./*$mcversion$*/.*;
//#else
import net.minecraft.server.v1_11_R1.EntityPlayer;
import net.minecraft.server.v1_11_R1.*;
import org.bukkit.craftbukkit.v1_11_R1.*;
//#endif

public class OfflinePlayerLoader {
	public static Player load(String exactPlayerName) {
		// big thanks to
		// https://github.com/lishid/OpenInv/blob/master/src/main/java/com/lishid/openinv/internal/v1_8_R1/PlayerDataManager.java
		// Offline inv here...

		try {
			UUID uuid = matchUser(exactPlayerName);
			if (uuid == null) {
				return null;
			}

			OfflinePlayer player = Bukkit.getOfflinePlayer(uuid);
			return loadFromOfflinePlayer(player);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		catch (Error e) {
			e.printStackTrace();
		}

		return null;
	}
	
	public static Player loadFromOfflinePlayer(OfflinePlayer player) {
		if (player == null) {
			return null;
		}
		
		GameProfile profile = new GameProfile(player.getUniqueId(), player.getName());
		MinecraftServer server = ((CraftServer) Bukkit.getServer()).getServer();
		// Create an entity to load the player data
		EntityPlayer entity = new EntityPlayer(server, server.getWorldServer(0), profile, new PlayerInteractManager(server.getWorldServer(0)));

		// Get the bukkit entity
		Player target = entity.getBukkitEntity();
		if (target != null) {
			// Load data
			target.loadData();
			// Return the entity
			return target;
		}
		
		return null;
	}

	public static UUID matchUser(String search) {
		OfflinePlayer[] offlinePlayers = Bukkit.getOfflinePlayers();
		for (OfflinePlayer player : offlinePlayers) {
			String name = player.getName();

			if (name == null) {
				continue;
			}
			if (name.equalsIgnoreCase(search)) {
				return player.getUniqueId();
			}
		}

		return null;
	}
}

@@ -201,202 +201,8 @@
 e;%0A%0A
-//#ifdefined mcversion%0A//$import net.minecraft.server./*$mcversion$*/.EntityPlayer;%0A//$import net.minecraft.server./*$mcversion$*/.*;%0A//$import org.bukkit.craftbukkit./*$mcversion$*/.*;%0A//#else%0A
 impo
@@ -225,25 +225,25 @@
 .server.v1_1
-1
+2
 _R1.EntityPl
@@ -276,25 +276,25 @@
 .server.v1_1
-1
+2
 _R1.*;%0Aimpor
@@ -326,24 +326,15 @@
 v1_1
-1
+2
 _R1.*;
-%0A//#endif
 %0A%0Apu

Update OfflinePlayerLoader.java