Solved Filewriter not creating text in .yml file

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

  1. So, I made a code that should write Text in a .yml file. But it doesn't so I have decided to ask for help here. There are no errors.

    FileWriter:
    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);
            if(!f.exists()){
                try {
                    f.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            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;
        }
    }
     
    Ranks:
    Code (Java):
    package dutil;

    import me.loliko.lolikobuyranks.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.getDataFolder().getAbsolutePath(), "ranks.yml");
        }

        public Ranks(String Name, int Value) {
            this.name = Name;
            this.Value = Value;
            this.f = new FileBuilder(MainScript.instance.getDataFolder().getAbsolutePath(), "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;
        }
    }
     
    createRank:
    Code (Java):
    package Commands.Admin;

    import dutil.Ranks;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;

    public class createRank implements CommandExecutor {
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(sender.hasPermission("dyzelbuyranks.addrank")){
                if(args.length <= 1){
                    sender.sendMessage("No Group Name named! OR no Money Value named!");
                    return true;
                }

                Ranks r = new Ranks(args[0], Integer.parseInt(args[1]));
                if(r.exist()){
                    r.delete();
                    r.setRank();
                }
            }
            return true;
        }
    }
     
     
  2. 1. Stop bumping that's annoying

    2. setValue does not involve save()
     
  3. it does.
    Code (Java):
    public Ranks setRank(){
            f.setValue("rank", getName());
            f.setValue("moneyNeeded", Value);
            f.save();
            return this;
        }
     
  4. You're right I misread it, thought it was setValue. What is inside the file, is it completely empty?
     
  5. yes it is completely empty
     
  6. f.exist() should be false at the beginning therefore the rank is never set
     
  7. you mean it created the file. In the same time it writes something in it?
     
  8. Sorry meant r.exist()

     
  9. i think you missunderstood. f.exists or r.exists checks if the "file" exists, not if the Rank exists
     
  10. Are you sure? Doesn't look that way to me

    public boolean exist(){
    return f.getString(name) != null;
    }
     
  11. oh no i forgot that r.exists is always false at the beginning f.exists checks for file sorry xD