Solved getInt in config returns 0 :(

Discussion in 'Plugin Development' started by Taeron, Jul 18, 2016.

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

    Taeron

    (SORRY IF MY ENGLISH IS BAD, I'M FRENCH :c)

    So, i'm currently coding a practice plugin and i'm using a yaml file to save players elo/stats

    But, when I use the getInt method, it always returns 0 :(

    Code:

    Join.java (setting default elo) :

    Code:
    package fr.taeron.divang.events;
    
    
    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.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    import fr.taeron.divang.dCore;
    import fr.taeron.divang.practice.Kits;
    import fr.taeron.divang.utils.NotifyStaff;
    
    public class Join implements Listener{
      
      
    @SuppressWarnings("unused")
    public Join(){
        File PlayerDataFile;
        FileConfiguration PlayerDataConfig;
    }
        private File PlayerDataFile;
        public static FileConfiguration PlayerDataConfig;
      
      
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onJoin(PlayerJoinEvent e) throws IOException{
            e.setJoinMessage("");
            Player p = e.getPlayer();
            p.teleport(Bukkit.getWorld("world").getSpawnLocation());
          
            p.setHealth(20);
            p.setFireTicks(0);
            p.setFoodLevel(20);
          
            PlayerDataFile = new File("plugins/Practice/data/"+p.getUniqueId()+".yml");
            PlayerDataConfig = new YamlConfiguration();
          
            if(!PlayerDataFile.exists())  {
              
              
               int defaultelo = 1000;
               PlayerDataConfig.save(PlayerDataFile);
               PlayerDataConfig.createSection("Pseudo");
               PlayerDataConfig.createSection("IP");
               PlayerDataConfig.createSection("Elo BuildUHC");
               PlayerDataConfig.createSection("Elo NoDebuff");
               PlayerDataConfig.createSection("Elo Combo");
               PlayerDataConfig.createSection("Elo Axe");
               PlayerDataConfig.save(PlayerDataFile);
               PlayerDataConfig.set("Pseudo", p.getName());
               PlayerDataConfig.set("IP", p.getAddress().getAddress().getHostAddress());
               PlayerDataConfig.set("Elo BuildUHC", defaultelo);
               PlayerDataConfig.set("Elo NoDebuff", defaultelo);
               PlayerDataConfig.set("Elo Combo", defaultelo);
               PlayerDataConfig.set("Elo Axe", defaultelo);
               PlayerDataConfig.save(PlayerDataFile);
             
            }
              
            Kits.giveMainItems(p);
          
            if(p.hasPermission(dCore.staffperm)){
                NotifyStaff.Info("§8(§4STAFF§8) §c" + p.getName() + " §7a rejoint le serveur.");   
            } else {
                if(p.hasPermission(dCore.supremeperm)){
                    Bukkit.broadcastMessage(dCore.prefix + "§b" + p.getName() + " §7a rejoint le serveur");
                    return;
                }
                if(p.hasPermission(dCore.goldperm)){
                    Bukkit.broadcastMessage(dCore.prefix + "§6" + p.getName() + " §7a rejoint le serveur");
                    return;
                }
                if(p.hasPermission(dCore.silverperm)){
                    Bukkit.broadcastMessage(dCore.prefix + "§7" + p.getName() + " §7a rejoint le serveur");
                    return;
                }
          
            }
        }
    }
    
    elo.java (/elo command):

    Code:
    package fr.taeron.divang.commands;
    
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import fr.taeron.divang.dCore;
    import fr.taeron.divang.events.Join;
    
    
    
    public class elo implements CommandExecutor{
     
    
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
         
            int uhc = Join.PlayerDataConfig.getInt("Elo BuildUHC");
            int pot = Join.PlayerDataConfig.getInt("Elo NoDebuff");
            int cb = Join.PlayerDataConfig.getInt("Elo Combo");
            int axe = Join.PlayerDataConfig.getInt("Elo Axe");
         
            p.sendMessage(dCore.prefix + "§3Ton elo §b(" + p.getName() + "):");
            p.sendMessage("§7- §3BuildUHC: §b" + uhc);
            p.sendMessage("§7- §3NoDebuff: §b" + pot);
            p.sendMessage("§7- §3Combo: §b" + cb);
            p.sendMessage("§7- §3Axe: §b" + axe);
            p.sendMessage("§cNote: tu ne peux pas afficher l'elo des autres joueurs.");
            return true;
        
            }
        }
     
     
     
     
    
    

    Message: http://prntscr.com/bujfqs


    Can anyone help me? :)
     
  2. Offline

    Tecno_Wizard

    @Taeron your english is better than a whole lot of people on this forum, trust me. Lol.

    Few things:
    1. Variables are always lowercase, then uppercase for every word after that, such as var, dataFile, or playerDataFile. It will make it a lot easier for us to know what is going on. Uppercases usually mean static fields.
    2. Currently, you're making a blank configuration every single time. Instead of using new YamlConfiguration, use YamlConfiguration.loadConfig(String filenameFile file)
     
    Last edited: Jul 20, 2016
  3. Offline

    Firestar311

    Not "String" but "File"

    Other than that @Tecno_Wizard is correct in all of his statements.
     
  4. Offline

    Tecno_Wizard

  5. Offline

    Taeron

    Thanks for your help, but I still don't understand :(.

    Can you give me an example please? :c


    Edit:

    Oh ok sorry I understand now :3

    I've tried
    Code:
    PlayerDataConfig = YamlConfiguration.loadConfiguration(Join.PlayerDataFile);
    And it works perfectly ! :) thanks a lot !
     
  6. @Taeron
    If your issue has been solved, please mark the thread as such :)
     
  7. Offline

    Tecno_Wizard

    @Taeron, sorry. My typo (typing error) was probably confusing you.

    "Currently, you're making a black blank configuration every single time."
     
Thread Status:
Not open for further replies.

Share This Page