Calling the method on an instance rather than the class not working with static type

那年仲夏 提交于 2019-12-25 10:01:38

问题


I am currently trying t create a plugin and I have can across an error that wont let me call a method because it needs to be static for something that happens in the method.

I sort of fixed this problem by using: package.Main main = new package.Main();.

Now, the problem I am left with is that when I actually call the method, using main.method(); it says that the code above needs to be static however when I do this, it then returns this error in console.

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

Any help would be awesome, thanks!

Edit - Code Samples

The method I'm trying to call is in my Main class in a separate package:

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);
}

Here, I am trying to call the method above from a different class, in a different package.

public class class {

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

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

回答1:


Based on this line Caused by: java.lang.IllegalArgumentException: Plugin already initialized! the plugin is already initialized somewhere else so this call would be redundant. Check out this thread in the bukkit forum for a more detailed explanation.



来源:https://stackoverflow.com/questions/31256521/calling-the-method-on-an-instance-rather-than-the-class-not-working-with-static

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!