java - Bukkit-使用VaultAPI的NPE(聊天)

原文 标签 java nullpointerexception minecraft bukkit bukkit-vault

尝试从GUI设置播放器后缀/前缀时,出现了用于设置所述前缀/后缀的空指针异常。 (我收到“您已应用x标签”消息,它确实关闭了广告资源。)

主类(定义和设置聊天的地方,而不是整个类)

public static Chat chat = null;
@SuppressWarnings("unused")
private TagsCommands tCmd;

public void onEnable() {
    if (Bukkit.getPluginManager().getPlugin("Vault") != null) {
        setupChat();
        createFolders();
        Message.console("&fPlugin successfully loaded");
        Bukkit.getPluginManager().registerEvents(new TagsGUI(), this);
        tCmd = new TagsCommands(this);
    } else {
        Message.console("&dCouldn't enable plugin as &aVault&c was not found");
        Bukkit.getPluginManager().disablePlugin(this);
        return;
    }
}

private boolean setupChat() {
    RegisteredServiceProvider<Chat> chatProvider = getServer().getServicesManager()
            .getRegistration(net.milkbowl.vault.chat.Chat.class);
    if (chatProvider != null) {
        chat = chatProvider.getProvider();

    }
    return (chat != null);
}


GUI类(同样,不是全部。错误在于第55行的此类。)

@EventHandler
public void inventoryClick(InventoryClickEvent event) {
    Player player = (Player) event.getWhoClicked();
    ItemStack click = event.getCurrentItem();
    if (opened.contains(player)) {
        event.setCancelled(true);
        File tags = new File("plugins/Tags/Pages", "One.yml");
        FileConfiguration tagsC = YamlConfiguration.loadConfiguration(tags);
        for (String title : tagsC.getConfigurationSection("Tags").getKeys(false)) {
            String tag = tagsC.getString("Tags." + title + ".Display");
            if (click.getItemMeta().getDisplayName().equals(Format.color(tag))) {
                if (player.hasPermission(tagsC.getString("Tags." + title + ".Permission"))) {
                    if (tagsC.getString("Tags." + title + ".Type").equalsIgnoreCase("prefix")) {
                        player.closeInventory();
                        Message.player("&fYou applied the '" + tag + "&f' tag", player);
                        Main.chat.setPlayerPrefix(player, Format.color(tag));
                        break;
                    } else if (tagsC.getString("Tags." + title + ".Type").equalsIgnoreCase("suffix")) {
                        player.closeInventory();
                        Message.player("&fYou applied the '" + tag + "&f' tag", player);
                        Main.chat.setPlayerSuffix(player, Format.color(tag));
                        break;
                    }
                } else {
                    Message.player("&fSorry, but you do not have permission to do this", player);
                    break;
                }
            }
        }
        return;
    }
}


第55行:
Main.chat.setPlayerSuffix(player,Format.color(tag));

控制台错误消息:

[11:13:05 INFO]: BearToothh issued server command: /tags
[11:13:12 ERROR]: Could not pass event InventoryClickEvent to Tags v1.0
org.bukkit.event.EventException: null
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.12.2.jar:git-Spigot-2cf50f0-2b93d83]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.12.2.jar:git-Spigot-2cf50f0-2b93d83]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) [spigot-1.12.2.jar:git-Spigot-2cf50f0-2b93d83]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) [spigot-1.12.2.jar:git-Spigot-2cf50f0-2b93d83]
        at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1889) [spigot-1.12.2.jar:git-Spigot-2cf50f0-2b93d83]
        at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:33) [spigot-1.12.2.jar:git-Spigot-2cf50f0-2b93d83]
        at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:10) [spigot-1.12.2.jar:git-Spigot-2cf50f0-2b93d83]
        at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.2.jar:git-Spigot-2cf50f0-2b93d83]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:?]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:?]
        at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.2.jar:git-Spigot-2cf50f0-2b93d83]
        at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot-1.12.2.jar:git-Spigot-2cf50f0-2b93d83]
        at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-2cf50f0-2b93d83]
        at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-2cf50f0-2b93d83]
        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-2cf50f0-2b93d83]
        at java.lang.Thread.run(Unknown Source) [?:?]
Caused by: java.lang.NullPointerException
        at me.askingg.tags.TagsGUI.inventoryClick(TagsGUI.java:55) ~[?:?]
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:?]
        at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:?]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:?]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.12.2.jar:git-Spigot-2cf50f0-2b93d83]
        ... 15 more

最佳答案

public static Chat chat = null;null,因此它返回NullPointerException。
您不能定义为null的聊天,也不能在不存在的聊天中设置前缀和内容^^
您必须为现有聊天定义Chat chat变量。

关于java - Bukkit-使用VaultAPI的NPE(聊天),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52553118/

相关文章:

java - Firefox/Chrome 和 Java GZIPOutputStream 之间的 GZIP/Deflate 错误

java - 为什么servlet不起作用?

java - word.set()方法在map reduce hadoop中抛出空指针异常

powershell - 如何在使用本地计算机上的PowerShell invoke-command远程运行的Minecraft服务器中输入命令?

java - 接口(interface)的java类型作为方法参数-如何将两个方法组合为一个

java - 通过热插拔机制在运行时更改方法

java - Tomcat/Java调试信息

android-studio - 更新后启动android studio时出现内部错误

linux - 如果进程已经在运行,则终止bash脚本

java - 尝试在聊天事件(Minecraft,Spigot)上设置玩家显示名称时出错