Solved I get an error when checking stats of a player who has never logged on. Why?

Discussion in 'Plugin Development' started by Dalamix, Jan 31, 2021.

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

    Dalamix

    The error I get is: org.bukkit.command.CommandException: Unhandled exception executing command 'playerstats' in plugin JoinRewards v1.4
    Let me give you some information on what I made.
    My plugin saves stats of players which is accessible via a command called /playerstats.
    Those stats include:
    Player kills
    Player deaths
    Zombie kills
    Skeleton kills
    Spider kills

    My problem is not with checking stats, that is seeing the stats of the player I choose as long as there is stats on them.
    But when I run an if statement which looks like this:
    Code:
    if (!stats.contains("player." + uuid +".kills"))
    it instead gives me an error which I stated on the top of this thread.
    I run two different classes, one main and the other is for file saving(used for stats).

    In the main class I have my playerstats(only including the part which got the error.
    Code:
      
    package me.dalamixtheleader;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.UUID;
    import java.util.logging.Logger;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Server;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.Potion;
    import org.bukkit.potion.PotionType;
    import org.bukkit.scheduler.BukkitScheduler;
    
    public class JoinRewards
    extends JavaPlugin
    implements Listener {
        public Files stats;
        public Permission playerPermission = new Permission("joinrewards.use");
        public Permission playerPermission1 = new Permission("joinrewards.getpots");
        public Permission playerPermission2 = new Permission("joinrewards.grant");
        ArrayList<Player> cooldown = new ArrayList();
        ArrayList<Player> cooldown1 = new ArrayList();
      
        public void onEnable() {
            this.getLogger().info("You can now use Join Rewards!");
            PluginManager pm = this.getServer().getPluginManager();
            pm.addPermission(this.playerPermission);
            pm.addPermission(this.playerPermission1);
            pm.addPermission(this.playerPermission2);
            pm.registerEvents((Listener)this, (Plugin)this);
            this.saveDefaultConfig();
            stats = new Files(getDataFolder(), "stats.yml");
            if(!stats.fileExists()) {
                stats.createFile();
                stats.loadFile();
                stats.saveFile();
            }
            stats.loadFile();
        }
      
        public void onDisable() {
            this.getLogger().info("Join Rewards is fully disabled.");
        }
      
        //public void sendMessage(Player p, String s, ChatColor c) {
            //p.sendMessage((Object)ChatColor.DARK_GRAY + "[Join" + ChatColor.DARK_RED + "Rewards]" + c + s);
        //}
      
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            PlayerInventory pi;
            Player player; //serves as a placeholder, hi future dala. "player" gets defined later on, normally as sender
            Player p = (Player)sender;
            if (cmd.getName().equalsIgnoreCase("playerstats") && sender instanceof Player && this.getConfig().getBoolean("playerstats") == true) {
                int length = args.length;
                if (length == 1) {
                    String pname = args[0];
                    Player t = p.getServer().getPlayer(pname);
                    String uuid = t.getUniqueId().toString();
                    if (!stats.contains("player." + uuid +".kills")) {
                        p.sendMessage((Object)ChatColor.RED + "That player doesn't exist or has no stats!");
                       // String s = "That player doesn't exist or has no stats!";
                       // ChatColor c = ChatColor.RED;
                       // sendMessage(p, s, c);
                        return true;
                    }
                    }

    Files class:
    Code:
    package me.dalamixtheleader;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.List;
    
    import org.bukkit.configuration.InvalidConfigurationException;
    import org.bukkit.configuration.file.YamlConfiguration;
    
    public class Files {
     
        private File folder;
        private File file;
        private YamlConfiguration config;
     
     
        public Files(File folder, String filename) {
            this.folder = folder;
            this.file = new File(folder, filename + ".yml");
            this.config = new YamlConfiguration();
        }
     
        public Files(File folder, File file) {
            this.folder = folder;
            this.file = file;
            this.config = new YamlConfiguration();
        }
    
        public boolean createFile(){
            if(!folder.exists()) {
                folder.mkdirs();
            }
         
            if(!file.exists()) {
                try {
                    file.createNewFile();
                    return true;
                }
                catch(Exception e) {
                    e.printStackTrace();
                    return false;
                }
            }
            else {
                return false;
            }
         
        }
     
        public boolean fileExists() {
            if(file.exists()) {
                return true;
             
            }else {
             
                return false;
             
            }
         
        }
     
        public boolean loadFile() {
            try {
                config.load(file);
                return true;
            } catch (FileNotFoundException e) {
                createFile();
                return false;
            } catch (IOException e) {
                createFile();
                return false;
            } catch (InvalidConfigurationException e) {
                createFile();
                return false;
            }
         
         
        }
     
        public boolean saveFile() {
         
            try {
                config.save(file);
                return true;
            } catch (IOException e) {
                System.out.println("Failed to save file " + file.getName());
                return false;
            }
        }
     
        public void set(String path, Object value) {
            config.set(path, value);
        }
     
        public boolean contains(String path) {
            if (config.contains(path)) {
                return true;
            } else {
                return false;
            }
        }
     
        public int getInt(String path) {
            return config.getInt(path);
        }
     
        public String getString(String path) {
            return config.getString(path);
        }
     
        public List<String> getStringList(String path){
            return config.getStringList(path);
        }
     
        public List<Integer> getIntList(String path){
            return config.getIntegerList(path);
        }
     
        public List<?> getList(String path) {
            return config.getList(path);
        }
     
        public boolean getBoolean(String path) {
            return config.getBoolean(path);
        }
     
    }

    Any help would be greatly appreciated, thanks




    UPDATE, SOLUTION:
    I have yet not fixed the issue but I found the problem.
    The problem is that when I do the command for a player that doesn't have stats, is that it searches for the player's uuid via checking the players inside the server.
    Problem with that is that if the player with no stats isn't in the server, it will fail to find
    the uuid of that player as it cannot find it in the server.
    All I gotta do is to use Mojang API, which has a feature for converting names to uuids and
    vice versa.
    Sadly this runs off a connection to the internet, but the server should run on internet anyways.
    Thanks for the suggestions!
     
    Last edited: Jan 31, 2021
  2. Offline

    CraftCreeper6

    @Dalamix
    You need to use YamlConfiguration#loadConfiguration() to initialize the config.
     
  3. Offline

    Dalamix

    If you are talking about contains() in Files class, then I'll try it out I guess.

    UPDATE:
    I added the line of code in the contains() boolean.
    Code:
        public boolean contains(String path) {
            config.loadConfiguration(file);
            if (config.contains(path)) {
                return true;
            } else {
                return false;
            }
        }
    It didn't work, same error.
     
    Last edited: Jan 31, 2021
  4. Offline

    CraftCreeper6

    @Dalamix
    Not there, in your constructor.
     
Thread Status:
Not open for further replies.

Share This Page