当我尝试使用我的 MC 插件启动服务器时,它显示一个错误

时间:2021-03-02 14:05:18

标签: java plugins minecraft

所以我做了一个插件,之前有无效的plugin.yml,现在,它在启用时有问题。它向我显示了这个错误:

Error occurred while enabling MinererSMP v1.0 (Is it up to date?)
java.lang.NullPointerException: null
        at com.carter.minererplugin.commands.infocommand.<init>(infocommand.java:12) ~[?:?]
        at com.carter.minererplugin.Main.onEnable(Main.java:10) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[server.jar:git-Bukkit-3eb7236]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:343) ~[server.jar:git-Bukkit-3eb7236]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:479) ~[server.jar:git-Bukkit-3eb7236]
        at org.bukkit.craftbukkit.v1_16_R3.CraftServer.enablePlugin(CraftServer.java:480) ~[server.jar:git-Bukkit-3eb7236]
        at org.bukkit.craftbukkit.v1_16_R3.CraftServer.enablePlugins(CraftServer.java:406) ~[server.jar:git-Bukkit-3eb7236]
        at net.minecraft.server.v1_16_R3.MinecraftServer.loadWorld(MinecraftServer.java:426) ~[server.jar:git-Bukkit-3eb7236]
        at net.minecraft.server.v1_16_R3.DedicatedServer.init(DedicatedServer.java:205) ~[server.jar:git-Bukkit-3eb7236]
        at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:786) ~[server.jar:git-Bukkit-3eb7236]
        at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$0(MinecraftServer.java:155) ~[server.jar:git-Bukkit-3eb7236]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_281]

插件实际加载,可以在插件列表(/plugins)中看到,但似乎只是命令不起作用。这是 main.java:

package com.carter.minererplugin;

import org.bukkit.plugin.java.JavaPlugin;
import com.carter.minererplugin.commands.infocommand;

public class Main extends JavaPlugin {
    @Override
    public void onEnable() {
        new infocommand(this);
    }
}

和 infocommand.java:

package com.carter.minererplugin.commands;

import org.bukkit.command.CommandExecutor;
import org.bukkit.entity.Player;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import com.carter.minererplugin.Main;

public class infocommand implements CommandExecutor {
    
    public infocommand(Main plugin) {
        plugin.getCommand("info").setExecutor(this);
    }
    
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if(!(sender instanceof Player)) {
            sender.sendMessage("You can't run this command from console.");
            return true;
        }
        
        Player p = (Player) sender;
        
        if (p.hasPermission("minerer.use")) {
            p.sendMessage("&lWelcome to MinererSMP!");
            p.sendMessage("--------------------------");
            p.sendMessage("This plugin is currently under heavy development.");
        }
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

要注册命令,您的主类中需要 this.getCommand("yourcommand").setExecutor(new YourCommandClass());。不要忘记在类开头的命令类中添加implements CommandExecutor,例如:public class YourCommand implements CommandExecutor {}

另外,删除 infocommand 类中的构造函数。