package com.bitlimit.Tweaks;

import org.bukkit.ChatColor;
import org.bukkit.*;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.command.*;
import org.bukkit.configuration.file.FileConfiguration;

public class TweaksCommandExecutor implements CommandExecutor {
    private final Tweaks plugin;


    public TweaksCommandExecutor(Tweaks plugin) {
        this.plugin = plugin;
    }

    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        /*  /tweaks tnt enable
            /tweaks tnt disable
            /tweaks weather enable
            /tweaks weather disable
            /tweaks slimes enable
            /tweaks slimes disable
        */

        if (sender.hasPermission("tweaks.*")) {
            FileConfiguration config = this.plugin.getConfig();

            if (args.length > 1) {
                boolean validParameter = isValidBooleanInput(args[1]);
                if (!validParameter && !args[1].toLowerCase().equals("location")) {
                    sender.sendMessage(ChatColor.RED + "Invalid second parameter: expected *able and its past participle, or standard YES/NO (capitalization agnostic).");
                    return false;
                }
                boolean newValue = parsedBooleanInput(args[1]);
                ConfigurationSection configurationSection = config.getConfigurationSection("preferences");

                if (args[0].toLowerCase().equals("tnt")) {
                    configurationSection.set("tnt", newValue);
                } else if (args[0].toLowerCase().equals("weather")) {
                    configurationSection.set("weather", newValue);
                } else if (args[0].toLowerCase().equals("slimes")) {
                    configurationSection.set("slimes", newValue);
                } else if (args[0].toLowerCase().equals("spawnitems")) {

                    if (args[1].toLowerCase().equals("location")) {

                        try {
                            ConfigurationSection section = config.getConfigurationSection("meta").createSection("spawnItems").createSection("location");

                            if (sender instanceof Player) {
                                Player player = (Player)sender;
                                this.putLocationInSection(player.getLocation(), section);
                                sender.sendMessage(ChatColor.GREEN + "Location set successfully.");
                            } else {
                                sender.sendMessage(ChatColor.RED + "Sender must be player.");
                            }

                            return true;
                        } catch (Exception e) {

                        }

                    } else {
                        configurationSection.set("spawnItems", newValue);
                    }
                } else {
                    sender.sendMessage(ChatColor.RED + "Invalid parameter. Expected TNT, weather, or slimes.");
                    return false;
                }

                String argument = args[0];
                if (argument.equals("tnt")) {
                    argument = "TNT";
                } else {
                    argument = capitalizedString(argument);
                }
                String newValueString = newValue ? "enabled" : "disabled";
                sender.sendMessage(ChatColor.AQUA + argument + " tweaks are now " + ChatColor.GOLD + newValueString +ChatColor.AQUA + ".");
            } else if (args.length == 1) {
                String argument = args[0].toLowerCase();
                if (argument.equals("tnt") || argument.equals("weather") || argument.equals("slimes") || argument.equals("spawnItems")) {
                    boolean enabled = config.getBoolean("enabled-" + argument);
                    if (argument.equals("tnt")) {
                        argument = "TNT";
                    } else {
                        argument = capitalizedString(argument);
                    }
                    if (enabled) {
                        sender.sendMessage(ChatColor.AQUA + argument + ChatColor.GREEN + " tweaks are currently enabled.");
                    } else {
                        sender.sendMessage(ChatColor.AQUA + argument + ChatColor.RED + " tweaks are currently disabled.");
                    }
                } else {
                    sender.sendMessage(ChatColor.AQUA + "Valid parameters: TNT, weather, slimes, or spawnItems to query state, optionally, followed by \"enabled\" or \"disabled\" to set.");
                }
            } else {
                sender.sendMessage(ChatColor.AQUA + "Valid parameters: TNT, weather, slimes, or spawnItems to query state, optionally, followed by \"enabled\" or \"disabled\" to set.");
            }

            // Save
            this.plugin.saveConfig();
        } else {
            sender.sendMessage(ChatColor.RED + "You don't have permission to execute this command.");
        }
        return false;
    }

    private boolean isValidBooleanInput(String string) {
        return string.equals("enable") || string.equals("enabled") || string.equals("true") || string.equals("YES") || string.equals("yes") || string.equals("disable") || string.equals("disabled") || string.equals("false") || string.equals("NO") || string.equals("no");
    }

    private boolean parsedBooleanInput(String string) {
        if (string.equals("enable") || string.equals("enabled") || string.equals("true") || string.equals("YES") || string.equals("yes")) {
            return true;
        } else if (string.equals("disable") || string.equals("disabled") || string.equals("false") || string.equals("NO") || string.equals("no")) {
            return false;
        }
        return false;
    }

    private String capitalizedString(String string)
    {
        return Character.toUpperCase(string.charAt(0)) + string.substring(1);
    }

    public void putLocationInSection(Location location, ConfigurationSection configurationSection) {
        configurationSection.set("world", location.getWorld().getName());
        configurationSection.set("x", location.getX());
        configurationSection.set("y", location.getY());
        configurationSection.set("z", location.getZ());
        configurationSection.set("yaw", location.getYaw());
        configurationSection.set("pitch", location.getPitch());
    }
}

@@ -1328,36 +1328,33 @@
 Input(args%5B1%5D);%0A
-    
+%09
             Conf
@@ -1353,425 +1353,168 @@
     
-ConfigurationSection configurationSection = config.getConfigurationSection(%22preferences%22);%0A%0A                if (args%5B0%5D.toLowerCase().equals(%22tnt%22)) %7B%0A                    configurationSection.set(%22tnt%22, newValue);%0A                %7D else if (args%5B0%5D.toLowerCase().equals(%22weather%22)) %7B%0A                    configurationSection.set(%22weather%22, newValue);%0A                %7D else if (args%5B0%5D.toLowerCase().equals(%22slimes%22))
+String modified = args%5B0%5D.toLowerCase();%0A%0A                if (modified.equals(%22tnt%22) %7C%7C modified.equals(%22weather%22) %7C%7C modified.equals(%22slimes%22))%0A               
  %7B%0A 
@@ -1530,32 +1530,42 @@
           config
+.getConfig
 urationSection.s
@@ -1566,20 +1566,31 @@
 tion
-.set(%22slimes
+(modified).set(%22enabled
 %22, n

Correct which configuration section is set. Reduce code duplicity as well.