From 89181972c252dce47a622c5a3111fbad67c9a0ac Mon Sep 17 00:00:00 2001 From: arnaudferreres Date: Thu, 22 Dec 2022 20:30:24 +0100 Subject: [PATCH] reload, join fix --- .../feusalamander/vmessage/Configuration.java | 30 +++++++++---- .../me/feusalamander/vmessage/Listeners.java | 45 ++++++++++--------- .../feusalamander/vmessage/ReloadCommand.java | 45 +++++++++++++++++++ .../me/feusalamander/vmessage/VMessage.java | 11 ++++- 4 files changed, 100 insertions(+), 31 deletions(-) create mode 100644 src/main/java/me/feusalamander/vmessage/ReloadCommand.java diff --git a/src/main/java/me/feusalamander/vmessage/Configuration.java b/src/main/java/me/feusalamander/vmessage/Configuration.java index 6afd241..82920cd 100644 --- a/src/main/java/me/feusalamander/vmessage/Configuration.java +++ b/src/main/java/me/feusalamander/vmessage/Configuration.java @@ -8,14 +8,15 @@ import java.nio.file.Path; import java.util.Objects; public final class Configuration { - private final String messageFormat; - private final String joinFormat; - private final String leaveFormat; - private final String changeFormat; - private final boolean messageEnabled; - private final boolean joinEnabled; - private final boolean leaveEnabled; - private final boolean changeEnabled; + private String messageFormat; + private String joinFormat; + private String leaveFormat; + private String changeFormat; + private boolean messageEnabled; + private boolean joinEnabled; + private boolean leaveEnabled; + private boolean changeEnabled; + private final Toml config; Configuration(Toml config) { messageFormat = config.getString("Message.format", ""); @@ -27,6 +28,8 @@ public final class Configuration { joinEnabled = config.getBoolean("Join.enabled", false); leaveEnabled = config.getBoolean("Leave.enabled", false); changeEnabled = config.getBoolean("Server-change.enabled", false); + this.config = config; + } static Configuration load(Path dataDirectory) { @@ -87,4 +90,15 @@ public final class Configuration { public boolean isChangeEnabled() { return this.changeEnabled; } + void reload(){ + messageFormat = config.getString("Message.format", ""); + joinFormat = config.getString("Join.format", ""); + leaveFormat = config.getString("Leave.format", ""); + changeFormat = config.getString("Server-change.format", ""); + + messageEnabled = config.getBoolean("Message.enabled", false); + joinEnabled = config.getBoolean("Join.enabled", false); + leaveEnabled = config.getBoolean("Leave.enabled", false); + changeEnabled = config.getBoolean("Server-change.enabled", false); + } } diff --git a/src/main/java/me/feusalamander/vmessage/Listeners.java b/src/main/java/me/feusalamander/vmessage/Listeners.java index 87167da..b566b67 100644 --- a/src/main/java/me/feusalamander/vmessage/Listeners.java +++ b/src/main/java/me/feusalamander/vmessage/Listeners.java @@ -3,8 +3,10 @@ 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.proxy.LoginPhaseConnection; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ServerConnection; @@ -15,6 +17,7 @@ import net.luckperms.api.LuckPermsProvider; import net.luckperms.api.model.user.User; import java.util.Objects; +import java.util.Optional; public final class Listeners { private static final LegacyComponentSerializer SERIALIZER = LegacyComponentSerializer.builder() @@ -55,21 +58,6 @@ public final class Listeners { } }); } - - @Subscribe - private void onJoin(PostLoginEvent e){ - if (!configuration.isJoinEnabled()) { - return; - } - Player p = e.getPlayer(); - String message = configuration.getJoinFormat().replace("#player#", p.getUsername()); - if (luckPermsAPI != null){ - User user = luckPermsAPI.getPlayerAdapter(Player.class).getUser(p); - message = message.replace("#prefix#", Objects.requireNonNull(user.getCachedData().getMetaData().getPrefix())); - } - proxyServer.sendMessage(SERIALIZER.deserialize(message)); - } - @Subscribe private void onLeave(DisconnectEvent e){ if (!configuration.isLeaveEnabled()) { @@ -87,21 +75,36 @@ public final class Listeners { @Subscribe private void onChange(ServerConnectedEvent e){ - if (!configuration.isChangeEnabled()) { + if (!configuration.isChangeEnabled()&&!configuration.isJoinEnabled()) { return; } - e.getPreviousServer().ifPresent(server -> { - RegisteredServer actual = e.getServer(); - Player p = e.getPlayer(); + Optional server = e.getPreviousServer(); + Player p = e.getPlayer(); + RegisteredServer actual = e.getServer(); + if(server.isPresent()){ + if (!configuration.isChangeEnabled()) { + return; + } + RegisteredServer pre = server.get(); String message = configuration.getChangeFormat() .replace("#player#", p.getUsername()) - .replace("#oldserver#", server.getServerInfo().getName()) + .replace("#oldserver#", pre.getServerInfo().getName()) .replace("#server#", actual.getServerInfo().getName()); if (luckPermsAPI != null){ User user = luckPermsAPI.getPlayerAdapter(Player.class).getUser(p); message = message.replace("#prefix#", Objects.requireNonNull(user.getCachedData().getMetaData().getPrefix())); } proxyServer.sendMessage(SERIALIZER.deserialize(message)); - }); + }else{ + if (!configuration.isJoinEnabled()) { + return; + } + String message = configuration.getJoinFormat().replace("#player#", p.getUsername()); + if (luckPermsAPI != null){ + User user = luckPermsAPI.getPlayerAdapter(Player.class).getUser(p); + message = message.replace("#prefix#", Objects.requireNonNull(user.getCachedData().getMetaData().getPrefix())); + } + proxyServer.sendMessage(SERIALIZER.deserialize(message)); + } } } diff --git a/src/main/java/me/feusalamander/vmessage/ReloadCommand.java b/src/main/java/me/feusalamander/vmessage/ReloadCommand.java new file mode 100644 index 0000000..9543d5d --- /dev/null +++ b/src/main/java/me/feusalamander/vmessage/ReloadCommand.java @@ -0,0 +1,45 @@ +package me.feusalamander.vmessage; +import com.velocitypowered.api.command.CommandSource; +import com.velocitypowered.api.command.SimpleCommand; +import com.velocitypowered.api.plugin.annotation.DataDirectory; + +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){ + this.dataDirectory = dataDirectory; + this.config = config; + } + + @Override + public void execute(final Invocation invocation) { + CommandSource source = invocation.source(); + String[] args = invocation.arguments(); + if(args.length == 1){ + return; + } + String s = args[0]; + if(s.equalsIgnoreCase("reload")){ + config.reload(); + } + } + + @Override + public boolean hasPermission(final Invocation invocation) { + return invocation.source().hasPermission("*"); + } + + @Override + public List suggest(final Invocation invocation) { + return List.of("reload"); + } + + @Override + public CompletableFuture> suggestAsync(final Invocation invocation) { + return CompletableFuture.completedFuture(List.of("reload")); + } +} diff --git a/src/main/java/me/feusalamander/vmessage/VMessage.java b/src/main/java/me/feusalamander/vmessage/VMessage.java index 4658e0e..28448fb 100644 --- a/src/main/java/me/feusalamander/vmessage/VMessage.java +++ b/src/main/java/me/feusalamander/vmessage/VMessage.java @@ -1,6 +1,9 @@ package me.feusalamander.vmessage; import com.google.inject.Inject; +import com.velocitypowered.api.command.CommandManager; +import com.velocitypowered.api.command.CommandMeta; +import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.plugin.Dependency; @@ -35,13 +38,17 @@ public class VMessage { @Subscribe private void onProxyInitialization(ProxyInitializeEvent event) { Configuration configuration = Configuration.load(dataDirectory); - if (configuration == null) { return; } - metricsFactory.make(this, 16527); proxy.getEventManager().register(this, new Listeners(proxy, configuration)); logger.info("Vmessage by FeuSalamander is working !"); + CommandManager commandManager = proxy.getCommandManager(); + CommandMeta commandMeta = commandManager.metaBuilder("Vmessage") + .plugin(this) + .build(); + SimpleCommand command = new ReloadCommand(dataDirectory, configuration); + commandManager.register(commandMeta, command); } }