为什么我的插件即使应该也不能通过PlayerDeathEvent

时间:2019-11-09 11:49:02

标签: java bukkit

我最近开始制作插件。我想创建一个插件,该插件将说“ xyz Player已加入/离开”和死亡消息。它适合加入并通过消灭不是玩家的实体而很好地工作。但是当Player杀死另一个Player时,控制台会返回它无法传递PlayerDeathEvent。

我尝试了谷歌搜索问题,但是之前的问题都没有帮助我解决地雷问题

控制台错误

[12:39:35] [Server thread/ERROR]: Could not pass event PlayerDeathEvent to Introduce v1.0
org.bukkit.event.EventException: null
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:529) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:514) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at org.bukkit.craftbukkit.v1_14_R1.event.CraftEventFactory.callPlayerDeathEvent(CraftEventFactory.java:726) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.EntityPlayer.die(EntityPlayer.java:520) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.EntityLiving.damageEntity(EntityLiving.java:1169) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.EntityHuman.damageEntity(EntityHuman.java:769) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.EntityPlayer.damageEntity(EntityPlayer.java:675) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.EntityHuman.attack(EntityHuman.java:1018) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.EntityPlayer.attack(EntityPlayer.java:1536) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.PlayerConnection.a(PlayerConnection.java:1864) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.PacketPlayInUseEntity.a(SourceFile:69) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.PacketPlayInUseEntity.a(SourceFile:13) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.TickTask.run(SourceFile:18) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.MinecraftServer.aX(MinecraftServer.java:910) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.MinecraftServer.executeNext(MinecraftServer.java:903) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.MinecraftServer.sleepForTick(MinecraftServer.java:887) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:820) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_211]
Caused by: java.lang.NullPointerException
        at JoinMessage.JoinMessage.onPlayerDeath(JoinMessage.java:54) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_211]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_211]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_211]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_211]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:316) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
        ... 24 more

Main.java:

package me.GraczBezNicku.Introduction;

import org.bukkit.plugin.java.JavaPlugin;
import JoinMessage.JoinMessage;

public class Main extends JavaPlugin 
{
    @Override
    public void onEnable()
    {
        saveDefaultConfig();

        new JoinMessage(this);
    }

}

Utility.java(用于简化聊天室的获取):

package Utilities;

import org.bukkit.ChatColor;

public class utility {

    public static String chat (String s)
    {
        return ChatColor.translateAlternateColorCodes('&', s);
    }
}

JoinMessage.java(我不认为我会将它用于死亡消息):

package JoinMessage;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;

import Utilities.utility;
import me.GraczBezNicku.Introduction.Main;

public class JoinMessage implements Listener
{
    private static Main plugin;

    public JoinMessage(Main plugin)
    {
        this.plugin = plugin;

        Bukkit.getPluginManager().registerEvents(this, plugin);
    }

    @EventHandler
    public void onJoin(PlayerJoinEvent e)
    {
        Player p = e.getPlayer();

        if(!p.hasPlayedBefore())
        {
            Bukkit.broadcastMessage(utility.chat(plugin.getConfig().getString("firstjoin_message").replace("<player>", p.getName())));
        }
        else
        {
            Bukkit.broadcastMessage(utility.chat(plugin.getConfig().getString("join_message").replace("<player>", p.getName())));
        }
    }

    @EventHandler
    public void onExit(PlayerQuitEvent e)
    {   
        Player p = e.getPlayer();

        Bukkit.broadcastMessage(utility.chat(plugin.getConfig().getString("leave_message").replace("<player>", p.getName())));
    }

    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent e)
    {
        if(e.getEntity().getKiller() instanceof Player)
        {
            PlayerDeathEvent deathe = null;
            deathe.setDeathMessage(null);

            Player p = e.getEntity();
            Player killer = e.getEntity().getKiller();

            if(p != killer)
            {
                killer.sendMessage(utility.chat("You have killed &7" + p.getDisplayName() + "!"));
                p.sendMessage(utility.chat("You have been killed by &7" + killer.getDisplayName()+ "!"));

                Bukkit.broadcastMessage(utility.chat("&7" + p.getDisplayName() + " Was killed by &7" + killer.getDisplayName()));
            }
            else if(killer == p)
            {
                Bukkit.broadcastMessage(utility.chat(" &7" + p.getDisplayName() + " Killed Himself!"));
            }

        }
        else
        {
            Player p = e.getEntity();

            Bukkit.broadcastMessage(utility.chat("&7" + p.getDisplayName() + " has died."));
        }
    }
}

config.yml:

firstJoin_message: '&a<player> &7has joined the server for the first time!'

join_message: '&e<player> &7has joined the server!'

leave_message: '&e<player> &7has left the server.'

plugin.yml:

name: Introduce
version: 1.0
author: GraczBezNicku
main: me.GraczBezNicku.Introduction.Main
description: Join/Leave message

预期结果是:“玩家xyz杀死zyx”。 实际结果:无法通过PlayerDeathEvent。

请寻求帮助,感谢您阅读本文! ; )

1 个答案:

答案 0 :(得分:0)

我很容易地使NullPointerException倍增。删除deathe.setdeathmessage(null)时,它可以工作!

 PlayerDeathEvent deathe = null;

它应该看起来像这样;

不是这个:

 PlayerDeathEvent deathe = null;
 deathe.setDeathMessage(null);