1.16.5 File doesn't get created

Discussion in 'Spigot Plugin Development' started by DracoCookie23, Jun 17, 2021.

  1. So, I made a plugin where you can buy Ranks from **** (I don't want to advertise it) with in-game money. But when I run it the file ranks.yml doesn't get created. So I decided to ask for help here. Here is my FileBuilder class:

    Code (Java):
    package dutil;

    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.configuration.file.YamlConfiguration;

    import java.io.File;
    import java.io.IOException;
    import java.util.List;
    import java.util.Set;

    public class FileBuilder {
        private final File f;
        private final YamlConfiguration c;

        public FileBuilder(String FilePath, String FileName){
            this.f = new File(FilePath, FileName);
            this.c = YamlConfiguration.loadConfiguration(this.f);

        }

        public FileBuilder setValue(String ValuePath, Object Value){
            c.set(ValuePath, Value);

            return this;
        }

        public int getInt(String ValuePath){
            return c.getInt(ValuePath);
        }

        public String getString(String ValuePath){
            return c.getString(ValuePath);
        }

        public Boolean getBoolean(String ValuePath){
            return c.getBoolean(ValuePath);
        }

        public List<String> getStringList(String ValuePath){
            return c.getStringList(ValuePath);
        }

        public Set<String> getKeys(boolean deep){
            return c.getKeys(deep);
        }

        public Boolean exist(){
            return f.exists();
        }

        public Double getDouble(String ValuePath){
            return c.getDouble(ValuePath);
        }

        public ConfigurationSection getConfiguration(String Section){
            return c.getConfigurationSection(Section);
        }

        public Long getLong(String ValuePath){
            return c.getLong(ValuePath);
        }

        public FileBuilder save(){
            try {
                this.c.save(this.f);
            } catch (IOException e) {
            }
            return this;
        }
    }
     
    and here is my ranks class:


    Code (Java):
    package dutil;

    import me.Draco.MainScript;

    import java.util.ArrayList;
    import java.util.List;

    public class Ranks {
        private static FileBuilder f;
        private String name;
        private int Value;

        public Ranks(){
            this.f = new FileBuilder(MainScript.instance.getConfig().getCurrentPath(), "ranks.yml");
        }

        public Ranks(String Name, int Value) {
            this.name = Name;
            this.Value = Value;
            this.f = new FileBuilder(MainScript.instance.getConfig().getCurrentPath(), "ranks.yml");
        }

        public String getName(){
            if(!exist()){
                delete();
                return null;
            }
            return this.name;
        }

        public Integer getValue(){
            if(!exist()){
                delete();
                return null;
            }
            return this.Value;
        }

        public boolean exist(){
            return f.getString(name) != null;
        }

        public Ranks delete(){
            f.setValue(name, null);
            f.save();
            return this;
        }

        public Ranks setRank(){
            f.setValue("rank", getName());
            f.setValue("moneyNeeded", Value);
            f.save();
            return this;
        }

        public List<Ranks> getRanks(){
            List<Ranks> temp = new ArrayList<>();
            for(String name : f.getKeys(false)){
                Ranks ranks = new Ranks(name, Value);
                if(ranks.exist()){
                    temp.add(ranks);
                }
            }
            return temp;
        }
    }
     
    I hope someone can help me with it!
     
  2. This does not actually create the file, it just creates an abstract object that represents a file. The File constructor never creates files. Use File#exists() and File#createNewFile()
     
  3. like this?


    Code (Java):
    if(!f.exists()){
                try {
                    f.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
     
  4. SteelPhoenix

    Moderator

    MainScript.instance.getConfig().getCurrentPath()
    Do you even know what ConfigurationSection#getCurrentPath() returns? It has nothing to do with files.

    Also, don't ignore exceptions.
     
  5. i do know, and it works in other ways of creating files, but i dont know if it works this way
     
  6. Just simply include ranks.yml in your jar