How to get a offline player and change their rank?

Discussion in 'Spigot Plugin Development' started by ZevoGaems, Jun 9, 2017.

  1. I made a ranking plugin but I want it to be able to change the rank of a offline player so how would I make it so it could get the player then change their rank. When I tried to it just kept saying dead code so here is what I have right now and I have a empty spot at the bottom and that was where I tried to detect if the player was offline.
    Code (Text):
            if(cmd.getName().equalsIgnoreCase("rank")) {
                if(!sender.hasPermission("minegrid.rank")) {
                    sender.sendMessage(ChatColor.RED + "Requested Permission Level \n OWNER");
                } else if(args.length != 2) {
                    sender.sendMessage(ChatColor.RED + "Usage, /rank (player) (rank)");
                } else if(args.length == 2) {
                    String p1 = args[0];
                    Player p = Bukkit.getPlayer(p1);
                    String pname = p.getName();
                    String rank = args[1];
                    if(rank.equals("Owner")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Owner!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Owner");
                        saveConfig();
                        p.setPlayerListName(ChatColor.RED + " " + p.getName());
                    }
                    if(rank.equals("Admin")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Admin!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Admin");
                        saveConfig();
                        p.setPlayerListName(ChatColor.RED + " " + p.getName());
                    }
                    if(rank.equals("Dev")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Dev!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Dev");
                        saveConfig();
                        p.setPlayerListName(ChatColor.RED + " " + p.getName());
                    }
                    if(rank.equals("Builder")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Builder!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Builder");
                        saveConfig();
                        p.setPlayerListName(ChatColor.RED + " " + p.getName());
                    }
                    if(rank.equals("Mod")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Mod!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Mod");
                        saveConfig();
                        p.setPlayerListName(ChatColor.RED + " " + p.getName());
                    }

                    if(rank.equals("Trainee")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Trainee!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Trainee");
                        saveConfig();
                        p.setPlayerListName(ChatColor.RED + " " + p.getName());
                    }
                    if(rank.equals("Lord")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Lord!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Lord");
                        saveConfig();
                        p.setPlayerListName(ChatColor.GOLD + " " + p.getName());
                    }
                    if(rank.equals("VIP")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to VIP!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "VIP");
                        saveConfig();
                        p.setPlayerListName(ChatColor.YELLOW + " " + p.getName());
                    }
                    if(rank.equals("Premium")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Premium!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Premium");
                        saveConfig();
                        p.setPlayerListName(ChatColor.AQUA + " " + p.getName());
                    }
                    if(rank.equals("Member")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Member!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Member");
                        saveConfig();
                        p.setPlayerListName(ChatColor.GRAY + " " + p.getName());
                    } else if(!rank.equals("Member") && !rank.equals("Premium") && !rank.equals("VIP") && !rank.equals("Lord") && !rank.equals("Trainee") && !rank.equals("Mod") && !rank.equals("Builder") && !rank.equals("Dev") && !rank.equals("Admin") && !rank.equals("Owner")) {
                        sender.sendMessage(ChatColor.RED + "Error, could not identify ranks \n Current ranks include \n Owner \n Admin \n Dev \n Builder \n Mod \n Trainee \n Lord \n VIP \n Premium \n Member \n Please note, ranks are case sensitive");
                    } else if(p == null) {
                        OfflinePlayer poffline = Bukkit.getOfflinePlayer(args[1]);
                       
                    }
                    }
            }
     
  2. Bukkit.getServer().getOfflinePlayer("Name");
     
  3. electronicboy

    IRC Staff

    because your code performs operations on your Player object, p, it says "Well, I can't be null at all, because if I was null I would have thrown an exception".

    check null from the result of getPlayer before doing anything with it

    (if you do it properly, you won't have to duplicate that whole if section... also, use else if, otherwise you needlessly start evaluating dozens of if statements needlessly.... better yet, fix your code properly and use a switch or something sane)
     
    • Agree Agree x 1
  4. So where would the if player == null go?
     
  5. After you defined the variable 'player'
     
  6. Ok so now I have this
    Code (Text):
            if(cmd.getName().equalsIgnoreCase("rank")) {
                if(!sender.hasPermission("minegrid.rank")) {
                    sender.sendMessage(ChatColor.RED + "Requested Permission Level \n OWNER");
                } else if(args.length != 2) {
                    sender.sendMessage(ChatColor.RED + "Usage, /rank (player) (rank)");
                } else if(args.length == 2) {
                    String p1 = args[0];
                    Player p = Bukkit.getPlayer(p1);
                    String pname = p.getName();
                    String rank = args[1];
                    if(p == null) {
                        OfflinePlayer offlinep = Bukkit.getServer().getOfflinePlayer(args[1]);
                       
                    }
                    if(rank.equals("Owner")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Owner!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Owner");
                        saveConfig();
                        p.setPlayerListName(ChatColor.RED + " " + p.getName());
                    }
                    if(rank.equals("Admin")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Admin!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Admin");
                        saveConfig();
                        p.setPlayerListName(ChatColor.RED + " " + p.getName());
                    }
                    if(rank.equals("Dev")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Dev!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Dev");
                        saveConfig();
                        p.setPlayerListName(ChatColor.RED + " " + p.getName());
                    }
                    if(rank.equals("Builder")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Builder!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Builder");
                        saveConfig();
                        p.setPlayerListName(ChatColor.RED + " " + p.getName());
                    }
                    if(rank.equals("Mod")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Mod!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Mod");
                        saveConfig();
                        p.setPlayerListName(ChatColor.RED + " " + p.getName());
                    }

                    if(rank.equals("Trainee")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Trainee!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Trainee");
                        saveConfig();
                        p.setPlayerListName(ChatColor.RED + " " + p.getName());
                    }
                    if(rank.equals("Lord")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Lord!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Lord");
                        saveConfig();
                        p.setPlayerListName(ChatColor.GOLD + " " + p.getName());
                    }
                    if(rank.equals("VIP")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to VIP!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "VIP");
                        saveConfig();
                        p.setPlayerListName(ChatColor.YELLOW + " " + p.getName());
                    }
                    if(rank.equals("Premium")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Premium!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Premium");
                        saveConfig();
                        p.setPlayerListName(ChatColor.AQUA + " " + p.getName());
                    }
                    if(rank.equals("Member")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Member!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Member");
                        saveConfig();
                        p.setPlayerListName(ChatColor.GRAY + " " + p.getName());
                    } else if(!rank.equals("Member") && !rank.equals("Premium") && !rank.equals("VIP") && !rank.equals("Lord") && !rank.equals("Trainee") && !rank.equals("Mod") && !rank.equals("Builder") && !rank.equals("Dev") && !rank.equals("Admin") && !rank.equals("Owner")) {
                        sender.sendMessage(ChatColor.RED + "Error, could not identify ranks \n Current ranks include \n Owner \n Admin \n Dev \n Builder \n Mod \n Trainee \n Lord \n VIP \n Premium \n Member \n Please note, ranks are case sensitive");
                    }
    But now what would I put in the part that says

    Code (Text):
                    if(p == null) {
                        OfflinePlayer offlinep = Bukkit.getServer().getOfflinePlayer(args[1]);
                       
                    }
    I am completely clueless
     
  7. Do the null check before the String pname.

    Well, do your code, but then with OfflinePlayer. Most methods are availeble except #sendMessage and some more.
     
  8.  
  9. I tried doing something and I got the stuff below. Also, the stuff below doesn't work and just gives me another error about the person being offline. Here is what I have
    Code (Text):
    if(cmd.getName().equalsIgnoreCase("rank")) {
                if(!sender.hasPermission("minegrid.rank")) {
                    sender.sendMessage(ChatColor.RED + "Requested Permission Level \n OWNER");
                } else if(args.length != 2) {
                    sender.sendMessage(ChatColor.RED + "Usage, /rank (player) (rank)");
                } else if(args.length == 2) {
                    if(Bukkit.getPlayer(args[0]) == null) {
                        OfflinePlayer p = Bukkit.getServer().getOfflinePlayer(args[0]);
                        String pname = p.getName();
                        String rank = args[1];
                        if(rank.equals("Owner")) {
                            Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Owner!", "minegrid.notify");
                            getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Owner");
                            saveConfig();
                        }
                        if(rank.equals("Admin")) {
                            Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Admin!", "minegrid.notify");
                            getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Admin");
                            saveConfig();
                        }
                        if(rank.equals("Dev")) {
                            Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Dev!", "minegrid.notify");
                            getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Dev");
                            saveConfig();
                        }
                        if(rank.equals("Builder")) {
                            Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Builder!", "minegrid.notify");
                            getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Builder");
                            saveConfig();
                        }
                        if(rank.equals("Mod")) {
                            Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Mod!", "minegrid.notify");
                            getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Mod");
                            saveConfig();
                        }

                        if(rank.equals("Trainee")) {
                            Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Trainee!", "minegrid.notify");
                            getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Trainee");
                            saveConfig();
                        }
                        if(rank.equals("Lord")) {
                            Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Lord!", "minegrid.notify");
                            getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Lord");
                            saveConfig();
                        }
                        if(rank.equals("VIP")) {
                            Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to VIP!", "minegrid.notify");
                            getConfig().set("ranked player." + p.getUniqueId() + ".rank", "VIP");
                            saveConfig();
                        }
                        if(rank.equals("Premium")) {
                            Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Premium!", "minegrid.notify");
                            getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Premium");
                            saveConfig();
                        }
                        if(rank.equals("Member")) {
                            Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Member!", "minegrid.notify");
                            getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Member");
                            saveConfig();
                        } else if(!rank.equals("Member") && !rank.equals("Premium") && !rank.equals("VIP") && !rank.equals("Lord") && !rank.equals("Trainee") && !rank.equals("Mod") && !rank.equals("Builder") && !rank.equals("Dev") && !rank.equals("Admin") && !rank.equals("Owner")) {
                            sender.sendMessage(ChatColor.RED + "Error, could not identify ranks \n Current ranks include \n Owner \n Admin \n Dev \n Builder \n Mod \n Trainee \n Lord \n VIP \n Premium \n Member \n Please note, ranks are case sensitive");
                        }
                    }
                    String p1 = args[0];
                    Player p = Bukkit.getPlayer(p1);
                    String pname = p.getName();
                    String rank = args[1];
                    if(rank.equals("Owner")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Owner!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Owner");
                        saveConfig();
                        p.setPlayerListName(ChatColor.RED + " " + p.getName());
                    }
                    if(rank.equals("Admin")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Admin!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Admin");
                        saveConfig();
                        p.setPlayerListName(ChatColor.RED + " " + p.getName());
                    }
                    if(rank.equals("Dev")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Dev!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Dev");
                        saveConfig();
                        p.setPlayerListName(ChatColor.RED + " " + p.getName());
                    }
                    if(rank.equals("Builder")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Builder!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Builder");
                        saveConfig();
                        p.setPlayerListName(ChatColor.RED + " " + p.getName());
                    }
                    if(rank.equals("Mod")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Mod!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Mod");
                        saveConfig();
                        p.setPlayerListName(ChatColor.RED + " " + p.getName());
                    }

                    if(rank.equals("Trainee")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Trainee!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Trainee");
                        saveConfig();
                        p.setPlayerListName(ChatColor.RED + " " + p.getName());
                    }
                    if(rank.equals("Lord")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Lord!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Lord");
                        saveConfig();
                        p.setPlayerListName(ChatColor.GOLD + " " + p.getName());
                    }
                    if(rank.equals("VIP")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to VIP!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "VIP");
                        saveConfig();
                        p.setPlayerListName(ChatColor.YELLOW + " " + p.getName());
                    }
                    if(rank.equals("Premium")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Premium!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Premium");
                        saveConfig();
                        p.setPlayerListName(ChatColor.AQUA + " " + p.getName());
                    }
                    if(rank.equals("Member")) {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to Member!", "minegrid.notify");
                        getConfig().set("ranked player." + p.getUniqueId() + ".rank", "Member");
                        saveConfig();
                        p.setPlayerListName(ChatColor.GRAY + " " + p.getName());
                    } else if(!rank.equals("Member") && !rank.equals("Premium") && !rank.equals("VIP") && !rank.equals("Lord") && !rank.equals("Trainee") && !rank.equals("Mod") && !rank.equals("Builder") && !rank.equals("Dev") && !rank.equals("Admin") && !rank.equals("Owner")) {
                        sender.sendMessage(ChatColor.RED + "Error, could not identify ranks \n Current ranks include \n Owner \n Admin \n Dev \n Builder \n Mod \n Trainee \n Lord \n VIP \n Premium \n Member \n Please note, ranks are case sensitive");
                    }
                    }
            }
     
  10. 1. You have design issues. Follow the DRY principle - don't repeat yourself. Under your "if" statements, you are constantly using similar statements to broadcast/change the ranks. You should reorganise it in a way such that 1 method will fit your needs.
    Say, you want to set a rank and broadcast the message:
    Code (Text):
    public void method(Player p, String r, ChatColor c) {
      broadcastMessage("..." + c + r)
      //set your config values
      //save
    }
    Way more elegant

    2. consider using "switch" to handle your cases
    it is slightly more optimised and efficient
     
  11. Or better yet:

    Code (Java):
    String rank = "Trainee";
    //verify rank input, then
    Bukkit.broadcast(ChatColor.RED + sender.getName() + " has changed " + pname + " to " + rank + "!", "minegrid.notify");
    getConfig().set("ranked player." + p.getUniqueId() + ".rank", rank);
    saveConfig();
    p.setPlayerListName(/* get prefix dynamically from config */ + " " + p.getName());
    //no if/switch needed