Skip to content

Commit fdb1c1d

Browse files
committed
Migrate old bungee configs
1 parent df17578 commit fdb1c1d

File tree

5 files changed

+71
-12
lines changed

5 files changed

+71
-12
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ apiToken = 'example-token'
5454

5555
# Watch servers in the proxy config and automatically remove them when they go offline
5656
# Note that this only works if you use .exaroton.me addresses in your velocity config.
57-
watch-servers = true
57+
watchServers = true
5858

5959
# Automatically start servers when the proxy starts
60-
[auto-start]
60+
[autoStartServers]
6161
enabled = false
6262
servers = ["example.exaroton.me"]
6363

6464
# Automatically stop servers when the proxy stops
65-
[auto-stop]
65+
[autoStopServers]
6666
enabled = false
6767
servers = ["example.exaroton.me"]
6868
```

buildSrc/src/main/groovy/multiloader-base.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
base {
9-
archivesName = "${plugin_id}-${project.name}-${minecraft_version}"
9+
archivesName = "${plugin_id}-${project.name}"
1010
}
1111

1212
java {

bungeecord/src/main/java/com/exaroton/proxy/ProxyPluginImpl.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
import com.exaroton.proxy.servers.ProxyServerManager;
44
import net.md_5.bungee.api.CommandSender;
5+
import net.md_5.bungee.config.Configuration;
6+
import net.md_5.bungee.config.ConfigurationProvider;
7+
import net.md_5.bungee.config.YamlConfiguration;
58

9+
import java.io.IOException;
10+
import java.nio.file.Files;
611
import java.util.Collection;
712
import java.util.stream.Collectors;
813

@@ -32,4 +37,46 @@ public ProxyServerManager<?> createProxyServerManager() {
3237
public Collection<String> getPlayers() {
3338
return bungeePlugin.getProxy().getPlayers().stream().map(CommandSender::getName).collect(Collectors.toList());
3439
}
40+
41+
@Override
42+
protected void migrateOldConfigFields() {
43+
if (configFile.isEmpty()) {
44+
var oldPluginDir = bungeePlugin.getProxy().getPluginsFolder().toPath().resolve("ExarotonBungeePlugin");
45+
var oldConfigFile = oldPluginDir.resolve("config.yml").toFile();
46+
47+
if (oldConfigFile.exists()) {
48+
Constants.LOG.info("Migrating old YAML config to TOML");
49+
var configProvider = ConfigurationProvider.getProvider(YamlConfiguration.class);
50+
try {
51+
var oldConfig = configProvider.load(oldConfigFile);
52+
53+
writeConfig(oldConfig, "");
54+
55+
Files.delete(oldConfigFile.toPath());
56+
try (var children = Files.list(oldPluginDir)) {
57+
if (children.findAny().isPresent()) {
58+
Constants.LOG.warn("Can't delete old plugin directory as it contains other files.");
59+
} else {
60+
Files.delete(oldPluginDir);
61+
}
62+
}
63+
} catch (IOException e) {
64+
Constants.LOG.error("Failed to migrate old YAML config: {}", e.getMessage(), e);
65+
}
66+
}
67+
}
68+
69+
super.migrateOldConfigFields();
70+
}
71+
72+
private void writeConfig(Object value, String key) {
73+
if (value instanceof Configuration) {
74+
var config = (Configuration) value;
75+
for (String otherKey : config.getKeys()) {
76+
writeConfig(config.get(otherKey), (key.isEmpty() ? "" : (key + ".")) + otherKey);
77+
}
78+
} else {
79+
configFile.set(key, value);
80+
}
81+
}
3582
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Every field you add must be added to the root build.gradle expandProps map.
33

44
# Project
5-
version=2.0.0
5+
version=2.0.0-SNAPSHOT
66
group=com.exaroton.proxy
77
java_version=21
88

proxy/src/main/java/com/exaroton/proxy/CommonProxyPlugin.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public abstract class CommonProxyPlugin {
5454
* @return a future that completes when setup is done
5555
*/
5656
public CompletableFuture<Void> setUp() {
57-
initializeConfig();
57+
loadConfig();
5858

5959
if (config.apiToken == null || config.apiToken.isEmpty() || config.apiToken.equals("example-token")) {
6060
throw new IllegalStateException("No API token provided. Please set the API token in the configuration file.");
@@ -154,13 +154,25 @@ public final ProxyServerManager<?> getProxyServerManager() {
154154
*/
155155
public abstract Collection<String> getPlayers();
156156

157-
protected void initializeConfig() {
158-
configFile = Services.platform().getConfig()
159-
.autoreload()
160-
.onAutoReload(this::onConfigLoaded)
161-
.autosave()
162-
.writingMode(WritingMode.REPLACE_ATOMIC)
157+
/**
158+
* Initializes the configFile field
159+
*/
160+
protected void initializeConfigFile() {
161+
if (configFile == null) {
162+
configFile = Services.platform().getConfig()
163+
.autoreload()
164+
.onAutoReload(this::onConfigLoaded)
165+
.autosave()
166+
.writingMode(WritingMode.REPLACE_ATOMIC)
163167
.build();
168+
}
169+
}
170+
171+
/**
172+
* Loads the config, migrates old fields and updates the config file
173+
*/
174+
protected void loadConfig() {
175+
initializeConfigFile();
164176
configFile.load();
165177
migrateOldConfigFields();
166178
onConfigLoaded(false);

0 commit comments

Comments
 (0)