I need help with my rank plugin

Discussion in 'Plugin Development' started by Liquided, Oct 10, 2020.

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

    Liquided

    Ive been trying to set a rank in my plugin. For some reason it wont work. I made it so by default when a player joins they get default rank and that works. But when I try to set it using a command it wont work.

    The PlayerJoinEvent uses:
    File file = new File("plugins/UltraBattles/players.yml");
    YamlConfiguration yml = YamlConfiguration.loadConfiguration(file);
    if (!p.hasPlayedBefore()) {
    yml.addDefault("players." + p.getUniqueId() + ".rank", defaultrank);
    yml.options().copyDefaults(true);
    try {
    yml.save(file);
    } catch (IOException eg) {
    eg.printStackTrace();
    }
    }
    Which works perfectly fine But the with the command doesnt work:
    File f = new File("plugins/UltraBattles/PlayerData/" + p.getUniqueId() + ".yml");
    YamlConfiguration yml = YamlConfiguration.loadConfiguration(f);
    yml.addDefault("players." + p.getUniqueId() + ".rank", name);
    yml.options().copyDefaults(true);
    try {
    yml.save(f);
    p.sendMessage(Util.chat("&4" + p.getDisplayName() + " &6has changed your rank to &4" + name));
    } catch (IOException e) {
    p.sendMessage(Util.chat("&cAn error has occured while setting that players rank!"));
    e.printStackTrace();
    }
    The console doesnt show any errors and the code also has no errors. Its only the file that wont set the rank.
     
  2. Offline

    Strahan

    Those two methods are using totally different files.
     
  3. Offline

    Liquided

    Oops I wrote the wrong one on accident, heres the correct one:
    PlayerJoinListener:
    File file = new File("plugins/UltraBattles/PlayerData/" + p.getUniqueId() + ".yml");
    YamlConfiguration yml = YamlConfiguration.loadConfiguration(file);
    if (!p.hasPlayedBefore()) {
    yml.addDefault("Information." + ".rank", defaultrank);
    yml.options().copyDefaults(true);
    try {
    yml.save(file);
    } catch (IOException eg) {
    eg.printStackTrace();
    }
    }
    SetRank Method:
    public void setRank(Player p, String name) {
    File f1 = new File("plugins/UltraBattles/ranks.yml");
    YamlConfiguration yml1 = YamlConfiguration.loadConfiguration(f1);
    File file = new File("plugins/UltraBattles/PlayerData/" + p.getUniqueId() + ".yml");
    YamlConfiguration yml = YamlConfiguration.loadConfiguration(file);
    if (yml1.contains(name)) {
    yml.set("Information.rank", name); yml.options().copyDefaults(true);
    yml.options().copyDefaults(true);
    try {
    yml.save(file);
    p.sendMessage(Util.chat("&4" + p.getDisplayName() + " &6has changed your rank to &4" + name));
    } catch (IOException e) {
    p.sendMessage(Util.chat("&cAn error has occured while setting that players rank!"));
    e.printStackTrace(); }
    }
    }
     
Thread Status:
Not open for further replies.

Share This Page