Solved How to use getConfig() not from the main class? (event listeners classes)

MkdirGit

New member
Jul 2, 2023
10
2
1
3
Israel
I'm stuck with getConfig() on listeners class.
how to fix this?

Java:
package example.example_for_u.JoinEvent;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.Plugin;


public class JoinEvent implements Listener {

   Plugin MyPlugin = Bukkit.getPluginManager().getPlugin("example_for_u");


    @EventHandler(priority = EventPriority.HIGHEST)
    public void onPlayerJoin(PlayerJoinEvent event) {


         Player JoinedPlayer = event.getPlayer();

        MyPlugin.getConfig().getBoolean("exampleBoolean")
    }
}

// how to get config from another class not from the main class?
 

Machine Maker

Paper Developer
Staff member
Dec 17, 2021
132
6
19
18
California
You create a constructor in JoinEvent that takes an instance of your main plugin and then assign it to a field in JoinEvent. Then when you register the event in your main class' onEnable method, you can pass this to the constructor for JoinEvent.
 

MkdirGit

New member
Jul 2, 2023
10
2
1
3
Israel
I found this more usefull :)
Java:
public class PluginName extends JavaPlugin {

public static PluginName instance;

@Override
public void onEnable() {
// Plugin shutdown logic
    instance = this;
}

@Override
public void onDisable() {
// Plugin shutdown logic

}

Source:

Thanks for the help anyway!
 

Machine Maker

Paper Developer
Staff member
Dec 17, 2021
132
6
19
18
California
While that does work, it really violates a core concept about object-oriented programming. Using static to store state like that can lead to issues if someone uses the unsupported reload command. It's highly recommended to stick to using objects as the language intended and pass state around through constructors (or more complicatedly, with dependency injection).