-2

I'm creating a new instance of nPlayer for each user that connects to my server.

nPlayer nPlayer = new nPlayer(player);

The nPlayer class contains the following with a bunch of other methods used to grab the private variable details.

private Player player;
private boolean muted;
private boolean admin;
private boolean mod;
private boolean pvp;

public nPlayer(Player player) {
    player = player;
    muted = false;
    admin = false;
}

example of method to grab variable details within the nPlayer class.

public boolean getAdmin() {
    return admin;
}

I was wondering how do I go about grabbing say the admin variable from Nplayer of a specific user from another class?

Zander17
  • 1,894
  • 5
  • 23
  • 31

4 Answers4

1

You create an instance with

nPlayer nplayer = new nPlayer();

"nplayer" is the name of the variable, choose one you like. You have to save the data of this instance somewhere if you want to access the data upon reopening the program. If you have the instance there (in this case 'nplayer'), you access the data like that:

boolean b = nplayer.getAdmin();

I should mention that you generally write class names always with the first letter capital, that would be "NPlayer".

Exceen
  • 765
  • 1
  • 4
  • 20
  • I'll take note of capitalizing the first letter of class names thanks. How would I go about accessing the information of that instance of NPlayer outside of the method it was defined in? – Zander17 Jul 22 '14 at 23:37
  • @user3577360: That's not possible. You have to give the pointer of the variable to the method. In Java it's simply the variablename. e.g.: "void someMethod(NPlayer nplayer) { ... } – Exceen Jul 23 '14 at 00:29
0

It's sort of hard to understand exactly what your saying, but I'm assuming your asking about how to get information about the Player that is stored in nPlayer.

Get the player associated with nPlayer.

public Player getPlayer(){
    return player;
}

A way to get details about the player

public String getPlayerName(){
    return player.getName();
}

EDIT:

Let me get this right you want to be able to get a players, lets call it network state from other methods in your game. I think you are just organizing your code incorrectly. Instead of attaching a Player to nPlayer why don't you just store the network state of the player on top of the player? This way you can just load in you can just store all the information in player and be able to access it wherever you want.

Class Player {

NetworkState network;

public void connectPlayer(NetworkState network){
    this.network = network;
}
public boolean isAdmin(){
     return network.isAdmin();
}

....

}
progrenhard
  • 2,333
  • 2
  • 14
  • 14
  • Player is actually from a library that I'm using hence why I'm using my own to store my own variables. – Zander17 Jul 22 '14 at 23:44
  • @user3577360 Just extend out player the include those properties. nPlayer extends Player then use nPlayer for all your code. – progrenhard Jul 22 '14 at 23:45
0

When a user connects, create an instance of nPlayer and attach it to the session. Then when needed, just retrieve the object form the session and access its variables through the setters and getters.

How to store objects in the session: How do you store Java objects in HttpSession?

Community
  • 1
  • 1
Alexandre Santos
  • 8,170
  • 10
  • 42
  • 64
0

For starters: Do not store Player instance. Store their UUID to prevent memory leaks.

Actual solution:

You shouldn't use the class name as a field name, since the compiler might confuse a static method for the instance, and two, that's why class names start with a capital letter.

When you fix that, you can use the instance name .getAdmin() to get the boolean. You can insert that into an if check and perform something if it returns true.