Config.yml

Discussion in 'Plugin Development' started by AbductedSnake, Feb 8, 2018.

Thread Status:
Not open for further replies.
  1. Offline

    AbductedSnake

    Hello. I was trying to learn how to use configs by making a plugin that adds alts to a player's name and you can check/remove/add them, but theres an error and not knowing how to do alts, I don't know how to fix it.
    Main:
    Code:
    package me.AbductedSnake.Alts;
    
    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class Main extends JavaPlugin {
    static Plugin plugin;
       
       
       
       
        public void onEnable() {
            plugin = this;
            getConfig().options().copyDefaults(true);
            saveConfig();
            getCommand("alts").setExecutor(new Commands());
        }
       
        public void onDisable() {
            saveConfig();
            plugin = null;
           
        }
       
       
       
        public static void registerEvents(org.bukkit.plugin.Plugin plugin, Listener... listeners) {
            for(Listener listener : listeners) {
                Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
               
            }
        }
        public static Plugin getPlugin() {
            return plugin;
        }
    }
    
    The stuff that's editing the config:
    Code:
    package me.AbductedSnake.Alts;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class Commands implements CommandExecutor {
       
        Main plugin;
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(!(sender instanceof Player)) {
                sender.sendMessage("You must be a player to use this command!");
                return true;
            }
            Player p = (Player)sender;
            if(cmd.getName().equalsIgnoreCase("alts")) {
                if(!p.hasPermission("alts.admin")) {
                    p.sendMessage(ChatColor.RED + "You do not have permission to execute this command.");
                    return true;
                }
                if(args.length < 1) {
                    p.sendMessage(ChatColor.GOLD + "Please use one of the subtypes below:");
                    p.sendMessage(ChatColor.DARK_GREEN + "/alts check <player>" + ChatColor.YELLOW + "- Check the specified player's known alternate accounts.");
                    p.sendMessage(ChatColor.DARK_GREEN + "/alts add <player> <alt>" + ChatColor.YELLOW + "- Links a player's alt to their main account.");
                    p.sendMessage(ChatColor.DARK_GREEN + "/alts remove <player> <alt> " + ChatColor.YELLOW + "- Removes a player's alt from their main account.");
                    return true;
                }
                if(args[0].equalsIgnoreCase("add")) {
                    if(args.length != 3) {
                        p.sendMessage(ChatColor.GOLD + "Usage: /alts add <player> <alt>");
                        return true;
                    }
                   
                    String t = args[2];
                    String a = args[3];
                    if(plugin.getConfig().get(a, t) != null) {
                        p.sendMessage(ChatColor.GOLD + "The specified alt is already listed as an alias.");
                    }
                    plugin.getConfig().set(t + ".", a);
                    plugin.saveConfig();
                    p.sendMessage(ChatColor.DARK_GREEN + "You have added the alt " + ChatColor.GREEN + a + ChatColor.DARK_GREEN + " to the account " + ChatColor.GREEN + t);
                }
                if(args[0].equalsIgnoreCase("remove")) {
                    if(args.length != 3) {
                        p.sendMessage(ChatColor.GOLD + "Usage: /alts remove <player> <alt>");
                        return true;
                    }
                    String t = args[2];
                    String a = args[3];
                    if(plugin.getConfig().get(a, t) == null) {
                        p.sendMessage(ChatColor.GOLD + "The specified alt is not listed as a known alias.");
                    }
                    plugin.getConfig().set(t + "." + a, null);
                    plugin.saveConfig();
                    p.sendMessage(ChatColor.DARK_GREEN + "You have removed the alt " + ChatColor.GREEN + a + ChatColor.DARK_GREEN + " from the account " + ChatColor.GREEN + t);
                    return true;
                }
                if(args[0].equalsIgnoreCase("check")) {
                    if(args.length != 2) {
                        p.sendMessage(ChatColor.GOLD + "Usage: /alts check <player>");
                        return true;
                    }
                    String t = args[2];
                    if(plugin.getConfig().get(t) == null) {
                        p.sendMessage(ChatColor.GOLD + "There are currently no known alternate accounts for the account " + t);
                    }
                    p.sendMessage(ChatColor.DARK_GREEN + "The alts of " + ChatColor.GREEN + t + ChatColor.DARK_GREEN + " are " + ChatColor.GREEN + plugin.getConfig().getString(t));
                }
            }
            return true;
        }
    
    }
    
    Config.yml:
    Code:
    alts: 
    Also, when I first installed the plugin, the "alts" line wasn't there, so I added it in myself and reloaded, and then it disappeared. I made it so that it saves the config & loads the defaults I think, but it doesn't do that anyway. Thanks for reading.
     
  2. Offline

    Machine Maker

    Right so in your Commands class, you define the Main plugin variable but you don't actually set it to anything. You need to add a constructor to the Commands class that takes a Main plugin as an argument.
     
  3. Offline

    AbductedSnake

    Would this work?:
    Code:
    private Main plugin = Main.getPlugin(Main.class);
    Also, the config isn't copying the defaults. I have it set to "alts:" but in the config in the server files it shows up as empty.
     
  4. Offline

    RcExtract

    plz show the error code. Also, as what
    @X1machinemaker1X said, pass the plugin instance through constructor:
    Code:
    public class Commands implements CommandExecutor {
    private Main plugin;
    public Commands(/*argument for passing plugin instance on construction*/Main main) {
    this.plugin = main;
    }...
    When setting the executor, do
    Code:
    getCommand(/*cmd name"/).setExecutor(new Commands(/*refer to this class*/this));
    More information about constructors can be found here.
    Main.getPlugin(Main.class) works, but not recommended.
     
Thread Status:
Not open for further replies.

Share This Page