Solved Access Config In Static Method

Discussion in 'Plugin Development' started by Compressions, Jul 5, 2013.

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

    Compressions

    Hey there, does anyone know of a way to access the config with getConfig() properly in a static method?
    Thanks! :)
     
  2. Offline

    hexagon

    You can store it in a static variable at your main class.
     
  3. Offline

    Ivan

    Make a static instance of your plugin, and use that.
     
  4. Offline

    Compressions

    hexagon Ivan Thanks for the help, but I've decided to completely recode my plugin as I was overusing the static keyword, and it caused limitations with Bukkit's built-in methods, so i will just be using instances of classes in my main class. However, I don't exactly understand getters and setters, so if you could help that would be fantastic! Thanks :)
     
  5. Offline

    hexagon

    Code:java
    1.  
    2. private JavaPlugin plugin;
    3.  
    4. public void setPlugin(Plugin plugin) {
    5. this.plugin = plugin;
    6. }
    7.  
    8. public JavaPlugin getPlugin() {
    9. return plugin;
    10. }


    That's it, getter and setter.
     
  6. Offline

    sayaad


    Imagine a method,
    void method(Argument arg){}

    Notice the "void" keyword. A simplified definition of "void" could be, nothing. That is, it returns nothing, it only executes code.

    Now look at a method that returns an object/primitive.
    Code:java
    1. String s(){
    2. return "s";
    3. }


    If I called that from another method that tells the player that string, it would be :
    player.sendMessage(s());

    Which would be the exact same as
    player.sendMessage("s");

    Now imagine a method in another class that returns a variable in that class :

    Code:java
    1. public class c{
    2. private String s = "s";
    3.  
    4. public void getS(){
    5. return s;
    6. }
    7. }


    If I were to do this:

    player.sendMessage(new c().getS());

    It would be the exact same as:

    player.sendMessage("s");

    In other words, getS() would return the variable called s in the class c, then send it to the player.

    For setters, it's the same.

    Code:java
    1. class c{
    2. private String s = "";
    3.  
    4. public void setS(String toSetTo){
    5. s = toSetTo;
    6. }
    7. }

    and executing c.setS("s");

    This would be the same as:

    public static String s = "";
    classThatHasThatStaticVariable.s = "s";

    However, the true beauty of the getter and the setter is that you're converting a variable into a method, and you're able to do awesome stuff like make it synchronized, which I recommend for every map/arraylist.

    Code:java
    1. class c{
    2. String s = "";
    3.  
    4. public synchronized String getS(){
    5. return s;
    6. }
    7. public synchronized void setS(String toSetTo){
    8. s = toSetTo;
    9. }
    10. }


    EDIT: Sorry for not using code tags, it takes up more space than this for 1 line lines of code.
     
    ProMCKingz and Compressions like this.
Thread Status:
Not open for further replies.

Share This Page