package com.github.intangir.Tweaks;

import java.io.File;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Boat;
import org.bukkit.entity.Horse;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Minecart;
import org.bukkit.entity.Player;
import org.bukkit.entity.Vehicle;
import org.bukkit.event.EventHandler;
import org.bukkit.event.vehicle.VehicleExitEvent;
import org.bukkit.inventory.ItemStack;

public class Vehicles extends Tweak
{
	Vehicles(Tweaks plugin) {
		super(plugin);
		CONFIG_FILE = new File(plugin.getDataFolder(), "vehicles.yml");
		TWEAK_NAME = "Tweak_Vehicles";
		TWEAK_VERSION = "1.0";

		fixExitMinecart = true;
		fixExitBoat = true;
		fixExitHorse = true;
		breakSwordInHand = true;
	}

	private boolean fixExitMinecart;
	private boolean fixExitBoat;
	private boolean fixExitHorse;
	private boolean breakSwordInHand;
	
	@EventHandler(ignoreCancelled = true)
	public void onVehicleExit(VehicleExitEvent e) {

		final LivingEntity exiter = e.getExited();
		final Vehicle vehicle = e.getVehicle();
		
		if(exiter instanceof Player) {
			if(	(fixExitMinecart && vehicle instanceof Minecart) || 
				(fixExitBoat	 && vehicle instanceof Boat) || 
				(fixExitHorse	 && vehicle instanceof Horse)) {
				
				final Location fixLoc = exiter.getLocation().add(0, 0.5, 0);
				
				server.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
	    			public void run() {
	    		    	
	    				exiter.teleport(fixLoc);
	    				ItemStack hand = ((Player)exiter).getItemInHand();
	    		    	
	    				// auto destroy boat/minecart if they are a holding a sword
	    				if(breakSwordInHand && hand != null && hand.getType().toString().contains("SWORD")) {
	    					ItemStack item = null;
	    					
	    					if(vehicle instanceof Minecart) {
	    						item = new ItemStack(Material.MINECART);
	    					} else if(vehicle instanceof Boat) {
	    						item = new ItemStack(Material.BOAT);
	    					}

	    					if(item != null) {
	    						exiter.getWorld().dropItem(fixLoc, item);
	    						vehicle.remove();
	    					}
	    				}
	    			}
	    		}, 2);
			}
		}
	}
}

@@ -1450,24 +1450,75 @@
  %09%0A%09    %09%09%09%09
+if(!exiter.isInsideVehicle())%0A%09    %09%09%09%09%7B%0A%09    %09%09%09%09%09
 exiter.telep
@@ -1530,16 +1530,17 @@
 ixLoc);%0A
+%09
 %09    %09%09%09
@@ -1587,24 +1587,25 @@
 emInHand();%0A
+%09
 %09    %09%09    %09
@@ -1597,32 +1597,33 @@
 ;%0A%09%09    %09%09    %09%0A
+%09
 %09    %09%09%09%09// auto
@@ -1675,16 +1675,17 @@
 a sword%0A
+%09
 %09    %09%09%09
@@ -1767,24 +1767,25 @@
 %22SWORD%22)) %7B%0A
+%09
 %09    %09%09%09%09%09It
@@ -1805,16 +1805,17 @@
 = null;%0A
+%09
 %09    %09%09%09
@@ -1817,16 +1817,17 @@
   %09%09%09%09%09%0A
+%09
 %09    %09%09%09
@@ -1858,24 +1858,25 @@
 Minecart) %7B%0A
+%09
 %09    %09%09%09%09%09%09i
@@ -1911,24 +1911,25 @@
 .MINECART);%0A
+%09
 %09    %09%09%09%09%09%7D 
@@ -1959,24 +1959,25 @@
 eof Boat) %7B%0A
+%09
 %09    %09%09%09%09%09%09i
@@ -2012,16 +2012,17 @@
 .BOAT);%0A
+%09
 %09    %09%09%09
@@ -2025,17 +2025,19 @@
  %09%09%09%09%09%7D%0A
-%0A
+%09%0A%09
 %09    %09%09%09
@@ -2053,24 +2053,25 @@
  != null) %7B%0A
+%09
 %09    %09%09%09%09%09%09e
@@ -2111,16 +2111,17 @@
  item);%0A
+%09
 %09    %09%09%09
@@ -2141,16 +2141,17 @@
 move();%0A
+%09
 %09    %09%09%09
@@ -2150,24 +2150,36 @@
 %09    %09%09%09%09%09%7D%0A
+%09%09    %09%09%09%09%7D%0A
 %09    %09%09%09%09%7D%0A%09

fix for minecart elevators