Wip

CodingEyes:  UPDATED CODE - Working with 1.6.4! (Soon to work with 1.7.2 when new library comes out)

HatMe.java

package net.serubin.hatme;

import java.util.logging.Logger;

import main.java.net.serubin.hatme.HatExecutor;
import main.java.net.serubin.hatme.HatPermsHandler;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

public class HatMe<notAllowedMsg, notAllowedMsg1, notAllowedMsg11> extends JavaPlugin {

    static Logger log = Logger.getLogger("Minecraft");
    private String name;
    private String version;

    /*
     * Chat Messages
     */
    private final String hatOn = "You now have a hat! Use /unhat to remove it.";
    private final String hatOff = "You have taken off your hat!";
    private final String noSpace = "You have no space to take of your hat!";
    private final String hatAlreadyOn = "You already have a hat on! Take it off with /unhat.";
    private final String airHead = "You have just tried to put air on your head. Good job.";

    private String notAllowedMsg;
    private HatPermsHandler permsHandler;
    private HatExecutor executor;
    private boolean debug = true;

    @Override
    public void onEnable() {
        version = this.getDescription().getVersion();
        name = this.getDescription().getName();
        info("Start Plugin... (Version: " + version + ")");
        getServer().getPluginManager();
        getConfig().options().copyDefaults(true);
        saveConfig();

        this.notAllowedMsg = getConfig()
                .getString("plugin.hatme.notAllowedMsg");
        this.permsHandler = new HatPermsHandler(this, getConfig());
        this.executor = new HatExecutor(this);
        getCommand("hat").setExecutor(this);
        getCommand("unhat").setExecutor(this);
        info("Plugin Loaded!");
    }

    @Override
    public void onDisable() {
        info("Plugin Stopping...");
        reloadConfig();
        saveConfig();
        info("Plugin Stopped.");
    }

    /*
     * Hat Messages
     */
    public String hatOnMessage() {
        return hatOn;
    }

    public String hatOffMessage() {
        return hatOff;
    }

    public String noSpaceMessage() {
        return noSpace;
    }

    public String hatAlreadyOnMessage() {
        return hatAlreadyOn;
    }
    public String airHeadMessage(){
        return airHead;
    }

    public void debug(String text) {
        if (debug)
            log.info("[" + name + "] Debug - " + text);
    }

    public void info(String text) {
        log.info("[" + name + "] " + text);
    }

    public void sendMessage(Player player, ChatColor color, String text) {
        player.sendMessage(color + "[" + name + "] " + text);
    }

