Solved How to cast an offline player to an online player?

Discussion in 'Plugin Development' started by Scrapnix, Sep 6, 2017.

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

    Scrapnix

    Hey there, I just want to cast an offline player to an online player.
    The problem is, I want to read out in the onEnable method some players from a config file which are saved in an ArrayList. After that I want to do smth with them. The problem is that it says that it cannot cast offline player to online player.

    Compact piece of code:
    Code:
        @Override
        public void onEnable() {
            readPlayers();
        }
    
    ---------
        @SuppressWarnings("deprecation")
        public void readPlayers() {
                for (String string : this.getConfig().getStringList("players.openJobs")) {
                    players.add(string);
                    countWeek((Player) Bukkit.getOfflinePlayer(string));   --- There's the problem
                }
        }
    
    ---------
    
        public void countWeek(Player p) {
            running.put(p, true);
            players.add(p.getName());
                    PermissionUser user = PermissionsEx.getUser(p);
                    ... unrelevant piece of code ...
         }
    
    The error:
    Code:
    [08:48:54 ERROR]: Error occurred while enabling UtopicSystem v1.0 (Is it up to date?)
    java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.CraftOfflinePlayer cannot be cast to org.bukkit.entity.Player
            at com.Scrap.spawn.Main.readPlayers(Main.java:219) ~[?:?]
            at com.Scrap.spawn.Main.onEnable(Main.java:84) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:741) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.Bukkit.reload(Bukkit.java:535) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Thread.java:748) [?:1.8.0_131]
    Is there a method to do that otherwise?

    I hope you can help me :D
     
  2. Offline

    Caderape2

    There's a method offlineplayer.getPlayer(), but it will return null if the player is offline of course.
     
  3. Offline

    LRK

    You could ise @Caderape2 answer or you can use Bukkit#getServer#getPlayer and check if its null or not.
     
  4. Offline

    Zombie_Striker

  5. Offline

    Scrapnix

    Hey there @LRK @Caderape2 @Zombie_Striker,
    I solved the problem myself, but thx for your help. The problem is that I had to read out the names and execute a method independently of their online status. Because of that I worked simply only with their names.
    //Closed
     
Thread Status:
Not open for further replies.

Share This Page