如何将主类的实例传递给多个侦听器? [英] How do I pass instances of the main class to multiple listeners?
问题描述
所以,我一直在研究插件.一切都很好,直到我偶然发现了一个问题.我正在使用单独的Listener(implements Listener
)类来管理事件,但是对于其中某些事件,我需要访问JavaPlugin
方法,例如获取配置文件.
So, I've been working on a plugin. It was going great, until I stumbled on an issue. I'm using separate Listener (implements Listener
) classes to manage my events, but for some of them, I need to access JavaPlugin
methods, like for getting the configuration file.
在我的侦听器类中使用下面的构造函数是有效的,但是如果我尝试在另一个类中使用相同的东西,则Eclipse会给它加上一个可爱的红色下划线,表示blank final field plugin may have not been initialized
Using the constructor below in my listener class works, but if I try to use the same thing in another class, Eclipse gives it a lovely red underline, saying blank final field plugin may have not been initialized
private final MainClass plugin;
public ListenerClass(MainClass instance){
this.plugin = instance;
}
如前所述,以上代码有效,但仅适用于一个侦听器.
当使用返回对象的方法时,构造函数以下会引发NPE.我不知道这是否是因为插件实例实际上是 返回为null
还是 if 是否没有句柄是null
.
As said before, the above code works, but only for one listener.
The constructor below throws an NPE when using a method that returns an object. I don't know if this is because the plugin instance is actually returning as null
or if there is no handle for if it is null
.
// The only difference between this constructor and the last is that the final
// modifier was removed.
private MainClass plugin;
public ListenerClass(MainClass instance){
this.plugin = instance;
}
FileConfiguration config = plugin.getConfig(); // throws NPE
// Let's pretend that the line above isn't there, and I just use the getConfig() method instead.
@EventHandler
public void onJoin(PlayerJoinEvent event){
if(plugin.getConfig().getString("Strings.cat-toy").toLowerCase().equals("string")){ // throws NPE
// do stuff
}
}
推荐答案
我做的事情与大多数人不同.假设您的类extends JavaPlugin
是Main.java
,则应在任何方法内添加以下代码:
I do something a lot different than most people do. Say your class that extends JavaPlugin
is Main.java
, you would add the following code NOT inside of any methods:
public static Main plugin;
然后将其添加到您的onEnable()
:
plugin = this
因此,您的Main
类可能看起来像这样.
So, your Main
class could look something like this.
public class Main extends JavaPlugin{
public static Main plugin; //create the variable
@Override
public void onEnable(){
plugin = this; //assign plugin to this class
//other onEnable things
}
@Override
public void onDisable(){
//onDisable things
plugin = null; //best to put this AFTER running any methods here
}
}
然后,在要使用JavaPlugin
方法的任何类中,只需使用Main.plugin
Then, in any class that you want to use the JavaPlugin
methods in, just use Main.plugin
因此,如果您想在另一个类中获取插件的配置,则可以执行以下操作:
So, if you wanted to get the config for your plugin in another class, you could do:
YamlConfiguration config = Main.plugin.getConfig();
在将plugin
分配给主文件后,请确保执行此操作.假设Handler.setup()
在其中使用YamlConfiguration config = Main.plugin.getConfig();
,并且您想在启用插件时运行该方法.您可以这样做:
Just make sure that you do this AFTER you assign plugin
to your Main file. Let's say Handler.setup()
uses YamlConfiguration config = Main.plugin.getConfig();
in it, and you want to run that method when your plugin is enabled. You could do this:
public void onEnable(){
plugin = this;
Handler.setup();
}
但是不:
public void onEnable(){
Handler.setup();
plugin = this;
}
这篇关于如何将主类的实例传递给多个侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!