Skip to content

Commit

Permalink
add clearItem command to force clear the noteblocksound of a player head
Browse files Browse the repository at this point in the history
  • Loading branch information
thojo0 committed Jul 31, 2024
1 parent 6ff0e57 commit 0927bc5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"java.configuration.updateBuildConfiguration": "automatic"
"java.configuration.updateBuildConfiguration": "automatic",
"java.compile.nullAnalysis.mode": "automatic"
}
40 changes: 40 additions & 0 deletions src/main/java/de/thojo0/moreheadsounds/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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]";
Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -55,6 +92,9 @@ public List<String> 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;
}
}
5 changes: 4 additions & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ commands:
usage: /<command>
permissions:
moreheadsounds.reload:
description: Allows reloading the configuration
description: Allows reloading the configuration
moreheadsounds.clearItem:
description: Allows clearing the sound from the currently holding player head
default: true

0 comments on commit 0927bc5

Please sign in to comment.