getting saved array list

Discussion in 'Plugin Development' started by envic, Jul 30, 2016.

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

    envic

    Hi guys!

    I'd like to make a booster plugin...!
    so i thought for a cool down, and i said maybe it could be work by saving player in an array list!
    but still i had a problem! after reloading the server, array will lost!
    so i used to save array list and get it, by this:

    Code:
    if (!(data.getplayerData().getStringList("Envica.Boosters.BoostedPlayerInRay").contains(player))) {
                    // Adding potion effect
                    player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 18000, 1));
                    // Adding to Array list
                    PotionedPlayer.add(player);
                    // Saving updated Array list
                    data.getplayerData().set("Envica.Boosters.BoostedPlayerInRay", Arrays.asList(PotionedPlayer));
                    data.saveplayerData();
                    // Sending message
                    player.sendMessage(Pluginname + MSGAble);
                    // Running Scheduler
                    Bukkit.getScheduler().scheduleSyncDelayedTask(JavaPlugin.getProvidingPlugin(main.class),
                            new Runnable() {
                                public void run() {
                                    PotionedPlayer.remove(player);
                                    data.getplayerData().set("Envica.Boosters.BoostedPlayerInRay",
                                            Arrays.asList(PotionedPlayer));
                                    data.saveplayerData();
                                }
                            }, 1728000);
    
    But i guess i am wrong here:
    and
    In playerData.yml (Structure):

    Code:
    Envica:
    
      Boosters:
        BoostedPlayerInRay:
        - - ==: Player
            name: Cintux
          - ==: Player
            name: Vertigo
    I need it to be some thing like this:

    Code:
    Envica:
    
      Boosters:
        BoostedPlayerInRay:
        -  Cintux
        -  Vertigo
    What do i hava to do?
    saving player display name in YML file? some thing else?
    any better way to make cool down?


    Thank you :cool:
     
    Last edited: Jul 30, 2016
  2. Offline

    DarkenCake

    try player.getName().toString()
     
  3. Offline

    ArsenArsen

    Why .toString a string?
    Also save UUIDs, not names. Thats where you need toString
     
  4. @envic
    Also, if you want it to be a string list you actually have to create a string list kn your class, then save it, because right now you're only storing one Player object.. Which as @ArsenArsen pointed out should be replaced with UUID's.
     
  5. Offline

    envic

    @ArsenArsen
    @AlvinB

    Thanks,
    That problem solved but still i have a problem!

    when i save the array list into player Data file! and reload the server!
    i'll lost array list! so when code save a new array list into player data,
    new data are replacing with previous data!
    it's common but i need something like adding new data to previous data!
    How do i suppose to do it?

    Is it possible to force a String array list to load (Strings) from path data's in OnEnable?
     
  6. Offline

    ArsenArsen

    @envic saveConfig(), and also if you have a saveResource for config.yml set its replace to false.
     
  7. @envic
    First, get the data from the file, make your modifications and set the data, this way we get what data was previously stored, instead of creating new data each time.
     
  8. Offline

    envic

    @ArsenArsen

    save player Data:
    Code:
    public void saveplayerData() {
            if (playerData == null || playerDataFile == null) {
                return;
            }
            try {
                getplayerData().save(playerDataFile);
            } catch (IOException ex) {
                main.getinstance().getLogger().log(Level.SEVERE, "Could not save config to " + playerDataFile, ex);
            }
        }
    as i know for saving resource:
    but i's using for saveing defaults!

    How can i to Disable replacing in saveplayerData void!?
     
  9. Offline

    ArsenArsen

    Its not that method, its something else. I dont see what
     
  10. Offline

    envic

    Soooo! how do i stop replacing!?

    @ArsenArsen

    Code:
    @Override
        public void onEnable() {
            // install Events
            Bukkit.getServer().getPluginManager().registerEvents(new RullBook(), this);
            Bukkit.getServer().getPluginManager().registerEvents(new MiningPowerBooster(), this);
            Bukkit.getServer().getPluginManager().registerEvents(new Signs(), this);
            EnableHelp();
            EnableActive();
            EnableBoost();
            // install file config.yml
            this.getConfig().options().copyDefaults(true);
            Configsetup();
            playerData.getinstance().saveDefaultplayerData();
        }
    
        @Override
        public void onDisable() {
            // Save playerData.yml
            playerData.getinstance().getplayerData().set("Envica.Boosters.BoostedPlayerInRay",
                    Arrays.asList(MiningPowerBooster.getInstance().PotionedPlayer));
            getLogger().info("Gheyre fa'al shod");
        }
    Code:
    package ir.envica.ee.main;
    
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.Reader;
    import java.io.UnsupportedEncodingException;
    import java.util.logging.Level;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class playerData implements Listener {
    
        private static playerData instance;
        private FileConfiguration playerData = null;
        private File playerDataFile = null;
    
        public playerData() {
            instance = this;
    
        }
        public static playerData getinstance() {
            return instance == null ? instance = new playerData() : instance;
        }
    
        // Check if playerdata.yml is null or not!
        public void installplayerDataFolder() {
            if (playerDataFile == null) {
                playerDataFile = new File(JavaPlugin.getProvidingPlugin(main.class).getDataFolder(), "playerData.yml");
            }
            playerData = YamlConfiguration.loadConfiguration(playerDataFile);
    
            // Look for defaults in the jar
            Reader defConfigStream = null;
            try {
                defConfigStream = new InputStreamReader(main.getinstance().getResource("playerData.yml"), "UTF8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            if (defConfigStream != null) {
                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                playerData.setDefaults(defConfig);
            }
        }
    
        // To get playerData.yml
        public FileConfiguration getplayerData() {
            if (playerData == null) {
                installplayerDataFolder();
            }
            return playerData;
        }
    
        public void saveplayerData() {
            if (playerData == null || playerDataFile == null) {
                return;
            }
            try {
                getplayerData().save(playerDataFile);
            } catch (IOException ex) {
                main.getinstance().getLogger().log(Level.SEVERE, "Could not save config to " + playerDataFile, ex);
            }
        }
    
        public void saveDefaultplayerData() {
            if (playerDataFile == null) {
                playerDataFile = new File(JavaPlugin.getProvidingPlugin(main.class).getDataFolder(), "playerData.yml");
            }
            if (!playerDataFile.exists()) {
                JavaPlugin.getProvidingPlugin(main.class).saveResource("playerData.yml", false);
            }
        }
    
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 25, 2017
  11. Offline

    ArsenArsen

  12. Offline

    Oxyorum

    @envic Okay. It seems that you are trying to save player data into one configuration file, where you can store the cooldown time for each player. I am not sure why you are using the saveDefault method and the like. It isn't really necessary here for what you are doing. Refactor your code and remove use of any of the "default" methods.

    Just use:
    Code:
    config.saveConfig()
    for saving the configuration after you make changes.

    And use:
    Code:
    config = YamlConfiguration.loadConfiguration(file)
    to load the configuration file.

    If you are concerned about whether the file you are writing to exists or not, do this before loading the config:
    Code:
    File f = new File(plugin.getDataFolder(), "player.yml");
    file.createNewFile(); //this method creates the file only if it doesn't already exist, so you don't have to check to see if the file exists or not
    
    Also, have a look at the configuration reference, so you can see how to set up the paths the way you want: Configuration API Reference
     
Thread Status:
Not open for further replies.

Share This Page