Skip to content

Commit

Permalink
added Chat module
Browse files Browse the repository at this point in the history
  • Loading branch information
GIGABAIT93 committed Mar 21, 2024
1 parent 70c5c7b commit 4ce367e
Show file tree
Hide file tree
Showing 14 changed files with 388 additions and 12 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ VCMI Velocity Plugin - This one offers a variety of modules for detailed server
- `/vcmireload`: Reloads the plugin configuration file.
- `/vcmimodules`: Displays a list of all available modules.
- `/vpl -v`: Display plugin list.
- `/psend <player/all> <server>`: Sends the specified player to the specified server.

## Modules
### PlayerTime:
Expand Down Expand Up @@ -202,4 +203,35 @@ events:
```

### ChatManager:
Provides the ability to manage chat messages.
```yaml
# Chat Manager
# Placeholders: {player}, {server}, {message}
# Global chat
global:
enabled: true
alias: '!'
command: g
permission: vcmi.chat.global
see_all: true
format: '&8[&6G&8] &a{player} &6=> &f{message}'
# Staff chat
staff:
enabled: true
alias: '@'
command: s
permission: vcmi.chat.staff
format: '&8&l[&4&lS&8&l] &b&l{server} &a&l{player} &6&l=> &f&l{message}'
# Alert chat
alert:
enabled: true
alias: ''
command: alert
# If empty, everyone can use this chat and see the messages
permission: vcmi.chat.alert
see_all: true
format: '&8[&4ALERT&8] &f{message}'
```

5 changes: 4 additions & 1 deletion src/main/java/com/vcmi/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public static void registerCommand(String command, String alias, SimpleCommand C
CommandManager commandManager = server.getCommandManager();
CommandMeta commandMeta = commandManager.metaBuilder(command).aliases(alias).plugin(server).build();
commandManager.register(commandMeta, CommandClass);

}

public static void unregisterCommand(String string) {
server.getCommandManager().unregister(string);
}

public static String capitalize(String text) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/vcmi/VCMI.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import com.vcmi.config.Database;
import com.vcmi.config.Lang;
import com.vcmi.modules.Modules;
import com.vcmi.modules.chat.ChatEventListener;
import com.vcmi.modules.event.EventManager;
import com.vcmi.modules.rcon.server.RconServerModule;
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.KickedFromServerEvent;
import com.velocitypowered.api.event.player.PlayerChatEvent;
import com.velocitypowered.api.event.player.ServerConnectedEvent;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.event.proxy.ProxyShutdownEvent;
Expand Down Expand Up @@ -70,6 +72,11 @@ public void onServerSwitch(ServerConnectedEvent event) {
EventManager.onServerSwitch(event);
}

@Subscribe
public void onPlayerMessage(PlayerChatEvent event) {
ChatEventListener.onPlayerMessage(event);
}


@Subscribe
public void onEnable(ProxyInitializeEvent event) {
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/com/vcmi/commands/PlayerSendCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.vcmi.commands;

import com.vcmi.VCMI;
import com.vcmi.config.Lang;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.server.RegisteredServer;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

public class PlayerSendCommand implements SimpleCommand {
@Override
public void execute(Invocation invocation) {
CommandSource source = invocation.source();
if (!hasPermission(invocation)) {
source.sendMessage(Lang.no_perms.get());
return;
}
String[] args = invocation.arguments();
if (args.length < 2) {
source.sendMessage(Lang.send_usage.get());
return;
}

Optional<RegisteredServer> toServer = VCMI.server.getServer(args[1]);
if (toServer.isEmpty()) {
source.sendMessage(Lang.server_not_found.replace("{server}", args[1]));
return;
}

if (args[0].equals("all")) {
for (Player p : VCMI.server.getAllPlayers()) {
p.createConnectionRequest(toServer.get()).fireAndForget();
}
source.sendMessage(Lang.send_success.replace("{player}", "all", "{server}", args[1]));
return;
}

Optional<Player> player = VCMI.server.getPlayer(args[0]);
if (player.isEmpty()) {
source.sendMessage(Lang.player_not_found.replace("{player}", args[0]));
return;
}

player.get().createConnectionRequest(toServer.get()).fireAndForget();
source.sendMessage(Lang.send_success.replace("{player}", args[0], "{server}", args[1]));
}

@Override
public CompletableFuture<List<String>> suggestAsync(Invocation invocation) {
ArrayList<String> args = new ArrayList<>();
int argNum = invocation.arguments().length;
if (argNum == 0 || argNum == 1) {
args.add("all");
for (Player player : VCMI.server.getAllPlayers()) {
args.add(player.getUsername().trim());
}
}
if (argNum == 2) {
VCMI.server.getAllServers().forEach(serverConnection -> args.add(serverConnection.getServerInfo().getName()));
}
return CompletableFuture.completedFuture(args);
}

@Override
public boolean hasPermission(Invocation invocation) {
return invocation.source().hasPermission("vcmi.send.player");
}
}
10 changes: 6 additions & 4 deletions src/main/java/com/vcmi/commands/PluginsCommand.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.vcmi.commands;

import com.vcmi.Message;
import com.vcmi.VCMI;
import com.vcmi.config.Lang;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.plugin.PluginContainer;

import java.util.Collection;
import java.util.stream.Collectors;

Expand All @@ -18,18 +20,18 @@ public void execute(Invocation invocation) {
return;
}
Collection<PluginContainer> plugins = VCMI.server.getPluginManager().getPlugins();

int pluginCount = plugins.size();
if (args.length == 1 && args[0].equals("-v")) {
String pluginList = plugins.stream()
.map(plugin -> "&6"+plugin.getDescription().getName().orElse("Unknown") + " &7" + plugin.getDescription().getVersion().orElse("Unknown"))
.map(plugin -> "&6" + plugin.getDescription().getName().orElse("Unknown") + " &7" + plugin.getDescription().getVersion().orElse("Unknown"))
.collect(Collectors.joining(", "));
source.sendMessage(Message.convert("&aPlugins: " + pluginList));
return;
}
String pluginList = plugins.stream()
.map(plugin -> "&6"+plugin.getDescription().getName().orElse("Unknown"))
.map(plugin -> "&6" + plugin.getDescription().getName().orElse("Unknown"))
.collect(java.util.stream.Collectors.joining(", "));
source.sendMessage(Message.convert("&aPlugins: &6" + pluginList));
source.sendMessage(Message.convert("&aPlugins (" + pluginCount + "): &6" + pluginList));
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/vcmi/config/Lang.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public enum Lang {
rcon_unknown_error("rcon_unknown_error"), rcon_empty_command("rcon_empty_command"), rcon_invalid_command_or_server("rcon_invalid_command_or_server"),
player_time("player_time"), player_not_found("player_not_found"), player_time_other("player_time_other"), player_time_usage("player_time_usage"),
player_time_days("player_time_days"), player_time_hours("player_time_hours"), player_time_minutes("player_time_minutes"), player_time_seconds("player_time_seconds"),
help("help"), player_time_top("player_time_top"), player_time_top_entry("player_time_top_entry");
help("help"), player_time_top("player_time_top"), player_time_top_entry("player_time_top_entry"), send_usage("send_usage"), send_success("send_success"),
server_not_found("server_not_found");

private final String key;

Expand Down
75 changes: 75 additions & 0 deletions src/main/java/com/vcmi/config/data/ChatYAML.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.vcmi.config.data;

import com.vcmi.Message;
import com.vcmi.VCMI;
import org.simpleyaml.configuration.file.YamlConfiguration;
import org.simpleyaml.configuration.file.YamlFile;

import java.io.File;

public class ChatYAML {
private static final String FILE_PATH = VCMI.pluginPath + File.separator + "chats.yml";
private static YamlFile yamlFile;

private ChatYAML() {
throw new IllegalStateException("Utility class");
}

public static void reload() {
try {
yamlFile = new YamlFile(FILE_PATH);
if (!yamlFile.exists()) {
yamlFile.createNewFile(true);
}
yamlFile.load();

yamlFile.setHeader(
"Chat Manager \n" + "Placeholders: {player}, {server}, {message}"
);

yamlFile.setComment("global", "Global chat");
setConfigValue("global.enabled", true);
setConfigValue("global.alias", "!");
setConfigValue("global.command", "g");
setConfigValue("global.permission", "vcmi.chat.global");
setConfigValue("global.see_all", true);
setConfigValue("global.format", "&8[&6G&8] &a{player} &6=> &f{message}");

yamlFile.setComment("staff", "Staff chat");
setConfigValue("staff.enabled", true);
setConfigValue("staff.alias", "@");
setConfigValue("staff.command", "s");
setConfigValue("staff.permission", "vcmi.chat.staff");
setConfigValue("global.see_all", false);
setConfigValue("staff.format", "&8&l[&4&lS&8&l] &b&l{server} &a&l{player} &6&l=> &f&l{message}");

yamlFile.setComment("alert", "Alert chat");
setConfigValue("alert.enabled", true);
setConfigValue("alert.alias", "");
setConfigValue("alert.command", "alert");
yamlFile.setComment("alert.permission", "If empty, everyone can use this chat and see the messages");
setConfigValue("alert.permission", "");
setConfigValue("alert.see_all", true);
setConfigValue("alert.format", "&8[&4ALERT&8] &f{message}");

yamlFile.save();
} catch (Exception e) {
Message.error(e.getMessage());
}
}

private static void setConfigValue(String path, Object defaultValue) {
if (!yamlFile.contains(path)) {
yamlFile.set(path, defaultValue);
}
}

public static YamlConfiguration getReloadedFile() {
reload();
return new YamlConfiguration(yamlFile);
}

public static YamlConfiguration getConfig() {
return new YamlConfiguration(yamlFile);
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/vcmi/config/data/ConfigYAML.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ private static void populateModules() {
yamlFile.setComment("modules.text-reader", "Text Reader module. Allows reading and outputting text files and chat");
setConfigValue("modules.text-reader", true);

yamlFile.setComment("modules.chat-manager", "Chat manager. Allows you to manage chat");
setConfigValue("modules.chat-manager", true);


}

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/vcmi/config/data/LangYAML.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ public static void reload() {
setConfigValue("player_time_top", "&aTop players by time:");
setConfigValue("player_time_top_entry", "&a{position}. &6{player} - {time}");

yamlFile.setComment("send_usage", "Send Module");
setConfigValue("send_usage", "&6Usage: /psend &6{player} {server}");
setConfigValue("send_success", "&aPlayer {player} sent to server {server}");
setConfigValue("server_not_found", "&cServer {server} not found");


yamlFile.setComment("help", "Help");
setConfigValue("help", "&6Available commands:"
+ "\n&6/vcmi &7- &6Show help."
Expand All @@ -90,6 +96,7 @@ public static void reload() {
+ "\n&6/bash [script/reload] [args] &7- &6Executes the specified Bash script."
+ "\n&6/rules &7- &6Reads the specified text file."
+ "\n&6/readme &7- &6Reads the specified text file."
+ "\n&6/psend [player/all] [server]&7- &6Sends the specified player to the specified server."
);


Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/vcmi/modules/FakeConsolePlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void setCustomChatCompletions(@NotNull Collection<String> collection) {

@Override
public String getUsername() {
return null;
return "Console";
}

@Override
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/vcmi/modules/Modules.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.vcmi.modules;

import com.vcmi.commands.HelpCommand;
import com.vcmi.commands.ModulesCommand;
import com.vcmi.commands.PluginsCommand;
import com.vcmi.commands.*;
import com.vcmi.config.Config;
import com.vcmi.Message;
import com.vcmi.Util;
import com.vcmi.commands.ReloadCommand;
import com.vcmi.modules.bash.BashModule;
import com.vcmi.modules.chat.ChatModule;
import com.vcmi.modules.event.EventsModule;
import com.vcmi.modules.php.PhpModule;
import com.vcmi.modules.playertime.PlayerTimeModule;
Expand All @@ -26,7 +24,8 @@ public class Modules {
"events-manager", new ModuleConfig(EventsModule::enable, EventsModule::disable),
"request-module", new ModuleConfig(RequestsModule::enable, RequestsModule::disable),
"player-time", new ModuleConfig(PlayerTimeModule::enable, PlayerTimeModule::disable),
"text-reader", new ModuleConfig(TextReaderModule::enable, TextReaderModule::disable)
"text-reader", new ModuleConfig(TextReaderModule::enable, TextReaderModule::disable),
"chat-manager", new ModuleConfig(ChatModule::enable, ChatModule::disable)
);

public Modules() {
Expand Down Expand Up @@ -54,6 +53,7 @@ private void registerCommands() {
Util.registerCommand("vcmi", "vcmihelp", new HelpCommand());
Util.registerCommand("vcmimodules", "vmodules", new ModulesCommand());
Util.registerCommand("vpl", "vplugins", new PluginsCommand());
Util.registerCommand("psend", "vpsend", new PlayerSendCommand());
}

private static class ModuleConfig {
Expand Down
Loading

0 comments on commit 4ce367e

Please sign in to comment.