Solved Creating and getting an instance of a Player

Discussion in 'Plugin Development' started by MrSparkzz, Jul 30, 2014.

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

    MrSparkzz

    I have a class called "User," basically a Player class but with my own methods and data within it.

    I'm guessing that I can create a new instance of the User class every time someone logs in.

    My question is; how can I grab a specific instance of the User class?
     
  2. Offline

    Garris0n

    What do you mean? Is it a class extending Player? If not, what is it extending? What's wrong with...its...constructor?
     
  3. Offline

    MrSparkzz

    Garris0n Nice to see you again, haven't been on here in a bit.

    Basically. I have a class that has my own methods and data for example, I have a boolean variable that can tell if a player is looking in another player's inventory.

    Now if I want to check to see if a player is doing that, I want to be able to get an instance of that class for a specific player. Then check to see if that variable is set or not. (sorry if that was confusing).
     
  4. Offline

    AoH_Ruthless

    MrSparkzz
    Global Player field
    Pass a player parameter through constructor + initialize global player field
    Getters + Setters
    Boom
     
  5. Offline

    MrSparkzz

    AoH_Ruthless
    How would this work? Could you give me an example?
     
  6. Offline

    fireblast709

    MrSparkzz have a Map that maps player -> User instance. On join you put them in the Map, on quit you remove them. For offline usage, you would need to rely on OfflinePlayer objects.
     
  7. Offline

    MrSparkzz

    fireblast709
    I was thinking about something like that, but I was wondering if there was a way that was even simpler than that.

    Does anyone know how Bukkit does it with the Player class? I get very confused when I look through the source code of bukkit and plugins like essentials and factions.
     
  8. Offline

    AoH_Ruthless

    MrSparkzz
    The bukkit source may not help you as much as you think it will on this case.

    I think the following will help clear up some issues, but I am not sure what you are asking:

    Code:java
    1. // We want to create our own player-type but store other data.
    2. public class PlayerModel { // We don't need to implement Player or anything because we aren't changing any of that data; that would be just foolish. Instead we are building off the player to add more data to them.
    3.  
    4. // Because we are storing each model based on an individual player, we need a global field
    5. private Player player;
    6.  
    7. // Constructor passes a player instance to initialize the field.
    8. public PlayerModel(Player player) {
    9. this.player = player;
    10. }
    11.  
    12. // You might need to be able to retrieve the player who serves as the basis for the model.
    13. public Player getPlayer() {
    14. return player;
    15. }
    16.  
    17. public void someMethod() {
    18. // Some method you want to call for every PlayerModel, such as store data, etc..
    19. }
    20. }
     
  9. Offline

    MrSparkzz

    AoH_Ruthless
    Something I'm confused about is; how would you get a specific Player? for example if two players logged in and I wanted to get one of them, how would I do that with the code you supplied?
     
  10. Offline

    izarooni

    Create a HashMap with UUID as a key and your custom player class as the value.
    Create a PlayerJoinEvent and add the player uuid to that map aswell as a new custom player class something like this:
    Code:java
    1. plugin.map.put(e.getPlayer().getUniqueId(), new CustomPlayer());

    then if you want to access you'd create constructors that take the class container the hashmap as a paramter.
    Code:java
    1. public YourClass(Class class) {
    2. this.class = class;
    3. }

    then grab the player class instance by doing something like this:
    Code:java
    1. CraftPlayer cp = class.map.get(player.getUniqueId());

    then you'd get your methods using cp.
    Hope this helps, I'm not very good at explaining things.

    Edit:
    You'd also have to do a loop through the online players in your onEnable method and add players & custom player classes because if someone reloads the server, that HashMap will clear and you'll get a bunch of null errors.
     
  11. Offline

    MrSparkzz

  12. Offline

    xTigerRebornx

    MrSparkzz People have given you valid solutions. If you need further explanation, say so. Post the class you want to assign to a Player and what you have tried.
     
  13. Offline

    MrSparkzz

  14. Offline

    xTigerRebornx

    MrSparkzz The simple solution is the use a way of pairing the class with the Player, You could use many things.
    WeakHashMap for a Player -> Wrapper (where the Wrapper is your class for storing data), Player Metadata (not persistant), Map for a UUID -> Wrapper (for persistence). Any of the ideas would work, and I doubt could get much simpler then that (I doubt simplicity is really needed, as the above are already fairly simple)
     
    MrSparkzz likes this.
  15. Offline

    AoH_Ruthless

    MrSparkzz
    Yeah, sorry for never responding.
    This is somewhat basic Java though; you would create a new instance of the class for each Player. Then you would dispose of each class when you are done with it if you ever are to avoid memory leaks.
     
  16. Offline

    MrSparkzz

    AoH_Ruthless xTigerRebornx
    Ruthless's method was exactly what I was looking for, but I was wondering how I would call a specific instance of the class.
     
  17. Offline

    AoH_Ruthless

    MrSparkzz
    Its your code, we have no idea where you want to use it.

    Create a new instance in any class and then create a getter if you wish, store all of the instances in a list, mapping, etc..
     
    MrSparkzz likes this.
  18. Offline

    MrSparkzz

    AoH_Ruthless
    Oh, alright. Yeah, now I feel kinda dumb. Thanks everyone. The only thing I really had an issue with was getting a specific instance of a class. I couldn't find it anywhere on here, but I guess the only way is to use a list of some sort.
     
Thread Status:
Not open for further replies.

Share This Page