Help with Custom Player Class

Discussion in 'Plugin Development' started by PandazNWafflez, May 20, 2012.

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

    PandazNWafflez

    I want to create a custom player class. I have done this so far:

    • Made a LcPlayer interface, it extends SpoutPlayer (SpoutPlayer extends Player).
    • Made a LcCraftPlayer class, it extends SpoutCraftPlayer. It has a constructor that calls super.
    What I need to know is how to use it properly. I haven't looked thoroughly, but I presume a new instance of the Player class is called whenever a player joins. How do I make it so there is a new instance of LcPlayer / LcCraftPlayer along with Player / CraftPlayer and SpoutPlayer / SpoutCraftPlayer?
    This may be a bit of a complicated question, because not many people make custom player classes.
     
  2. Offline

    Njol

    Why do you need a custom player class?
     
  3. Offline

    PandazNWafflez

    For custom player methods. This can't be done any other way without a LOT of code. I just need it to work in the way SpoutPlayer / SpoutCraftPlayer does.

    I also know that NoCheatPlus uses a custom player class, so it's not just Spout.
     
  4. Offline

    blackwolf12333

    You might just look at there code, and if it isn't open source you just decompile it:p
    greetz blackwolf12333
     
  5. Offline

    PandazNWafflez

    Indeed, actually it is open source. But their code confuses me, I'm not sure how much of it I need, as they add a lot of other custom things as well. I kinds need to know how to do it as simply as possible without adding a lot of things that won't end up being necessary.

    This is still not solved.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  6. Offline

    Njol

    You do know that you can easily replace any instance method (e.g. player.method(args)) with a static method (e.g. PlayerFuncs.method(player, args))? Thus as long as you don't have to override any methods you don't need to make subclasses.
     
  7. Offline

    PandazNWafflez

    I do have to override a small amount, but still some.

    Would it work if I just used Player as normal then cast it to my player class when wanting to use it?
     
  8. Offline

    Njol

    No, that does not work.

    Which methods do you want to override exactly? I'd first try to rule out any possibility not to use a custom player class.

    If it is simply not possible without a custom class, I guess you have to listen to PlayerJoinEvent (or similar) and replace the player instance in Bukkit. By quickly looking at CB's source I found the following:
    Code:
            for (Object obj : server.getHandle().players) {
                EntityPlayer player = (EntityPlayer) obj;
                if (player.name.equalsIgnoreCase(getName())) {
                    return (player.netServerHandler != null) ? player.netServerHandler.getPlayer() : null;
                }
            }
    Instead of returning the player you would want to set the player of course.
     
  9. Offline

    PandazNWafflez

    Mind telling me which class that's in so I can have a more detailed look?
     
  10. Offline

    Njol

    This snipplet is from CraftOfflinePlayer.getPlayer(), and CraftServer.getOnlinePlayers() is similar.
     
  11. Offline

    nisovin

    As far as I can tell, NoCheatPlus just uses a wrapper class, they don't actually extend Player.
     
Thread Status:
Not open for further replies.

Share This Page