From 5993af255bac26d1f3e13003e1e1e0c447166c42 Mon Sep 17 00:00:00 2001 From: FeuSalamander Date: Fri, 30 Sep 2022 17:17:50 +0200 Subject: [PATCH] added the config file --- README.md | 3 +- pom.xml | 14 ++--- .../me/feusalamander/vmessage/vmessage.java | 54 ++++++++++++++++--- src/main/resources/config.toml | 7 +++ 4 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 src/main/resources/config.toml diff --git a/README.md b/README.md index 21e153e..67ec27e 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,5 @@ Ever wanted to have your messages sent globally across your Velocity proxy? Here - No Setup, just Drag & Drop! - LuckPerms Prefix Support - Velocity Support -- (thanks @JosTheDude for the description) \ No newline at end of file +- (thanks @JosTheDude for the description) +- You can also check my Bukkit plugin https://github.com/FeuSalamander/MiniWalls \ No newline at end of file diff --git a/pom.xml b/pom.xml index 94de4f3..71c00ff 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ Vmessage Vmessage - 1.0 + 1.1 jar Vmessage @@ -160,11 +160,11 @@ 3.1.1 provided - - net.luckperms - api - 5.4 - provided - + + net.luckperms + api + 5.4 + provided + diff --git a/src/main/java/me/feusalamander/vmessage/vmessage.java b/src/main/java/me/feusalamander/vmessage/vmessage.java index 96159b1..693da0b 100644 --- a/src/main/java/me/feusalamander/vmessage/vmessage.java +++ b/src/main/java/me/feusalamander/vmessage/vmessage.java @@ -1,9 +1,11 @@ package me.feusalamander.vmessage; import com.google.inject.Inject; +import com.moandjiezana.toml.Toml; import com.velocitypowered.api.event.player.PlayerChatEvent; 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; @@ -12,16 +14,20 @@ 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; @Plugin( id = "vmessage", name = "Vmessage", - version = "1.0", + version = "1.1", description = "A velocity plugin that creates a multi server chat for the network", authors = {"FeuSalamander"} ) public class vmessage { - @Inject private final ProxyServer proxy; @Inject @@ -29,31 +35,63 @@ public class vmessage { @Inject private final Metrics.Factory metricsFactory; @Inject - public vmessage(ProxyServer proxy, Logger logger, Metrics.Factory metricsFactory) { + 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; } - + public String message = ""; @Subscribe public void onProxyInitialization(ProxyInitializeEvent event) { logger.info("Vmessage by FeuSalamander is working !"); Metrics metrics = metricsFactory.make(this, 16527); + createconfig(); + message = getmessage("Message-format.format"); } @Subscribe public void onMessage(PlayerChatEvent e){ proxy.getAllServers().stream().forEach(registeredServer -> sendMessage(registeredServer, e.getPlayer(), e.getMessage())); } - public void sendMessage(RegisteredServer s, Player p, String m){ + private void sendMessage(RegisteredServer s, Player p, String m){ + message = message.replaceAll("#player#", p.getUsername()); + message = message.replaceAll("#message#",m); + message = message.replaceAll("#server#",p.getCurrentServer().get().getServerInfo().getName()); + message = message.replaceAll("&", "§"); if(!(Objects.equals(p.getCurrentServer().get().getServerInfo().getName(), s.getServerInfo().getName()))){ if(proxy.getPluginManager().getPlugin("luckperms").isPresent()){ LuckPerms api = LuckPermsProvider.get(); User user = api.getPlayerAdapter(Player.class).getUser(p); - String f = user.getCachedData().getMetaData().getPrefix().replaceAll("&", "§"); - s.sendMessage(Component.text("§a("+p.getCurrentServer().get().getServerInfo().getName()+") "+f+" "+p.getUsername()+" §8§l> §r"+m)); + message = message.replaceAll("#prefix#", user.getCachedData().getMetaData().getPrefix().replaceAll("&", "§")); + s.sendMessage(Component.text(message)); + message = getmessage("Message-format.format"); }else{ - s.sendMessage(Component.text("§a("+p.getCurrentServer().get().getServerInfo().getName()+") "+"§r "+p.getUsername()+" §8§l> §r"+m)); + s.sendMessage(Component.text(message)); + message = getmessage("Message-format.format"); } } } + 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(); + } + } + } + private String getmessage(String s){ + File directory = dataDirectory.toFile(); + File f = new File(directory, "config.toml"); + Toml config = new Toml().read(f); + return config.getString(s); + } } diff --git a/src/main/resources/config.toml b/src/main/resources/config.toml new file mode 100644 index 0000000..6b2bc28 --- /dev/null +++ b/src/main/resources/config.toml @@ -0,0 +1,7 @@ +[Message-format] +#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) +#- "#message#" : return the player's message +#- "#server#" : return the player's server name +format = "§a(#server#) #prefix# #player# §8§l> §r#message#" \ No newline at end of file