Skip to content

Commit ee236fc

Browse files
committed
Add commands with usage and reload/status functionality
1 parent c2dbc5e commit ee236fc

File tree

4 files changed

+86
-10
lines changed

4 files changed

+86
-10
lines changed

velocity/src/main/java/it/renvins/serverpulse/velocity/ServerPulseVelocity.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.nio.file.Path;
44

55
import com.google.inject.Inject;
6+
import com.velocitypowered.api.command.CommandMeta;
67
import com.velocitypowered.api.event.Subscribe;
78
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
89
import com.velocitypowered.api.plugin.Plugin;
@@ -21,6 +22,7 @@
2122
import it.renvins.serverpulse.common.metrics.DiskRetriever;
2223
import it.renvins.serverpulse.common.platform.Platform;
2324
import it.renvins.serverpulse.common.scheduler.TaskScheduler;
25+
import it.renvins.serverpulse.velocity.commands.ServerPulseCommand;
2426
import it.renvins.serverpulse.velocity.config.VelocityConfiguration;
2527
import it.renvins.serverpulse.velocity.config.VelocityDatabaseConfiguration;
2628
import it.renvins.serverpulse.velocity.config.VelocityMetricsConfiguration;
@@ -36,7 +38,6 @@
3638
public class ServerPulseVelocity {
3739

3840
@Getter private final ProxyServer server;
39-
private final Path dataDirectory;
4041
private final PulseLogger logger;
4142

4243
private final VelocityConfiguration config;
@@ -50,7 +51,6 @@ public class ServerPulseVelocity {
5051
@Inject
5152
public ServerPulseVelocity(ProxyServer server, @DataDirectory Path dataDirectory, Logger logger) {
5253
this.server = server;
53-
this.dataDirectory = dataDirectory;
5454

5555
this.logger = new VelocityLogger(logger);
5656
this.config = new VelocityConfiguration(logger, dataDirectory, "config.yml");
@@ -79,12 +79,17 @@ public void onProxyInitialization(ProxyInitializeEvent event) {
7979
this.databaseService = new DatabaseService(logger, platform, dbConfig, scheduler);
8080
this.metricsService = new MetricsService(logger, platform, metricsConfig, scheduler);
8181

82+
ServerPulseProvider.register(new ServerPulseVelocityAPI(databaseService, metricsService, diskRetriever, pingRetriever));
83+
8284
databaseService.load();
8385
if (server.isShuttingDown()) {
8486
return;
8587
}
8688

8789
metricsService.load();
88-
ServerPulseProvider.register(new ServerPulseVelocityAPI(databaseService, metricsService, diskRetriever, pingRetriever));
90+
91+
CommandMeta meta = server.getCommandManager().metaBuilder("serverpulsevelocity")
92+
.plugin(this).aliases("spv").build();
93+
server.getCommandManager().register(meta, new ServerPulseCommand(config).createCommand());
8994
}
9095
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package it.renvins.serverpulse.velocity.commands;
2+
3+
import com.mojang.brigadier.Command;
4+
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
5+
import com.mojang.brigadier.context.CommandContext;
6+
import com.velocitypowered.api.command.BrigadierCommand;
7+
import com.velocitypowered.api.command.CommandSource;
8+
import it.renvins.serverpulse.api.ServerPulseProvider;
9+
import it.renvins.serverpulse.common.utils.ChatUtils;
10+
import it.renvins.serverpulse.velocity.config.VelocityConfiguration;
11+
import lombok.RequiredArgsConstructor;
12+
import net.kyori.adventure.text.Component;
13+
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
14+
15+
@RequiredArgsConstructor
16+
public class ServerPulseCommand {
17+
18+
private final VelocityConfiguration config;
19+
20+
public BrigadierCommand createCommand() {
21+
// Create the main command
22+
LiteralArgumentBuilder<CommandSource> command =
23+
BrigadierCommand.literalArgumentBuilder("serverpulsevelocity")
24+
.executes(context -> {
25+
CommandSource source = context.getSource();
26+
27+
Component message = LegacyComponentSerializer.legacySection().deserialize(
28+
ChatUtils.format(config.getConfig().getString("messages.usage")));
29+
source.sendMessage(message);
30+
31+
return Command.SINGLE_SUCCESS;
32+
});
33+
34+
command.then(BrigadierCommand.literalArgumentBuilder("reload")
35+
.requires(source -> source.hasPermission("serverpulse.reload"))
36+
.executes(this::executeReload)
37+
);
38+
39+
command.then(BrigadierCommand.literalArgumentBuilder("status")
40+
.requires(source -> source.hasPermission("serverpulse.status"))
41+
.executes(this::executeStatus)
42+
);
43+
44+
return new BrigadierCommand(command.build());
45+
}
46+
47+
private int executeReload(CommandContext<CommandSource> context) {
48+
CommandSource source = context.getSource();
49+
50+
Component message;
51+
if (config.load()) {
52+
message = LegacyComponentSerializer.legacySection().deserialize(
53+
ChatUtils.format(config.getConfig().getString("messages.reloadConfig")));
54+
} else {
55+
message = LegacyComponentSerializer.legacySection().deserialize(
56+
ChatUtils.format(config.getConfig().getString("messages.reloadConfigError")));
57+
}
58+
59+
source.sendMessage(message);
60+
return Command.SINGLE_SUCCESS;
61+
}
62+
63+
private int executeStatus(CommandContext<CommandSource> context) {
64+
CommandSource source = context.getSource();
65+
66+
Component message = LegacyComponentSerializer.legacySection().deserialize(
67+
ChatUtils.format(ServerPulseProvider.get().getDatabaseService().isConnected() ?
68+
config.getConfig().getString("messages.statusConnected") :
69+
config.getConfig().getString("messages.statusNotConnected")));
70+
71+
source.sendMessage(message);
72+
return Command.SINGLE_SUCCESS;
73+
}
74+
}

velocity/src/main/java/it/renvins/serverpulse/velocity/config/VelocityConfiguration.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public VelocityConfiguration(Logger logger, Path dataDir, String name) {
2929
this.config = new YamlFile(new File(dataDir.toFile(), name));
3030
}
3131

32-
public void load() {
32+
public boolean load() {
3333
try {
3434
if (!config.exists()) {
3535
if (copyDefaultsFromResource()) {
@@ -42,8 +42,10 @@ public void load() {
4242
logger.info("Loading configuration file: " + name);
4343
}
4444
config.load();
45+
return true;
4546
} catch (Exception e) {
4647
logger.error("Failed to load configuration file: " + name, e);
48+
return false;
4749
}
4850
}
4951

velocity/src/main/resources/config.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,8 @@ metrics:
1010
tags:
1111
server: "velocity1"
1212
messages:
13-
noPerms: "&7[&bServer&7Pulse] &7You don't have &bpermission &7to use this &bcommand&7."
13+
usage: "&7[&bServer&7Pulse] &7Please use &b/serverpulsevelocity [status|reload]&7."
1414
reloadConfig: "&7[&bServer&7Pulse] &7Configuration &breloaded&7."
1515
reloadConfigError: "&7[&bServer&7Pulse] &7Error &breloading &7configuration..."
16-
noArgs: "&7[&bServer&7Pulse] &7You need to specify a &bcommand&7: &breload&7, &bstatus&7."
17-
playerOnly: "&7[&bServer&7Pulse] &7This command can only be used by &bplayers&7."
18-
noCommand: "&7[&bServer&7Pulse] &7This command is not &bavailable&7."
19-
reloadConfigUsage: "&7[&bServer&7Pulse] &7Usage: &b/serverpulse reload&7."
20-
statusConfigUsage: "&7[&bServer&7Pulse] &7Usage: &b/serverpulse status&7."
2116
statusConnected: "&7[&bServer&7Pulse] &7Connected to &bInfluxDB&7."
2217
statusNotConnected: "&7[&bServer&7Pulse] &7Not connected to &bInfluxDB&7."

0 commit comments

Comments
 (0)