Solved Casting issues

Discussion in 'Plugin Development' started by TheFl4me, Jun 21, 2015.

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

    TheFl4me

    Hi so I am coding my own Player class (ePlayer) but i am having issues casting to the bukkit craftplayer.

    example:

    Code:
    @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            ePlayer p = (ePlayer) e.getPlayer();
    and i get this error:

    Code:
    [21:48:52 ERROR]: Could not pass event PlayerJoinEvent to eAdmin v2.0.5
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at net.minecraft.server.v1_8_R3.PlayerList.onPlayerJoin(PlayerList.java:282) [craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at net.minecraft.server.v1_8_R3.PlayerList.a(PlayerList.java:142) [craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at net.minecraft.server.v1_8_R3.LoginListener.b(LoginListener.java:115) [craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at net.minecraft.server.v1_8_R3.LoginListener.c(LoginListener.java:53) [craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at net.minecraft.server.v1_8_R3.NetworkManager.a(NetworkManager.java:222) [craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at net.minecraft.server.v1_8_R3.ServerConnection.c(SourceFile:168) [craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:748) [craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:632) [craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:540) [craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        at java.lang.Thread.run(Thread.java:745) [?:1.7.0_71]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer cannot be cast to elite.minecraft.plugin.admin.ePlayer
        at elite.minecraft.plugin.admin.listeners.JoinQuitEventsListener.onPlayerJoin(JoinQuitEventsListener.java:16) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_71]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_71]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_71]
        at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_71]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[craftbukkit-1.8.6.jar:git-Bukkit-e8c6403]
        ... 14 more
    
     
  2. Offline

    sam_0208

    You can just do Player p = e.getPlayer();
     
  3. Offline

    TheFl4me

    no i need it to be ePlayer for other stuff
     
  4. Offline

    sam_0208

    Oh i am sorry read it to fast
     
  5. Offline

    1Rogue Retired Staff

    And you can't cast it, because a Player is not an ePlayer. Store your information for the player in your ePlayer class, and use a map (with the player's UUID as the key) to store it.
     
  6. Offline

    JBoss925

    I'm assuming your "ePlayer" class has a player attribute. Just use a constructor and use the player from the event in the constructor.
     
  7. Offline

    teej107

    @TheFl4me the reason why casting works is because of inheritance. Your ePlayer has no relation to CraftPlayer
     
  8. Offline

    Zombie_Striker

    @TheFl4me
    Make a simple constructor like so:
    Your event
    Code:
    ePlayer p = new ePlayer(Player);
    You ePlayer class:
    Code:
    Player player;
    
    public ePlayer(Player p){
    this.player = p;
    }
     
  9. Offline

    xTrollxDudex

    Delegate instead
     
  10. Offline

    TheFl4me

    Thank you thats exactly what i needed simple, short, effective thanks alot :)
     
Thread Status:
Not open for further replies.

Share This Page