在实例上调用方法而不是不使用静态类型的类

时间:2015-07-06 22:08:59

标签: java static minecraft bukkit

我目前正在尝试创建一个插件,我可以跨越一个不会让我调用方法的错误,因为它需要对方法中发生的事情保持静态。

我通过使用package.Main main = new package.Main();来修复此问题。

现在,我留下的问题是当我实际调用该方法时,使用main.method();它表示上面的代码需要是静态的,但是当我这样做时,它会在控制台中返回此错误。

org.bukkit.command.CommandException: Unhandled exception executing command 'devmode' in plugin DevelopmentMode v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-e91aed8]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit.jar:git-Bukkit-e91aed8]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:621) ~[craftbukkit.jar:git-Bukkit-e91aed8]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1079) [craftbukkit.jar:git-Bukkit-e91aed8]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:939) [craftbukkit.jar:git-Bukkit-e91aed8]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:37) [craftbukkit.jar:git-Bukkit-e91aed8]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:9) [craftbukkit.jar:git-Bukkit-e91aed8]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit.jar:git-Bukkit-e91aed8]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_71]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_71]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [craftbukkit.jar:git-Bukkit-e91aed8]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:676) [craftbukkit.jar:git-Bukkit-e91aed8]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit.jar:git-Bukkit-e91aed8]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:632) [craftbukkit.jar:git-Bukkit-e91aed8]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:540) [craftbukkit.jar:git-Bukkit-e91aed8]
        at java.lang.Thread.run(Unknown Source) [?:1.7.0_71]
Caused by: java.lang.ExceptionInInitializerError
        at package.commands.devModeClickCommands.Main(devModeClickCommands.java:22) ~[?:?]
        at package.commands.devMode.onCommand(devMode.java:60) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-e91aed8]
        ... 15 more
Caused by: java.lang.IllegalArgumentException: Plugin already initialized!
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:98) ~[craftbukkit.jar:git-Bukkit-e91aed8]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[craftbukkit.jar:git-Bukkit-e91aed8]
        at package.devmode.Main.<init>(Main.java:12) ~[?:?]
        at package.commands.activateDevMode.<clinit>(activateDevMode.java:20) ~[?:?]
        at package.commands.devModeClickCommands.Main(devModeClickCommands.java:22) ~[?:?]
        at package.commands.devMode.onCommand(devMode.java:60) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-e91aed8]
        ... 15 more
Caused by: java.lang.IllegalStateException: Initial initialization
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:101) ~[craftbukkit.jar:git-Bukkit-e91aed8]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[craftbukkit.jar:git-Bukkit-e91aed8]
        at package.devmode.Main.<init>(Main.java:12) ~[?:?]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)~[?:1.7.0_71]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)~[?:1.7.0_71]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.7.0_71]
        at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.7.0_71]
        at java.lang.Class.newInstance(Unknown Source) ~[?:1.7.0_71]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:52) ~[craftbukkit.jar:git-Bukkit-e91aed8]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:129) ~[craftbukkit.jar:git-Bukkit-e91aed8]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:328) ~[craftbukkit.jar:git-Bukkit-e91aed8]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) ~[craftbukkit.jar:git-Bukkit-e91aed8]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugins(CraftServer.java:288) ~[craftbukkit.jar:git-Bukkit-e91aed8]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:719) ~[craftbukkit.jar:git-Bukkit-e91aed8]
        at org.bukkit.Bukkit.reload(Bukkit.java:543) ~[craftbukkit.jar:git-Bukkit-e91aed8]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) ~[craftbukkit.jar:git-Bukkit-e91aed8]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit.jar:git-Bukkit-e91aed8]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:621) ~[craftbukkit.jar:git-Bukkit-e91aed8]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:607) ~[craftbukkit.jar:git-Bukkit-e91aed8]
        at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:371) ~[craftbukkit.jar:git-Bukkit-e91aed8]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:336) ~[craftbukkit.jar:git-Bukkit-e91aed8]
        ... 3 more

任何帮助都会很棒,谢谢!

修改 - 代码示例

我尝试调用的方法是在我的Main类中的一个单独的包中:

public void newCountdown() {
    System.out.print("I MADE IT!");
    this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
        public void run() {
            for (Player player : Bukkit.getServer().getOnlinePlayers()) {
                player.sendMessage("Hey");
            }
        }                   
}, 0, 20);
}

在这里,我试图从另一个类,在不同的包中调用上面的方法。

public class class {

    package.Main main = new package.Main();

    public static void activate(Player player) {
        main.newCountdown();
    }

1 个答案:

答案 0 :(得分:0)

基于此行Caused by: java.lang.IllegalArgumentException: Plugin already initialized!,插件已在其他位置初始化,因此此调用将是多余的。查看bukkit论坛中的this thread以获取更详细的说明。