Solved Referencing public variables from main, in listener?

Discussion in 'Plugin Development' started by pers2981, Apr 11, 2013.

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

    pers2981

    Hey there,

    So inside my main I create a hashmap that stores an integer value.

    Code:
    public final HashMap<Player, Integer> PlayerClass = new HashMap<Player, Integer>(); 
    
    I attempt to read this from my listener class but I receive an error.

    Code:
    <MainName>.this.PlayerClass.get(Whatever) == Whatever
    
    What am I doing wrong? Am I attempting to access the hashmap wrong?
     
  2. Pass your main class' pointer to your other class through the constructor.

    Accessing it by ClassName.var means that you're requesting a static object, as opposed to instanced object which currently is.
     
  3. Offline

    pers2981

    How would I do that in code?
     
  4. Offline

    pers2981

  5. Offline

    Drew1080

    Code:
      
            private MyPlugin plugin; // pointer to your main class, unrequired if you don't need methods from the main class
     
            public AnotherClass(MyPlugin plugin) {
                this.plugin = plugin;
           
     
        }
    'AnotherClass' is the class name.


    You would then use inside AnotherClass

    Code:
    plugin.PlayerClass.get(Whatever)
     
  6. Offline

    pers2981

    I feel really stupid.

    [​IMG]

    What in gods name am I doing wrong?
     
  7. pers2981

    That's not named like the class, click "Change to constructor".
     
  8. Offline

    pers2981

    Alright?

    So I have the following.
    Code:
    private MinecraftLegend plugin; // pointer to your main class, unrequired if you don't need methods from the main class
     
        public MinecraftLegend(MinecraftLegend plugin)
        {
            this.plugin = plugin;
        }
    
    When I go into my playerLis class and try 'plugin.PlayerClass.get(whatever)'

    [​IMG]
     
  9. Offline

    Drew1080

    public MinecraftLegend(MinecraftLegend plugin)
    should be if its in your PlayerLis class
    public PlayerLis(MinecraftLegend plugin)
     
  10. Offline

    microgeek

    Make it static? Rofl
     
Thread Status:
Not open for further replies.

Share This Page