Search code examples
javabukkit

How to get variables value in another method - confused about how to proceed


I'm pretty new to Java (and first question I ask on Stackoverflow, too) and right now I'm struggling about how to pass some variables values defined in a method to another.

I've done multiple searchs about things like global variables, ArrayList, HashMap, etc, but the only thing that seems to be what I'm searching ( Global variables in Java) let me a little more confused about how to proceed.

I've try to use ArrayList for that, but it haven't work - and I don't know if I can use it for what I want to day anyways...

Here's my code:

public static void creationGuilde(String[] args, Player player, String playerName)
{
    String nomGuilde = args[2];
    String message1 = "Votre nouvelle guilde se nommera " + nomGuilde + ".";
    TextComponent confirmer = new TextComponent("Cliquez ici pour confirmer");
    TextComponent annuler = new TextComponent("cliquez ici pour annuler");
    String message2 = confirmer + "OU" + annuler + ".";
    player.sendMessage(message1);
    player.sendMessage(message2);
    confirmer.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/creationGuildeConfirmer"));
    annuler.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/creationGuildeAnnuler"));
}

private void onPreCreationGuildeCommand(PlayerCommandPreprocessEvent event)
{
    if (event.getMessage().equals("creationGuildeConfirmer"))
    {
        String guilde = CommandeGuilde.creationGuilde(nomGuilde);
        event.getPlayer().sendMessage("Félicitations! Vous venez de créer la guilde " +guilde); // <-- Here, trying to get the value of 'guilde' in the 'creationGuilde' method...
    }
}

What I want to do is from the "onPreCreationGuildeCommand" method, I want to get the 'nomGuilde' value from the "creationGuilde" method to put it on my last sendMessage.

I hope that my question is clear enough. Thanks for helping me on this.


Solution

  • The easiest and probably best solution is to define nonGuilde as global variable.
    Code should look like this:

    class YourClassName{
        //Must be null
        //otherwise if you call onPreCreationGuildeCommand before creationGuilde
        //you would get error because variable hasn't been initialized
        private String nomGuilde = null;
    
        public static void creationGuilde(String[] args, Player player, String playerName){
            //set the value
            //value will persist outside the method because variable is global
            nomGuilde = args[2];
        }
    
        private void onPreCreationGuildeCommand(PlayerCommandPreprocessEvent event){
            // Here you can do anything with variable, for example:
            System.out.println(nomGuilde);
        }
    }
    

    Also my advice to you is to read a bit about variables and their scope(block of code where specific variable is visible) and their lifetime(how long does variable exist in memory)