Adding players in an ArrayList to a config

Discussion in 'Spigot Plugin Development' started by DecisionsYT, May 16, 2016.

  1. Hey!

    Title explains it all

    If players are in an ArrayList, how do I add those players to a definable section in the config file?
     
  2. for(String text : ArrayName.toString()){
    getConfig().set("Part." + text);
    }

    somthing like that, correct me if im wrong.
     
  3. okay, will test it
     
  4. banned is my ArrayList name
     
  5. If you want to store players into a config, I don't think you can just do .toString(). You'll either have to come up with your own way to serialize the player object, or just store their UUID.
     
  6. show me how youve made ur array list.
     
  7. Oohoh, if your storing the actual player in the array list, dont do that. You can cause memory leaks. store their uuid or their player name then you can just use Bukkit.getPlayer("name") to get the player.
     
    • Agree Agree x 1
  8. If it's for a large server, you'll want to make a file for each player or use an sql database.
    If it's for a small server, use a for loop to iterate through the list and set each value using their UUID as the key.
     
  9. The reason I'm using ArrayLists, is because I'm getting an error when doing the following.

    If I ban myself, it works fine.
    Unbanning myself, (on an alt) It gives me an error
     
  10. Ban player class:

    Code (Text):
    package Punishments;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import me.DecisionsYT.Main;

    public class Ban implements CommandExecutor, Listener {

       
       
    Main plugin;
        public Ban(Main passedPlugin) {
            this.plugin = passedPlugin;
        }
       
       
       

        @SuppressWarnings("deprecation")
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String string, String[] args)  {
           
           
            if (sender.hasPermission("ChatX.Ban")) {
                if (args.length == 0) {
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Ban-Usage")));
                    return true;
                }
            }
           
            Player target = Bukkit.getServer().getPlayer(args[0]);
           
            if (target == null) {
                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Invalid-Player")));
                return true;      
            }
           
           
            String message = "";
           
            if (args.length > 1) {
                for (int i = 1; i < args.length; ++i) {
                    message += args[i] + " ";
                }
            }else{
                message = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Ban-Default-Reason").replace("{target}", target.getName()));
            }
            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Banned-Player").replace("{target}", target.getName())));
            target.kickPlayer(ChatColor.translateAlternateColorCodes('&', message));
           
            target.setBanned(true);
            target.kickPlayer(ChatColor.translateAlternateColorCodes('&', message));
           
       
            return true;
           
        }
       
       
       
    }
     
    Unban player class:

    Code (Text):
    package Punishments;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import me.DecisionsYT.Main;

    public class Unban implements CommandExecutor {

    Main plugin;
       
        public Unban(Main passedPlugin) {
            this.plugin = passedPlugin;
        }

        @SuppressWarnings("deprecation")
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String string, String[] args)  {
           
           
            if (sender.hasPermission("ChatX.Unban")) {
                if (args.length == 0) {
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Unban-Usage")));
                    return true;
                }
            }
           
            OfflinePlayer target = Bukkit.getOfflinePlayer(args[0]);
           
            if (sender.hasPermission("ChatX.Unban")) {
                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Unbanned-Player")));
                target.setBanned(false);
            }
           
           
            return true;
        }
       
       
       
    }
     
     
  11. An ArrayList is a list, so you can write the list to the config.
    https://hub.spigotmc.org/javadocs/spigot/org/bukkit/configuration/MemorySection.html
    To write to the config: config.set("path to it", arrayListVariable);

    To get from the config: arrayListVariable = config.getStringList("path to it");

    For example to remove a name from a list (Though UUID is better):
    Code (Text):
    List<String> bannedPlayers = config.getStringList("path to it");
    bannedPlayers.remove(variableOfThePlayersName);
    config.set("path to it", bannedPlayers);
     
    • Like Like x 1
  12. the OP needs to learn Java, end of story.
     
  13. Java? I thought this was html. :(
     
    • Agree Agree x 1
  14. You have Java pseudo code in your signature, you should stop trying to be funny before you take it out lol, just put your github in that xD
     
  15. I wouldn't say specifically "learning java" as in the syntax, because that's false--I think it's mostly about the conventions of bukkit. Someone who is unfamiliar with bukkit but knows java might try shit like saving Player objects to a file, but a "bukkit" developer would learn that the proper way to save player information is per UUID. Maybe it could be argued that "learning java" means learning all the conventions of OOP, being able to read other's code and understand frameworks, but I (and a lot of other people) would say it's all part of a bigger learning curve, and bukkit is without a doubt a very easy java api to use. It's not such an awful thing that he's making plugins and asking for help, and "learning java" is definitely not how you save a list of players to a config.
     
    • Agree Agree x 1
    • Optimistic Optimistic x 1
  16. That's very wrong, saving objects to arrays and iterating through it to do it is normal for a dev, they wouldn't save it to a file unless they needed it through all instances of the program. If the OP knew Java he wouldn't be having this issue, if you look at his other threads you'll learn he does not know Java. Sure learning java is not how you save a list of players to a config but maybe if he knew Java he could problem solve on his own looking at the config.set method and it's arguments. If the OP were to learn Java he would most likely not have this issue and many other issues he's had in his other threads.
     
  17. Oh I never even notice it so whoops
     
  18. My bad, that was an awful example and I wasn't thinking too clearly. But it makes sense in the idea of someone knowing java, but not knowing bukkit--its a plausible mistake to save to a file rather than bukkit's config system, and they'll iterate through the collection, but will the type be a Player object, or a UUID, or a username? What format will they choose to store? Will they make calls to streams or databases on the main server thread? A responsible dev might do their research, but everyone has to learn these things one way or another. It just seemed to me that you were discounting it for "not knowing java."
    I've replied to some of his threads before, he has come along pretty well with his learning.
    This is where we learn to disagree, I just don't think just "learning java" is an acceptable solution to get into the programming language, you make programs and write code. And logically, writing code in ways that interest you is more beneficial than not, I would say especially with such an easy and powerful api that bukkit/spigot provides.