Solved Enabling a command and disabling a command!

Discussion in 'Spigot Plugin Development' started by MGPro, Jul 22, 2017.

  1. Hello Spigot,
    So i am currently working on my first plugin i have it all working and have tested it. But i don't know how i would add something in the config to say enable a command in the plugin!
    For example:
    I have a command that's /teamspeak
    in config there will be the line to edit the message when u do /teamspeak and another line that says enable_teamspeak: true/false how would i do this Thanks!
     
  2. If you have already registered the config, have a Playercommandpreprocessevent, just get the config get the section you want and then have an if statement to check if the command the player typed is true/false in the config
    .
     
  3. Sorry. I kind of understand that. But how would i add it? Thanks!
     
  4. If you mean how to add something in the config, you could just set it the normal spigot way:
    Code (Text):
    this.getConfig().set("bla bla bla", true/false);
    or if you are using a default plugin, you could just add it there!

    Mike
     
  5. Thanks.
    public class Main extends JavaPlugin implements Listener {

    public static FileConfiguration config;
    public static File conf;

    public void onEnable() {
    System.out.println("§7We successfully loaded the plugin!");

    this.registerCommands();

    config = getConfig();
    config.options().copyDefaults(true);
    conf = new File(getDataFolder(), "config.yml");
    saveConfig();
    saveDefaultConfig();
    }

    private void registerCommands() {
    this.getCommand("teamspeak").setExecutor(new TeamSpeakCommand());
    this.getCommand("website").setExecutor(new WebsiteCommand());
    this.getCommand("discord").setExecutor(new DiscordCommand());
    }

    public FileConfigurationOptions getFileConfigurationOptions()
    {
    return getConfig().options();
    }
    }

    That is my code so far where would i add it this is for my main class btw! Sorry its just i learning and i dont get all of this stuff yet!
     

  6. First of all some tips:
    • Use the "Code" tag to post code or use some kind of online service like hastebin!
    • Don't use Main as the name of your classes. Java does not like it and you may get compiling errors!

    Now in-topic again. As I see, in your on enable, you save the Default config. That means that you have it created in your file package. If you have it then, go in it and set the sections you want to set. Example:
    Code (Text):
    Section1:
      Messages:
        msg: This is an example
    To get that, you would do:
    Code (Text):
    this.getConfig().getString("Section1.Messages.msg");
    Remember, mostly everything is case sensitive in Java!
     
    • Agree Agree x 1
  7. Ok thanks for the tips. but im getting confused!

    This is the config!
    Code (Text):
    #This is what the message says when you type the command!

    Teamspeak: '&7Our &a&lTEAMSPEAK &7is &ats.sample.net'
    Website: '&7Our &a&lWEBSITE &7is &asample.net'
    Discord: '&7Our &a&lDISCORD &7is &adiscord.com'
    This is the plugin.yml
    Code (Text):
    name: ReviveTest
    main: net.revivehq.plugins.revive.Main
    version: 1
    commands:
      teamspeak:
        description: You can see the teamspeak ip!
        usage: /<command>
        aliases: [ts,ts3,ts2]
      website:
        description: You can see the website address!
        usage: /<command>
        aliases: [web,site]
      discord:
        description: You can see the discord invite code!
        usage: /<command>
    This is my main class :
    Code (Text):
    package net.revivehq.plugins.revive;

    import java.io.File;

    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.FileConfigurationOptions;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.revivehq.plugins.revive.commands.DiscordCommand;
    import net.revivehq.plugins.revive.commands.TeamSpeakCommand;
    import net.revivehq.plugins.revive.commands.WebsiteCommand;

    public class Main extends JavaPlugin implements Listener {
       
        public static FileConfiguration config;
        public static File conf;
       
        public void onEnable() {
        System.out.println("§7§lWe successfully Enabled rTeamspeak! §c§lENJOY THE PLUGIN!");
           
        this.registerCommands();
       
        config = getConfig();
        config.options().copyDefaults(true);
        conf = new File(getDataFolder(), "config.yml");
        saveConfig();
        saveDefaultConfig();
        }
       
        private void registerCommands() {
            this.getCommand("teamspeak").setExecutor(new TeamSpeakCommand());
            this.getCommand("website").setExecutor(new WebsiteCommand());
            this.getCommand("discord").setExecutor(new DiscordCommand());
    }
       
        public FileConfigurationOptions getFileConfigurationOptions()
        {
            return getConfig().options();
        }
        public void onDisable() {
            System.out.println("§7§lWe successfully Disabled rTeamspeak! §c§lSEE YOU SOON!!");
        }
    }

     
    and this is the command:
    Code (Text):
    package net.revivehq.plugins.revive.commands;

    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;

    import net.revivehq.plugins.revive.Main;


    public class WebsiteCommand implements CommandExecutor {

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] arg) {
            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7&m-|-----------------------------|-"));
            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.config.getString("Website")));
            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7&m-|-----------------------------|-"));
            return true;
        }

    }
     
    and finally this is the configmanager:
    Code (Text):
    package net.revivehq.plugins.revive.utils;

    import java.util.*;
    import org.bukkit.plugin.*;
    import java.io.*;
    import org.bukkit.configuration.file.*;

    public class ConfigManager
    {
        public static Map<String, FileConfiguration> configs;
       
        static {
            ConfigManager.configs = new HashMap<String, FileConfiguration>();
        }
       
        public static boolean isFileLoaded(final String fileName) {
            return ConfigManager.configs.containsKey(fileName);
        }
       
        public static void load(final Plugin plugin, final String fileName) {
            final File file = new File(plugin.getDataFolder(), fileName);
            if (!file.exists()) {
                try {
                    plugin.saveResource(fileName, false);
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (!isFileLoaded(fileName)) {
                ConfigManager.configs.put(fileName, (FileConfiguration)YamlConfiguration.loadConfiguration(file));
            }
        }
       
        public static FileConfiguration get(final String fileName) {
            if (isFileLoaded(fileName)) {
                return ConfigManager.configs.get(fileName);
            }
            return null;
        }
       
        public static boolean update(final String fileName, final String path, final Object value) {
            if (isFileLoaded(fileName) && !ConfigManager.configs.get(fileName).contains(path)) {
                ConfigManager.configs.get(fileName).set(path, value);
                return true;
            }
            return false;
        }
       
        public static void set(final String fileName, final String path, final Object value) {
            if (isFileLoaded(fileName)) {
                ConfigManager.configs.get(fileName).set(path, value);
            }
        }
       
        public void addComment(final String fileName, final String path, final String... comments) {
            if (isFileLoaded(fileName)) {
                for (final String comment : comments) {
                    if (!ConfigManager.configs.get(fileName).contains(path)) {
                        ConfigManager.configs.get(fileName).set("_COMMENT_" + comments.length, (Object)(" " + comment));
                    }
                }
            }
        }
       
        public static void remove(final String fileName, final String path) {
            if (isFileLoaded(fileName)) {
                ConfigManager.configs.get(fileName).set(path, (Object)null);
            }
        }
       
        public static boolean contains(final String fileName, final String path) {
            return isFileLoaded(fileName) && ConfigManager.configs.get(fileName).contains(path);
        }
       
        public static void reload(final Plugin plugin, final String fileName) {
            final File file = new File(plugin.getDataFolder(), fileName);
            if (isFileLoaded(fileName)) {
                try {
                    ConfigManager.configs.get(fileName).load(file);
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
       
        public static void save(final Plugin plugin, final String fileName) {
            final File file = new File(plugin.getDataFolder(), fileName);
            if (isFileLoaded(fileName)) {
                try {
                    ConfigManager.configs.get(fileName).save(file);
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
     
    This is all of it. What do i have to change to be able to disable the commands!
     
  8. Okey, first of all i'd advise you to look more into Java and the Bukkit/Spigot API.

    In your IDE, have you made a file called "config.yml"?

    I guess you have. go in there and make a new section named whatever you want. in that section have a boollean for each command you want to block, and by default set it to true or false.

    now on your code, go to your main class and make an event. A PlayerCommandPreProcessEvent. check if the command the player typed exists in the config, if it does get if its true or false, and then if its true, just cancel the event.

    Hope that's enough!

    Mike
     
  9. Ok i got it now i understand thanks mike!
     
    • Like Like x 1