package me.markeh.massivecoreforgefixes;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;

import com.massivecraft.massivecore.engine.EngineMassiveCoreDatabase;

public class MCFFEvents implements Listener {
	
	private static MCFFEvents i;
	public static MCFFEvents get() {
		if (i == null) i = new MCFFEvents();
		
		return i;
	}

	// -------------------------------------------- //
	// setSenderReferencesLater
	// -------------------------------------------- //
	// setSenderReferencesLater solves an issue where initiateSender is
	// always called too early for Cauldron/KCauldron. This is due to
	// the mod load delay when logging in. This enables 3 major events
	// so we can run setSenderReferencesSoon later on. 
	
	@EventHandler
	public void onPlayerJoin(PlayerJoinEvent event) {
		MassiveCoreForgeFixes.get().getServer().getPluginManager().registerEvents(this.generateInitiateSenderListener(event.getPlayer()), MassiveCoreForgeFixes.get());
	}
	
	private Listener generateInitiateSenderListener(final Player player) {
		return new Listener() {
			@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = false)
			public void executeInitiateSender(PlayerMoveEvent event) { this.executeInitiateSender(); }
			
			@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = false)
			public void executeInitiateSender(PlayerCommandPreprocessEvent event) { this.executeInitiateSender(); }
			
			@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = false)
			public void executeInitiateSender(AsyncPlayerChatEvent event) { this.executeInitiateSender(); }

			public void executeInitiateSender() {				
				// Now run setSenderReferencesSoon
				try {
					EngineMassiveCoreDatabase.setSenderReferencesSoon(player, null);
				} catch (Exception e) {
					
					// Probably using an older version
					try {
						Class.forName("com.massivecraft.massivecore.MassiveCoreEngineMain")
							.getMethod("setSenderReferencesSoon", Player.class)
							.invoke(this, player);
						
					} catch (Exception e2) {
						e2.printStackTrace();
					}
				}
				// Unregister this listener
				HandlerList.unregisterAll(this);
			}
		};
	}
}

@@ -704,39 +704,32 @@
 ----------------
----- //
 %0A%09// setSenderRe
@@ -790,15 +790,8 @@
 ----
----- //
 %0A%09//
@@ -831,16 +831,20 @@
 an issue
+%0A%09//
  where i
@@ -859,20 +859,16 @@
 ender is
-%0A%09//
  always 
@@ -877,16 +877,20 @@
 lled too
+%0A%09//
  early f
@@ -971,16 +971,20 @@
 in. This
+%0A%09//
  enables
@@ -998,20 +998,16 @@
 r events
-%0A%09//
  so we c
@@ -1008,24 +1008,28 @@
 o we can run
+%0A%09//
  setSenderRe
@@ -1513,32 +1513,65 @@
 veEvent event) %7B
+ if (event.getPlayer() == player)
  this.executeIni
@@ -1732,32 +1732,65 @@
 ssEvent event) %7B
+ if (event.getPlayer() == player)
  this.executeIni
@@ -1951,16 +1951,49 @@
 event) %7B
+ if (event.getPlayer() == player)
  this.ex

Tidy up + only run for that player