I feel like a noob but...

Discussion in 'Plugin Development' started by ColonelHedgehog, Nov 10, 2014.

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

    ColonelHedgehog

    EDIT: Am noob, I figured out I don't need to implement Player. X3

    Code:
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer cannot be cast to tk.ColonelHedgehog.Dash.API.Entity.Racer
    
    So there's the problem.
    I've made an abstract implementation of Player, called Racer. Racer is pretty much just an API class. What I tried to do is:
    Code:
                            powerup.doOnRightClick((Racer) p);
    My Racer class looks like:
    Code:
    package tk.ColonelHedgehog.Dash.API.Entity;
    import org.bukkit.entity.Horse;
    import org.bukkit.entity.Player;
    import org.bukkit.metadata.FixedMetadataValue;
    import tk.ColonelHedgehog.Dash.Core.Main;
    import tk.ColonelHedgehog.Dash.Events.PlayerMoveListener;
    /**
    * Created by ColonelHedgehog on 11/8/14.
    * You have freedom to modify given sources. Please credit me as original author.
    * Keep in mind that this is not for sale.
    */
    public abstract class Racer implements Player
    {
        public Horse getHorse()
        {
            return getVehicle() != null && getVehicle() instanceof Horse ? (Horse) getVehicle() : null;
        }
        public int getScore()
        {
            return PlayerMoveListener.evalPlace(this);
        }
        public int getLap()
        {
            return getMetadata("playerLap").get(0).asInt();
        }
        public int getMarkerPosition()
        {
            return getMetadata("markerPos").get(0).asInt();
        }
        public void setLap(int lap)
        {
            setMetadata("playerLap", new FixedMetadataValue(Main.plugin, lap));
        }
        public void setMarkerPostion(int position)
        {
            setMetadata("markerPos", new FixedMetadataValue(Main.plugin, position));
        }
        public boolean isInLine()
        {
            return getMetadata("playerInLine").get(0).asBoolean();
        }
        public Racer getRacer()
        {
            return this;
        }
    }
    
    So... if it implements Player, shouldn't it be possible to cast Player to Racer? Is there a method that one might use to convert a Player to this Racer class? I feel like the answer is right in front of my face, but I have no idea and I feel like a code-noob. >_< This may be more of a Java question than anything else.
     
  2. Offline

    Hoolean

    Nope, as the Player Object CraftBukkit is returning is CraftBukkit's own implementation of it, CraftPlayer. While a Player Object that is a Racer can be cast to Player, a Player Object that is not cannot be.

    Implementing classes cannot be cast to eachother, as essentially what you are trying to do here is cast CraftPlayer to Racer.

    EDIT: oops, missed your edit. I'll leave this here just in case anyone else has similar issues, although you are right, you shouldn't need to implement Player in a plugin
     
  3. Offline

    ColonelHedgehog

    Thanks for the detailed analysis! Now I know what not to do next time. :p
     
Thread Status:
Not open for further replies.

Share This Page