From e912f64756ceef542ecc60f333751f9a2e48fcd7 Mon Sep 17 00:00:00 2001 From: 4drian3d Date: Sat, 17 Dec 2022 15:33:31 -0500 Subject: [PATCH] Improved Plugin Logic --- .../feusalamander/vmessage/Configuration.java | 90 ++++++++++ .../me/feusalamander/vmessage/Listeners.java | 107 ++++++++++++ .../me/feusalamander/vmessage/VMessage.java | 47 +++++ .../me/feusalamander/vmessage/vmessage.java | 161 ------------------ src/main/resources/config.toml | 8 +- 5 files changed, 248 insertions(+), 165 deletions(-) create mode 100644 src/main/java/me/feusalamander/vmessage/Configuration.java create mode 100644 src/main/java/me/feusalamander/vmessage/Listeners.java create mode 100644 src/main/java/me/feusalamander/vmessage/VMessage.java delete mode 100644 src/main/java/me/feusalamander/vmessage/vmessage.java diff --git a/src/main/java/me/feusalamander/vmessage/Configuration.java b/src/main/java/me/feusalamander/vmessage/Configuration.java new file mode 100644 index 0000000..6afd241 --- /dev/null +++ b/src/main/java/me/feusalamander/vmessage/Configuration.java @@ -0,0 +1,90 @@ +package me.feusalamander.vmessage; + +import com.moandjiezana.toml.Toml; + +import java.io.InputStream; +import java.nio.file.Files; +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; + + Configuration(Toml config) { + 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); + } + + static Configuration load(Path dataDirectory) { + Path f = createConfig(dataDirectory); + if (f != null) { + Toml config = new Toml().read(f.toFile()); + return new Configuration(config); + } + return null; + } + + private static Path createConfig(Path dataDirectory){ + try { + if (Files.notExists(dataDirectory)){ + Files.createDirectory(dataDirectory); + } + Path f = dataDirectory.resolve("config.toml"); + if (Files.notExists(f)){ + try (InputStream stream = Configuration.class.getResourceAsStream("/config.toml")) { + Files.copy(Objects.requireNonNull(stream), f); + } + } + return f; + } catch(Exception e) { + e.printStackTrace(); + return null; + } + } + + public String getMessageFormat() { + return this.messageFormat; + } + + public String getJoinFormat() { + return this.joinFormat; + } + + public String getLeaveFormat() { + return this.leaveFormat; + } + + public String getChangeFormat() { + return this.changeFormat; + } + + public boolean isMessageEnabled() { + return this.messageEnabled; + } + + public boolean isJoinEnabled() { + return this.joinEnabled; + } + + public boolean isLeaveEnabled() { + return this.leaveEnabled; + } + + public boolean isChangeEnabled() { + return this.changeEnabled; + } +} diff --git a/src/main/java/me/feusalamander/vmessage/Listeners.java b/src/main/java/me/feusalamander/vmessage/Listeners.java new file mode 100644 index 0000000..87167da --- /dev/null +++ b/src/main/java/me/feusalamander/vmessage/Listeners.java @@ -0,0 +1,107 @@ +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.player.PlayerChatEvent; +import com.velocitypowered.api.event.player.ServerConnectedEvent; +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.serializer.legacy.LegacyComponentSerializer; +import net.luckperms.api.LuckPerms; +import net.luckperms.api.LuckPermsProvider; +import net.luckperms.api.model.user.User; + +import java.util.Objects; + +public final class Listeners { + private static final LegacyComponentSerializer SERIALIZER = LegacyComponentSerializer.builder() + .character('&') + .hexColors() + .build(); + private LuckPerms luckPermsAPI; + private final Configuration configuration; + private final ProxyServer proxyServer; + + Listeners(ProxyServer proxyServer, Configuration configuration) { + if (proxyServer.getPluginManager().getPlugin("luckperms").isPresent()){ + this.luckPermsAPI = LuckPermsProvider.get(); + } + this.configuration = configuration; + this.proxyServer = proxyServer; + } + + @Subscribe + private void onMessage(PlayerChatEvent e){ + if (!configuration.isMessageEnabled()) { + return; + } + Player p = e.getPlayer(); + String m = e.getMessage(); + String message = configuration.getMessageFormat() + .replace("#player#", p.getUsername()) + .replace("#message#", m) + .replace("#server#", p.getCurrentServer().orElseThrow().getServerInfo().getName()); + if (luckPermsAPI != null){ + User user = luckPermsAPI.getPlayerAdapter(Player.class).getUser(p); + message = message.replace("#prefix#", Objects.requireNonNull(user.getCachedData().getMetaData().getPrefix())); + } + final String finalMessage = message; + proxyServer.getAllServers().forEach(server -> { + if (!Objects.equals(p.getCurrentServer().map(ServerConnection::getServerInfo).orElse(null), server.getServerInfo())){ + server.sendMessage(SERIALIZER.deserialize(finalMessage)); + } + }); + } + + @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()) { + return; + } + Player p = e.getPlayer(); + String message = configuration.getLeaveFormat() + .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 onChange(ServerConnectedEvent e){ + if (!configuration.isChangeEnabled()) { + return; + } + e.getPreviousServer().ifPresent(server -> { + RegisteredServer actual = e.getServer(); + Player p = e.getPlayer(); + String message = configuration.getChangeFormat() + .replace("#player#", p.getUsername()) + .replace("#oldserver#", server.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)); + }); + } +} diff --git a/src/main/java/me/feusalamander/vmessage/VMessage.java b/src/main/java/me/feusalamander/vmessage/VMessage.java new file mode 100644 index 0000000..4658e0e --- /dev/null +++ b/src/main/java/me/feusalamander/vmessage/VMessage.java @@ -0,0 +1,47 @@ +package me.feusalamander.vmessage; + +import com.google.inject.Inject; +import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; +import com.velocitypowered.api.event.Subscribe; +import com.velocitypowered.api.plugin.Dependency; +import com.velocitypowered.api.plugin.Plugin; +import com.velocitypowered.api.plugin.annotation.DataDirectory; +import com.velocitypowered.api.proxy.ProxyServer; +import org.slf4j.Logger; +import java.nio.file.Path; + +@Plugin( + id = "vmessage", + name = "Vmessage", + version = "1.2", + description = "A velocity plugin that creates a multi server chat for the network", + authors = {"FeuSalamander"}, + dependencies = { @Dependency(id = "luckperms", optional = true) } +) +public class VMessage { + private final ProxyServer proxy; + private final Logger logger; + private final Metrics.Factory metricsFactory; + private final Path dataDirectory; + + @Inject + public VMessage(ProxyServer proxy, Logger logger, Metrics.Factory metricsFactory, @DataDirectory Path dataDirectory) { + this.proxy = proxy; + this.logger = logger; + this.metricsFactory = metricsFactory; + this.dataDirectory = dataDirectory; + } + + @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 !"); + } +} diff --git a/src/main/java/me/feusalamander/vmessage/vmessage.java b/src/main/java/me/feusalamander/vmessage/vmessage.java deleted file mode 100644 index 5719589..0000000 --- a/src/main/java/me/feusalamander/vmessage/vmessage.java +++ /dev/null @@ -1,161 +0,0 @@ -package me.feusalamander.vmessage; -import com.google.inject.Inject; -import com.moandjiezana.toml.Toml; -import com.velocitypowered.api.event.connection.DisconnectEvent; -import com.velocitypowered.api.event.connection.LoginEvent; -import com.velocitypowered.api.event.player.*; -import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; -import com.velocitypowered.api.event.Subscribe; -import com.velocitypowered.api.plugin.Plugin; -import com.velocitypowered.api.plugin.annotation.DataDirectory; -import com.velocitypowered.api.proxy.Player; -import com.velocitypowered.api.proxy.ProxyServer; -import com.velocitypowered.api.proxy.server.RegisteredServer; -import net.kyori.adventure.text.Component; -import net.luckperms.api.LuckPerms; -import net.luckperms.api.LuckPermsProvider; -import net.luckperms.api.model.user.User; -import org.slf4j.Logger; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Objects; -import java.util.Optional; -import java.util.concurrent.CompletableFuture; - -@Plugin( - id = "vmessage", - name = "Vmessage", - version = "1.2", - description = "A velocity plugin that creates a multi server chat for the network", - authors = {"FeuSalamander"} -) -public class vmessage { - @Inject - private final ProxyServer proxy; - @Inject - private final Logger logger; - @Inject - private final Metrics.Factory metricsFactory; - @Inject - private final @DataDirectory Path dataDirectory; - @Inject - public vmessage(ProxyServer proxy, Logger logger, Metrics.Factory metricsFactory, @DataDirectory Path dataDirectory) { - this.proxy = proxy; - this.logger = logger; - this.metricsFactory = metricsFactory; - this.dataDirectory = dataDirectory; - } - private String messageraw = ""; - private String joinraw = ""; - private String leaveraw = ""; - private String changeraw = ""; - private String message = ""; - private String join = ""; - private String leave = ""; - private String change = ""; - private boolean messageb = false; - private boolean joinb = false; - private boolean leaveb = false; - private boolean changeb = false; - @Subscribe - private void onProxyInitialization(ProxyInitializeEvent event) { - logger.info("Vmessage by FeuSalamander is working !"); - Metrics metrics = metricsFactory.make(this, 16527); - File directory = dataDirectory.toFile(); - File f = new File(directory, "config.toml"); - Toml config = new Toml().read(f); - createconfig(); - if(config.getBoolean("Message.enabled"))messageb = true; - if(config.getBoolean("Join.enabled"))joinb = true; - if(config.getBoolean("Leave.enabled"))leaveb = true; - if(config.getBoolean("Server-change.enabled"))changeb = true; - messageraw = config.getString("Message.format"); - joinraw = config.getString("Join.format"); - leaveraw = config.getString("Leave.format"); - changeraw = config.getString("Server-change.format"); - } - @Subscribe - private void onMessage(PlayerChatEvent e){ - if(messageb){ - Player p = e.getPlayer(); - String m = e.getMessage(); - message = messageraw.replaceAll("#player#", p.getUsername()).replaceAll("#message#",m).replaceAll("#server#",p.getCurrentServer().get().getServerInfo().getName()).replaceAll("&", "§"); - if(proxy.getPluginManager().getPlugin("luckperms").isPresent()){ - LuckPerms api = LuckPermsProvider.get(); - User user = api.getPlayerAdapter(Player.class).getUser(p); - message = message.replaceAll("#prefix#", user.getCachedData().getMetaData().getPrefix().replaceAll("&", "§")); - } - proxy.getAllServers().forEach(registeredServer -> sendMessage(registeredServer, p)); - message = ""; - } - } - @Subscribe - private void onJoin(LoginEvent e){ - if(joinb){ - Player p = e.getPlayer(); - join = joinraw.replaceAll("#player#", p.getUsername()); - if(proxy.getPluginManager().getPlugin("luckperms").isPresent()){ - LuckPerms api = LuckPermsProvider.get(); - User user = api.getPlayerAdapter(Player.class).getUser(p); - join = join.replaceAll("#prefix#", user.getCachedData().getMetaData().getPrefix().replaceAll("&", "§")); - } - proxy.getAllServers().forEach(registeredServer -> registeredServer.sendMessage(Component.text(join))); - join = ""; - } - } - @Subscribe - private void onLeave(DisconnectEvent e){ - if(leaveb){ - Player p = e.getPlayer(); - leave = leaveraw.replaceAll("#player#", p.getUsername()); - if(proxy.getPluginManager().getPlugin("luckperms").isPresent()){ - LuckPerms api = LuckPermsProvider.get(); - User user = api.getPlayerAdapter(Player.class).getUser(p); - leave = leave.replaceAll("#prefix#", user.getCachedData().getMetaData().getPrefix().replaceAll("&", "§")); - } - proxy.getAllServers().forEach(registeredServer -> registeredServer.sendMessage(Component.text(leave))); - leave = ""; - } - } - @Subscribe - private void onChange(ServerConnectedEvent e){ - if(changeb){ - Optional old = e.getPreviousServer(); - RegisteredServer New = e.getServer(); - Player p = e.getPlayer(); - if(old.isPresent()){ - change = changeraw.replaceAll("#player#", p.getUsername()).replaceAll("#oldserver#", old.get().getServerInfo().getName()).replaceAll("#server#", New.getServerInfo().getName()); - if(proxy.getPluginManager().getPlugin("luckperms").isPresent()){ - LuckPerms api = LuckPermsProvider.get(); - User user = api.getPlayerAdapter(Player.class).getUser(p); - change = change.replaceAll("#prefix#", user.getCachedData().getMetaData().getPrefix().replaceAll("&", "§")); - } - proxy.getAllServers().forEach(registeredServer -> registeredServer.sendMessage(Component.text(change))); - change = ""; - } - } - } - private void sendMessage(RegisteredServer s, Player p){ - if(!(Objects.equals(p.getCurrentServer().get().getServerInfo().getName(), s.getServerInfo().getName()))){ - s.sendMessage(Component.text(message)); - } - } - private void createconfig(){ - File dataDirectoryFile = this.dataDirectory.toFile(); - if (!dataDirectoryFile.exists()){ - dataDirectoryFile.mkdir(); - } - File f = new File(dataDirectoryFile, "config.toml"); - if(!(f.exists())){ - try{ - InputStream file = this.getClass().getResourceAsStream("/config.toml"); - Files.copy(file, f.toPath()); - }catch (IOException e){ - e.printStackTrace(); - } - } - } -} diff --git a/src/main/resources/config.toml b/src/main/resources/config.toml index cd7d74e..39fa1e8 100644 --- a/src/main/resources/config.toml +++ b/src/main/resources/config.toml @@ -4,19 +4,19 @@ #- "#prefix#" : return the player's luckperms prefix (don't place it if you don't have luckperms on your server) #- "#message#" : return the player's message #- "#server#" : return the player's server name -format = "§a(#server#) #prefix# #player# §8§l> §r#message#" +format = "&a(#server#) #prefix# #player# &8&l> &r#message#" enabled = true [Join] #place holders: #- "#player#" : return the player name #- "#prefix#" : return the player's luckperms prefix (don't place it if you don't have luckperms on your server) -format = "§7#prefix# #player# §ejoined the network" +format = "&7#prefix# #player# &ejoined the network" enabled = true [Leave] #place holders: #- "#player#" : return the player name #- "#prefix#" : return the player's luckperms prefix (don't place it if you don't have luckperms on your server) -format = "#§7prefix# #player# §eleft the network" +format = "#&7prefix# #player# &eleft the network" enabled = true [Server-change] #place holders: @@ -24,5 +24,5 @@ enabled = true #- "#prefix#" : return the player's luckperms prefix (don't place it if you don't have luckperms on your server) #- "#oldserver#" : return the previous player's server name #- "#server#" : return the new player's server name -format = "§7#prefix# #player# §eleft §c#oldserver# §eto join §a#server#" +format = "&7#prefix# #player# &eleft &c#oldserver# &eto join &a#server#" enabled = true \ No newline at end of file