From bb335136f2d48d70b691180df13ab408588b3c58 Mon Sep 17 00:00:00 2001 From: thojo0 <53666000+thojo0@users.noreply.github.com> Date: Fri, 22 Nov 2024 17:10:17 +0100 Subject: [PATCH] make vars final + fix NullPointerException in getTextureHash --- .../java/de/thojo0/moreheadsounds/App.java | 11 +--- .../thojo0/moreheadsounds/EventListener.java | 56 +++++++++---------- 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/src/main/java/de/thojo0/moreheadsounds/App.java b/src/main/java/de/thojo0/moreheadsounds/App.java index c65c35c..5f18813 100644 --- a/src/main/java/de/thojo0/moreheadsounds/App.java +++ b/src/main/java/de/thojo0/moreheadsounds/App.java @@ -8,7 +8,6 @@ import org.bukkit.block.Skull; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; -import org.bukkit.command.PluginCommand; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.SkullMeta; @@ -32,9 +31,6 @@ public class App extends JavaPlugin { public void onEnable() { saveDefaultConfig(); getServer().getPluginManager().registerEvents(eventListener, this); - PluginCommand command = getCommand(getName().toLowerCase()); - command.setExecutor(this); - command.setTabCompleter(this); getLogger().info(getName() + " activated!"); } @@ -51,10 +47,9 @@ public boolean onCommand(CommandSender sender, Command command, String label, St if (args.length == 0) { sender.sendMessage(customPrefixFail + "You need to type something after /" + label); - return true; } - switch (args[0]) { + switch (args[0].toLowerCase()) { case "reload": if (!sender.hasPermission(command.getName() + ".reload")) { sender.spigot().sendMessage(new ComponentBuilder(customPrefixFail) @@ -65,7 +60,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St runReload(sender); break; - case "clearItem": + case "clearitem": if (!sender.hasPermission(command.getName() + ".clearItem")) { sender.spigot().sendMessage(new ComponentBuilder(customPrefixFail) .append(new TranslatableComponent("commands.help.failed")).color(ChatColor.RED) @@ -81,7 +76,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St runClearItem(player); break; - case "getHash": + case "gethash": if (!sender.hasPermission(command.getName() + ".getHash")) { sender.spigot().sendMessage(new ComponentBuilder(customPrefixFail) .append(new TranslatableComponent("commands.help.failed")).color(ChatColor.RED) diff --git a/src/main/java/de/thojo0/moreheadsounds/EventListener.java b/src/main/java/de/thojo0/moreheadsounds/EventListener.java index da33d16..ad49952 100644 --- a/src/main/java/de/thojo0/moreheadsounds/EventListener.java +++ b/src/main/java/de/thojo0/moreheadsounds/EventListener.java @@ -1,9 +1,11 @@ package de.thojo0.moreheadsounds; +import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; + import org.bukkit.Instrument; import org.bukkit.Material; import org.bukkit.NamespacedKey; @@ -12,7 +14,7 @@ import org.bukkit.block.BlockFace; import org.bukkit.block.Skull; import org.bukkit.block.data.type.NoteBlock; -import org.bukkit.configuration.MemorySection; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Item; import org.bukkit.event.EventHandler; @@ -25,7 +27,7 @@ public class EventListener implements Listener { // Mapping from texture hashes to lists of NamespacedKeys for sounds - private HashMap> textureToSounds = new HashMap<>(); + private final Map> textureToSounds = new HashMap<>(); EventListener(FileConfiguration config) { reloadConfig(config); @@ -39,7 +41,7 @@ public class EventListener implements Listener { public void reloadConfig(FileConfiguration config) { textureToSounds.clear(); // Get all configuration values - Map configValues = config.getValues(false); + final Map configValues = config.getValues(false); // Iterate over all available sounds for (Sound sound : Sound.values()) { // Get the NamespacedKey for the current sound @@ -47,7 +49,7 @@ public void reloadConfig(FileConfiguration config) { // Iterate over the configuration values configValues.forEach((textureHash, c) -> { // Get the list of excluded sounds for the current texture - List exclude = ((MemorySection) c).getStringList("exclude"); + final List exclude = ((ConfigurationSection) c).getStringList("exclude"); for (String ex : exclude) { // Split the exclude string into namespace and key String[] splitEx = ex.split(":"); @@ -62,7 +64,7 @@ public void reloadConfig(FileConfiguration config) { return; } // Get the list of included sounds for the current texture - List include = ((MemorySection) c).getStringList("include"); + final List include = ((ConfigurationSection) c).getStringList("include"); for (String in : include) { // Split the include string into namespace and key String[] splitIn = in.split(":"); @@ -86,32 +88,28 @@ public void reloadConfig(FileConfiguration config) { @EventHandler public void onPlayerInteract(PlayerInteractEvent event) { // Get the block that was clicked - Block block = event.getClickedBlock(); + final 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) return; // Get the data for the note block - NoteBlock blockdata = (NoteBlock) block.getBlockData(); + final NoteBlock blockdata = (NoteBlock) block.getBlockData(); // Check if the note block is associated with a custom head if (blockdata.getInstrument() != Instrument.CUSTOM_HEAD) return; // Get the block above the note block - Block usedHead = block.getRelative(BlockFace.UP); + final Block usedHead = block.getRelative(BlockFace.UP); // Check if the block above is a 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(); - PlayerProfile owner = skull.getOwnerProfile(); - // Check owner exist - if (owner == null) - return; - String textureHash = getTextureHash(owner); + final Skull skull = (Skull) usedHead.getState(); + final String textureHash = getTextureHash(skull.getOwnerProfile()); // Check if textureHash is defined - if (!textureToSounds.containsKey(textureHash)) + if (textureHash == null || !textureToSounds.containsKey(textureHash)) return; // Get the possible sounds for the texture hash - ArrayList possibleSounds = textureToSounds.get(textureHash); + final List possibleSounds = textureToSounds.get(textureHash); // Set the note block sound based on the note value skull.setNoteBlockSound(possibleSounds.get(((blockdata.getNote().getId() + 1) % 25) % possibleSounds.size())); // Update the skull without sending updates to clients or applying physics @@ -127,20 +125,17 @@ public void onBlockDropItem(BlockDropItemEvent event) { continue; // Get the metadata for the player head and its texture hash - SkullMeta metaData = (SkullMeta) item.getItemStack().getItemMeta(); + final SkullMeta metaData = (SkullMeta) item.getItemStack().getItemMeta(); + + // Get the textureHash from the item + String textureHash = getTextureHash(metaData.getOwnerProfile()); + // Try again from block + if (textureHash == null) + textureHash = getTextureHash(((Skull) event.getBlockState()).getOwnerProfile()); - 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)) { + if (textureHash != null && textureToSounds.containsKey(textureHash)) { metaData.setNoteBlockSound(null); item.getItemStack().setItemMeta(metaData); } @@ -149,7 +144,12 @@ 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("/"); + if (owner == null) + return null; + final URL skin = owner.getTextures().getSkin(); + if (skin == null) + return null; + final String[] textureUrl = skin.toExternalForm().split("/"); return textureUrl[textureUrl.length - 1]; } } \ No newline at end of file