【问题标题】:Minecraft plugin error java.lang.NullPointerException: null我的世界插件错误 java.lang.NullPointerException: null
【发布时间】:2019-11-19 15:26:17
【问题描述】:

我最近开始制作 Minecraft 插件,但出现了一些错误,例如此链接 https://paste.ubuntu.com/p/yHs2pQWf8t/

主要

package org.devoflua.hello;

import org.bukkit.plugin.java.JavaPlugin;
import org.devoflua.hello.commands.HelloCommand;

public class Main extends JavaPlugin {

    @Override
    public void onEnable() {
        System.out.print("Okie");
        new HelloCommand(this);
    }
}

命令

package org.devoflua.hello.commands;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.devoflua.hello.Main;

public class HelloCommand implements CommandExecutor {

    @SuppressWarnings("unused")
    private Main plugin;

    public HelloCommand(Main plugin) {
        this.plugin = plugin;
        plugin.getCommand("hello").setExecutor(this);
    }

    @Override
    public boolean onCommand(CommandSender Sender, Command Command, String label, String[] arg) {

        if (!(Sender instanceof Player)) {
            Sender.sendMessage("Only senders can use this command");
            return true;
        }

        Player p = (Player) Sender;

        if (p.hasPermission("hello.use")) {
            p.sendMessage("hi");
            return true;
        } else {
            p.sendMessage("You do not have permission to send this message");
        }

        return false;
    }
}

我相信错误来自第 16 行的命令类。 我在互联网上搜索,但没有发现任何东西可以帮助我解决这个问题。

【问题讨论】:

  • What is a NullReferenceException, and how do I fix it? 的可能重复第 16 行只有两件事可以为空:pluginplugin.getCommand("hello")
  • 首先,您应该尊重变量/参数命名等约定。此外,如果该命令未在plugin.yml 中注册,则可能会发生该错误,并且我不会让该类将自身设置为executor 到命令hello。所以你应该在主课上这样做。

标签: minecraft bukkit


【解决方案1】:

因此,您必须在此处解决一些问题,因为这将无法正常工作。

首先,您必须使用setExecutor() 而不是创建它的新实例。 这可以通过将其添加到您的 onEnable() 来完成:

this.getCommand("mycommand").setExecutor(new CommandKit());

您还必须在plugin.yml 中指定此命令,这里有一些关于它的文档: https://www.spigotmc.org/wiki/plugin-yml/

然后你可以删除你的 `HelloCommand()',你将不再需要它了。

这样你的错误应该会消失,如果你仍然有一些错误出现,请告诉我。

【讨论】:

  • 他设置了 Executor... 在构造函数中他的 HelloCommand-Class 里面有 plugin.getCommand("hello").setExecutor(this); 所以在我看来他没有做错任何事。他的问题真的可能是他没有在他的plugin.yml中注册命令
  • 是的,我知道,但在 Main 类中工作是一种偏好。可能在plugin.yml是的
【解决方案2】:

NullPointerException 意味着在您的代码的某个点,一个对象或其他任何东西都是“null”。让我为您的主类和命令类提供一个良好的结构。

主类:

package org.devoflua.hello;

import org.bukkit.plugin.java.JavaPlugin;
import org.devoflua.hello.commands.HelloCommand;

public class Main extends JavaPlugin {

public void onEnable() {
    System.out.println("Plugin Enabled");
    getCommand("hello").setExecutor(new HelloCommand();
    }

    public void onDisable() {
        System.out.println("Plugin Disabled");
    }
}

CommandExecutor 类:

package org.devoflua.hello.commands;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.devoflua.hello.Main;

public class HelloCommand implements CommandExecutor{

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (sender instanceof Player) {
        Player p = (Player) sender;
        if (p.hasPermission("hello.use")) {
            p.sendMessage("hi");
        } else {
            p.sendMessage("You do not have permission to send this message");
        }
    } else {
        sender.sendMessage("Only senders can use this command");
    }
    return true;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-14
    • 2019-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多