Improved MiniMessage integration

Removed some legacy color usages in components
This commit is contained in:
Adrian 2023-01-29 12:15:05 -05:00
parent a7f97ea105
commit 25d0fdaa29
5 changed files with 55 additions and 66 deletions

View File

@ -157,7 +157,7 @@
<dependency> <dependency>
<groupId>com.velocitypowered</groupId> <groupId>com.velocitypowered</groupId>
<artifactId>velocity-api</artifactId> <artifactId>velocity-api</artifactId>
<version>3.1.1</version> <version>3.1.2-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
@ -166,10 +166,5 @@
<version>5.4</version> <version>5.4</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-minimessage</artifactId>
<version>4.12.0</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,10 +1,6 @@
package me.feusalamander.vmessage; package me.feusalamander.vmessage;
import com.moandjiezana.toml.Toml; 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.File;
import java.io.InputStream; import java.io.InputStream;

View File

@ -2,24 +2,19 @@ package me.feusalamander.vmessage;
import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.connection.DisconnectEvent; 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.PlayerChatEvent;
import com.velocitypowered.api.event.player.ServerConnectedEvent; 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.Player;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.ServerConnection; import com.velocitypowered.api.proxy.ServerConnection;
import com.velocitypowered.api.proxy.server.RegisteredServer; 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.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.luckperms.api.LuckPerms; import net.luckperms.api.LuckPerms;
import net.luckperms.api.LuckPermsProvider; import net.luckperms.api.LuckPermsProvider;
import net.luckperms.api.model.user.User; import net.luckperms.api.model.user.User;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
@ -34,7 +29,7 @@ public final class Listeners {
private final ProxyServer proxyServer; private final ProxyServer proxyServer;
Listeners(ProxyServer proxyServer, Configuration configuration) { Listeners(ProxyServer proxyServer, Configuration configuration) {
if (proxyServer.getPluginManager().getPlugin("luckperms").isPresent()){ if (proxyServer.getPluginManager().getPlugin("luckperms").isPresent()) {
this.luckPermsAPI = LuckPermsProvider.get(); this.luckPermsAPI = LuckPermsProvider.get();
} }
this.configuration = configuration; this.configuration = configuration;
@ -42,43 +37,44 @@ public final class Listeners {
} }
@Subscribe @Subscribe
private void onMessage(PlayerChatEvent e){ private void onMessage(PlayerChatEvent e) {
if (!configuration.isMessageEnabled()) { if (!configuration.isMessageEnabled()) {
return; return;
} }
message(e.getPlayer(), e.getMessage()); message(e.getPlayer(), e.getMessage());
} }
@Subscribe @Subscribe
private void onLeave(DisconnectEvent e){ private void onLeave(DisconnectEvent e) {
if (!configuration.isLeaveEnabled()) { if (!configuration.isLeaveEnabled()) {
return; return;
} }
Player p = e.getPlayer(); Player p = e.getPlayer();
if(p.getCurrentServer().isEmpty()){ if (p.getCurrentServer().isEmpty()) {
return; return;
} }
String message = configuration.getLeaveFormat() String message = configuration.getLeaveFormat()
.replace("#player#", p.getUsername()); .replace("#player#", p.getUsername());
if (luckPermsAPI != null){ if (luckPermsAPI != null) {
message = luckperms(message, p); message = luckperms(message, p);
} }
if(configuration.isMinimessageEnabled()){ if (configuration.isMinimessageEnabled()) {
proxyServer.sendMessage(mm.deserialize(message.replaceAll("§", ""))); proxyServer.sendMessage(mm.deserialize(message.replace("§", "")));
}else{ } else {
proxyServer.sendMessage(SERIALIZER.deserialize(message)); proxyServer.sendMessage(SERIALIZER.deserialize(message));
} }
} }
@Subscribe @Subscribe
private void onChange(ServerConnectedEvent e){ private void onChange(ServerConnectedEvent e) {
if (!configuration.isChangeEnabled()&&!configuration.isJoinEnabled()) { if (!configuration.isChangeEnabled() && !configuration.isJoinEnabled()) {
return; return;
} }
Optional<RegisteredServer> server = e.getPreviousServer(); Optional<RegisteredServer> server = e.getPreviousServer();
Player p = e.getPlayer(); Player p = e.getPlayer();
RegisteredServer actual = e.getServer(); RegisteredServer actual = e.getServer();
if(server.isPresent()){ if (server.isPresent()) {
if (!configuration.isChangeEnabled()) { if (!configuration.isChangeEnabled()) {
return; return;
} }
@ -87,55 +83,58 @@ public final class Listeners {
.replace("#player#", p.getUsername()) .replace("#player#", p.getUsername())
.replace("#oldserver#", pre.getServerInfo().getName()) .replace("#oldserver#", pre.getServerInfo().getName())
.replace("#server#", actual.getServerInfo().getName()); .replace("#server#", actual.getServerInfo().getName());
if (luckPermsAPI != null){ if (luckPermsAPI != null) {
message = luckperms(message, p); message = luckperms(message, p);
} }
if(configuration.isMinimessageEnabled()){ if (configuration.isMinimessageEnabled()) {
proxyServer.sendMessage(mm.deserialize(message.replaceAll("§", ""))); proxyServer.sendMessage(mm.deserialize(message.replace("§", "")));
}else{ } else {
proxyServer.sendMessage(SERIALIZER.deserialize(message)); proxyServer.sendMessage(SERIALIZER.deserialize(message));
} }
}else{ } else {
if (!configuration.isJoinEnabled()) { if (!configuration.isJoinEnabled()) {
return; return;
} }
String message = configuration.getJoinFormat().replace("#player#", p.getUsername()); String message = configuration.getJoinFormat().replace("#player#", p.getUsername());
if (luckPermsAPI != null){ if (luckPermsAPI != null) {
message = luckperms(message, p); message = luckperms(message, p);
} }
if(configuration.isMinimessageEnabled()){ if (configuration.isMinimessageEnabled()) {
proxyServer.sendMessage(mm.deserialize(message.replaceAll("§", ""))); proxyServer.sendMessage(mm.deserialize(message.replace("§", "")));
}else{ } else {
proxyServer.sendMessage(SERIALIZER.deserialize(message)); 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); 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())); 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())); message = message.replace("#suffix#", Objects.requireNonNull(user.getCachedData().getMetaData().getSuffix()));
} }
return message; return message;
} }
public void message(Player p, String m){
public void message(Player p, String m) {
String message = configuration.getMessageFormat() String message = configuration.getMessageFormat()
.replace("#player#", p.getUsername()) .replace("#player#", p.getUsername())
.replace("#message#", m) .replace("#message#", m)
.replace("#server#", p.getCurrentServer().orElseThrow().getServerInfo().getName()); .replace("#server#", p.getCurrentServer().orElseThrow().getServerInfo().getName());
if (luckPermsAPI != null){ if (luckPermsAPI != null) {
message = luckperms(message, p); message = luckperms(message, p);
} }
final String finalMessage = message; final Component finalMessage;
proxyServer.getAllServers().forEach(server -> { if (configuration.isMinimessageEnabled()) {
if (!Objects.equals(p.getCurrentServer().map(ServerConnection::getServerInfo).orElse(null), server.getServerInfo())){ finalMessage = mm.deserialize(message.replace("§", ""));
if(configuration.isMinimessageEnabled()){ } else {
server.sendMessage(mm.deserialize(finalMessage.replaceAll("§", ""))); finalMessage = SERIALIZER.deserialize(message);
}else{
server.sendMessage(SERIALIZER.deserialize(finalMessage));
} }
proxyServer.getAllServers().forEach(server -> {
if (!Objects.equals(p.getCurrentServer().map(ServerConnection::getServerInfo).orElse(null), server.getServerInfo())) {
server.sendMessage(finalMessage);
} }
}); });
} }

