diff --git a/src/main/java/eu/m724/tweaks/module/wordcoords/WordCoordsModule.java b/src/main/java/eu/m724/tweaks/module/wordcoords/WordCoordsModule.java index a052b42..603b33d 100644 --- a/src/main/java/eu/m724/tweaks/module/wordcoords/WordCoordsModule.java +++ b/src/main/java/eu/m724/tweaks/module/wordcoords/WordCoordsModule.java @@ -19,13 +19,17 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.jetbrains.annotations.NotNull; import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.NoSuchElementException; -public class WordCoordsModule extends TweaksModule implements CommandExecutor { +public class WordCoordsModule extends TweaksModule implements CommandExecutor, Listener { private WordList wordList; private WordCoordsConverter converter; @@ -40,6 +44,7 @@ public class WordCoordsModule extends TweaksModule implements CommandExecutor { this.converter = new WordCoordsConverter(wordList); registerCommand("wordcoords", this); + registerEvents(this); } @Override @@ -59,28 +64,21 @@ public class WordCoordsModule extends TweaksModule implements CommandExecutor { z = player.getLocation().getBlockZ(); encode = true; - } else { - if (args.length > 1) { - try { - double dx = Double.parseDouble(args[0]); - double dz = Double.parseDouble(args[args.length > 2 ? 2 : 1]); + } else if (args.length > 1) { + try { + double dx = Double.parseDouble(args[0]); + double dz = Double.parseDouble(args[args.length > 2 ? 2 : 1]); - if (dx > Integer.MAX_VALUE || dx < Integer.MIN_VALUE || dz > Integer.MAX_VALUE || dz < Integer.MIN_VALUE) { - sender.spigot().sendMessage(Language.getComponent("wordCoordsOutOfRange", ChatColor.RED)); - return true; - } + if (dx > Integer.MAX_VALUE || dx < Integer.MIN_VALUE || dz > Integer.MAX_VALUE || dz < Integer.MIN_VALUE) { + sender.spigot().sendMessage(Language.getComponent("wordCoordsOutOfRange", ChatColor.RED)); + return true; + } - x = (int) dx; - z = (int) dz; + x = (int) dx; + z = (int) dz; - encode = true; - } catch (NumberFormatException ignored) { } - } - - if (!encode) { - String strArgs = String.join(" ", args); - words = smartDetectWords(strArgs); - } + encode = true; + } catch (NumberFormatException ignored) { } } if (encode) { @@ -98,9 +96,22 @@ public class WordCoordsModule extends TweaksModule implements CommandExecutor { sender.spigot().sendMessage(components); } else { - int[] xz = converter.decode(words); - x = xz[0]; - z = xz[1]; + String strArgs = String.join(" ", args); + words = smartDetectWords(strArgs); + + if (words.length == 0) { + sender.spigot().sendMessage(Language.getComponent("wordCoordsNoWords", ChatColor.GRAY)); + return true; + } + + try { + int[] xz = converter.decode(words); + x = xz[0]; + z = xz[1]; + } catch (NoSuchElementException e) { + sender.spigot().sendMessage(Language.getComponent("wordCoordsInvalidWord", ChatColor.RED, e.getMessage())); + return true; + } String encoded = "///" + String.join(".", words); @@ -109,7 +120,9 @@ public class WordCoordsModule extends TweaksModule implements CommandExecutor { .color(ChatColor.GRAY) .append("%d, %d".formatted(x, z)) .color(ChatColor.AQUA) // TODO improve color - .append("±8") + .event(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, "%d, %d".formatted(x, z))) + .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text("Click to copy"))) + .append(" ±8") .color(ChatColor.GRAY) .create(); sender.spigot().sendMessage(components); @@ -141,4 +154,13 @@ public class WordCoordsModule extends TweaksModule implements CommandExecutor { return words.toArray(String[]::new); } + + @EventHandler + public void onCommand(PlayerCommandPreprocessEvent event) { + if (event.getMessage().startsWith("///")) { + event.setCancelled(true); + + event.getPlayer().performCommand("wordcoords " + event.getMessage().substring(3)); + } + } } diff --git a/src/main/resources/strings.properties b/src/main/resources/strings.properties index e9dda4d..b628eab 100644 --- a/src/main/resources/strings.properties +++ b/src/main/resources/strings.properties @@ -42,4 +42,6 @@ durabilityDisabled = Disabled durability alert # When console executes /wordcoords without arguments wordCoordsPlayerOnly = Only players can execute this command without arguments. -wordCoordsOutOfRange = Those coordinates are invalid. \ No newline at end of file +wordCoordsOutOfRange = Those coordinates are invalid. +wordCoordsInvalidWord = Invalid word: "%s" +wordCoordsNoWords = Please provide the Z coordinate. \ No newline at end of file