    public boolean onCommand(CommandSender sender, Command cmd,
            String commandLabel, String[] args) {
        if (commandLabel.equalsIgnoreCase("hat")) {
            // No arguments? Standard operation.
            if (args.length == 0 && (sender instanceof Player)) {
                Player player = (Player) sender;
                if (!permsHandler.checkHatPerms(player)) {
                    sender.sendMessage(ChatColor.RED
                            + "[HatMe] You do not have permission to use this command.");
                    return true;
                }

                if (!permsHandler.checkRestrict(player)) {
                    sender.sendMessage(ChatColor.RED + "[HatMe] "
                            + notAllowedMsg);
                    return true;
                }

                // if item is allowed, do hat
                if (!executor.hatOn(player))
                    return false;
                return true;

            } else if (args.length == 1 && (sender instanceof Player)) {
                Player player = (Player) sender;
                if (args[0].equalsIgnoreCase("-a")) {

                    if (!permsHandler.checkHatAllPerms(player)) {
                        sender.sendMessage(ChatColor.RED
                                + "[HatMe] You do not have permission to use this command.");
                        return true;
                    }

                    if (player.getItemInHand().getTypeId() == 0) {
                        player.sendMessage(ChatColor.YELLOW
                                + "[HatMe] You just tried to put air on your head. Good job.");
                        return true;
                    }

                    if (!permsHandler.checkRestrict(player)) {
                        sender.sendMessage(ChatColor.RED + "[HatMe] "
                                + notAllowedMsg);
                        return true;
                    }

                    if (!executor.hatOnAll(player))
                        return false;
                    return true;

                } else if (checkArgInt(args[0])) {
                    if (!permsHandler.checkGivePerms(player, args[0])) {
                        sender.sendMessage(ChatColor.RED
                                + "[HatMe] You do not have permission to use this command.");
                        return true;
                    }

                    if (!permsHandler.checkItemRestrict(args[0], player)) {
                        sender.sendMessage(ChatColor.RED + "[HatMe] "
                                + notAllowedMsg);
                        return true;
                    }

                    if (!executor.giveHat(player, Integer.parseInt(args[0])))
                        return false;
                    return true;
                }
            } else {
                sender.sendMessage(ChatColor.RED
                        + "[HatMe] You have used this command incorrectly, or you are the console.");
                return true;
            }
        }

        if (commandLabel.equalsIgnoreCase("unhat")) {
            // if command is unhat do unhat
            if (args.length >= 1 || !(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED
                        + "[HatMe] This command does not take any arguments. If you are the console, you can't use this.");
                return true;
            } else {
                Player player = (Player) sender;
                // check is hat is 0
                if (player.getInventory().getHelmet() == null) {
                    player.sendMessage(ChatColor.YELLOW
                            + "[HatMe] You have taken the air from your head.");
                    return true;
                } else {
                    executor.hatOff(player);
                    return true;
                }
            }
        }
        return false;
    }

    public boolean checkArgInt(String arg) {
        try {
            Integer.parseInt(arg);
        } catch (NumberFormatException ex) {
            // Item was not an int, do nothing
            return false;
        }
        return true;
    }
}




HatPermsHandler.java

package main.java.net.serubin.hatme;

import java.util.List;

import net.serubin.hatme.HatMe;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;

public class HatPermsHandler {
    private boolean restrictionEnabled;
    private List<Integer> unrestrictBlocks;
    public HatPermsHandler(HatMe<?, ?, ?> plugin, FileConfiguration config) {
        this.unrestrictBlocks = config.getIntegerList("plugin.hatme.allowed");
        this.restrictionEnabled = config.getBoolean("plugin.hatme.restrict");
    }
    
    public boolean checkHatPerms(Player player) {
        if (player.hasPermission("hatme.hat") || 
                player.hasPermission("hatme.hat." + player.getItemInHand().getTypeId()) || player.getItemInHand().getTypeId() == 0)
            return true;
        
        return false;
    }
    
    public boolean checkHatAllPerms(Player player) {
        if (player.hasPermission("hatme.hat.all") || 
                player.hasPermission("hatme.hat.all." + player.getItemInHand().getTypeId()))
            return true;
        
        return false;
    }
    
    public boolean checkGivePerms(Player player, String arg) {
        if (player.hasPermission("hatme.hat.give") || 
                player.hasPermission("hatme.hat.give." + arg))
            return true;
        
        return false;
    }

    public boolean checkRestrict(Player player) {
        int heldItem = player.getItemInHand().getTypeId();
        if(!restrictionEnabled) return true;
        if (player.hasPermission("hatme.norestrict"))
            return true;
        if (unrestrictBlocks.contains(heldItem))
            return true;
        return false;
    }

    public boolean checkItemRestrict(String arg, Player player) {
        if(!restrictionEnabled) return true;
        if (player.hasPermission("hatme.norestrict"))
            return true;
        if (unrestrictBlocks.contains(Integer.parseInt(arg)))
            return true;
        return false;
    }
}


HatExecutor.java


package main.java.net.serubin.hatme;

import net.serubin.hatme.HatMe;

import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;

public class HatExecutor {

    private HatMe<?, ?, ?> plugin;

    public HatExecutor(HatMe<?, ?, ?> plugin) {
        this.plugin = plugin;
    }

