File saving issue

Discussion in 'Plugin Development' started by Praxounet, Jan 8, 2019.

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

    Praxounet

    Hi guys, I'm currently making a party plugin, and I'm trying to make a new file to keep player's nicknames in it

    Code:
    Main.newConfig(PartyFile, PartyConfig, Main.getInstance().getDataFolder() + File.separator + "Party" + File.separator + player.getName() + "Party.yml");
    
    and basically what is happening it that "PartyFile" and "PartyConfig" are not recognised, while I'm clearly telling him what they are in my main class

    (my main class: )

    Code:
    public static void newConfig(File file, FileConfiguration config, String place) {
            file = new File(place);
            if(!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            config = (YamlConfiguration.loadConfiguration(file));
            try {
                config.save(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    If you have any ideas/questions please go ahead I'm sick of it (I'm not sure if I'm telling enough though)
     
  2. Offline

    KarimAKL

    @Praxounet
    1. I would recommend the player's uuid and not name. (they could change name)
    2. Not sure if this has anything to do with it but maybe try this instead:
    Code:Java
    1. public static void newConfig(String path) {
    2. File file = new File(path);
    3. if (!file.exists()) {
    4. try {
    5. file.createNewFile();
    6. } catch (IOException e) {
    7. e.printStackTrace();
    8. }
    9. }
    10. //The following part isn't really needed if you don't write something to the file.
    11. FileConfiguration config = YamlConfiguration.loadConfiguration(file);
    12. try {
    13. config.save(file);
    14. } catch (IOException e) {
    15. e.printStackTrace();
    16. }
    17. }

    Other than that you could print some messages to the console in between the code.
     
  3. @Praxounet

    It might be a bad idea to use names as file names because it would contain characters that are not allowed as file names. (I don't know the minecraft naming rules, so I am not sure.)
    And like Karim said, you should not store the names of players, but their UUID.

    Could you post your entire main class so we can check what goes wrong exactly?
     
  4. Offline

    Praxounet

    sure ! This is my main class :

    Code:
    package fr.praxounet.party;
    
    import java.io.File;
    import java.io.IOException;
    
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import fr.praxounet.party.commands.party;
    import fr.praxounet.party.events.EventsClass;
    import net.md_5.bungee.api.ChatColor;
    
    
    
    public class Main extends JavaPlugin {
       
         private static Main instance;
       
    
            public static Main getInstance() {
                return instance;
            }
            fr.zeprof2code.core.core core = (fr.zeprof2code.core.core) Bukkit.getPluginManager().getPlugin("schaferrcore");
        public void onEnable() {
            instance = this;
            getServer().getConsoleSender().sendMessage(ChatColor.DARK_GREEN + "\n\n[PARTY] plugin by Praxounet has been activated.\n");
            getCommand("party").setExecutor(new party());
            getServer().getPluginManager().registerEvents(new EventsClass(), this);
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
    
        public void onDisable() {
            getServer().getConsoleSender().sendMessage(ChatColor.RED + "\n\n[Hub] plugin by Praxounet has been desactived.\n");
    
        }
    
        public static void newConfig(File file, FileConfiguration config, String place) {
            file = new File(place);
            if(!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            config = (YamlConfiguration.loadConfiguration(file));
            try {
                config.save(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    }
    (I will apply the necessary adjustments later ! )
     
  5. Offline

    Praxounet

  6. Offline

    KarimAKL

  7. Offline

    Praxounet

    which one ?
     
  8. Offline

    timtower Administrator Administrator Moderator

    The newConfig method
     
Thread Status:
Not open for further replies.

Share This Page