From 25d0fdaa298aeed072209ebe09ce7af906f3f943 Mon Sep 17 00:00:00 2001 From: Adrian Date: Sun, 29 Jan 2023 12:15:05 -0500 Subject: [PATCH 1/2] Improved MiniMessage integration Removed some legacy color usages in components --- pom.xml | 7 +- .../feusalamander/vmessage/Configuration.java | 4 - .../me/feusalamander/vmessage/Listeners.java | 75 +++++++++---------- .../feusalamander/vmessage/ReloadCommand.java | 18 ++--- .../feusalamander/vmessage/SendCommand.java | 17 ++--- 5 files changed, 55 insertions(+), 66 deletions(-) diff --git a/pom.xml b/pom.xml index 3793bf1..9ec0e9d 100644 --- a/pom.xml +++ b/pom.xml @@ -157,7 +157,7 @@ com.velocitypowered velocity-api - 3.1.1 + 3.1.2-SNAPSHOT provided @@ -166,10 +166,5 @@ 5.4 provided - - net.kyori - adventure-text-minimessage - 4.12.0 - diff --git a/src/main/java/me/feusalamander/vmessage/Configuration.java b/src/main/java/me/feusalamander/vmessage/Configuration.java index ade23a7..7b72f5a 100644 --- a/src/main/java/me/feusalamander/vmessage/Configuration.java +++ b/src/main/java/me/feusalamander/vmessage/Configuration.java @@ -1,10 +1,6 @@ package me.feusalamander.vmessage; import com.moandjiezana.toml.Toml; -import com.velocitypowered.api.command.CommandSource; -import com.velocitypowered.api.command.SimpleCommand; -import com.velocitypowered.api.proxy.Player; -import net.kyori.adventure.text.Component; import java.io.File; import java.io.InputStream; diff --git a/src/main/java/me/feusalamander/vmessage/Listeners.java b/src/main/java/me/feusalamander/vmessage/Listeners.java index 6be8f9a..fa8bb9d 100644 --- a/src/main/java/me/feusalamander/vmessage/Listeners.java +++ b/src/main/java/me/feusalamander/vmessage/Listeners.java @@ -2,24 +2,19 @@ package me.feusalamander.vmessage; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.connection.DisconnectEvent; -import com.velocitypowered.api.event.connection.PostLoginEvent; -import com.velocitypowered.api.event.connection.PreLoginEvent; import com.velocitypowered.api.event.player.PlayerChatEvent; import com.velocitypowered.api.event.player.ServerConnectedEvent; -import com.velocitypowered.api.event.player.TabCompleteEvent; -import com.velocitypowered.api.proxy.LoginPhaseConnection; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ServerConnection; import com.velocitypowered.api.proxy.server.RegisteredServer; +import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.luckperms.api.LuckPerms; import net.luckperms.api.LuckPermsProvider; import net.luckperms.api.model.user.User; -import java.text.SimpleDateFormat; -import java.time.LocalDateTime; import java.util.Objects; import java.util.Optional; @@ -34,7 +29,7 @@ public final class Listeners { private final ProxyServer proxyServer; Listeners(ProxyServer proxyServer, Configuration configuration) { - if (proxyServer.getPluginManager().getPlugin("luckperms").isPresent()){ + if (proxyServer.getPluginManager().getPlugin("luckperms").isPresent()) { this.luckPermsAPI = LuckPermsProvider.get(); } this.configuration = configuration; @@ -42,43 +37,44 @@ public final class Listeners { } @Subscribe - private void onMessage(PlayerChatEvent e){ + private void onMessage(PlayerChatEvent e) { if (!configuration.isMessageEnabled()) { return; } message(e.getPlayer(), e.getMessage()); } + @Subscribe - private void onLeave(DisconnectEvent e){ + private void onLeave(DisconnectEvent e) { if (!configuration.isLeaveEnabled()) { return; } Player p = e.getPlayer(); - if(p.getCurrentServer().isEmpty()){ + if (p.getCurrentServer().isEmpty()) { return; } String message = configuration.getLeaveFormat() .replace("#player#", p.getUsername()); - if (luckPermsAPI != null){ + if (luckPermsAPI != null) { message = luckperms(message, p); } - if(configuration.isMinimessageEnabled()){ - proxyServer.sendMessage(mm.deserialize(message.replaceAll("§", ""))); - }else{ + if (configuration.isMinimessageEnabled()) { + proxyServer.sendMessage(mm.deserialize(message.replace("§", ""))); + } else { proxyServer.sendMessage(SERIALIZER.deserialize(message)); } } @Subscribe - private void onChange(ServerConnectedEvent e){ - if (!configuration.isChangeEnabled()&&!configuration.isJoinEnabled()) { + private void onChange(ServerConnectedEvent e) { + if (!configuration.isChangeEnabled() && !configuration.isJoinEnabled()) { return; } Optional server = e.getPreviousServer(); Player p = e.getPlayer(); RegisteredServer actual = e.getServer(); - if(server.isPresent()){ + if (server.isPresent()) { if (!configuration.isChangeEnabled()) { return; } @@ -87,55 +83,58 @@ public final class Listeners { .replace("#player#", p.getUsername()) .replace("#oldserver#", pre.getServerInfo().getName()) .replace("#server#", actual.getServerInfo().getName()); - if (luckPermsAPI != null){ + if (luckPermsAPI != null) { message = luckperms(message, p); } - if(configuration.isMinimessageEnabled()){ - proxyServer.sendMessage(mm.deserialize(message.replaceAll("§", ""))); - }else{ + if (configuration.isMinimessageEnabled()) { + proxyServer.sendMessage(mm.deserialize(message.replace("§", ""))); + } else { proxyServer.sendMessage(SERIALIZER.deserialize(message)); } - }else{ + } else { if (!configuration.isJoinEnabled()) { return; } String message = configuration.getJoinFormat().replace("#player#", p.getUsername()); - if (luckPermsAPI != null){ + if (luckPermsAPI != null) { message = luckperms(message, p); } - if(configuration.isMinimessageEnabled()){ - proxyServer.sendMessage(mm.deserialize(message.replaceAll("§", ""))); - }else{ + if (configuration.isMinimessageEnabled()) { + proxyServer.sendMessage(mm.deserialize(message.replace("§", ""))); + } else { proxyServer.sendMessage(SERIALIZER.deserialize(message)); } } } - private String luckperms(String message, Player p){ + + private String luckperms(String message, Player p) { User user = luckPermsAPI.getPlayerAdapter(Player.class).getUser(p); - if(message.contains("#prefix#")){ + if (message.contains("#prefix#")) { message = message.replace("#prefix#", Objects.requireNonNull(user.getCachedData().getMetaData().getPrefix())); } - if(message.contains("#suffix#")){ + if (message.contains("#suffix#")) { message = message.replace("#suffix#", Objects.requireNonNull(user.getCachedData().getMetaData().getSuffix())); } return message; } - public void message(Player p, String m){ + + public void message(Player p, String m) { String message = configuration.getMessageFormat() .replace("#player#", p.getUsername()) .replace("#message#", m) .replace("#server#", p.getCurrentServer().orElseThrow().getServerInfo().getName()); - if (luckPermsAPI != null){ + if (luckPermsAPI != null) { message = luckperms(message, p); } - final String finalMessage = message; + final Component finalMessage; + if (configuration.isMinimessageEnabled()) { + finalMessage = mm.deserialize(message.replace("§", "")); + } else { + finalMessage = SERIALIZER.deserialize(message); + } proxyServer.getAllServers().forEach(server -> { - if (!Objects.equals(p.getCurrentServer().map(ServerConnection::getServerInfo).orElse(null), server.getServerInfo())){ - if(configuration.isMinimessageEnabled()){ - server.sendMessage(mm.deserialize(finalMessage.replaceAll("§", ""))); - }else{ - server.sendMessage(SERIALIZER.deserialize(finalMessage)); - } + if (!Objects.equals(p.getCurrentServer().map(ServerConnection::getServerInfo).orElse(null), server.getServerInfo())) { + server.sendMessage(finalMessage); } }); } diff --git a/src/main/java/me/feusalamander/vmessage/ReloadCommand.java b/src/main/java/me/feusalamander/vmessage/ReloadCommand.java index 2349ffc..47bff7b 100644 --- a/src/main/java/me/feusalamander/vmessage/ReloadCommand.java +++ b/src/main/java/me/feusalamander/vmessage/ReloadCommand.java @@ -1,17 +1,17 @@ package me.feusalamander.vmessage; + import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.format.TextColor; +import net.kyori.adventure.text.format.NamedTextColor; import java.nio.file.Path; -import java.util.concurrent.CompletableFuture; -import java.util.List; public final class ReloadCommand implements SimpleCommand { final Path dataDirectory; final Configuration config; - ReloadCommand(Path dataDirectory, Configuration config){ + + ReloadCommand(Path dataDirectory, Configuration config) { this.dataDirectory = dataDirectory; this.config = config; } @@ -20,14 +20,14 @@ public final class ReloadCommand implements SimpleCommand { public void execute(final Invocation invocation) { CommandSource source = invocation.source(); String[] args = invocation.arguments(); - if(args.length == 0){ - source.sendMessage(Component.text("§cUsage: /vmessage reload")); + if (args.length == 0) { + source.sendMessage(Component.text("Usage: /vmessage reload", NamedTextColor.RED)); return; } String s = args[0]; - if(s.equalsIgnoreCase("reload")){ - if(!source.hasPermission("*")){ - source.sendMessage(Component.text("§cYou don't have the permission to do that")); + if (s.equalsIgnoreCase("reload")) { + if (!source.hasPermission("*")) { + source.sendMessage(Component.text("You don't have the permission to do that", NamedTextColor.RED)); return; } config.reload(); diff --git a/src/main/java/me/feusalamander/vmessage/SendCommand.java b/src/main/java/me/feusalamander/vmessage/SendCommand.java index 58019a9..7d66fbd 100644 --- a/src/main/java/me/feusalamander/vmessage/SendCommand.java +++ b/src/main/java/me/feusalamander/vmessage/SendCommand.java @@ -4,28 +4,27 @@ import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.proxy.Player; import net.kyori.adventure.text.Component; - -import java.nio.file.Path; -import java.util.List; -import java.util.concurrent.CompletableFuture; +import net.kyori.adventure.text.format.NamedTextColor; public final class SendCommand implements SimpleCommand { VMessage main; - SendCommand(VMessage main){ + + SendCommand(VMessage main) { this.main = main; } + @Override public void execute(final Invocation invocation) { CommandSource source = invocation.source(); String[] args = invocation.arguments(); - if(!(source instanceof Player)){ + if (!(source instanceof Player)) { return; } - if(args.length == 0){ - source.sendMessage(Component.text("§cUsage: /sendall *your message*")); + if (args.length == 0) { + source.sendMessage(Component.text("Usage: /sendall *your message*", NamedTextColor.RED)); return; } - Player p = (Player)source; + Player p = (Player) source; String s = args[0]; main.listeners.message(p, s); } From 6fdd81c9330c36a9821d4027a771b9bb69df4e5b Mon Sep 17 00:00:00 2001 From: Adrian Date: Sun, 29 Jan 2023 13:41:56 -0500 Subject: [PATCH 2/2] Implement tab complete and permissions check on commands --- .../feusalamander/vmessage/ReloadCommand.java | 34 ++++++++++++------- .../feusalamander/vmessage/SendCommand.java | 18 +++++----- .../me/feusalamander/vmessage/VMessage.java | 2 +- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/main/java/me/feusalamander/vmessage/ReloadCommand.java b/src/main/java/me/feusalamander/vmessage/ReloadCommand.java index 47bff7b..28ccab2 100644 --- a/src/main/java/me/feusalamander/vmessage/ReloadCommand.java +++ b/src/main/java/me/feusalamander/vmessage/ReloadCommand.java @@ -5,33 +5,43 @@ import com.velocitypowered.api.command.SimpleCommand; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; -import java.nio.file.Path; +import java.util.List; public final class ReloadCommand implements SimpleCommand { - final Path dataDirectory; - final Configuration config; + private final Configuration config; - ReloadCommand(Path dataDirectory, Configuration config) { - this.dataDirectory = dataDirectory; + ReloadCommand(Configuration config) { this.config = config; } @Override public void execute(final Invocation invocation) { - CommandSource source = invocation.source(); - String[] args = invocation.arguments(); + final CommandSource source = invocation.source(); + final String[] args = invocation.arguments(); if (args.length == 0) { source.sendMessage(Component.text("Usage: /vmessage reload", NamedTextColor.RED)); return; } - String s = args[0]; + final String s = args[0]; if (s.equalsIgnoreCase("reload")) { - if (!source.hasPermission("*")) { - source.sendMessage(Component.text("You don't have the permission to do that", NamedTextColor.RED)); - return; - } config.reload(); source.sendMessage(Component.text("The Vmessage's config has been succefully reloaded")); } } + + @Override + public boolean hasPermission(final Invocation invocation) { + return invocation.source().hasPermission("vmessage.command"); + } + + private static final List suggestion = List.of("reload"); + + @Override + public List suggest(final Invocation invocation) { + final String[] args = invocation.arguments(); + if (args.length == 0 || (args.length == 1 && "reload".startsWith(args[0]))) { + return suggestion; + } + return List.of(); + } } diff --git a/src/main/java/me/feusalamander/vmessage/SendCommand.java b/src/main/java/me/feusalamander/vmessage/SendCommand.java index 7d66fbd..b4ff4f1 100644 --- a/src/main/java/me/feusalamander/vmessage/SendCommand.java +++ b/src/main/java/me/feusalamander/vmessage/SendCommand.java @@ -7,7 +7,7 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; public final class SendCommand implements SimpleCommand { - VMessage main; + private final VMessage main; SendCommand(VMessage main) { this.main = main; @@ -15,17 +15,19 @@ public final class SendCommand implements SimpleCommand { @Override public void execute(final Invocation invocation) { - CommandSource source = invocation.source(); - String[] args = invocation.arguments(); - if (!(source instanceof Player)) { - return; - } + final CommandSource source = invocation.source(); + final String[] args = invocation.arguments(); if (args.length == 0) { source.sendMessage(Component.text("Usage: /sendall *your message*", NamedTextColor.RED)); return; } - Player p = (Player) source; - String s = args[0]; + final Player p = (Player) source; + final String s = args[0]; main.listeners.message(p, s); } + + @Override + public boolean hasPermission(final Invocation invocation) { + return invocation.source() instanceof Player; + } } diff --git a/src/main/java/me/feusalamander/vmessage/VMessage.java b/src/main/java/me/feusalamander/vmessage/VMessage.java index 51b5e10..4f4657b 100644 --- a/src/main/java/me/feusalamander/vmessage/VMessage.java +++ b/src/main/java/me/feusalamander/vmessage/VMessage.java @@ -50,7 +50,7 @@ public class VMessage { CommandMeta commandMeta = commandManager.metaBuilder("Vmessage") .plugin(this) .build(); - SimpleCommand command = new ReloadCommand(dataDirectory, configuration); + SimpleCommand command = new ReloadCommand(configuration); commandManager.register(commandMeta, command); CommandMeta sendmeta = commandManager.metaBuilder("sendall") .plugin(this)