Skip to content

Commit b4be9eb

Browse files
committed
make config updater better
1 parent 7a18649 commit b4be9eb

File tree

2 files changed

+106
-29
lines changed

2 files changed

+106
-29
lines changed

src/main/java/lol/hyper/toolstats/tools/config/ConfigUpdater.java

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
package lol.hyper.toolstats.tools.config;
1919

2020
import lol.hyper.toolstats.ToolStats;
21-
import lol.hyper.toolstats.tools.config.versions.Version6;
22-
import lol.hyper.toolstats.tools.config.versions.Version7;
23-
import lol.hyper.toolstats.tools.config.versions.Version8;
24-
import lol.hyper.toolstats.tools.config.versions.Version9;
21+
import lol.hyper.toolstats.tools.config.versions.*;
2522

2623
public class ConfigUpdater {
2724

@@ -34,31 +31,30 @@ public ConfigUpdater(ToolStats toolStats) {
3431
public void updateConfig() {
3532
int version = toolStats.config.getInt("config-version");
3633

37-
switch(version) {
38-
case 5: {
39-
// Version 5 to 6
40-
Version6 version6 = new Version6(toolStats);
41-
version6.update();
42-
break;
43-
}
44-
case 6: {
45-
// Version 6 to 7
46-
Version7 version7 = new Version7(toolStats);
47-
version7.update();
48-
break;
49-
}
50-
case 7: {
51-
// Version 7 to 8
52-
Version8 version8 = new Version8(toolStats);
53-
version8.update();
54-
break;
55-
}
56-
case 8: {
57-
// Version 8 to 9
58-
Version9 version9 = new Version9(toolStats);
59-
version9.update();
60-
break;
61-
}
34+
// Version 5 to 6
35+
if (version == 5) {
36+
Version6 version6 = new Version6(toolStats);
37+
version6.update();
38+
}
39+
// Version 6 to 7
40+
if (version == 6) {
41+
Version7 version7 = new Version7(toolStats);
42+
version7.update();
43+
}
44+
// Version 7 to 8
45+
if (version == 7) {
46+
Version8 version8 = new Version8(toolStats);
47+
version8.update();
48+
}
49+
// Version 8 to 9
50+
if (version == 8) {
51+
Version9 version9 = new Version9(toolStats);
52+
version9.update();
53+
}
54+
// Version 9 to 10
55+
if (version == 9) {
56+
Version10 version10 = new Version10(toolStats);
57+
version10.update();
6258
}
6359
}
6460
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* This file is part of ToolStats.
3+
*
4+
* ToolStats is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* ToolStats is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with ToolStats. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package lol.hyper.toolstats.tools.config.versions;
19+
20+
import lol.hyper.toolstats.ToolStats;
21+
22+
import java.io.File;
23+
import java.io.IOException;
24+
25+
public class Version10 {
26+
27+
private final ToolStats toolStats;
28+
29+
/**
30+
* Used for updating from version 9 to 10.
31+
*
32+
* @param toolStats ToolStats instance.
33+
*/
34+
public Version10(ToolStats toolStats) {
35+
this.toolStats = toolStats;
36+
}
37+
38+
/**
39+
* Perform the config update.
40+
*/
41+
public void update() {
42+
// save the old config first
43+
try {
44+
toolStats.config.save("plugins" + File.separator + "ToolStats" + File.separator + "config-9.yml");
45+
} catch (IOException exception) {
46+
toolStats.logger.severe("Unable to save config-9.yml!");
47+
throw new RuntimeException(exception);
48+
}
49+
50+
// we make this super verbose so that admins can see what's being added
51+
toolStats.logger.info("Updating config.yml to version 10.");
52+
toolStats.config.set("config-version", 10);
53+
54+
// Add missing values I forgot...
55+
toolStats.logger.info("Adding entry for enabled.created-by.fishing-rod");
56+
toolStats.config.set("enabled.created-by.fishing-rod", true);
57+
toolStats.logger.info("Adding entry for enabled.created-date.fishing-rod");
58+
toolStats.config.set("enabled.created-date.fishing-rod", true);
59+
toolStats.logger.info("Adding entry for enabled.fished-tag.fishing-rod");
60+
toolStats.config.set("enabled.fished-tag.fishing-rod", true);
61+
toolStats.logger.info("Adding entry for enabled.looted-tag.fishing-rod");
62+
toolStats.config.set("enabled.looted-tag.fishing-rod", true);
63+
toolStats.logger.info("Adding entry for enabled.traded-tag.fishing-rod");
64+
toolStats.config.set("enabled.traded-tag.fishing-rod", true);
65+
toolStats.logger.info("Adding entry for enabled.spawned-in.fishing-rod");
66+
toolStats.config.set("enabled.spawned-in.fishing-rod", true);
67+
68+
toolStats.logger.info("Adding entry for enabled.crops-harvested");
69+
toolStats.config.set("enabled.crops-harvested", true);
70+
71+
// save the config and reload it
72+
try {
73+
toolStats.config.save("plugins" + File.separator + "ToolStats" + File.separator + "config.yml");
74+
} catch (IOException exception) {
75+
toolStats.logger.severe("Unable to save config.yml!");
76+
throw new RuntimeException(exception);
77+
}
78+
toolStats.loadConfig();
79+
toolStats.logger.info("Config has been updated to version 10. A copy of version 9 has been saved as config-9.yml");
80+
}
81+
}

0 commit comments

Comments
 (0)