Skip to content

Commit 7f64c11

Browse files
committed
Add Velocity configuration and metrics integration, including YAML support and database configuration
1 parent 13480af commit 7f64c11

File tree

5 files changed

+155
-0
lines changed

5 files changed

+155
-0
lines changed

velocity/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ repositories {
1414
name = "papermc"
1515
url = uri("https://repo.papermc.io/repository/maven-public/")
1616
}
17+
maven { url = uri("https://jitpack.io") }
1718
}
1819

1920
dependencies {
2021
implementation(project(":api"))
2122
implementation(project(":common"))
23+
implementation("com.github.Carleslc.Simple-YAML:Simple-Yaml:1.8.4")
2224

2325
compileOnly("com.velocitypowered:velocity-api:3.4.0-SNAPSHOT")
2426
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package it.renvins.serverpulse.velocity.config;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
9+
import lombok.Getter;
10+
import org.simpleyaml.configuration.file.YamlFile;
11+
import org.slf4j.Logger;
12+
13+
public class VelocityConfiguration {
14+
15+
private final Logger logger;
16+
private final String name;
17+
18+
@Getter private final YamlFile config;
19+
20+
public VelocityConfiguration(Logger logger, Path dataDir, String name) {
21+
this.logger = logger;
22+
this.name = name;
23+
24+
try {
25+
Files.createDirectories(dataDir);
26+
} catch (IOException e) {
27+
logger.error("Failed to create data directory: " + dataDir, e);
28+
}
29+
this.config = new YamlFile(new File(dataDir.toFile(), name));
30+
}
31+
32+
public void load() {
33+
try {
34+
if (!config.exists()) {
35+
if (copyDefaultsFromResource()) {
36+
logger.info("Created configuration file: " + name);
37+
} else {
38+
config.createNewFile();
39+
logger.info("Configuration file not found, created a new one: " + name);
40+
}
41+
} else {
42+
logger.info("Loading configuration file: " + name);
43+
}
44+
config.load();
45+
} catch (Exception e) {
46+
logger.error("Failed to load configuration file: " + name, e);
47+
}
48+
}
49+
50+
private boolean copyDefaultsFromResource() {
51+
try (InputStream in = getClass().getResourceAsStream(name)) {
52+
if (in != null) {
53+
Files.copy(in, config.getConfigurationFile().toPath());
54+
return true;
55+
}
56+
} catch (IOException e) {
57+
logger.error("Failed to copy default configuration file: " + name, e);
58+
return false;
59+
}
60+
}
61+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package it.renvins.serverpulse.velocity.config;
2+
3+
import it.renvins.serverpulse.common.config.DatabaseConfiguration;
4+
import lombok.RequiredArgsConstructor;
5+
6+
@RequiredArgsConstructor
7+
public class VelocityDatabaseConfiguration implements DatabaseConfiguration {
8+
9+
private final VelocityConfiguration configuration;
10+
11+
@Override
12+
public String getHost() {
13+
return configuration.getConfig().getString("metrics.influxdb.url");
14+
}
15+
16+
@Override
17+
public String getOrg() {
18+
return configuration.getConfig().getString("metrics.influxdb.org");
19+
}
20+
21+
@Override
22+
public String getToken() {
23+
return configuration.getConfig().getString("metrics.influxdb.token");
24+
}
25+
26+
@Override
27+
public String getBucket() {
28+
return configuration.getConfig().getString("metrics.influxdb.bucket");
29+
}
30+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package it.renvins.serverpulse.velocity.config;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import it.renvins.serverpulse.common.config.MetricsConfiguration;
7+
import lombok.RequiredArgsConstructor;
8+
9+
@RequiredArgsConstructor
10+
public class VelocityMetricsConfiguration implements MetricsConfiguration {
11+
12+
private final VelocityConfiguration configuration;
13+
14+
@Override
15+
public String getServerTag() {
16+
return configuration.getConfig().getString("metrics.tags.server");
17+
}
18+
19+
@Override
20+
public String getMeasurementTable() {
21+
return configuration.getConfig().getString("metrics.influxdb.table");
22+
}
23+
24+
@Override
25+
public long getMetricsInterval() {
26+
return configuration.getConfig().getLong("metrics.interval");
27+
}
28+
29+
@Override
30+
public Map<String, String> getTags() {
31+
Map<String, Object> tags = configuration.getConfig().getConfigurationSection("metrics.tags").getValues(false);
32+
Map<String, String> stringTags = new HashMap<>();
33+
tags.forEach((key, value) -> {
34+
if (value instanceof String && !key.equalsIgnoreCase("server") && !key.equalsIgnoreCase("world")) {
35+
stringTags.put(key, (String) value);
36+
}
37+
});
38+
return stringTags;
39+
}
40+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
metrics:
2+
enabled: true
3+
interval: 5
4+
influxdb:
5+
url: http://localhost:8086 # The URL of the InfluxDB API
6+
org: my-org # The organization where metrics are stored
7+
bucket: metrics_db # The bucket where metrics are stored
8+
token: my-token # The token to access the InfluxDB API (WRITE AND READ ACCESS)
9+
table: minecraft_stats # The table where metrics are stored
10+
tags:
11+
server: "bed1"
12+
messages:
13+
noPerms: "&7[&bServer&7Pulse] &7You don't have &bpermission &7to use this &bcommand&7."
14+
reloadConfig: "&7[&bServer&7Pulse] &7Configuration &breloaded&7."
15+
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."
21+
statusConnected: "&7[&bServer&7Pulse] &7Connected to &bInfluxDB&7."
22+
statusNotConnected: "&7[&bServer&7Pulse] &7Not connected to &bInfluxDB&7."

0 commit comments

Comments
 (0)