getConfig in Custom Method

Discussion in 'Plugin Development' started by infopaste, Mar 24, 2014.

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

    infopaste

    Hello,

    [​IMG]



    [​IMG]

    How do I getConfig in a custom method?
     
  2. Offline

    ImPhantom

    infopaste
    What is the error when you hover over the getConfig();
     
  3. Offline

    sockmonkey1

    You have to get the config from your plugin, which means that you have to get it from the main class where it extends java plugin.

    In your main class add this method and add a field for Plugin plugin.
    Code:java
    1. public class MainClass {
    2. private static MainClass plugin;
    3. ...
    4. }

    Code:java
    1. public static Plugin getPlugin() {
    2. JavaPlugin plugin = Main.plugin;
    3. return plugin;
    4. }


    and this in the onenable

    Code:java
    1. @Override
    2. public void onEnable() {
    3. plugin = this;


    When you want to use getconfig do MainClass.getPlugin.getConfig(...);
     
  4. Offline

    infopaste

    sockmonkey1
    The method I posted above are already located in the main class
     
  5. It's "getConfig()" not "getConfig" .
     
  6. Offline

    infopaste

    DanyBv sorry I ment getConfig() .


    I have updated the post with the error!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  7. That is your main class?
     
  8. Offline

    infopaste

  9. Then try "this.getConfig().getInt(...)".
     
  10. Offline

    infopaste

    DanyBv
    Just finish tried that but it says: Cannot use this in a static context
     
  11. Offline

    Zach_1919

    infopaste How have you all not gotten this yet? It's simple: The custom method you made has the static modifier. The getConfig() method is not static, so you cannot use it in a static method. You must remove static in order for it to work.
     
  12. Offline

    infopaste

    Zach_1919
    But I need it static in order for this too work:
    Code:java
    1. public static void showPlayers(Player pl) {
    2. for (Player p : Bukkit.getOnlinePlayers()) {
    3. pl.showPlayer(p);
    4. }
    5. try {
    6. handleDye(pl);
    7. } catch (Exception ignored) {
    8. }
    9. resetDye(pl);
    10. }
     
  13. Offline

    xGhOsTkiLLeRx

    Whats the point that you want to use static methods?
    Any benefit that the method is available for all instances?

    Suggestion:
    Unless you do not need a static method, remove all static modifiers.
     
  14. Offline

    Zach_1919

    infopaste The only reason you need that method to be static is because your handleDyes() is static, so you cannot access that in a non-static method. Make that one non-static as well and everything will be fine.

    I understand that you might want it to be static so that you can do something like:
    Code:java
    1.  
    2. MainClass.handleDyes();
    3.  

    Obviously, it is nicer to do that, but Bukkit did not make their API with static compatibility. All of the JavaPlugin stuff is non-static, so if you want to use it, all your stuff must be non-static. Sorry :p If you want to use your methods in another class, put this constructor and this variable in the new class:
    Code:java
    1.  
    2. Main plugin;
    3.  
    4. public NewClass(Main m)
    5. {
    6. plugin = m;
    7. }
    8.  

    And then put this in your main class:
    Code:java
    1.  
    2. NewClass NewClass = new NewClass(this);
    3.  

    Then in your NewClass you can access your methods like this:
    Code:java
    1.  
    2. plugin.handleDyes();
    3.  
     
  15. infopaste In the event that you do need your method to be static, you could always use a getter to call the getConfig() method, as sockmonkey1 was trying to demonstrate. (Yes, you could also use the instance to call a non-static version of your method. Would work either way)
     
Thread Status:
Not open for further replies.

Share This Page