Solved Creating a new "Player" interface

Discussion in 'Plugin Development' started by Webbeh, Oct 22, 2014.

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

    Webbeh

    Hi.

    I'm creating a small personnalized plugin that check player bans, forum access code and a lot of other stuff.

    I was willing to add new methods and variables to all my players, and started by creating a new interface, like this :
    Code:
    package net.raidstone.includes;
     
    public interface Player extends org.bukkit.entity.Player {
        boolean checkBanned();
        void setDebug(boolean val);
        boolean isDebug();
        void debug(String message);
        org.bukkit.entity.Player getPlaye();
    }
    
    And also created a class implementing this interface.

    Code:
    package net.raidstone.includes;
     
    import net.raidstone.RaidStone;
    import org.bukkit.ChatColor;
     
    public abstract class PlayerClass implements Player {
     
        public final String prefixdebug = ChatColor.DARK_GRAY + "[DBG] " +ChatColor.GRAY;
        Integer kills=0;
        Integer deaths=0;
        Integer mvps=0;
        Integer traps=0;
        Integer poIntegers=0;
        boolean debug=false;
     
        public Player PlayerClass(Player p)
        {
            return p;
        }
        @Override
        public void setDebug(boolean val) {
            this.debug=val;
        }
        @Override
        public boolean isDebug() {
            return this.debug;
        }
        @Override
        public void debug(String message) {
            if(this.debug)
            {
                this.sendMessage(prefixdebug+""+message);
            }
        }
    }
    
    ...BUT, I'm experimenting troubles actually casting any Player or CommandSender type to this.

    I tried this :

    Code:
    org.bukkit.entity.Player pl = (org.bukkit.entity.Player) sender;
    Player p = (Player) pl;
    
    from inside my main class, but it gave me a "cannot cast" error :
    Code:
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer cannot be cast to some.package.includes.Player
    

    I KNOW I could use other ways, like creating a new personal MyPlayer type that extends Player type, or using hashmaps, or metadata, or whatever, but I would really like to know if this is doable, and what I'm missing.
     
  2. Offline

    fireblast709

    Webbeh just create a class that holds that data, and link instances of that class using a Map.
     
  3. Offline

    Webbeh

    As previously told, I do not want to use hashmaps, metadata, or a "MyPlayer" class.
     
  4. Offline

    fireblast709

    Webbeh override the bukkitEntity field in EntityPlayer with an instance that extends CraftPlayer (in order to prevent CraftBukkit from breaking)
     
  5. Offline

    Webbeh

    fireblast709 I'm not sure how to implement that exactly, you got an example ? I should have specified that I'm pretty newcomer in the java language and learned everything from coding a few smaller bukkit plugins.

    From what I understood :
    create a new class that extends org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer (although I use Bukkit.jar and not CraftBukkit.jar)

    and override the bukkitEntity field ? How ?
     
  6. Offline

    fireblast709

    Webbeh it is a field in net.minecraft.server.<version>.Entity. You will need reflection to replace it. And yes, for the CraftPlayer class you will need to use Bukkit, there is no pure Bukkit method to achieve what you are trying to achieve.
     
  7. Offline

    Webbeh

    Okay, well, that's more or less what I wanted to know.

    Also, since it's version-specific, I guess there is no way to have my code NOT be broken at every update... So I think I'm going to simply create a new MyPlayer class, which is the best of the other available options for me.

    Thanks for the tip.
     
Thread Status:
Not open for further replies.

Share This Page