Solved Save/Load ArrayList onDisable and onEnable?

Discussion in 'Plugin Development' started by Arrxzon, Jan 7, 2014.

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

    Arrxzon

  2. Offline

    adam753

    If you know it works for hashmaps, why wouldn't it work for arraylists?
     
  3. Offline

    leimekiller

    Store in YML?
     
  4. Offline

    Arrxzon

    Because i haven't got it to save or load
    Here is my onDisable and onEnable
    Code:java
    1.  
    2. private ArrayList<Player> cooldown = new ArrayList<Player>();
    3. public void onEnable() {
    4. if(!getDataFolder().exists()) {
    5. getDataFolder().mkdir();
    6. }
    7. try{
    8. cooldown = SaveLoad.load("plugins\\RankKits\\cooldowns.bin");
    9. }
    10. catch(Exception e){
    11. e.printStackTrace();
    12. }
    13. getLogger().info("RankKits Enabled!");
    14. if (!setupEconomy()) {
    15. getLogger().severe(ChatColor.RED + "Disabled due to no Vault dependency found!");
    16. getServer().getPluginManager().disablePlugin(this);
    17. return;
    18. }
    19. }
    20.  
    21. public void onDisable() {
    22. try{
    23. SaveLoad.save(cooldown, "plugins\\RankKits\\cooldowns.bin");
    24. }
    25. catch(Exception e){
    26. e.printStackTrace();
    27. }
    28.  
    29. }


    the SaveLoad class
    Code:java
    1. package me.Arrxzon.rankkits;
    2.  
    3. import java.io.FileInputStream;
    4. import java.io.FileOutputStream;
    5. import java.io.ObjectInputStream;
    6. import java.io.ObjectOutputStream;
    7.  
    8. public class SaveLoad {
    9. //@author Tomsik68<[email][email protected][/email]>
    10. public static <T extends Object> void save(T obj,String path) throws Exception
    11. {
    12. oos.writeObject(obj);
    13. oos.flush();
    14. oos.close();
    15. }
    16. public static <T extends Object> T load(String path) throws Exception
    17. {
    18. @SuppressWarnings("unchecked")
    19. T result = (T)ois.readObject();
    20. ois.close();
    21. return result;
    22. }
    23. }
     
  5. Offline

    mmonkey

    Arrxzon

    Players are not serializable, you cannot save players! You can however use player.getName(). And you will have to change the ArrayList to:

    ArrayList<String>
     
    Arrxzon likes this.
  6. Offline

    Arrxzon

    Ok arraylists are changed but were would i have to use player.getName()
    Here is the cooldown
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    2.  
    3. final Player p = (Player) sender;
    4. //command args
    5. if (cooldown.contains(p)) {
    6. p.sendMessage(ChatColor.RED + "This kit is on cooldown!");
    7. return false;
    8. }
    9. //do stuff
    10. cooldown.add(p);
    11. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    12. public void run() {
    13. cooldown.remove(p);
    14. p.sendMessage(ChatColor.AQUA + "you can now use kit" + ChatColor.GREEN + " Private" + ChatColor.AQUA + " again!");
    15. }
    16. }, 1200);
    17. return true;
    18. }
     
  7. Offline

    mmonkey

    Arrxzon
    On line 5:
    Code:java
    1. if (cooldown.contains(p.getName())) {

    On line 10:
    Code:java
    1. cooldown.add(p.getName());

    On line 13:
    Code:java
    1. cooldown.remove(p.getName());

    So anytime you access the ArrayLists, instead of using a Player (p), just change to a player's name: p.getName().
     
    Arrxzon likes this.
  8. Offline

    Arrxzon

    mmonkey
    thanks but it don't remember after a server reload/restart unfortunately any ideas ?
     
  9. Offline

    mmonkey

    Arrxzon
    Delete your plugin's folder (not to be confused with "plugins" folder) and restart server.
     
    Arrxzon likes this.
  10. Offline

    Arrxzon

    mmonkey
    no difference
    except a FileNotFoundException before it creates a new one
     
  11. Offline

    mmonkey

    Arrxzon

    There is nothing to save because you are you add the player to the cooldown list, then after the cooldown period you delete them from the cooldown list, so when you save with your onDisable(), the list is empty. It is saving and loading, but it is saving and loading an empty list.

    Why do you need to save your cooldown list?
     
  12. Offline

    Arrxzon

    mmonkey
    This is a kits plugin
    I don't believe the list should be empty as i join test server, execute the kit command, adding me to the cooldown then i reload the server within the cooldown period but i am then able to get a kit again after the reload/restart even do the cooldown should still be active

    I need to save the cooldown list so after server restarts any active cooldowns will remain active
     
  13. Offline

    mmonkey

    Arrxzon
    Can I see the full code of the class containing the onCommand method?
     
  14. Offline

    Arrxzon

    mmonkey
    There is not much more than above except help menu, permissions, vault economy integration and
    instead of //do stuff above there is the kits and it's repeated multiple times so theres about 10 kits each with it's own cooldown and it's own array list, does that work ?
     
  15. Offline

    mmonkey

    Arrxzon
    Can you post, or send me a pm of the code where you have the kits and cooldowns. I need to see how you implement the lists.
     
  16. Offline

    Arrxzon

    mmonkey
    damn i really hate how pastebin warps text but here you go
    and thanks
    http://pastebin.com/*****

    edit:just found out there is a button to disable text wrapping xD
     
  17. Offline

    mmonkey

    Arrxzon

    It's because every time you call SaveLoad.save() you are overriding the file. Instead of naming the file "hashmap.bin" you can call it "cooldown1.bin" , "cooldown2.bin", "cooldown2.bin" etc...
     
    Arrxzon likes this.
  18. Offline

    Arrxzon

    ah nice, another thing do, now after a reload the player will not get removed after the cooldown time
     
  19. Offline

    mmonkey

    Arrxzon

    Right, so after SaveLoad.load, check if players are in the cool down, if so, run the same method you call in your onCommand.
     
    Arrxzon likes this.
  20. Offline

    lucaspeedstack

    Arrxzon
    Sorry, I know this post is quite old now, but I was having the same problem, could you provide me the method you use to create the directory you use. I am really stuck on that!
     
Thread Status:
Not open for further replies.

Share This Page