Added PlayerDeathEvent to change the death message/text

This commit is contained in:
themode 2020-12-22 05:26:37 +01:00
parent 460813b6e0
commit b5feb35619
3 changed files with 89 additions and 15 deletions

View File

@ -500,36 +500,45 @@ public class Player extends LivingEntity implements CommandSender {
public void kill() {
if (!isDead()) {
// send death screen text to the killed player
{
ColoredText deathText;
JsonMessage chatMessage;
// get death screen text to the killed player
{
if (lastDamageSource != null) {
deathText = lastDamageSource.buildDeathScreenText(this);
} else { // may happen if killed by the server without applying damage
deathText = ColoredText.of("Killed by poor programming.");
}
}
// get death message to chat
{
if (lastDamageSource != null) {
chatMessage = lastDamageSource.buildDeathMessage(this);
} else { // may happen if killed by the server without applying damage
chatMessage = ColoredText.of(getUsername() + " was killed by poor programming.");
}
}
// Call player death event
PlayerDeathEvent playerDeathEvent = new PlayerDeathEvent(this, deathText, chatMessage);
callEvent(PlayerDeathEvent.class, playerDeathEvent);
deathText = playerDeathEvent.getDeathText();
chatMessage = playerDeathEvent.getChatMessage();
// #buildDeathScreenText can return null, check here
if (deathText != null) {
CombatEventPacket deathPacket = CombatEventPacket.death(this, Optional.empty(), deathText);
playerConnection.sendPacket(deathPacket);
}
}
// send death message to chat
{
JsonMessage chatMessage;
if (lastDamageSource != null) {
chatMessage = lastDamageSource.buildDeathMessage(this);
} else { // may happen if killed by the server without applying damage
chatMessage = ColoredText.of(getUsername() + " was killed by poor programming.");
}
// #buildDeathMessage can return null, check here
if (chatMessage != null) {
MinecraftServer.getConnectionManager().broadcastMessage(chatMessage);
}
}
}
super.kill();
}

View File

@ -0,0 +1,61 @@
package net.minestom.server.event.player;
import net.minestom.server.chat.ColoredText;
import net.minestom.server.chat.JsonMessage;
import net.minestom.server.entity.Player;
import net.minestom.server.event.PlayerEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Called when a player die in {@link Player#kill()}.
*/
public class PlayerDeathEvent extends PlayerEvent {
private ColoredText deathText;
private JsonMessage chatMessage;
public PlayerDeathEvent(@NotNull Player player, ColoredText deathText, JsonMessage chatMessage) {
super(player);
this.deathText = deathText;
this.chatMessage = chatMessage;
}
/**
* Gets the text displayed in the death screen.
*
* @return the death text, can be null
*/
@Nullable
public ColoredText getDeathText() {
return deathText;
}
/**
* Changes the text displayed in the death screen.
*
* @param deathText the death text to display, null to remove
*/
public void setDeathText(@Nullable ColoredText deathText) {
this.deathText = deathText;
}
/**
* Gets the message sent to chat.
*
* @return the death chat message
*/
@Nullable
public JsonMessage getChatMessage() {
return chatMessage;
}
/**
* Changes the text sent in chat
*
* @param chatMessage the death message to send, null to remove
*/
public void setChatMessage(@Nullable JsonMessage chatMessage) {
this.chatMessage = chatMessage;
}
}

View File

@ -135,6 +135,10 @@ public class PlayerInit {
}
});
globalEventHandler.addEventCallback(PlayerDeathEvent.class, event -> {
event.setChatMessage(ColoredText.of("custom death message"));
});
globalEventHandler.addEventCallback(PlayerBlockPlaceEvent.class, event -> {
if (event.getHand() != Player.Hand.MAIN)
return;