Fix chat formatting, fireworks particle fallback, and bounty head metadata

This commit is contained in:
NotAlexNoyle committed 2026-03-09 08:13:49 -04:00
1 parent 5726b86bd5
commit 0560fdfd5d
3 files changed
+278 -10

No files matched your search

@@ -18,6 +18,7 @@ import com.tcoded.playerbountiesplus.command.PlayerBountiesPlusAdminCmd;
import com.tcoded.playerbountiesplus.hook.currency.DiamondBankOGHook;
import com.tcoded.playerbountiesplus.hook.currency.EconomyHook;
import com.tcoded.playerbountiesplus.hook.team.TeamHook;
import com.tcoded.playerbountiesplus.listener.BountyHeadListener;
import com.tcoded.playerbountiesplus.listener.DeathListener;
import com.tcoded.playerbountiesplus.listener.GuiProtectionListener;
import com.tcoded.playerbountiesplus.manager.BountyDataManager;
@@ -144,6 +145,7 @@ public final class PlayerBountiesOG extends JavaPlugin {
// Listeners.
this.getServer().getPluginManager().registerEvents(new DeathListener(this, this.diamondBankAPI, this.luckPerms),
this);
this.getServer().getPluginManager().registerEvents(new BountyHeadListener(this, this.diamondBankAPI), this);
this.getServer().getPluginManager().registerEvents(new GuiProtectionListener(this), this);
final List<Plugin> plugins = ImmutableList.copyOf(this.getServer().getPluginManager().getPlugins());
@@ -0,0 +1,241 @@
package com.tcoded.playerbountiesplus.listener;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Skull;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import com.tcoded.playerbountiesplus.PlayerBountiesOG;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.trueog.diamondbankog.api.DiamondBankAPIJava;
public class BountyHeadListener implements Listener {
private final DiamondBankAPIJava diamondBankAPI;
private final NamespacedKey targetKey;
private final NamespacedKey amountKey;
public BountyHeadListener(PlayerBountiesOG plugin, DiamondBankAPIJava diamondBankAPI) {
this.diamondBankAPI = diamondBankAPI;
this.targetKey = new NamespacedKey(plugin, DeathListener.BOUNTY_HEAD_NAME_KEY);
this.amountKey = new NamespacedKey(plugin, DeathListener.BOUNTY_HEAD_AMOUNT_KEY);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlace(BlockPlaceEvent event) {
if (!isHeadMaterial(event.getBlockPlaced().getType())) {
return;
}
final ItemMeta itemMeta = event.getItemInHand().getItemMeta();
if (itemMeta == null) {
return;
}
final String targetName = itemMeta.getPersistentDataContainer().get(targetKey, PersistentDataType.STRING);
final Double bountyAmount = itemMeta.getPersistentDataContainer().get(amountKey, PersistentDataType.DOUBLE);
if (targetName == null || bountyAmount == null) {
return;
}
final BlockState state = event.getBlockPlaced().getState();
if (!(state instanceof Skull skull)) {
return;
}
final PersistentDataContainer blockData = skull.getPersistentDataContainer();
blockData.set(targetKey, PersistentDataType.STRING, targetName);
blockData.set(amountKey, PersistentDataType.DOUBLE, bountyAmount);
skull.update(true, false);
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBreak(BlockBreakEvent event) {
if (!isHeadMaterial(event.getBlock().getType())) {
return;
}
final BlockState state = event.getBlock().getState();
if (!(state instanceof Skull skull)) {
return;
}
final String targetName = skull.getPersistentDataContainer().get(targetKey, PersistentDataType.STRING);
final Double bountyAmount = skull.getPersistentDataContainer().get(amountKey, PersistentDataType.DOUBLE);
if (targetName == null || bountyAmount == null) {
return;
}
final ItemStack drop = new ItemStack(Material.PLAYER_HEAD, 1);
final SkullMeta meta = (SkullMeta) drop.getItemMeta();
meta.setOwnerProfile(skull.getOwnerProfile());
applyBountyData(meta, targetName, bountyAmount);
drop.setItemMeta(meta);
event.setDropItems(false);
event.getBlock().setType(Material.AIR, false);
event.getBlock().getWorld().dropItemNaturally(event.getBlock().getLocation(), drop);
}
@EventHandler(ignoreCancelled = true)
public void onRightClick(PlayerInteractEvent event) {
if (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK) {
return;
}
final ItemStack item = event.getItem();
if (item != null && isHeadMaterial(item.getType())) {
sendBountyHeadActionbar(event.getPlayer(), item.getItemMeta());
}
if (event.getClickedBlock() == null || !isHeadMaterial(event.getClickedBlock().getType())) {
return;
}
final BlockState state = event.getClickedBlock().getState();
if (!(state instanceof Skull skull)) {
return;
}
sendBountyHeadActionbar(event.getPlayer(), skull.getPersistentDataContainer());
}
@EventHandler(ignoreCancelled = true)
public void onHover(PlayerMoveEvent event) {
final Player player = event.getPlayer();
final Block target = player.getTargetBlockExact(6);
if (target == null || !isHeadMaterial(target.getType())) {
return;
}
final BlockState state = target.getState();
if (!(state instanceof Skull skull)) {
return;
}
sendBountyHeadActionbar(player, skull.getPersistentDataContainer());
}
private void sendBountyHeadActionbar(Player player, ItemMeta itemMeta) {
if (itemMeta == null) {
return;
}
sendBountyHeadActionbar(player, itemMeta.getPersistentDataContainer());
}
private void sendBountyHeadActionbar(Player player, PersistentDataContainer dataContainer) {
final String targetName = dataContainer.get(targetKey, PersistentDataType.STRING);
final Double bountyAmount = dataContainer.get(amountKey, PersistentDataType.DOUBLE);
if (targetName == null || bountyAmount == null) {
return;
}
final String diamonds = formatDiamonds(bountyAmount);
player.sendActionBar(Component.text("Bounty Head: ", NamedTextColor.RED)
.append(Component.text(targetName, NamedTextColor.WHITE)).append(Component.text(" - ", NamedTextColor.GRAY))
.append(Component.text(diamonds + " diamonds", NamedTextColor.AQUA)));
}
private void applyBountyData(SkullMeta meta, String targetName, double bountyAmount) {
meta.setDisplayName("§c" + targetName + "'s Bounty Head");
final List<String> lore = new ArrayList<>();
lore.add("§7Target: §f" + targetName);
lore.add("§7Beheaded for: §b" + formatDiamonds(bountyAmount));
meta.setLore(lore);
meta.getPersistentDataContainer().set(targetKey, PersistentDataType.STRING, targetName);
meta.getPersistentDataContainer().set(amountKey, PersistentDataType.DOUBLE, bountyAmount);
}
private String formatDiamonds(double diamonds) {
try {
return diamondBankAPI.shardsToDiamonds(diamondBankAPI.diamondsToShards(diamonds));
} catch (RuntimeException runtimeException) {
return String.valueOf(diamonds);
}
}
private static boolean isHeadMaterial(Material material) {
return material == Material.PLAYER_HEAD || material == Material.PLAYER_WALL_HEAD;
}
}
@@ -6,6 +6,7 @@ import java.util.concurrent.ThreadLocalRandom;
import org.apache.commons.lang3.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Particle;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
@@ -15,6 +16,7 @@ import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.persistence.PersistentDataType;
import com.destroystokyo.paper.profile.PlayerProfile;
import com.tcoded.playerbountiesplus.PlayerBountiesOG;
@@ -30,6 +32,9 @@ import net.trueog.utilitiesog.UtilitiesOG;
public class DeathListener implements Listener {
public static final String BOUNTY_HEAD_NAME_KEY = "bounty_head_target";
public static final String BOUNTY_HEAD_AMOUNT_KEY = "bounty_head_diamonds";
private final PlayerBountiesOG plugin;
private final DiamondBankAPIJava diamondBankAPI;
private final LuckPerms luckPerms;
@@ -137,19 +142,19 @@ public class DeathListener implements Listener {
if (Double.compare(awardedAmount, bounty) == 0) {
message = killerDisplay + " &akilled " + victimDisplay + " &aand earned the &b" + awardedDisplay
message = killerDisplay + "&r &akilled " + victimDisplay + "&r &aand earned the &b" + awardedDisplay
+ " &abounty!";
} else {
message = killerDisplay + " &akilled " + victimDisplay + " &aand claimed the &b" + bountyDisplay
message = killerDisplay + "&r &akilled " + victimDisplay + "&r &aand claimed the &b" + bountyDisplay
+ " &abounty for &b" + awardedDisplay + "&a!";
}
} else {
message = killerDisplay + " &akilled " + victimDisplay + " &aand claimed the &b" + bountyDisplay
message = killerDisplay + "&r &akilled " + victimDisplay + "&r &aand claimed the &b" + bountyDisplay
+ " &abounty!";
}
@@ -159,12 +164,12 @@ public class DeathListener implements Listener {
}
final boolean beheaded = maybeDropPlayerHead(event, victim);
final boolean beheaded = maybeDropPlayerHead(event, victim, bounty);
if (beheaded) {
Bukkit.getOnlinePlayers().forEach(player -> UtilitiesOG.trueogMessage(player,
killerDisplay + " &abeheaded " + victimDisplay + "&a!"));
killerDisplay + "&r &abeheaded " + victimDisplay + "&r&a!"));
}
@@ -179,16 +184,29 @@ public class DeathListener implements Listener {
player.playSound(player.getLocation(), Sound.UI_TOAST_CHALLENGE_COMPLETE, 1.0F, 1.0F);
player.spawnParticle(Particle.FIREWORKS_SPARK, killer.getLocation().add(0D, 1D, 0D), 40, 0.6D, 0.8D, 0.6D,
0.01D);
player.spawnParticle(Particle.FIREWORKS_SPARK, victim.getLocation().add(0D, 1D, 0D), 40, 0.6D, 0.8D, 0.6D,
0.01D);
final Particle fireworkParticle = resolveFireworkParticle();
player.spawnParticle(fireworkParticle, killer.getLocation().add(0D, 1D, 0D), 40, 0.6D, 0.8D, 0.6D, 0.01D);
player.spawnParticle(fireworkParticle, victim.getLocation().add(0D, 1D, 0D), 40, 0.6D, 0.8D, 0.6D, 0.01D);
player.spawnParticle(Particle.TOTEM, killer.getLocation().add(0D, 1D, 0D), 20, 0.5D, 0.7D, 0.5D, 0.01D);
});
}
private Particle resolveFireworkParticle() {
try {
return Particle.valueOf("FIREWORK");
} catch (IllegalArgumentException exception) {
return Particle.FIREWORKS_SPARK;
}
}
private String formatDiamonds(double diamonds) {
try {
@@ -224,7 +242,7 @@ public class DeathListener implements Listener {
}
private boolean maybeDropPlayerHead(PlayerDeathEvent event, Player victim) {
private boolean maybeDropPlayerHead(PlayerDeathEvent event, Player victim, double bounty) {
final double dropChance = Math.max(0D,
Math.min(100D, plugin.getConfig().getDouble("bounty-head-drop-chance", 50D)));
@@ -239,6 +257,13 @@ public class DeathListener implements Listener {
final PlayerProfile profile = Bukkit.createProfile(victim.getUniqueId());
headMeta.setPlayerProfile(profile);
headMeta.setDisplayName("§c" + victim.getName() + "'s Bounty Head");
final String bountyDisplay = formatDiamonds(bounty);
headMeta.setLore(java.util.List.of("§7Target: §f" + victim.getName(), "§7Beheaded for: §b" + bountyDisplay));
headMeta.getPersistentDataContainer().set(new NamespacedKey(plugin, BOUNTY_HEAD_NAME_KEY),
PersistentDataType.STRING, victim.getName());
headMeta.getPersistentDataContainer().set(new NamespacedKey(plugin, BOUNTY_HEAD_AMOUNT_KEY),
PersistentDataType.DOUBLE, bounty);
head.setItemMeta(headMeta);
event.getDrops().add(head);