Arraylists being deleted after server reload!

Discussion in 'Plugin Development' started by aliano99, Jun 9, 2014.

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

    aliano99

    Hey guys
    I have this major problem, I'm coding this plugin where when you use a command you get added to an arraylist, the plugin then recognizes that their name is in the arraylists and so they won't be able to successfully use the command again. Here's some of the code
    Code:
    public class Main extends JavaPlugin {
        ArrayList<String> PremiumReclaim = new ArrayList<String>();
        ArrayList<String> kitused = new ArrayList<String>();
        ArrayList<String> SupporterReclaim = new ArrayList<String>();
        ArrayList<String> VIPReclaim = new ArrayList<String>();
    
    Code:
    public class PremiumReclaim implements CommandExecutor {
        public Main plugin;
        public PremiumReclaim(Main instance){
            plugin = instance;
        }
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String [] args) {
            Player p = (Player) sender;
            if (commandLabel.equalsIgnoreCase("PremiumReclaim")){
                if(p.hasPermission("premiumreclaim.use")){
                if(!plugin.PremiumReclaim.contains(p.getName())){
                    if(!plugin.kitused.contains(p.getName())){ 
                        plugin.PremiumReclaim.add(p.getName());
                        plugin.kitused.add(p.getName());


    The problem is when the server has a reload, all the arrays are wiped from the arraylist. This means when they perform the command again they will be able to due to their names not being on the list. Is there a way to permanently keep their names on an arraylist, or some way to constantly add them to the list somehow after a reload? Thanks!
     
  2. Offline

    Azubuso

    aliano99 Arrays wiped from arraylist? wat

    Anyway, you can simply store the lists in (example) YAML files, which is also the easiest way, by doing:
    PHP:
    getConfig().set("List1"YourArrayList);
    getConfig().save();
     
    // Loading it
    List<String> list = getConfig().getStringList("List1");
     
  3. Offline

    aliano99

    Oh that's not a bad solution. I can use that for now, but is there any other way to do this except by it generating files?
     
  4. Offline

    DoctorDark

    aliano99

    If you want the ArrayList to persist between reloads you'll have to serialise it to a file onDisable and serialise it onEnable,

    Also I'd suggest you to use a HashMap for kits, you do not need more than one and you can even check what kit a player has.
     
  5. Offline

    spy_1134

    When a plugin or the server is reloaded you lose all of your saved variables from before.
    Inside of your onDisable method you need to devise a method of saving all of your information to disk and then check to see if there is any information in your onEnable and load it back up.

    To expand on the above, you'd do something like this:
    Code:java
    1.  
    2. @Override
    3. public void onEnable() {
    4. getConfig().load("list.yaml");
    5. list = getConfig().getList("YourListName");
    6. }
    7.  
    8. @Override
    9. public void onDisable() {
    10. getConfig().set(<Your list name here!>, <Your array list variable>);
    11. getConfig().save(list.yaml);
    12. }
    13.  
     
  6. Offline

    aliano99

    Hey
    Sorry I've just started coding Java, and this is the first file I've ever generated using Java and I'm already struggling xD
    Where in my code should I place this
    Code:
    getConfig().set("List1", YourArrayList);
    getConfig().save();
    Do I place it in my main class? If so, here's my full main class. Can you tell me how I incorporate this into here?
    Code:
    package com.aliano99.Tut;
     
     
    import java.util.ArrayList;
     
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin {
        ArrayList<String> PremiumReclaim = new ArrayList<String>();
        ArrayList<String> kitused = new ArrayList<String>();
        ArrayList<String> SupporterReclaim = new ArrayList<String>();
        ArrayList<String> VIPReclaim = new ArrayList<String>();
     
        public void onEnable() {
            commands();
        }
        public void commands(){
        getCommand ("PremiumReclaim").setExecutor(new PremiumReclaim(this));   
        getCommand ("SupporterReclaim").setExecutor(new SupporterReclaim(this));
        getCommand ("VIPReclaim").setExecutor(new VIPReclaim(this));
        }
       
     
    }
       
     
    
    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  7. Offline

    fireblast709

    aliano99 If this is your first experience with Java, then you are doing it wrong. It is often suggested to get some experience with Java first before you start with Bukkit. That aside, the Bukkit API has specific methods in JavaPlugin that you should override and use for this case, which can be found in the documentation
     
    Garris0n likes this.
Thread Status:
Not open for further replies.

Share This Page