简体   繁体   English

我想在我的主类/Java minecraft Paper 插件上使用我的其他类包括监听器

[英]I want to use my other class includes listener on my main class / Java minecraft Paper plugin

  • I use gradle for build, Java, minecraft, Paper plugin我使用 gradle 构建、Java、minecraft、Paper 插件
  • ctrl+f , figure out " the problem " ctrl+f ,找出“问题”

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.

相关问题 (Java) 如何在我的插件中使用 minecraft 原始命令? - (Java) How can I use minecraft original commands in my plugin? 我想创建一个类来创建文件,并使用主类检查该文件是否已创建,但是我的代码失败。 (Java) - I want to create a class to create a file, and use main class to check if that file is created, but my code fail. (Java) 从Java中的其他类引用我的主类的实例 - Referencing the instance of my main class from other class in java 在 Minecraft bukkit 插件中找不到主要 class - Cannot find main class in Minecraft bukkit plugin 找不到主类-Minecraft插件 - Cannot find main class - Minecraft plugin 我想在我的ActionListener类中使用一个对象 - I want to use an object in my ActionListener Class 为什么我不必导入我刚刚在我的主要 class 中使用它的 class? (爪哇) - Why don't I have to import a class I just made to use it in my main class? (Java) 如何在主类中访问Java中的类 - How do I access a Class in Java in my main class 我想在 Java 中的另一个 class 上使用来自一个 class 的变量 - I want to use my variable from one class on another class in Java 如何从Action Listener类中的For循环中获取值(int)以在主类中使用? - How to get the value(int) from a For Loop inside a Action Listener class to use in my main class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM