package com.alecgorge.minecraft.jsonapi.util;

import java.io.File;

//#ifdefined mcversion
//$import net.minecraft.server./*$mcversion$*/.*;
//$import org.bukkit.craftbukkit./*$mcversion$*/.*;
//#if mc17OrNewer=="yes"
import net.minecraft.util.com.mojang.authlib.GameProfile;
//#endif
//#else
import net.minecraft.server.v1_7_R1.*;
import org.bukkit.craftbukkit.v1_7_R1.*;
//#endif

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

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

		int index = 0;
		for (World w : Bukkit.getWorlds()) {
			try {
				// See if the player has data files

				// Find the player folder
				File playerfolder = new File(w.getWorldFolder(), "players");
				if (!playerfolder.exists()) {
					return null;
				}

				Player target = null;
				MinecraftServer server = null;
				try {
					server = ((CraftServer) Bukkit.getServer()).getServer();
				}
				catch (Exception e) {
					server = ((CraftServer) Bukkit.getServer()).getHandle().getServer();
				}

				// Create an entity to load the player data
				// #if mc17OrNewer=="yes"
				EntityPlayer entity = new EntityPlayer(server, server.getWorldServer(index), new GameProfile("fake_for_jsonapi_offline_player_loading", exactPlayerName), new PlayerInteractManager(server.getWorldServer(index)));
				// #else
				// $EntityPlayer entity = new EntityPlayer(server,
				// server.getWorldServer(index), exactPlayerName, new
				// PlayerInteractManager(server.getWorldServer(index)));
				// #endif

				// Get the bukkit entity
				target = (entity == null) ? null : entity.getBukkitEntity();

				if (target != null) {
					// Load data
					target.loadData();
					// Return the entity
					return target;
				}
			}
			catch (Exception e) {
				e.printStackTrace();
			}
			catch (Error e) {
				e.printStackTrace();
			}
			finally {
				index++;
			}
		}

		return null;
	}
}

@@ -711,16 +711,18 @@
 here...%0A
+%09%09
 %0A%09%09int i
@@ -1112,21 +1112,17 @@
 );%0A%09%09%09%09%7D
-%0A%09%09%09%09
+ 
 catch (E
@@ -1270,17 +1270,16 @@
 a%0A%09%09%09%09//
- 
 #if mc17
@@ -1508,24 +1508,27 @@
 ndex)));
+%09%09%09%09
 %0A%09%09%09%09//
- 
 #else%0A%09%09
@@ -1531,17 +1531,16 @@
 e%0A%09%09%09%09//
- 
 $EntityP
@@ -1578,23 +1578,16 @@
 (server,
-%0A%09%09%09%09//
  server.
@@ -1629,23 +1629,16 @@
 ame, new
-%0A%09%09%09%09//
  PlayerI
@@ -1690,17 +1690,16 @@
 ;%0A%09%09%09%09//
- 
 #endif%0A%0A
@@ -1913,28 +1913,25 @@
 ;%0A%09%09%09%09%7D%0A%09%09%09%7D
-%0A%09%09%09
+ 
 catch (Excep
@@ -1969,20 +1969,17 @@
 ();%0A%09%09%09%7D
-%0A%09%09%09
+ 
 catch (E
@@ -2017,20 +2017,17 @@
 ();%0A%09%09%09%7D
-%0A%09%09%09
+ 
 finally 

Revert "missed a file whilte formatting"