Skip to content

Commit

Permalink
add getHash posible block position argument to get from placed block
Browse files Browse the repository at this point in the history
use minecraft internal translations for command errors if posible

Co-authored-by: PYZR <[email protected]>
  • Loading branch information
thojo0 and PYZR committed Aug 6, 2024
1 parent 6eeaa7b commit f311a57
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 22 deletions.
54 changes: 53 additions & 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.2</version>
<version>1.2.1</version>

<name>moreheadsounds</name>
<!-- FIXME change it to the project's website
Expand Down Expand Up @@ -44,5 +44,57 @@
</includes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.4.0</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>install</phase>
<goals>
<goal>clean</goal>
</goals>
<configuration>
<excludeDefaultDirectories>true</excludeDefaultDirectories>
<failOnError>false</failOnError>
<filesets>
<fileset>
<directory>server/plugins</directory>
<followSymlinks>false</followSymlinks>
<useDefaultExcludes>false</useDefaultExcludes>
<includes>
<include>${project.name}-*.${project.packaging}</include>
</includes>
</fileset>
</filesets>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
<executions>
<execution>
<id>copy-files-on-build</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/server/plugins</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}</directory>
<include>${project.name}-${project.version}.${project.packaging}</include>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
124 changes: 104 additions & 20 deletions src/main/java/de/thojo0/moreheadsounds/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.List;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Skull;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
Expand All @@ -13,15 +15,16 @@
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.profile.PlayerProfile;

import net.md_5.bungee.api.ChatColor;
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]";
public String customPrefixFail = "§c[§7" + getName() + "§c]";
public String customPrefix = "§a[§e" + getName() + "§a] ";
public String customPrefixFail = "§c[§7" + getName() + "§c] ";

private final EventListener eventListener = new EventListener(getConfig());

Expand All @@ -47,47 +50,86 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
player = (Player) sender;

