package me.jacobcrofts.simplestructureloader.commands;

import java.io.IOException;

import org.bukkit.GameMode;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import me.jacobcrofts.simplestructureloader.api.StructureAPI;
import me.jacobcrofts.simplestructureloader.managers.SelectionManager;
import me.jacobcrofts.simplestructureloader.util.Selection;

public class CmdSave implements CommandExecutor {
	
	private SelectionManager manager;
	
	public CmdSave(SelectionManager manager) {
		this.manager = manager;
	}

	@Override
	public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
		
		if (sender instanceof Player) {
			Player player = (Player) sender;
			
			if (player.getGameMode() != GameMode.CREATIVE) {
				return false;
			}
			
			if (!(player.isOp() || player.hasPermission("simple-structures.ALL"))) {
				return false;
				// TODO: custom error messages
			}
			
			Selection selection = this.manager.getSelection(player);
			selection.saveCurrentSelection();
			try {
				StructureAPI.writeToFile(args[0], selection);
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
			
			return true;
			
		}
		
		return false;
	}

}

@@ -78,16 +78,45 @@
 ption;%0A%0A
+import org.bukkit.ChatColor;%0A
 import o
@@ -881,35 +881,131 @@
 TIVE) %7B%0A%09%09%09%09
-return fals
+player.sendMessage(ChatColor.RED + %22You must be in creative mode to execute this command.%22);%0A%09%09%09%09return tru
 e;%0A%09%09%09%7D%0A%09%09%09%0A
@@ -1088,56 +1088,115 @@
 %09%09%09%09
-return false;%0A%09%09%09%09// TODO: custom error messages
+player.sendMessage(ChatColor.RED + %22You do not have permission to perform this command.%22);%0A%09%09%09%09return true;
 %0A%09%09%09
@@ -1298,16 +1298,20 @@
 tion();%0A
+%09%09%09%0A
 %09%09%09try %7B
@@ -1423,57 +1423,214 @@
 %09%09%09%09
-return false;%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09return true;%0A%09%09%09%0A%09%09%7D%0A%09%09
+player.sendMessage(ChatColor.RED + %22Something went wrong whe we tried to write your structure to a file.%22);%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09return true;%0A%09%09%09%0A%09%09%7D%0A%09%09%0A%09%09System.out.println(%22Command must be executed by a Player.%22);
 %0A%09%09r

Add feature: custom error messages