mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-28 03:57:50 +01:00
Added PlayerDeathEvent to change the death message/text
This commit is contained in:
parent
460813b6e0
commit
b5feb35619
@ -500,36 +500,45 @@ public class Player extends LivingEntity implements CommandSender {
|
|||||||
public void kill() {
|
public void kill() {
|
||||||
if (!isDead()) {
|
if (!isDead()) {
|
||||||
|
|
||||||
// send death screen text to the killed player
|
ColoredText deathText;
|
||||||
|
JsonMessage chatMessage;
|
||||||
|
|
||||||
|
// get death screen text to the killed player
|
||||||
{
|
{
|
||||||
ColoredText deathText;
|
|
||||||
if (lastDamageSource != null) {
|
if (lastDamageSource != null) {
|
||||||
deathText = lastDamageSource.buildDeathScreenText(this);
|
deathText = lastDamageSource.buildDeathScreenText(this);
|
||||||
} else { // may happen if killed by the server without applying damage
|
} else { // may happen if killed by the server without applying damage
|
||||||
deathText = ColoredText.of("Killed by poor programming.");
|
deathText = ColoredText.of("Killed by poor programming.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// #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
|
// get death message to chat
|
||||||
{
|
{
|
||||||
JsonMessage chatMessage;
|
|
||||||
if (lastDamageSource != null) {
|
if (lastDamageSource != null) {
|
||||||
chatMessage = lastDamageSource.buildDeathMessage(this);
|
chatMessage = lastDamageSource.buildDeathMessage(this);
|
||||||
} else { // may happen if killed by the server without applying damage
|
} else { // may happen if killed by the server without applying damage
|
||||||
chatMessage = ColoredText.of(getUsername() + " was killed by poor programming.");
|
chatMessage = ColoredText.of(getUsername() + " was killed by poor programming.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// #buildDeathMessage can return null, check here
|
|
||||||
if (chatMessage != null) {
|
|
||||||
MinecraftServer.getConnectionManager().broadcastMessage(chatMessage);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// #buildDeathMessage can return null, check here
|
||||||
|
if (chatMessage != null) {
|
||||||
|
MinecraftServer.getConnectionManager().broadcastMessage(chatMessage);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
super.kill();
|
super.kill();
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -135,6 +135,10 @@ public class PlayerInit {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
globalEventHandler.addEventCallback(PlayerDeathEvent.class, event -> {
|
||||||
|
event.setChatMessage(ColoredText.of("custom death message"));
|
||||||
|
});
|
||||||
|
|
||||||
globalEventHandler.addEventCallback(PlayerBlockPlaceEvent.class, event -> {
|
globalEventHandler.addEventCallback(PlayerBlockPlaceEvent.class, event -> {
|
||||||
if (event.getHand() != Player.Hand.MAIN)
|
if (event.getHand() != Player.Hand.MAIN)
|
||||||
return;
|
return;
|
||||||
|
Loading…
Reference in New Issue
Block a user