package org.c4k3.Events;

import java.util.Collection;
import java.util.UUID;

import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;

/** This class is responsible for handling the /event command.
 * 
 * Saves all the player's information.
 *  Teleports users to the set starting location of the event.
 *  And prepares them for the event. */
public class EventCommand implements CommandExecutor {

	public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

		Player player = null;
		if (sender instanceof Player) {
			player = (Player) sender;
		}

		/* Command sender must be an ingame player */
		if (player == null) {
			Events.instance.getLogger().info("You must be a player to use this command.");
			return true;
		}

		/* Event must be active */
		if (!Event.getIsActive()) {
			player.sendMessage(ChatColor.RED + "Event is currently closed for new contestants.\n"
					+ "Please wait until the next event starts.");
			return true;
		}

		/* If the event is not setup yet */
		if (!Event.getIsComplete()) {
			sender.sendMessage(ChatColor.RED + "Woops. Looks like the event is set to active,\n" +
					"but I'm missing some information about the event.\n" +
					"Please tell the nice admin that they're missing something :)");
			return true;
		}

		UUID uuid = player.getUniqueId();

		/* Player is already in the event */
		if (Event.isPlayerActive(uuid)) {
			sender.sendMessage(ChatColor.RED + "You cannot join the same event twice.\n" +
					"If you wish to quit the event, type /kill");
			return true;
		}

		if (player.isInsideVehicle())
			player.leaveVehicle();

		/* Everything seems to be in order. Saving all player info for when the event is over */
		String sPlayer = player.getName();
		Location playerLocation = player.getLocation();
		int playerFoodLevel = player.getFoodLevel();
		int playerLevel = player.getLevel();
		float playerXP = player.getExp();
		ItemStack[] armorContents = player.getInventory().getArmorContents();
		ItemStack[] inventoryContents = player.getInventory().getContents();
		Double playerHealth = player.getHealth();
		Collection<PotionEffect> potionEffects = player.getActivePotionEffects();
		GameMode gameMode = player.getGameMode();

		/* Save all this data */
		EventPlayer eventPlayer = new EventPlayer(uuid, sPlayer, playerLocation, playerFoodLevel, playerLevel, playerXP, armorContents, inventoryContents, playerHealth, potionEffects, gameMode, false);
		eventPlayer.save();

		/* Log it all, just in case */
		String sInventoryContents = "";

		for (ItemStack itemStack : armorContents) {
			if (itemStack.getType() == Material.AIR)
				continue;

			String tmp = itemStack.getType().toString()
				+ "." + itemStack.getAmount()
				+ "." + itemStack.getDurability()
				+ "." + itemStack.getEnchantments().toString();
			tmp.replaceAll(" ", "");
			sInventoryContents += " " + tmp;
		}


		for (ItemStack itemStack : inventoryContents) {
			if (itemStack == null)
				continue;

			String tmp = itemStack.getType().toString()
				+ "." + itemStack.getAmount()
				+ "." + itemStack.getDurability()
				+ "." + itemStack.getEnchantments().toString();
			tmp = tmp.replaceAll(" ", "");
			sInventoryContents += " " + tmp;
		}

		Events.instance.getLogger().info(sPlayer + " is participating in event: " + playerLevel + sInventoryContents);

		/* Now we reset all their stuff and teleport them off */
		player.setFoodLevel(20);
		player.setLevel(0);
		player.setExp(0);
		player.getInventory().clear();
		player.getInventory().setArmorContents(null);
		player.setHealth(player.getMaxHealth());
		for (PotionEffect potionEffect : player.getActivePotionEffects())
			player.removePotionEffect(potionEffect.getType());
		player.setGameMode(GameMode.SURVIVAL);
		player.teleport(Event.getStartLocation());

		player.sendMessage(ChatColor.AQUA + "Teleporting you to the event arena."
				+ "\nWhen you die, you will be teleported back with all your items and XP in order.");

		return true;

	}

}


@@ -2907,367 +2907,8 @@
 %0D%0A%0D%0A
-%09%09for (ItemStack itemStack : armorContents) %7B%0D%0A%09%09%09if (itemStack.getType() == Material.AIR)%0D%0A%09%09%09%09continue;%0D%0A%0D%0A%09%09%09String tmp = itemStack.getType().toString()%0D%0A%09%09%09%09+ %22.%22 + itemStack.getAmount()%0D%0A%09%09%09%09+ %22.%22 + itemStack.getDurability()%0D%0A%09%09%09%09+ %22.%22 + itemStack.getEnchantments().toString();%0D%0A%09%09%09tmp.replaceAll(%22 %22, %22%22);%0D%0A%09%09%09sInventoryContents += %22 %22 + tmp;%0D%0A%09%09%7D%0D%0A%0D%0A%0D%0A
 %09%09fo

Remove doublelogging Apparently armor items are now also gotten through the inventory.getContents() call