Java 龙头:三个 类 不合作

Java Spigot: three classes not cooperating

假设我有三个 classes:GUI、OpenGui 和 BanGUIMain。 图形用户界面:

public class GUI {

    private BanGUIMain main = new BanGUIMain(); /*line 16*/
    public void createOptionsGUI(Player p) {}
}

OpenGui:

public class OpenGui implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (sender instanceof Player) {
            if (sender.hasPermission("bangui.opengui")) {
                GUI gui = new GUI(); /*line 14*/
                gui.createOptionsGUI((Player) sender);
            }
        }
        return false;
    }
}

BanGUIMain:

public final class BanGUIMain extends JavaPlugin { /*this is line 12*/
// much stuff that does not matter
}

好吧,我正在编写一个 Spigot 插件。基本上,当我调用 /opengui 命令(我注册了它)时,我得到这个错误:

[16:29:55 ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing command 'opengui' in plugin BanGUIMain v1.0.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[server.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[server.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[server.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [server.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [server.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [server.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [server.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils.run(SourceFile:13) [server.jar:git-Spigot-db6de12-18fbb24]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_271]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_271]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [server.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [server.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [server.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [server.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [server.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_271]
Caused by: java.lang.IllegalArgumentException: Plugin already initialized!
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:122) ~[server.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[server.jar:git-Spigot-db6de12-18fbb24]
        at me.roughlyunderscore.plugins.premium.bangui.main.BanGUIMain.<init>(BanGUIMain.java:12) ~[?:?]
        at me.roughlyunderscore.plugins.premium.bangui.main.gui.GUI.<init>(GUI.java:16) ~[?:?]
        at me.roughlyunderscore.plugins.premium.bangui.main.commands.OpenGui.onCommand(OpenGui.java:14) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[server.jar:git-Spigot-db6de12-18fbb24]
        ... 15 more
Caused by: java.lang.IllegalStateException: Initial initialization
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:125) ~[server.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[server.jar:git-Spigot-db6de12-18fbb24]
        at me.roughlyunderscore.plugins.premium.bangui.main.BanGUIMain.<init>(BanGUIMain.java:12) ~[?:?]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_271]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_271]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_271]
        at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_271]
        at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_271]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[server.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[server.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329) ~[server.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) ~[server.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugins(CraftServer.java:292) ~[server.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:198) ~[server.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:525) ~[server.jar:git-Spigot-db6de12-18fbb24]
        ... 1 more

大概是这样:

  1. 调用 opengui 命令将转到主要 class BanGUIMain,它会在其中找到已注册的 OpenGui。
  2. 转到 OpenGui,在那里找到新的 GUI() 行。
  3. 转到 GUI class,在那里找到新的 BanGUIMain() 行并可能尝试再次注册命令或其他。

    好吧,我的代码无论如何都不起作用。可能它只是循环遍历所有 classes。但为什么我不能那样做?使用完后我应该做一个反“new GUI()”吗?我对 Java.
  4. 很陌生

经过搜索,您可能遇到了与this相同的问题。在那里,它被告知删除它正在创建 main class.

的新实例的代码段

根据您的情况,尝试在您的 GUI 中删除此部分 class:

private BanGUIMain main = new BanGUIMain();

服务器正在返回 Plugin already initialized! 错误,因为当插件 class 加载程序已经加载它时插件正在加载它的主要 class,这就是为什么你应该删除代码上面提到了


编辑:

如果你想访问 main class 中的方法和变量,你可以通过在你的 GUI class 中声明一个 main class 的字段变量并赋值来实现或者在构造函数中初始化它的值,像这样:

public class GUI {

    BanGUIMain plugin;
    
    GUI(BanGUIMain plugin) {
        this.plugin = plugin;
    }
}

然后,如果你想在你的主要 class 中使用它,你可以这样做:

GUI gui = new GUI(this);

但是在您的代码中,您使用了 OpenGui 中的 GUI class,因此您还需要在 OpenGui 中添加一个构造函数 class:

public class OpenGui implements CommandExecutor {

    BanGUIMain plugin;

    OpenGui(BanGUIMain plugin) {
        this.plugin = plugin;
    }

    /* few lines skipped */

    GUI gui = new GUI(plugin);

}