reload, join fix

This commit is contained in:
arnaudferreres 2022-12-22 20:30:24 +01:00
parent 6608ac8efa
commit 89181972c2
4 changed files with 100 additions and 31 deletions

View File

@ -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);
}
}

View File

@ -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<RegisteredServer> 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));
}
}
}

View File

@ -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<String> suggest(final Invocation invocation) {
return List.of("reload");
}
@Override
public CompletableFuture<List<String>> suggestAsync(final Invocation invocation) {
return CompletableFuture.completedFuture(List.of("reload"));
}
}

View File

@ -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);
}
}