Player variable

Discussion in 'Plugin Development' started by Metra, Dec 10, 2011.

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

    Metra

    Hello, it's possible to create a player variable, example :
    String myPlayerVar;
    myPlayerVar = myFunction();

    Thanks for your help.

    Cordially, Metra
     
  2. Offline

    Slamakans

    Wait what, I don't even...

    Please rephrase and explain more in-depth about what you want to happen.
     
  3. Offline

    Evangon

    I think he/she is trying to say if it's possible to set a string value to a function...?
     
  4. Offline

    DomovoiButler

    like
    Code:
    String var = getVar(playerName);
    
    public String getVar(Player name){
    
    }
    ????
     
  5. You need to learn more java. Regardless, here are some answers to different questions that you are asking
    Ways to get the Player object
    In a command:
    Code:java
    1.  
    2. if (sender instanceof Player) {
    3. Player player = (Player) sender;
    4. }
    5.  

    From a player driven event:
    Code:java
    1.  
    2. Player player = event.getPlayer();
    3.  

    From a string:
    Code:java
    1.  
    2. Bukkit.getServer().getPlayer(playerName);
    3.  

    How to return a value from a method
    So, this is basic Java, and doesn't require Bukkit. Lets take this scenario:
    Theres a class, named Human. The constructor is:
    Code:java
    1.  
    2. private String name;
    3.  
    4. public Human(String name) {
    5. this.name = name;
    6. }
    7.  

    You need a way to get the human's name from another class, since it is private. The method to get the players name would look something like this:
    Code:java
    1.  
    2. public String getName() {
    3. return name;
    4. }
    5.  

    Key points:
    We replace void with the type of object we would like to return.
    At the end of the method we must return a value.

    Now, what if we sometimes don't want to return a value? In this method, if a random number is greater than 5 we will return null (Which you should already know about)
    Code:java
    1.  
    2. public String getName() {
    3. if (new Random().nextInt(10) > 5) {
    4. return null;
    5. }
    6. return name;
    7. }
    8.  

    Thats about it! Hope it helped :)
     
  6. Offline

    DomovoiButler

    i think his not asking for a name of the player...if it was, even a noob can figure that out
    instead he said Variable..not name
    more def: my theory is he is trying to get a string of a specific player using his own function
    but he doesnt know how to do that...simple as making a function called
    public String hisFunction(Player playerName){
    }
     
  7. Offline

    Metra

    Thanks for you help, I will test
     
  8. Tell us how it worked :)
     
Thread Status:
Not open for further replies.

Share This Page