Files
src
build.gradle
settings.gradle
Code
4 188
src/main/java/com/kodari/fabledeathrecap/FableDeathRecapPlugin.java
package com.kodari.fabledeathrecap;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XSound;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
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.PlayerRespawnEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public final class FableDeathRecapPlugin extends JavaPlugin implements Listener {
private final Map<UUID, DeathRecap> pendingRecaps = new HashMap<>();
@Override
public void onEnable() {
getServer().getPluginManager().registerEvents(this, this);
}
@EventHandler
public void onPlayerDeath(PlayerDeathEvent event) {
Player player = event.getEntity();
String cause = player.getLastDamageCause() == null
? "CUSTOM"
: player.getLastDamageCause().getCause().name();
Player killer = player.getKiller();
pendingRecaps.put(player.getUniqueId(), new DeathRecap(
cause,
killer == null ? null : killer.getName(),
survivalTip(cause)
));
}
@EventHandler
public void onPlayerRespawn(PlayerRespawnEvent event) {
Player player = event.getPlayer();
DeathRecap recap = pendingRecaps.remove(player.getUniqueId());
if (recap == null) {
return;
}
Bukkit.getScheduler().runTask(this, () -> {
if (!player.isOnline()) {
return;
}
player.openInventory(createRecap(player, recap));
playEcho(player);
});
}
private Inventory createRecap(Player player, DeathRecap recap) {
Inventory inventory = Bukkit.createInventory(null, 27, color("&6Once Upon a Time..."));
ItemStack star = XMaterial.matchXMaterial("NETHER_STAR")
.map(XMaterial::parseItem)
.orElse(null);
if (star != null) {
ItemMeta meta = star.getItemMeta();
if (meta != null) {
meta.setDisplayName(color("&e✦ Your final chapter ✦"));
meta.setLore(Arrays.asList(
color("&7You died from: &f" + causeName(recap.cause())),
color("&7Killer: &f" + (recap.killer() == null ? "The world" : recap.killer())),
color("&7Next time: &a" + recap.survivalTip())
));
star.setItemMeta(meta);
}
inventory.setItem(4, star);
}
ItemStack skin = XMaterial.matchXMaterial("PLAYER_HEAD")
.map(XMaterial::parseItem)
.orElse(null);
if (skin != null) {
ItemMeta itemMeta = skin.getItemMeta();
if (itemMeta instanceof SkullMeta skullMeta) {
skullMeta.setOwningPlayer(player);
skullMeta.setDisplayName(color("&d" + player.getName() + "&7's echo"));
skin.setItemMeta(skullMeta);
}
inventory.setItem(13, skin);
}
return inventory;
}
private void playEcho(Player player) {
XSound.matchXSound("BLOCK_NOTE_BLOCK_CHIME")
.ifPresent(sound -> player.playSound(player.getLocation(), sound.get(), 0.35f, 0.72f));
Bukkit.getScheduler().runTaskLater(this, () -> {
if (player.isOnline()) {
XSound.matchXSound("BLOCK_NOTE_BLOCK_CHIME")
.ifPresent(sound -> player.playSound(player.getLocation(), sound.get(), 0.16f, 0.58f));
}
}, 6L);
}
private String causeName(String cause) {
return switch (cause) {
case "FALL" -> "a fall";
case "FIRE", "FIRE_TICK", "LAVA", "HOT_FLOOR" -> "fire or heat";
case "DROWNING" -> "drowning";
case "ENTITY_ATTACK" -> "a melee attack";
case "ENTITY_EXPLOSION", "BLOCK_EXPLOSION" -> "an explosion";
case "VOID" -> "the void";
case "POISON" -> "poison";
case "WITHER" -> "the wither effect";
case "STARVATION" -> "starvation";
case "PROJECTILE" -> "a projectile";
case "MAGIC" -> "magic";
case "LIGHTNING" -> "lightning";
case "SUFFOCATION" -> "suffocation";
case "FREEZE" -> "freezing";
case "CONTACT" -> "a prickly block";
case "CRAMMING" -> "being crushed";
default -> cause.toLowerCase().replace('_', ' ');
};
}
private String survivalTip(String cause) {
return switch (cause) {
case "FALL" -> "Use water, slow falling, or feather falling.";
case "FIRE", "FIRE_TICK", "LAVA", "HOT_FLOOR" -> "Carry fire resistance and keep water nearby.";
case "DROWNING" -> "Come up for air or bring water-breathing gear.";
case "ENTITY_ATTACK" -> "Keep your shield up and create some distance.";
case "ENTITY_EXPLOSION", "BLOCK_EXPLOSION" -> "Back away from danger and use blast protection.";
case "VOID" -> "Keep blocks or an escape item ready near the edge.";
case "POISON", "WITHER", "MAGIC" -> "Bring healing supplies and avoid the source of the effect.";
case "STARVATION" -> "Keep your hunger bar full with food.";
case "PROJECTILE" -> "Use cover or a shield when ranged enemies attack.";
case "SUFFOCATION" -> "Move away from blocks closing in around you.";
case "FREEZE" -> "Avoid powdered snow or wear leather boots.";
case "CONTACT" -> "Watch your step around dangerous blocks.";
case "CRAMMING" -> "Make room before too many mobs crowd together.";
case "LIGHTNING" -> "Find shelter during thunderstorms.";
default -> "Stay alert, bring supplies, and try a different path.";
};
}
private String color(String text) {
return ChatColor.translateAlternateColorCodes('&', text);
}
private record DeathRecap(String cause, String killer, String survivalTip) {
}
}