From 79de05fb84b54a4aff4d4630cf3df43389b1e2d5 Mon Sep 17 00:00:00 2001 From: Niklas Date: Fri, 26 Jun 2020 17:28:40 +0200 Subject: [PATCH] added player head drop --- .../hardcore_revive/HardcoreRevive.java | 2 +- .../hardcore_revive/PlayerDeathListener.java | 42 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/kapelle/hardcore_revive/PlayerDeathListener.java diff --git a/src/main/java/org/kapelle/hardcore_revive/HardcoreRevive.java b/src/main/java/org/kapelle/hardcore_revive/HardcoreRevive.java index 5b82c93..7d2ac84 100644 --- a/src/main/java/org/kapelle/hardcore_revive/HardcoreRevive.java +++ b/src/main/java/org/kapelle/hardcore_revive/HardcoreRevive.java @@ -7,6 +7,6 @@ public class HardcoreRevive extends JavaPlugin { @Override public void onEnable() { this.getLogger().info("hardcore-revive plugin enabled"); + this.getServer().getPluginManager().registerEvents(new PlayerDeathListener(this), this); } - } \ No newline at end of file diff --git a/src/main/java/org/kapelle/hardcore_revive/PlayerDeathListener.java b/src/main/java/org/kapelle/hardcore_revive/PlayerDeathListener.java new file mode 100644 index 0000000..bb748c4 --- /dev/null +++ b/src/main/java/org/kapelle/hardcore_revive/PlayerDeathListener.java @@ -0,0 +1,42 @@ +package org.kapelle.hardcore_revive; + +import org.bukkit.Material; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.PlayerDeathEvent; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.inventory.meta.SkullMeta; +import org.bukkit.plugin.java.JavaPlugin; + +public class PlayerDeathListener implements Listener { + private JavaPlugin plugin; + + public PlayerDeathListener(JavaPlugin plugin){ + this.plugin = plugin; + } + + @EventHandler + public void onPlayerDeath(PlayerDeathEvent event){ + this.plugin.getLogger().info("Player died. Dropping head"); + try{ + Player player = event.getEntity(); + + ItemStack head = new ItemStack(Material.PLAYER_HEAD); + SkullMeta headMeta = (SkullMeta) head.getItemMeta(); + assert headMeta != null; + + headMeta.setDisplayName("Hopes and dreams of " + player.getDisplayName()); + headMeta.setOwningPlayer(player); + + head.setItemMeta(headMeta); + + player.getWorld().dropItemNaturally(player.getLocation(),head); + }catch (Exception e){ + this.plugin.getLogger().severe("Failed to drop player head: " + e.getMessage()); + } + + } +}