package org.royaldev.royalcommands.rcommands;

import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.royaldev.royalcommands.Config;
import org.royaldev.royalcommands.MessageColor;
import org.royaldev.royalcommands.RUtils;
import org.royaldev.royalcommands.RoyalCommands;
import org.royaldev.royalcommands.WorldManager;

public class CmdClearInventory implements CommandExecutor {

    private final RoyalCommands plugin;

    public CmdClearInventory(RoyalCommands instance) {
        plugin = instance;
    }

    @Override
    public boolean onCommand(CommandSender cs, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("clearinventory")) {
            if (!plugin.ah.isAuthorized(cs, "rcmds.clearinventory")) {
                RUtils.dispNoPerms(cs);
                return true;
            }
            if (args.length < 1 && !(cs instanceof Player)) {
                cs.sendMessage(cmd.getDescription());
                cs.sendMessage(cmd.getUsage().replace("<command>", label));
                return true;
            }
            final OfflinePlayer t = (args.length > 0) ? RUtils.getOfflinePlayer(args[0]) : (OfflinePlayer) cs;
            if (!t.getName().equalsIgnoreCase(cs.getName()) && !plugin.ah.isAuthorized(cs, "rcmds.others.clearinventory")) {
                cs.sendMessage(MessageColor.NEGATIVE + "You don't have permission to clear other players' inventories.");
                return true;
            }
            if (plugin.ah.isAuthorized(t, "rcmds.exempt.clearinventory") && !t.getName().equalsIgnoreCase(cs.getName())) {
                cs.sendMessage(MessageColor.NEGATIVE + "You can't clear that player's inventory.");
                return true;
            }
            String world = null;
            if (t.isOnline()) world = ((Player) t).getWorld().getName();
            else if (args.length > 1) world = args[1];
            if (world == null) {
                cs.sendMessage(MessageColor.NEGATIVE + "You need to specify a world to clear the inventory on!");
                return true;
            }
            if (!t.isOnline() && !Config.separateInv) {
                cs.sendMessage(MessageColor.NEGATIVE + "Cannot modify offline inventories unless inventory separation is on!");
                return true;
            }
            final Inventory i = WorldManager.il.getOfflinePlayerInventory(t, world);
            if (i == null) {
                cs.sendMessage(MessageColor.NEGATIVE + "No inventory found.");
                return true;
            }
            i.clear();
            WorldManager.il.saveInventory(t, world, i);
            if (t.isOnline()) {
                final Player p = (Player) t;
                if (p.getWorld().getName().equalsIgnoreCase(world)) p.getInventory().setContents(i.getContents());
            }
            cs.sendMessage(MessageColor.POSITIVE + "You have cleared the inventory of " + MessageColor.NEUTRAL + t.getName() + MessageColor.POSITIVE + " in world " + MessageColor.NEUTRAL + world + MessageColor.POSITIVE + ".");
            if (t.isOnline() && !t.getName().equalsIgnoreCase(cs.getName()))
                ((Player) t).sendMessage(MessageColor.POSITIVE + "Your inventory for " + MessageColor.NEUTRAL + world + MessageColor.POSITIVE + " has been cleared by " + MessageColor.NEUTRAL + cs.getName() + MessageColor.POSITIVE + ".");
            return true;
        }
        return false;
    }

}

@@ -2466,32 +2466,737 @@
    return true;%0A
+            %7D else if (t.isOnline() && !Config.separateInv) %7B%0A                final Player p = (Player) t;%0A                p.getInventory().clear();%0A                cs.sendMessage(MessageColor.POSITIVE + %22You have cleared the inventory of %22 + MessageColor.NEUTRAL + t.getName() + MessageColor.POSITIVE + %22 in world %22 + MessageColor.NEUTRAL + world + MessageColor.POSITIVE + %22.%22);%0A                if (!p.getName().equalsIgnoreCase(cs.getName()))%0A                    p.sendMessage(MessageColor.POSITIVE + %22Your inventory for %22 + MessageColor.NEUTRAL + world + MessageColor.POSITIVE + %22 has been cleared by %22 + MessageColor.NEUTRAL + cs.getName() + MessageColor.POSITIVE + %22.%22);%0A                return true;%0A
             %7D%0A  

Allow inventory clearing if separate inventories are off