Bukkit.getOfflinePlayer(args[0]); Help?

Discussion in 'Spigot Plugin Development' started by Moderance, May 23, 2016.

  1. So I am redoing the /deop and /op commands so it 'fits' with my server style and I can add my own permissions, etc, blah. blah.

    I know getOfflinePlayer() is depreciated, but is there another way to get the player who is offline? At the moment I'm doing this:

    Code (Text):
                            Player checkOfflinePlayer = (Player) Bukkit.getOfflinePlayer(args[0]);
                            if (checkOfflinePlayer.hasPlayedBefore()) {
                                if (checkOfflinePlayer.isOp()) {
                                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7You have deoped " + checkOfflinePlayer.getName() + " who is offline"));
                                } else {
                                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7" + checkOfflinePlayer.getName() + " is not an operator"));
                            } else {
                                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7" + args[0] + " has never played before!"));
    However, this is the console error:

    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_9_R1.CraftOfflinePlayer cannot be cast to org.bukkit.entity.Player
    at test.moderance.commands.DeOp.onCommand(DeOp.java:41) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-e6f93f4-ed60c01]
    ... 15 more

    Line 41:

    Code (Text):
    Player checkOfflinePlayer = (Player) Bukkit.getOfflinePlayer(args[0]);
    Thank you for any help given.
  2. You're casting an OfflinePlayer to a Player. Of course it will throw an error. Remove the cast and try it.
    • Like Like x 1
    • Agree Agree x 1
  3. What are you wanting to do exactly? DeOP players that are offline?
  4. Correct
    • Informative Informative x 1
  5. OHH.. I'm so stupid, thanks!


    Code (Text):
    OfflinePlayer checkOfflinePlayer = Bukkit.getOfflinePlayer(args[0]);
    Another Question:

    This is going to the else instead of passing through as equals, even though the uuid is matching the checkPlayer

    public String uuid = "069a79f4-44e9-4726-a5be-fca90e38aaf5";

    if (checkPlayer.getUniqueId().equals(uuid)) {
    //is player
    } else {
    //not player
  6. try if (checkPlayer.getUniqueId().equals(UUID.fromString(uuid))) {}
    • Like Like x 1
  7. Thanks <3
    • Friendly Friendly x 1