    public boolean hatOn(Player player) {
        ItemStack itemHand = player.getItemInHand();

        if (itemHand == null || itemHand.getTypeId() == 0) {
            if (player.getInventory().getHelmet() == null) {
                plugin.sendMessage(player, ChatColor.RED,
                        plugin.airHeadMessage());
                return true;
            } else {
                if (hatOff(player)) {
                    return true;
                } else {
                    return false;
                }
            }
        } else {

            if (!headEmpty(player)) {
                plugin.sendMessage(player, ChatColor.RED,
                        plugin.hatAlreadyOnMessage());
                // TODO swap helmet with hand
                return true;
            }

            if (moveOneToHead(itemHand, player)) {
                plugin.sendMessage(player, ChatColor.YELLOW,
                        plugin.hatOnMessage());
                return true;
            } else {
                return false;
            }
        }
    }

    public boolean hatOnAll(Player player) {
        ItemStack itemHand = player.getItemInHand();

        if (itemHand == null) {
            plugin.sendMessage(player, ChatColor.RED, plugin.airHeadMessage());
            // TODO take off hat
            return true;
        } else {

            if (!headEmpty(player)) {
                plugin.sendMessage(player, ChatColor.RED,
                        plugin.hatAlreadyOnMessage());
                return true;
            }

            if (moveAllToHead(itemHand, player)) {
                plugin.sendMessage(player, ChatColor.YELLOW,
                        plugin.hatOnMessage());
                return true;
            } else {
                return false;
            }
        }
    }

    public boolean giveHat(Player player, int itemID) {
        if (itemID == 0) {
            plugin.sendMessage(player, ChatColor.RED, plugin.airHeadMessage());
            return true;
        } else {

            if (!headEmpty(player)) {
                plugin.sendMessage(player, ChatColor.RED,
                        plugin.hatAlreadyOnMessage());
                return true;
            }

            if (setHead(itemID, player)) {
                plugin.sendMessage(player, ChatColor.YELLOW,
                        plugin.hatOnMessage());
                return true;
            } else {
                return false;
            }
        }
    }

    public boolean hatOff(Player player) {
        PlayerInventory inventory = player.getInventory();
        int empty = inventory.firstEmpty();
        ItemStack itemHead = inventory.getHelmet();
        if (empty == -1) {
            plugin.sendMessage(player, ChatColor.RED, plugin.noSpaceMessage());
            return true;
        } else {
            inventory.setHelmet(null);
            inventory.setItem(empty, itemHead);
            plugin.sendMessage(player, ChatColor.YELLOW, plugin.hatOffMessage());
            return true;
        }
    }

    private boolean moveOneToHead(ItemStack itemHand, Player player) {
        PlayerInventory playerInv = player.getInventory();

        // if hand has only 1 item
        if (itemHand.getAmount() == 1) {
            playerInv.setHelmet(itemHand);
            playerInv.setItemInHand(null);
            return true;
        } else {
            ItemStack newHelmet = new ItemStack(itemHand.getType(), 1,
                    itemHand.getDurability());
            plugin.debug("itemHand Original: " + itemHand.getAmount());
            newHelmet.setAmount(1);
            playerInv.setHelmet(newHelmet);
            itemHand.setAmount((itemHand.getAmount() - 1));
            plugin.debug("itemHand New: " + itemHand.getAmount());
            return true;
        }
    }

    private boolean moveAllToHead(ItemStack itemHand, Player player) {
        PlayerInventory playerInv = player.getInventory();
        playerInv.setHelmet(itemHand);
        playerInv.setItemInHand(null);
        return true;
    }

    private boolean setHead(int itemID, Player player) {
        PlayerInventory playerInv = player.getInventory();
        playerInv.setHelmet(new ItemStack(itemID));
        return true;
    }

    private boolean headEmpty(Player player) {
        return (player.getInventory().getHelmet() == null) ? true : false;
    }
}




config.yml  - NO CHANGE
plugin.yml - NO CHANGE


(I still don't fully know how to use GitHub yet so sorry for the poor markup! 

-CodingEyes