package net.wizardsoflua.testenv.player;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.UUID;

import javax.annotation.Nullable;

import com.mojang.authlib.GameProfile;

import net.minecraft.world.WorldServer;

public class WolFakePlayerFactory {

  // TODO can we change the ID and the Name?
  private final String fakePlayerName = "[WolFakePlayer]"; // "[Minecraft]"
  private final GameProfile fakeProfile =
      new GameProfile(UUID.fromString("41C82C87-7AfB-4024-BA57-13D2C99CAE77"), fakePlayerName);

  private @Nullable WorldServer currentWorld;
  private @Nullable WolFakePlayer fakePlayer;

  public void onLoadWorld(WorldServer world) {
    this.currentWorld = world;
    System.out.println("onLoadWorld");
  }

  public void onUnloadWorld(WorldServer world) {
    System.out.println("onUnloadWorld");
    if (fakePlayer != null) {
      // TODO other things to do?
      fakePlayer = null;
    }
  }

  public WolFakePlayer getFakePlayer() {
    checkNotNull(currentWorld, "currentWorld==null!");
    if (fakePlayer == null) {
      fakePlayer = new WolFakePlayer(currentWorld, fakeProfile);
    }
    return fakePlayer;
  }

}

@@ -101,16 +101,67 @@
 tNull;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A
 import j
@@ -603,43 +603,61 @@
 ate 
-@Nullable WorldServer currentWorld;
+List%3CWolFakePlayer%3E fakePlayers = new ArrayList%3C%3E();%0A
 %0A  p
@@ -679,30 +679,30 @@
 e Wo
-lFakePlayer fakePlayer
+rldServer currentWorld
 ;%0A%0A 
@@ -912,32 +912,33 @@
 orld%22);%0A    if (
+!
 fakePlayer != nu
@@ -935,16 +935,19 @@
 ayer
- != null
+s.isEmpty()
 ) %7B%0A
@@ -1000,15 +1000,17 @@
 ayer
- = null
+s.clear()
 ;%0A  
@@ -1136,25 +1136,42 @@
 ayer
- == null) %7B%0A     
+s.isEmpty()) %7B%0A      WolFakePlayer
  fak
@@ -1226,16 +1226,51 @@
 ofile);%0A
+      fakePlayers.add(fakePlayer);%0A
     %7D%0A  
@@ -1288,16 +1288,24 @@
 kePlayer
+s.get(0)
 ;%0A  %7D%0A%0A%7D

Refactor WolFakePlayerFactory