From d95b78c421cd02880e8abf2cb2c410e428e4a936 Mon Sep 17 00:00:00 2001 From: thojo0 <53666000+thojo0@users.noreply.github.com> Date: Wed, 31 Jul 2024 01:14:12 +0200 Subject: [PATCH] owner null fix --- .../thojo0/moreheadsounds/EventListener.java | 39 ++++++++++++------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/main/java/de/thojo0/moreheadsounds/EventListener.java b/src/main/java/de/thojo0/moreheadsounds/EventListener.java index b627c6b..eb16942 100644 --- a/src/main/java/de/thojo0/moreheadsounds/EventListener.java +++ b/src/main/java/de/thojo0/moreheadsounds/EventListener.java @@ -21,6 +21,7 @@ import org.bukkit.event.block.BlockDropItemEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.meta.SkullMeta; +import org.bukkit.profile.PlayerProfile; public class EventListener implements Listener { // Mapping from texture hashes to lists of NamespacedKeys for sounds @@ -82,29 +83,28 @@ public void onPlayerInteract(PlayerInteractEvent event) { // Get the block that was clicked Block block = event.getClickedBlock(); // Check if the action was a right-click on a note block - if (event.getAction() != Action.RIGHT_CLICK_BLOCK || block.getType() != Material.NOTE_BLOCK) { + if (event.getAction() != Action.RIGHT_CLICK_BLOCK || block.getType() != Material.NOTE_BLOCK) return; - } // Get the data for the note block NoteBlock blockdata = (NoteBlock) block.getBlockData(); // Check if the note block is associated with a custom head - if (blockdata.getInstrument() != Instrument.CUSTOM_HEAD) { + if (blockdata.getInstrument() != Instrument.CUSTOM_HEAD) return; - } // Get the block above the note block Block usedHead = block.getRelative(BlockFace.UP); // Check if the block above is a player head - if (usedHead.getType() != Material.PLAYER_HEAD) { + if (usedHead.getType() != Material.PLAYER_HEAD) return; - } // Get the skull data and texture hash for the player head Skull skull = (Skull) usedHead.getState(); - String[] textureUrl = skull.getOwnerProfile().getTextures().getSkin().toString().split("/"); - String textureHash = textureUrl[textureUrl.length - 1]; + PlayerProfile owner = skull.getOwnerProfile(); + // Check owner exist + if (owner == null) + return; + String textureHash = getTextureHash(owner); // Check if textureHash is defined - if (!textureToSounds.containsKey(textureHash)) { + if (!textureToSounds.containsKey(textureHash)) return; - }; // Get the possible sounds for the texture hash ArrayList possibleSounds = textureToSounds.get(textureHash); // Set the note block sound based on the note value @@ -124,10 +124,15 @@ public void onBlockDropItem(BlockDropItemEvent event) { // Get the metadata for the player head and its texture hash SkullMeta metaData = (SkullMeta) item.getItemStack().getItemMeta(); - // Extracts the texture hash from the player head's skin URL - String[] textureUrl = metaData.getOwnerProfile().getTextures().getSkin().toString().split("/"); - String textureHash = textureUrl[textureUrl.length - 1]; - + PlayerProfile owner = metaData.getOwnerProfile(); + // Check owner exist + if (owner == null) { + // Try again from block instead of item + owner = ((Skull) event.getBlockState()).getOwnerProfile(); + if (owner == null) + continue; + } + String textureHash = getTextureHash(owner); // If the texture hash is in the textureToSounds map, remove the sound from the // metadata if (textureToSounds.containsKey(textureHash)) { @@ -136,4 +141,10 @@ public void onBlockDropItem(BlockDropItemEvent event) { } } } + + // Extracts the texture hash from the player head's skin URL + public static String getTextureHash(PlayerProfile owner) { + String[] textureUrl = owner.getTextures().getSkin().toString().split("/"); + return textureUrl[textureUrl.length - 1]; + } } \ No newline at end of file