View File

@ -1,17 +1,17 @@
package me.feusalamander.vmessage; package me.feusalamander.vmessage;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.command.SimpleCommand;
import net.kyori.adventure.text.Component; 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.nio.file.Path;
import java.util.concurrent.CompletableFuture;
import java.util.List;
public final class ReloadCommand implements SimpleCommand { public final class ReloadCommand implements SimpleCommand {
final Path dataDirectory; final Path dataDirectory;
final Configuration config; final Configuration config;
ReloadCommand(Path dataDirectory, Configuration config){
ReloadCommand(Path dataDirectory, Configuration config) {
this.dataDirectory = dataDirectory; this.dataDirectory = dataDirectory;
this.config = config; this.config = config;
} }
@ -20,14 +20,14 @@ public final class ReloadCommand implements SimpleCommand {
public void execute(final Invocation invocation) { public void execute(final Invocation invocation) {
CommandSource source = invocation.source(); CommandSource source = invocation.source();
String[] args = invocation.arguments(); String[] args = invocation.arguments();
if(args.length == 0){ if (args.length == 0) {
source.sendMessage(Component.text("§cUsage: /vmessage reload")); source.sendMessage(Component.text("Usage: /vmessage reload", NamedTextColor.RED));
return; return;
} }
String s = args[0]; String s = args[0];
if(s.equalsIgnoreCase("reload")){ if (s.equalsIgnoreCase("reload")) {
if(!source.hasPermission("*")){ if (!source.hasPermission("*")) {
source.sendMessage(Component.text("§cYou don't have the permission to do that")); source.sendMessage(Component.text("You don't have the permission to do that", NamedTextColor.RED));
return; return;
} }
config.reload(); config.reload();

View File

@ -4,28 +4,27 @@ import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.CompletableFuture;
public final class SendCommand implements SimpleCommand { public final class SendCommand implements SimpleCommand {
VMessage main; VMessage main;
SendCommand(VMessage main){
SendCommand(VMessage main) {
this.main = main; this.main = main;
} }
@Override @Override
public void execute(final Invocation invocation) { public void execute(final Invocation invocation) {
CommandSource source = invocation.source(); CommandSource source = invocation.source();
String[] args = invocation.arguments(); String[] args = invocation.arguments();
if(!(source instanceof Player)){ if (!(source instanceof Player)) {
return; return;
} }
if(args.length == 0){ if (args.length == 0) {
source.sendMessage(Component.text("§cUsage: /sendall *your message*")); source.sendMessage(Component.text("Usage: /sendall *your message*", NamedTextColor.RED));
return; return;
} }
Player p = (Player)source; Player p = (Player) source;
String s = args[0]; String s = args[0];
main.listeners.message(p, s); main.listeners.message(p, s);
} }