Implementing Player, creating a CustomPlayer class

Discussion in 'Plugin Development' started by Laserhog, Aug 20, 2012.

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

    Laserhog

    Hi,

    I've been reading a few posts here about implementing Player in the hopes of creating a custom Player class. Some people are saying it cannot be done, while others say to use "reflection", and others giving ways to do it that will break with each update, and so on. I personally have tried simply implementing it and am receiving "cannot cast" error messages. I have not given "reflection" a go yet as I have no clue what it is and where to begin, and am reluctant to try the methods that will break with each update.

    Does anyone out there have any "easier" and more stable ways to achieve the same result? All I want to do is store more information about the player (which in my case is a HeroClass, health, mana, etc). I have an idea for it but would like to see if anyone else has a better grasp of this problem and has something they might offer.

    Thanks :)
     
  2. Offline

    escortkeel

    You aren't going to want to extend Player to do this. Its a very, super, super, super, dooper, bad idea. :p

    Instead, create a custom class along the lines of PlayerInfo, which contains all required info. Then, use ConfigurationSerializable and ConfigurationSerialization to save the classes in a config file.
     
  3. Offline

    Laserhog

    I didn't extend Player, I implemented it.

    Making a class "PlayerInfo" is exactly what I did :D My idea was to store them in a static HashMap next to their respective Player. However, what exactly are ConfigurationSerializable and ConfigurationSerialization?

    Also, thanks for the prompt reply :)
     
  4. Offline

    escortkeel

    np! :D

    You might want to check out my SkyBukkit plugin where I did exactly this ;). Specifically:

    https://github.com/escortkeel/SkyBu.../java/me/escortkeel/skybukkit/PlayerInfo.java
    https://github.com/escortkeel/SkyBu...escortkeel/skybukkit/SkyBukkitPlugin.java#L67
    https://github.com/escortkeel/SkyBu...scortkeel/skybukkit/SkyBukkitPlugin.java#L216
    https://github.com/escortkeel/SkyBu...scortkeel/skybukkit/SkyBukkitPlugin.java#L234

    What I am basically doing there is:

    • Creating a PlayerInfo class.
    • It implements configuration serializable (and therefore has a constructor which takes a map and a method called serialize)
    • I call
    Code:
    ConfigurationSerialization.registerClass(PlayerInfo.class);
    • I use data.set("nameofconfigentry", theHashMapWhichContainsThePlayerInfoClass) to save
    • I use:
    Code:
    if (data.getConfigurationSection("players") != null) {
                players = new HashMap(data.getConfigurationSection("players").getValues(true));
            } else {
                players = new HashMap();
            }
    to load the config file (where data is my custom YamlConfiguration file.

    If you need any more help, I'd be happy to comply. ;)

    ~Keeley
     
    Laserhog likes this.
  5. Offline

    Laserhog

    So is the Serialization only really needed if you want to actually STORE the data into a YAML config file? So if I wanted to only keep the data active until the server was closed/reset a simple HashMap would suit it fine? That being said, I would definately see this being used in what I'm doing, just want to clarify this :D
     
  6. Offline

    escortkeel

    Yes you are correct. :)

    One final word of advice:

    Use a HashMap to map a String to a PlayerInfo, not a Player to a PlayerInfo. It will make your life a hell of a lot easier. ;)

    P.S. You don't have to implement Player in the PlayerInfo. :)
     
  7. Offline

    Laserhog

    String instead of Player, will remember that.

    Haha, yea ik not to implement Player in PlayerInfo. Otherwise I would just be running into the same problems as before xD

    Thanks a bunch mate. :D
     
  8. Offline

    escortkeel

Thread Status:
Not open for further replies.

Share This Page