package com.rylinaux.hat;

/*
 * #%L
 * Hat
 * %%
 * Copyright (C) 2014 Hat
 * %%
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * #L%
 */


import org.bukkit.ChatColor;
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;

/**
 * Handle commands related to hats.
 *
 * @author rylinaux
 */
public class HatCommands implements CommandExecutor {

    /**
     * The plugin instance.
     */
    private final Hat plugin;

    /**
     * Construct the object.
     *
     * @param plugin the plugin instance.
     */
    public HatCommands(Hat plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

        if (!(sender instanceof Player)) {
            sender.sendMessage("You must be a player to use this command.");
            return true;
        }

        Player player = (Player) sender;

        if (!(player.hasPermission("hat.use"))) {
            player.sendMessage(ChatColor.RED + "You do not have permission to do that.");
            return true;
        }

        if (args.length == 0) {

            if (plugin.getHatManager().hasHat(player)) {
                plugin.getHatManager().removeHat(player);
                player.sendMessage(Hat.PREFIX + "Your hat has been removed and your previous item restored.");
            } else {
                player.sendMessage(Hat.PREFIX + "Use '/hat <id>' or '/hat <name>' to get a hat.");
            }

            return true;

        }

        String[] data = args[0].split(":");


        if (data.length == 0 || Material.matchMaterial(data[0]) == null) {
            player.sendMessage(Hat.PREFIX + "Not a valid material.");
            return true;
        }

        Material material = Material.matchMaterial(data[0]);

//        String[] data = args[0].split(":");
//
//        if (data.length == 0) {
//            player.sendMessage(Hat.PREFIX + "Not a valid material.");
//            return true;
//        }
//
//        Material material = Material.matchMaterial(data[0]);
//
//        if (material == null) {
//            player.sendMessage(Hat.PREFIX + "Not a valid material.");
//            return true;
//        }

        ItemStack item;

        if (data.length > 1) {
            item = new ItemStack(material, 1, Short.parseShort(data[1]));
        } else {
            item = new ItemStack(material, 1);
        }

        if (plugin.getHatManager().hasHat(player)) {
            plugin.getHatManager().updateHat(player, item);
        } else {
            plugin.getHatManager().addHat(player, item);
        }

        player.sendMessage(Hat.PREFIX + "Your hat has been set to " + material.name() + ".");

        return true;

    }

}

@@ -2988,417 +2988,8 @@
 );%0A%0A
-//        String%5B%5D data = args%5B0%5D.split(%22:%22);%0A//%0A//        if (data.length == 0) %7B%0A//            player.sendMessage(Hat.PREFIX + %22Not a valid material.%22);%0A//            return true;%0A//        %7D%0A//%0A//        Material material = Material.matchMaterial(data%5B0%5D);%0A//%0A//        if (material == null) %7B%0A//            player.sendMessage(Hat.PREFIX + %22Not a valid material.%22);%0A//            return true;%0A//        %7D%0A%0A
     

Remove commented code.