[英]I want to use my other class includes listener on my main class / Java minecraft Paper plugin
I made each of these plugins separately.我分别制作了每个插件。 Now, I am putting them together into one plugin but separating each class.
现在,我将它们放在一个插件中,但将每个类分开。 Then, this error occurred.
然后,发生了这个错误。 I want to solve this problem.
我想解决这个问题。 I thought
third_function.third_listener
could be used for that place, but It seems to be worng...我以为
third_function.third_listener
可以用于那个地方,但它似乎已经磨损了......
package com;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.GameRule;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
public class mainclass extends JavaPlugin implements Listener {
@Override
public void onEnable() {
getLogger().warning("Server started.");
first_function first = new first_function(this);
first.onEnable();
second_function second = new second_function(this);
second.onEnable();
third_function third = new third_function(this);
third.onEnable();
PlayerJoinEvent third_listener;
third.onPlayerJoin(third_function.third_listener); // the problem. please help me, how can I get rid of this error?
}
}
class first_function implements Listener {
private final mainclass plugin;
first_function(mainclass plugin) {
this.plugin = plugin;
}
void onEnable() {
this.plugin.getLogger().warning("Hello, world!");
}
}
class second_function implements Listener {
private final mainclass plugin;
second_function(mainclass plugin) {
this.plugin = plugin;
}
void onEnable() {
World world = this.plugin.getServer().getWorld("world");
world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, false);
}
}
class third_function implements Listener {
private final mainclass plugin;
third_function(mainclass plugin) {
this.plugin = plugin;
}
Listener third_listener = (this);
void onEnable() {
this.plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
@EventHandler
void onPlayerJoin(PlayerJoinEvent event) {
event.getPlayer().sendActionBar(Component.text("Welcome",(NamedTextColor.BLUE)));
}
}
You should register the listener and not try to call the method yourself.您应该注册侦听器,而不是尝试自己调用该方法。 For example:
例如:
@Override
public void onEnable() {
third_function third = new third_function(this);
getServer().getPluginManager().registerEvents(third, this);
}
Also, I mostly suggest you to use global java names convention, such as name ThirdFunction
instead of third_function
for example另外,我主要建议您使用全局 java 名称约定,例如名称
ThirdFunction
而不是third_function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.