Skip to content

Commit

Permalink
owner null fix
Browse files Browse the repository at this point in the history
  • Loading branch information
thojo0 committed Jul 30, 2024
1 parent c2db99a commit d95b78c
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/main/java/de/thojo0/moreheadsounds/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<NamespacedKey> possibleSounds = textureToSounds.get(textureHash);
// Set the note block sound based on the note value
Expand All @@ -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)) {
Expand All @@ -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];
}
}

0 comments on commit d95b78c

Please sign in to comment.