package markehme.factionsplus.listeners;

import java.io.File;

import markehme.factionsplus.Utilities;
import markehme.factionsplus.config.Config;

import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;


//import com.massivecraft.factions.FLocation;
import com.massivecraft.factions.entity.Board;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.MPlayer;
import com.massivecraft.factions.entity.UPlayer;
import com.massivecraft.mcore.ps.PS;

public class AnnounceListener implements Listener{
	@EventHandler(priority=EventPriority.MONITOR)
	public void onPlayerJoin(PlayerJoinEvent event) {
		Player player = event.getPlayer();
		
		UPlayer me = UPlayer.get(player);
		
		if(Config._announce.showLastAnnounceOnLogin._) {
			File fAF = new File(Config.folderAnnouncements, me.getFactionId());
			if(fAF.exists()) {
				try {
					event.getPlayer().sendMessage(Utilities.readFileAsString(fAF));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	@EventHandler(priority=EventPriority.MONITOR)
	public void onPlayerMove(PlayerMoveEvent event) {
		if(event.isCancelled()) {
			return;
		}
		if(Config._announce.showLastAnnounceOnLandEnter._) {
			if (event.getFrom().equals(event.getTo())) return;

			Player player = event.getPlayer();
			UPlayer me = UPlayer.get( player );
						
			Faction factionHere = Board.get(event.getTo()).getFactionAt( PS.valueOf( event.getTo() ) );
			 
			PS p = PS.valueOf(event.getTo());
			
			Faction f = Board.get(event.getTo()).getFactionAt(PS.valueOf(event.getTo()));
			
			if ( Board.get(event.getFrom()).getFactionAt( PS.valueOf( event.getFrom() ) ) != Board.get( event.getTo() ).getFactionAt( PS.valueOf( event.getTo() ) ) ) {
				
				if(factionHere.getId().equals(me.getFactionId())) {
					File fAF=new File(Config.folderAnnouncements, me.getFactionId());
					if(fAF.exists()) {
						try {
							event.getPlayer().sendMessage(Utilities.readFileAsString(fAF));
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			}
		}
	}

}

@@ -146,36 +146,8 @@
 g;%0A%0A
-import org.bukkit.Location;%0A
 impo
@@ -171,24 +171,24 @@
 ity.Player;%0A
+
 import org.b
@@ -387,11 +387,8 @@
 t;%0A%0A
-%0A//
 impo
@@ -420,59 +420,8 @@
 ons.
-FLocation;%0Aimport com.massivecraft.factions.entity.
 Boar
@@ -460,18 +460,13 @@
 ons.
-entity.Fac
+FLoca
 tion
@@ -504,16 +504,9 @@
 ons.
-entity.M
+F
 Play
@@ -546,22 +546,16 @@
 ons.
-entity.U
+F
 Player
+s
 ;%0Aim
@@ -580,19 +580,24 @@
 aft.
-mcore.ps.PS
+factions.Faction
 ;%0A%0Ap
@@ -787,12 +787,9 @@
 ;%0A%09%09
-%0A%09%09U
+F
 Play
@@ -792,31 +792,34 @@
 Player me = 
-U
+F
 Player
+s.i
 .get(player)
@@ -820,18 +820,16 @@
 layer);%0A
-%09%09
 %0A%09%09if(Co
@@ -1408,17 +1408,17 @@
 r();%0A%09%09%09
-U
+F
 Player m
@@ -1425,38 +1425,32 @@
 e = 
-U
+F
 Player
+s.i
 .get(
- 
 player
- 
 );%0A
-%09%09%09%09%09%09%0A
 %09%09%09F
@@ -1483,94 +1483,31 @@
 .get
-(event.getTo()).getFactionAt( PS.valueOf( event.getTo() ) );%0A%09%09%09 %0A%09%09%09PS p = PS.valueOf
+FactionAt(new FLocation
 (eve
@@ -1521,160 +1521,52 @@
 o())
+)
 ;%0A
-%09%09%09
 %0A%09%09%09
-Faction f = Board.get(event.getTo()).getFactionAt(PS.valueOf(event.getTo()));%0A%09%09%09%0A%09%09%09if ( Board.get(event.getFrom()).getFactionAt( PS.valueOf( 
+if (Board.getFactionAt(new FLocation(
 even
@@ -1576,19 +1576,17 @@
 etFrom()
- ) 
+)
 ) != Boa
@@ -1595,78 +1595,50 @@
 .get
-( event.getTo() ).getFactionAt( PS.valueOf( event.getTo() ) ) ) %7B%0A%09%09%09%09
+FactionAt(new FLocation(event.getTo()))) %7B
 %0A%09%09%09

Revert "Possibly ready for 2.0.0, needs testing."