Need some help please!

Discussion in 'Plugin Development' started by OhYes, May 10, 2016.

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

    OhYes

    I tried going on the Spigot forums, but NO help whatsoever. xD

    Anyway, the commands work, but when I log in, it doesn't change the config whatsoever, and the /mmr get Oh_Yes command just returns with "0"
    I really need some urgent attention to this, it's killing my insides. o-o

    Config:
    Code:
    # ----[ MMR Config ]----
    # Here you can edit MMR values manually, or adjust general settings.
    # Below is a list of Players that are registered Ranked players along with their
    # UUID (Unique User ID) and their MMR Rating.
    DefaultMMR: 1000
    Players:
    Listener:
    Code:
    package me.ohyes.mmr;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    public class MMRListener implements Listener {
    
        Main plugin;
       
        @SuppressWarnings("static-access")
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e){
            int defaultMMR = plugin.config.getInt("DefaultMMR");
            Player logged = e.getPlayer();
            String uuid = logged.getUniqueId().toString();
            if(plugin.config.contains(logged.getName() + " | " + uuid)){
                return;
            }else{
                plugin.config.createSection(logged.getName() + " | " + uuid);
                plugin.config.createPath(plugin.config.getConfigurationSection(logged.getName() + " | " + uuid), "MMR");
                plugin.config.set(plugin.config.getConfigurationSection(logged.getName() + " | " + uuid + ".MMR").toString(), defaultMMR);
                plugin.saveConfig();
        }
        }
    }
    Main:
    Code:
    package me.ohyes.mmr;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class Main extends JavaPlugin{
       
        FileConfiguration config = this.getConfig();
        public static String prefix = ChatColor.RED + "[" + ChatColor.GOLD + "MMR" + ChatColor.RED + "]";
       
        @Override
        public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(new MMRListener(), this);
            this.getConfig().options().copyDefaults();
            saveDefaultConfig();
        }
       
        @Override
        public void onDisable(){
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(!(sender instanceof Player)){
                sender.sendMessage("[MMR] You cannot use commands for this plugin as the Console");
                return false;
            }else{
                Player p = (Player) sender;
                if(commandLabel.equalsIgnoreCase("mmr")){
                    if(args.length == 0){
                        sendHelp(p);
                        return false;
                    }else if(args.length == 2){
                        if(args[0].equalsIgnoreCase("get")){
                            try{
                                Player target = Bukkit.getPlayer(args[1]);
                                p.sendMessage(prefix + ChatColor.YELLOW + getMMR(target));
                            }catch(Exception e){
                                p.sendMessage(prefix + ChatColor.RED + " Error. The target player is most likely not online.");
                                return false;
                            }
                        }
                    }else{
                        sendHelp(p);
                    }
                }
            }
           
            return false;
        }
       
        //
        //Voids and methods
        //
    
    
        public void sendHelp(Player p) {
            p.sendMessage(prefix + ChatColor.RED + " Unknown usage.");
            p.sendMessage(ChatColor.YELLOW + "/MMR help - Brings up this dialogue.");
            p.sendMessage(ChatColor.YELLOW + "/MMR get <user> - Views the user's MMR rating.");
            p.sendMessage(ChatColor.YELLOW + "/MMR add <user> <value> - Adds MMR to a user.");
            p.sendMessage(ChatColor.YELLOW + "/MMR subtract <user> <value> - Removes MMR from a user.");
            p.sendMessage(ChatColor.YELLOW + "/MMR set <user> <value> - Sets the user's MMR rating.");
            return;
        }
       
        public void addMMR(Player p, int amount){
            config.set(p.getName() + " | " + p.getUniqueId().toString() + ".MMR", config.getInt(p.getName() + " | " + p.getUniqueId().toString() + ".MMR") + amount);
        }
       
        public void subtractMMR(Player p, int amount){
            config.set(p.getName() + " | " + p.getUniqueId().toString() + ".MMR", config.getInt(p.getName() + " | " + p.getUniqueId().toString() + ".MMR") - amount);
        }
       
        public int getMMR(Player p){
            int mmr = config.getInt(p.getName() + " | " + p.getUniqueId().toString() + ".MMR");
            return mmr;
        }
       
        public void setMMR(Player p, int amount){
            config.set(p.getName() + " | " + p.getUniqueId().toString() + ".MMR",amount);
        }
    
    }
     
  2. Offline

    MisterErwin

    @OhYes I would say that plugin in your listener is never set :)
     
  3. Offline

    Zombie_Striker

    You need to set plugin equal to something, or else this will throw NPES

    If this method does nothing, remove it.

    This resets the config every time it reloads. Make sure the config does not exist before you set it.

    Use cmd.getName instead of Commandlabel.

    Make sure target is not null before using it. Don't use try/catch statements for this, as you should never intend to receive an exception.
    Main problem: You forget to save the config after you set values in it.
     
  4. Offline

    OhYes

    Thanks for the help. Would I set the plugin equal to something like: Main plugin = new Main(); ?
    Also, the way I have the Listener set up, if the player changed their name, would it create a new path? And reset their MMR rating? Thanks.
     
  5. Offline

    mcdorli

    No, you can't create a new insteance from the main class, and use UUIDs.
     
  6. Offline

    OhYes

    Soooo, what do I do?

    this is my main code and listener class code NOW. When I run it brings up an error in the console saying [MMR] Error enabling MMR v1.0 (is it up to date?)

    The commands still work, but the config doesn't add players when they join still. :c

    main:
    Code:
    package me.ohyes.mmr;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class Main extends JavaPlugin{
       
        FileConfiguration config = this.getConfig();
        public static String prefix = ChatColor.RED + " [" + ChatColor.GOLD + "MMR" + ChatColor.RED + "] ";
       
        @Override
        public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(new MMRListener(), this);
            config.options().copyDefaults();
            saveDefaultConfig();
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(!(sender instanceof Player)){
                sender.sendMessage("[MMR] You cannot use commands for this plugin as the Console");
                return false;
            }else{
                Player p = (Player) sender;
                if(cmd.getName().equalsIgnoreCase("mmr")){
                    if(args.length == 0){
                        sendHelp(p);
                        return false;
                    }else if(args.length == 1){
                        if(args[0].equalsIgnoreCase("help")){
                            sendHelp(p);
                        }else{
                            p.sendMessage(prefix + ChatColor.RED + "Unknown usage.");
                            sendHelp(p);
                        }
                    }else if(args.length == 2){
                        if(args[0].equalsIgnoreCase("get")){
                            try{
                                Player target = Bukkit.getPlayer(args[1]);
                                p.sendMessage(prefix + ChatColor.YELLOW + target.getName() + "'s MMR: " + ChatColor.AQUA + getMMR(target));
                            }catch(Exception e){
                                p.sendMessage(prefix + ChatColor.RED + "Error. " + args[1] + " is most likely not online.");
                                return false;
                            }
                        }else{
                            p.sendMessage(prefix + ChatColor.RED + "Unknown usage.");
                            sendHelp(p);
                        }
                    }else if(args.length == 3){
                        if(args[0].equalsIgnoreCase("set")){
                            try{
                                Player target = Bukkit.getPlayer(args[1]);
                                int amount = Integer.parseInt(args[2]);
                               
                                if(amount < 0){p.sendMessage(prefix + ChatColor.RED + "The amount must be 0 or greater.");}else{
                               
                                setMMR(target, amount);
                                p.sendMessage(prefix + ChatColor.YELLOW + target.getName() + "'s MMR was set to: " + ChatColor.AQUA + amount);
                               
                                }
                            }catch(Exception e){
                                p.sendMessage(prefix + ChatColor.RED + "Error. " + args[1] + " is most likely not online. Or you entered an invalid amount.");
                                return false;
                            }
                           
                        }else if(args[0].equalsIgnoreCase("add")){
                            try{
                                Player target = Bukkit.getPlayer(args[1]);
                                int amount = Integer.parseInt(args[2]);
                               
                                if(amount < 0){p.sendMessage(prefix + ChatColor.RED + "The amount must be 0 or greater.");}else{
                               
                                addMMR(target, amount);
                                p.sendMessage(prefix + ChatColor.YELLOW + target.getName() + "'s MMR was set to: " + ChatColor.AQUA + getMMR(target));
                               
                                }
                            }catch(Exception e){
                                p.sendMessage(prefix + ChatColor.RED + "Error. " + args[1] + " is most likely not online. Or you entered an invalid amount.");
                                return false;
                            }
                        }else if(args[0].equalsIgnoreCase("subtract")){
                            try{
                                Player target = Bukkit.getPlayer(args[1]);
                                int amount = Integer.parseInt(args[2]);
                               
                                if(amount > getMMR(target)){p.sendMessage(prefix + ChatColor.RED + "The amount cannot reduce the player's MMR to below 0.");}else{
                               
                                subtractMMR(target, amount);
                                p.sendMessage(prefix + ChatColor.YELLOW + target.getName() + "'s MMR was set to: " + ChatColor.AQUA + getMMR(target));
                               
                                }
                            }catch(Exception e){
                                p.sendMessage(prefix + ChatColor.RED + "Error. " + args[1] + " is most likely not online. Or you entered an invalid amount.");
                                return false;
                            }
                        }else{
                            p.sendMessage(prefix + ChatColor.RED + "Unknown usage.");
                            sendHelp(p);
                        }
                    }else{
                        p.sendMessage(prefix + ChatColor.RED + "Unknown usage.");
                        sendHelp(p);
                    }
                }
            }
           
            return false;
        }
       
        //
        //Voids and methods
        //
    
    
        public void sendHelp(Player p) {
            p.sendMessage(ChatColor.RED + "-----" + prefix + ChatColor.RED + "-----");
            p.sendMessage(ChatColor.YELLOW + "/MMR help - Brings up this dialogue.");
            p.sendMessage(ChatColor.YELLOW + "/MMR get <user> - Views the user's MMR rating.");
            p.sendMessage(ChatColor.YELLOW + "/MMR add <user> <value> - Adds MMR to a user.");
            p.sendMessage(ChatColor.YELLOW + "/MMR subtract <user> <value> - Removes MMR from a user.");
            p.sendMessage(ChatColor.YELLOW + "/MMR set <user> <value> - Sets the user's MMR rating.");
            return;
        }
       
        public void addMMR(Player p, int amount){
            config.set(p.getName() + " | " + p.getUniqueId().toString() + ".MMR", config.getInt(p.getName() + " | " + p.getUniqueId().toString() + ".MMR") + amount);
            saveConfig();
        }
       
        public void subtractMMR(Player p, int amount){
            config.set(p.getName() + " | " + p.getUniqueId().toString() + ".MMR", config.getInt(p.getName() + " | " + p.getUniqueId().toString() + ".MMR") - amount);
            saveConfig();
        }
       
        public int getMMR(Player p){
            int mmr = config.getInt(p.getName() + " | " + p.getUniqueId().toString() + ".MMR");
            return mmr;
        }
       
        public void setMMR(Player p, int amount){
            config.set(p.getName() + " | " + p.getUniqueId().toString() + ".MMR",amount);
            saveConfig();
        }
    
    }
    
    Listener:
    Code:
    package me.ohyes.mmr;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    public class MMRListener implements Listener {
    
        Main plugin = new Main();
       
        @SuppressWarnings("static-access")
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e){
            int defaultMMR = plugin.config.getInt("DefaultMMR");
            Player logged = e.getPlayer();
            String uuid = logged.getUniqueId().toString();
            if(plugin.config.contains(logged.getName() + " | " + uuid)){
                return;
            }else{
                plugin.config.createSection(logged.getName() + " | " + uuid);
                plugin.config.createPath(plugin.config.getConfigurationSection(logged.getName() + " | " + uuid), "MMR");
                plugin.config.set(plugin.config.getConfigurationSection(logged.getName() + " | " + uuid + ".MMR").toString(), defaultMMR);
                plugin.saveConfig();
        }
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 11, 2016
  7. Offline

    mcdorli

    You can't do new Main(), use a constructor and pass an instance of it (using this) around.

    I won't provide an example code form something such simple
     
  8. Offline

    OhYes

    Right, okay. I did that. The config loads.. but NOW it STILL doesn't add my UUID and username to the config with the defaultMMR when I join.......

    Listener
    Code:
    package me.ohyes.mmr;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    public class MMRListener implements Listener {
    
        private Main plugin;
       
        public MMRListener(Main plugin){
            this.plugin = plugin;
        }
       
       
        @SuppressWarnings("static-access")
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e){
            int defaultMMR = plugin.config.getInt("DefaultMMR");
            Player logged = e.getPlayer();
            String uuid = logged.getUniqueId().toString();
            if(plugin.config.contains(logged.getName() + " | " + uuid)){
                return;
            }else{
                plugin.config.createSection(logged.getName() + " | " + uuid);
                plugin.config.createPath(plugin.config.getConfigurationSection(logged.getName() + " | " + uuid), "MMR");
                plugin.config.set(plugin.config.getConfigurationSection(logged.getName() + " | " + uuid + ".MMR").toString(), defaultMMR);
                plugin.saveConfig();
        }
        }
    }
     
  9. Offline

    Zombie_Striker

    @OhYes
    If "config" is not the same as "getConfig()", then you cannot use saveConfig() to save the file. In that case, you would need to save it using YMLConfiguration.save(File). If it is instead that config is the same as getConfig(), then you may want to consider removing "config" as it would be redundant.
    BTW: These lines can be reduced to one line, as creating sections and paths are automatically done when you set a a value. Use the following instead:
    Code:
     plugin.config.set(logged.getName() + " | " + uuid + ".MMR", defaultMMR);
     
  10. Offline

    OhYes

    config is FileConfiguration config = this.getConfig();

    Okay, I just removed the Listener class and merged it into the Main, it's too annoying. I only have two problems now, the MMR on-join thing works great! But it seems to delete the DefaultMMR rating whenever I join?

    Before:
    Code:
    # ----[ MMR Config ]----
    # Here you can edit MMR values manually, or adjust general settings.
    # Below is a list of Players that are registered Ranked players along with their
    # UUID (Unique User ID) and their MMR Rating.
    DefaultMMR: 1000
    #
    # Above is the default MMR player's will get when they first log-in.
    #
    Players:
    After (login):
    Code:
    # ----[ MMR Config ]----
    # Here you can edit MMR values manually, or adjust general settings.
    # Below is a list of Players that are registered Ranked players along with their
    # UUID (Unique User ID) and their MMR Rating.
    Players:
      80c6b717-424f-456a-89ff-c6e18a012c04:
        Oh_Yes:
          MMR: 1000
    
    It looks like it deletes everything past default MMR including it, and before Players. Any knowledge?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 11, 2016
Thread Status:
Not open for further replies.

Share This Page