Config Help!

Discussion in 'Spigot Plugin Development' started by WildPlaysGames, May 14, 2017.

  1. So, I'm creating a Blacklist plugin.
    I looked over my code, and when the player is added to the arraylist, The player is added of course, but whenever I reload the server, it doesn't store its data. So I want to save their names who is added to the arraylist so then they can't get into the server again.
    Here is my code:
    Code (Text):
    public class Main extends JavaPlugin implements Listener {
       
        ArrayList<UUID> players = new ArrayList<>();
        String prefix = "§c[§eBlacklist§c] ";
        Player target;
       
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            Bukkit.getServer().getLogger().info("[Blacklist] has been enabled!");
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("blacklist")) {
                Player player = (Player) sender;
                if (!player.hasPermission("blacklist.players")) {
                    player.sendMessage(prefix + ChatColor.DARK_RED + "You are not able to use this command!");
                    return true;
                }
                if (args.length < 2) {
                    player.sendMessage(prefix + ChatColor.DARK_RED + "Incorrect usage! /blacklist <add/remove> <Player>");
                    return true;
                }
                target = Bukkit.getPlayer(args[1]);
                if (target == null) {
                    player.sendMessage(prefix + ChatColor.DARK_RED + "Could not find player: " + args[1]);
                    return true;
                }
                if (args[0].equalsIgnoreCase("add")) {
                    if (!players.contains(target)) {
                        players.add(player.getUniqueId());
                        player.sendMessage(prefix + ChatColor.RED + "Adding player to the list of players.... ");
                        player.sendMessage(prefix + ChatColor.RED + "One moment...");
                        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                            public void run() {
                                target.kickPlayer(prefix + ChatColor.RED + "You have been added to the blacklist by: " + player.getName() + "!");
                            }
                        }, 50L);
                    } else if (players.contains(target)) {
                        player.sendMessage(prefix + ChatColor.DARK_RED + "Blacklist already contains the player: " + args[0]);
                    }
                }
                if (args[0].equalsIgnoreCase("remove")) {
                    if (!players.contains(target.getUniqueId())) {
                        player.sendMessage(prefix + ChatColor.DARK_RED + "Player is not added to the list!");
                    } else if (players.contains(target.getUniqueId())) {
                        players.remove(target.getUniqueId());
                        player.sendMessage(prefix + ChatColor.DARK_RED + "Player removed!");
                    }
                }
            }
            return false;
        }
       
        @EventHandler
        public void on(AsyncPlayerPreLoginEvent e) {
            if (players.contains(e.getUniqueId())) {
                e.disallow(Result.KICK_WHITELIST, prefix + ChatColor.RED + "You have been added to the blacklist! You are not able to join!");
            }
        }
    }
     
    How would I do this?
     
  2. You aren't even storing the players in the config.yml...
    You need to use getConfig().set method and getString methods. lookup on Google how to work with bukkit config files
     
  3. You will need to save the list in a config (other methods are available such as MySQL and JSON).
    This is quite easy to setup. Inside your plugins resources folder (where your plugin.yml is) you will need to create a file called config.yml.

    In your onEnable() you need to tell your plugin to copy this file from inside the jar to a directory in the plugins folder. To do this put this in your onEnable()
    Code (Text):
    saveDefaultConfig()
    In the config.yml you made next to your plugin.yml in your IDE. You will need to add a section to store your blacklisted players. For example in your config.yml you could type:
    Code (Text):
    blacklist:
    To save the data in your onDisable() you can save your list of players by using spigots functions.
    Code (Text):
    getConfig().set("blacklist", your list of uuids);
    To load the players after the reload you can access the config and grab all the players like this:
    Code (Text):
    players = getConfig().getList("blacklist");
    Please note that getList may not be the correct one. You may need to cast what your retrieving to a list.

    There are other ways of storing the data so it looks more user friendly in the config file in your servers plugin directory.

    I hoped this helped and if you want more help just ask.!

    Please note that I wrote this on my phone so something's may be a bit off or incorrect!
     
  4. What about getStringList()? This will return an arraylist of strings which you wont need tp cast. But you will have to store uuids as strings in the list and use UUID.fromString() to compare them
     
  5. Well yes of course you can do that. It's up to the OP as he appears new to configs! I recommend they do some reading on them. Both Bukkit and Spigot pages have many tutorials about them!
     
  6. Don't forget to set this.saveConfig(); to save the config.
     
    • Friendly Friendly x 1
  7. The this won't be necessary I the OP's code as it is all in the main class so saveConfig(); will be fine on its own