Initial commit
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
package xyz.lncvrt.velocityClearChat;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.velocitypowered.api.plugin.annotation.DataDirectory;
|
||||
import org.spongepowered.configurate.CommentedConfigurationNode;
|
||||
import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
|
||||
import org.spongepowered.configurate.loader.ConfigurationLoader;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class ConfigManager {
|
||||
private final Path configPath;
|
||||
private ConfigurationLoader<CommentedConfigurationNode> loader;
|
||||
private CommentedConfigurationNode root;
|
||||
|
||||
@Inject
|
||||
public ConfigManager(@DataDirectory Path dataDirectory) {
|
||||
this.configPath = dataDirectory.resolve("config.conf");
|
||||
this.loader = HoconConfigurationLoader.builder().path(configPath).build();
|
||||
loadConfig();
|
||||
}
|
||||
|
||||
private void loadConfig() {
|
||||
try {
|
||||
if (Files.notExists(configPath)) {
|
||||
saveDefaultConfig();
|
||||
}
|
||||
root = loader.load();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void saveDefaultConfig() throws IOException {
|
||||
Files.createDirectories(configPath.getParent());
|
||||
|
||||
root = loader.load();
|
||||
root.node("lines")
|
||||
.set(100)
|
||||
.comment("The number of lines the chat clearer will send.");
|
||||
root.node("chat-cleared-message")
|
||||
.set("<green>Chat has been cleared by an admin</green>")
|
||||
.comment("The message to send when the chat is cleared by an admin.\nYou can use <player> if you would like to display who cleared the chat.");
|
||||
root.node("chat-clear-failed-message")
|
||||
.set("<red>Failed to get server info</red>")
|
||||
.comment("If velocity returns invalid information, this message will be displayed.");
|
||||
root.node("clear-permission")
|
||||
.set("lncvrt.velocityclearchat.clear")
|
||||
.comment("The permission an admin needs to clear the chat.");
|
||||
root.node("clear-on-server-swap")
|
||||
.set(true)
|
||||
.comment("On player swap, should we clear the chat history?");
|
||||
root.node("clear-on-server-swap-message")
|
||||
.set("")
|
||||
.comment("The message to display if \"clear-on-server-swap\" is enabled, set to \"\" to disable it.");
|
||||
loader.save(root);
|
||||
}
|
||||
|
||||
public int getLines() {
|
||||
return root.node("lines").getInt(1000);
|
||||
}
|
||||
|
||||
public String getClearedMessage() {
|
||||
return root.node("chat-cleared-message").getString("<green>Chat has been cleared by an admin</green>");
|
||||
}
|
||||
|
||||
public String getClearFailed() {
|
||||
return root.node("chat-clear-failed-message").getString("<red>Failed to get server info</red>");
|
||||
}
|
||||
|
||||
public String getClearPermission() {
|
||||
return root.node("clear-permission").getString("lncvrt.velocityclearchat.clear");
|
||||
}
|
||||
|
||||
public Boolean getClearOnSwap() {
|
||||
return root.node("clear-on-server-swap").getBoolean(false);
|
||||
}
|
||||
|
||||
public String getClearOnSwapMessage() {
|
||||
return root.node("clear-on-server-swap-message").getString("\"\"");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package xyz.lncvrt.velocityClearChat;
|
||||
|
||||
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.player.ServerConnectedEvent;
|
||||
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 net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.slf4j.Logger;
|
||||
import xyz.lncvrt.velocityClearChat.commands.ClearChat;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
@Plugin(id = "velocityclearchat", name = "VelocityClearChat", authors = {"Lncvrt"}, version = BuildConstants.VERSION)
|
||||
public class VelocityClearChat {
|
||||
private final Logger logger;
|
||||
private final ProxyServer proxyServer;
|
||||
public ConfigManager configManager;
|
||||
public final MiniMessage miniMessage = MiniMessage.miniMessage();
|
||||
|
||||
@Inject
|
||||
public VelocityClearChat(Logger logger, ProxyServer proxyServer, @DataDirectory Path dataDirectory) {
|
||||
this.logger = logger;
|
||||
this.proxyServer = proxyServer;
|
||||
this.configManager = new ConfigManager(dataDirectory);
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onProxyInitialize(ProxyInitializeEvent event) {
|
||||
CommandManager commandManager = proxyServer.getCommandManager();
|
||||
CommandMeta commandMeta = commandManager.metaBuilder("clearchat")
|
||||
.aliases("cc")
|
||||
.plugin(this)
|
||||
.build();
|
||||
|
||||
SimpleCommand commandToRegister = new ClearChat(this);
|
||||
|
||||
commandManager.register(commandMeta, commandToRegister);
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onPlayerSwitchServer(ServerConnectedEvent event) {
|
||||
if (configManager.getClearOnSwap()) {
|
||||
Player player = event.getPlayer();
|
||||
for (int i = 0; i < configManager.getLines(); i++) player.sendMessage(miniMessage.deserialize(" ".repeat((i % 10) + 1)));
|
||||
if (!configManager.getClearOnSwapMessage().isEmpty()) {
|
||||
player.sendMessage(miniMessage.deserialize(configManager.getClearOnSwapMessage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package xyz.lncvrt.velocityClearChat.commands;
|
||||
|
||||
import com.velocitypowered.api.command.CommandSource;
|
||||
import com.velocitypowered.api.command.SimpleCommand;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import com.velocitypowered.api.proxy.ServerConnection;
|
||||
import com.velocitypowered.api.proxy.server.RegisteredServer;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import xyz.lncvrt.velocityClearChat.ConfigManager;
|
||||
import xyz.lncvrt.velocityClearChat.VelocityClearChat;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class ClearChat implements SimpleCommand {
|
||||
private final MiniMessage miniMessage;
|
||||
private final ConfigManager configManager;
|
||||
|
||||
public ClearChat(VelocityClearChat plugin) {
|
||||
miniMessage = plugin.miniMessage;
|
||||
configManager = plugin.configManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(final Invocation invocation) {
|
||||
CommandSource source = invocation.source();
|
||||
|
||||
if (source instanceof Player player) {
|
||||
//no idea how to make this not so shitty
|
||||
ServerConnection server = player.getCurrentServer().orElse(null);
|
||||
if (server == null) {
|
||||
player.sendMessage(miniMessage.deserialize(configManager.getClearFailed()));
|
||||
return;
|
||||
}
|
||||
RegisteredServer registeredServer = server.getServer();
|
||||
if (registeredServer == null) {
|
||||
player.sendMessage(miniMessage.deserialize(configManager.getClearFailed()));
|
||||
return;
|
||||
}
|
||||
Collection<Player> players = registeredServer.getPlayersConnected();
|
||||
for (Player targetPlayer : players) {
|
||||
for (int i = 0; i < configManager.getLines(); i++) targetPlayer.sendMessage(miniMessage.deserialize(" ".repeat((i % 10) + 1)));
|
||||
targetPlayer.sendMessage(miniMessage.deserialize(configManager.getClearedMessage().replace("<player>", player.getUsername())));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(final Invocation invocation) {
|
||||
return invocation.source().hasPermission(configManager.getClearPermission());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> suggest(final Invocation invocation) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<List<String>> suggestAsync(final Invocation invocation) {
|
||||
return CompletableFuture.completedFuture(List.of());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package xyz.lncvrt.velocityClearChat;
|
||||
|
||||
// The constants are replaced before compilation
|
||||
public class BuildConstants {
|
||||
|
||||
public static final String VERSION = "${version}";
|
||||
}
|
||||
Reference in New Issue
Block a user