From 0927bc5df29d95dd702fb2021733c17a241a0803 Mon Sep 17 00:00:00 2001 From: thojo0 <53666000+thojo0@users.noreply.github.com> Date: Wed, 31 Jul 2024 22:05:00 +0200 Subject: [PATCH] add clearItem command to force clear the noteblocksound of a player head --- .vscode/settings.json | 3 +- .../java/de/thojo0/moreheadsounds/App.java | 40 +++++++++++++++++++ src/main/resources/plugin.yml | 5 ++- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index e0f15db..0be1c0c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,4 @@ { - "java.configuration.updateBuildConfiguration": "automatic" + "java.configuration.updateBuildConfiguration": "automatic", + "java.compile.nullAnalysis.mode": "automatic" } \ No newline at end of file diff --git a/src/main/java/de/thojo0/moreheadsounds/App.java b/src/main/java/de/thojo0/moreheadsounds/App.java index baf1a07..92129fe 100644 --- a/src/main/java/de/thojo0/moreheadsounds/App.java +++ b/src/main/java/de/thojo0/moreheadsounds/App.java @@ -3,10 +3,16 @@ import java.util.LinkedList; import java.util.List; +import org.bukkit.Material; 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; import org.bukkit.plugin.java.JavaPlugin; +import net.md_5.bungee.api.chat.ComponentBuilder; +import net.md_5.bungee.api.chat.TranslatableComponent; public class App extends JavaPlugin { public String customPrefix = "§a[§e" + getName() + "§a]"; @@ -31,6 +37,10 @@ public void onDisable() { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + Player player = null; + if (sender instanceof Player) + player = (Player) sender; + if (args.length == 0) { sender.sendMessage(customPrefixFail + " You need to type something after /" + label + "\n"); return true; @@ -45,6 +55,33 @@ public boolean onCommand(CommandSender sender, Command command, String label, St eventListener.reloadConfig(getConfig()); sender.sendMessage(customPrefix + " Configuration reloaded!"); return true; + + case "clearItem": + if (player == null) { + sender.sendMessage(customPrefixFail + " You can only this command as player!"); + return true; + } + if (!sender.hasPermission(command.getName() + ".clearItem")) { + sender.sendMessage(customPrefixFail + " You don't have permission to perform that command!"); + return true; + } + + ItemStack item = player.getInventory().getItemInMainHand(); + + // Check if the item is a player head + if (item.getType() != Material.PLAYER_HEAD) { + sender.spigot().sendMessage(new ComponentBuilder(customPrefixFail + " You arn't holding a ") + .append(new TranslatableComponent(Material.PLAYER_HEAD.getItemTranslationKey())).build()); + return true; + } + // Get the metadata for the player head + SkullMeta metaData = (SkullMeta) item.getItemMeta(); + // remove the sound from the metadata + metaData.setNoteBlockSound(null); + item.setItemMeta(metaData); + + sender.sendMessage(customPrefix + " Sound cleared from item!"); + return true; } return super.onCommand(sender, command, label, args); } @@ -55,6 +92,9 @@ public List onTabComplete(CommandSender sender, Command command, String if (args.length == 1 && (sender.hasPermission(command.getName() + ".reload"))) { autoComplete.add("reload"); } + if (args.length == 1 && (sender.hasPermission(command.getName() + ".clearItem"))) { + autoComplete.add("clearItem"); + } return autoComplete; } } \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index cdf8d3d..740b40b 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -10,4 +10,7 @@ commands: usage: / permissions: moreheadsounds.reload: - description: Allows reloading the configuration \ No newline at end of file + description: Allows reloading the configuration + moreheadsounds.clearItem: + description: Allows clearing the sound from the currently holding player head + default: true \ No newline at end of file