Skip to content

Commit

Permalink
add getHash command
Browse files Browse the repository at this point in the history
fixes #1
  • Loading branch information
thojo0 committed Aug 5, 2024
1 parent 0927bc5 commit a397297
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>de.thojo0</groupId>
<artifactId>moreheadsounds</artifactId>
<version>1.1</version>
<version>1.2</version>

<name>moreheadsounds</name>
<!-- FIXME change it to the project's website
Expand Down
102 changes: 78 additions & 24 deletions src/main/java/de/thojo0/moreheadsounds/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.profile.PlayerProfile;

import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TranslatableComponent;
import net.md_5.bungee.api.chat.hover.content.Text;

public class App extends JavaPlugin {
public String customPrefix = "§a[§e" + getName() + "§a]";
Expand Down Expand Up @@ -51,10 +56,8 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
sender.sendMessage(customPrefixFail + " You don't have permission to perform that command!");
return true;
}
reloadConfig();
eventListener.reloadConfig(getConfig());
sender.sendMessage(customPrefix + " Configuration reloaded!");
return true;
runReload(sender);
break;

case "clearItem":
if (player == null) {
Expand All @@ -65,36 +68,87 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
sender.sendMessage(customPrefixFail + " You don't have permission to perform that command!");
return true;
}
runClearItem(player);
break;

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());
case "getHash":
if (player == null) {
sender.sendMessage(customPrefixFail + " You can only this command as player!");
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;
if (!sender.hasPermission(command.getName() + ".getHash")) {
sender.sendMessage(customPrefixFail + " You don't have permission to perform that command!");
return true;
}
runGetHash(player);
break;
}
return super.onCommand(sender, command, label, args);
return true;
}

@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
LinkedList<String> autoComplete = new LinkedList<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");
if (args.length == 1) {
if (sender.hasPermission(command.getName() + ".reload")) {
autoComplete.add("reload");
}
if (sender.hasPermission(command.getName() + ".clearItem")) {
autoComplete.add("clearItem");
}
if (sender.hasPermission(command.getName() + ".getHash")) {
autoComplete.add("getHash");
}
}
return autoComplete;
}

public void runReload(CommandSender sender) {
reloadConfig();
eventListener.reloadConfig(getConfig());
sender.sendMessage(customPrefix + " Configuration reloaded!");
}

public void runClearItem(Player player) {
ItemStack item = player.getInventory().getItemInMainHand();

// Check if the item is a player head
if (item.getType() != Material.PLAYER_HEAD) {
player.spigot().sendMessage(new ComponentBuilder(customPrefixFail + " You arn't holding a ")
.append(new TranslatableComponent(Material.PLAYER_HEAD.getItemTranslationKey())).build());
return;
}
// Get the metadata for the player head
SkullMeta metaData = (SkullMeta) item.getItemMeta();
// remove the sound from the metadata
metaData.setNoteBlockSound(null);
item.setItemMeta(metaData);

player.sendMessage(customPrefix + " Sound cleared from item!");
}

public void runGetHash(Player player) {
ItemStack item = player.getInventory().getItemInMainHand();

// Check if the item is a player head
if (item.getType() != Material.PLAYER_HEAD) {
player.spigot().sendMessage(new ComponentBuilder(customPrefixFail + " You arn't holding a ")
.append(new TranslatableComponent(Material.PLAYER_HEAD.getItemTranslationKey())).build());
return;
}
// Get the metadata for the player head
SkullMeta metaData = (SkullMeta) item.getItemMeta();
PlayerProfile owner = metaData.getOwnerProfile();
// Check owner exist
if (owner == null) {
player.sendMessage(customPrefixFail + " Coudn't find any texture hash!");
return;
}

String textureHash = EventListener.getTextureHash(owner);

player.spigot().sendMessage(new ComponentBuilder(customPrefix + " Hash: §r").append(textureHash)
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text("Click to copy to clipboard")))
.event(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, textureHash)).build());
}
}
8 changes: 5 additions & 3 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
main: de.thojo0.moreheadsounds.App
name: MoreHeadSounds
version: 1.1
version: 1.2
authors: [thojo0, PYZ]
description: Allows the use of player heads for minecraft sounds
api-version: 1.20
Expand All @@ -12,5 +12,7 @@ permissions:
moreheadsounds.reload:
description: Allows reloading the configuration
moreheadsounds.clearItem:
description: Allows clearing the sound from the currently holding player head
default: true
description: Allows clearing the sound of the currently holding player head
default: true
moreheadsounds.getHash:
description: Allows reading the texture hash of the currently holding player head

0 comments on commit a397297

Please sign in to comment.