if (args.length == 0) {
sender.sendMessage(customPrefixFail + " You need to type something after /" + label + "\n");
sender.sendMessage(customPrefixFail + "You need to type something after /" + label);

return true;
}
switch (args[0]) {
case "reload":
if (!sender.hasPermission(command.getName() + ".reload")) {
sender.sendMessage(customPrefixFail + " You don't have permission to perform that command!");
sender.spigot().sendMessage(new ComponentBuilder(customPrefixFail)
.append(new TranslatableComponent("commands.help.failed")).color(ChatColor.RED)
.build());
return true;
}
runReload(sender);
break;

case "clearItem":
if (player == null) {
sender.sendMessage(customPrefixFail + " You can only this command as player!");
if (!sender.hasPermission(command.getName() + ".clearItem")) {
sender.spigot().sendMessage(new ComponentBuilder(customPrefixFail)
.append(new TranslatableComponent("commands.help.failed")).color(ChatColor.RED)
.build());
return true;
}
if (!sender.hasPermission(command.getName() + ".clearItem")) {
sender.sendMessage(customPrefixFail + " You don't have permission to perform that command!");
if (player == null) {
sender.spigot().sendMessage(new ComponentBuilder(customPrefixFail)
.append(new TranslatableComponent("permissions.requires.player")).color(ChatColor.RED)
.build());
return true;
}
runClearItem(player);
break;

case "getHash":
if (!sender.hasPermission(command.getName() + ".getHash")) {
sender.spigot().sendMessage(new ComponentBuilder(customPrefixFail)
.append(new TranslatableComponent("commands.help.failed")).color(ChatColor.RED)
.build());
return true;
}
if (player == null) {
sender.sendMessage(customPrefixFail + " You can only this command as player!");
sender.spigot().sendMessage(new ComponentBuilder(customPrefixFail)
.append(new TranslatableComponent("permissions.requires.player")).color(ChatColor.RED)
.build());
return true;
}
if (!sender.hasPermission(command.getName() + ".getHash")) {
sender.sendMessage(customPrefixFail + " You don't have permission to perform that command!");
if (args.length == 1) {
runGetHash(player);
break;
}
if (args.length < 4) {
player.spigot().sendMessage(new ComponentBuilder(customPrefixFail)
.append(new TranslatableComponent("argument.pos3d.incomplete")).color(ChatColor.RED)
.build());
return true;
}
try {
runGetHash(sender, player.getWorld().getBlockAt(Integer.parseInt(args[1]),
Integer.parseInt(args[2]), Integer.parseInt(args[3])));
} catch (NumberFormatException e) {
player.spigot().sendMessage(new ComponentBuilder(customPrefixFail)
.append(new TranslatableComponent("parsing.int.expected")).color(ChatColor.RED)
.build());
return true;
}
runGetHash(player);
break;

default:
sender.spigot().sendMessage(new ComponentBuilder(customPrefixFail)
.append(new TranslatableComponent("commands.help.failed")).color(ChatColor.RED)
.build());
break;
}
return true;
}

@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
Player player = null;
if (sender instanceof Player)
player = (Player) sender;

LinkedList<String> autoComplete = new LinkedList<String>();
if (args.length == 1) {
if (sender.hasPermission(command.getName() + ".reload")) {
Expand All @@ -100,21 +142,42 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
autoComplete.add("getHash");
}
}
if (args.length > 1) {
if (args[0].equals("getHash") && player != null && sender.hasPermission(command.getName() + ".getHash")) {
Block block = player.getTargetBlockExact(5);
if (block != null) {
switch (args.length) {
case 2:
autoComplete.add(block.getX() + "");
autoComplete.add(block.getX() + " " + block.getY());
autoComplete.add(block.getX() + " " + block.getY() + " " + block.getZ());
break;
case 3:
autoComplete.add(block.getY() + "");
autoComplete.add(block.getY() + " " + block.getZ());
break;
case 4:
autoComplete.add(block.getZ() + "");
break;
}
}
}
}
return autoComplete;
}

public void runReload(CommandSender sender) {
reloadConfig();
eventListener.reloadConfig(getConfig());
sender.sendMessage(customPrefix + " Configuration reloaded!");
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 ")
player.spigot().sendMessage(new ComponentBuilder(customPrefixFail + "You arn't holding a ")
.append(new TranslatableComponent(Material.PLAYER_HEAD.getItemTranslationKey())).build());
return;
}
Expand All @@ -124,15 +187,15 @@ public void runClearItem(Player player) {
metaData.setNoteBlockSound(null);
item.setItemMeta(metaData);

player.sendMessage(customPrefix + " Sound cleared from item!");
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 ")
player.spigot().sendMessage(new ComponentBuilder(customPrefixFail + "You arn't holding a ")
.append(new TranslatableComponent(Material.PLAYER_HEAD.getItemTranslationKey())).build());
return;
}
Expand All @@ -141,14 +204,35 @@ public void runGetHash(Player player) {
PlayerProfile owner = metaData.getOwnerProfile();
// Check owner exist
if (owner == null) {
player.sendMessage(customPrefixFail + " Coudn't find any texture hash!");
player.sendMessage(customPrefixFail + "Coudn't find any texture hash!");
return;
}
runGetHash(player, owner);
}

String textureHash = EventListener.getTextureHash(owner);
public void runGetHash(CommandSender sender, Block block) {
// Check if the block above is a player head
if (block.getType() != Material.PLAYER_HEAD) {
sender.spigot().sendMessage(new ComponentBuilder(customPrefixFail + "Target block isn't a ")
.append(new TranslatableComponent(Material.PLAYER_HEAD.getItemTranslationKey())).build());
return;
}
// Get the skull data and texture hash for the player head
Skull skull = (Skull) block.getState();
PlayerProfile owner = skull.getOwnerProfile();
// Check owner exist
if (owner == null) {
sender.sendMessage(customPrefixFail + "Coudn't find any texture hash!");
return;
}
runGetHash(sender, 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")))
public void runGetHash(CommandSender sender, PlayerProfile owner) {
String textureHash = EventListener.getTextureHash(owner);
sender.spigot().sendMessage(new ComponentBuilder(customPrefix + "Hash: §r").append(textureHash)
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
new Text(new TranslatableComponent("chat.copy.click"))))
.event(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, textureHash)).build());
}
}
2 changes: 1 addition & 1 deletion 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.2
version: 1.2.1
authors: [thojo0, PYZ]
description: Allows the use of player heads for minecraft sounds
api-version: 1.20
Expand Down

0 comments on commit f311a57

Please sign in to comment.