Skip to content

Commit 9d17eca

Browse files
committed
Added LanguageMigrator
1 parent 317d22c commit 9d17eca

4 files changed

Lines changed: 140 additions & 5 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
<dependency>
8787
<groupId>plugily.projects</groupId>
8888
<artifactId>MiniGamesBox-Classic</artifactId>
89-
<version>1.4.2-SNAPSHOT6</version>
89+
<version>1.4.3</version>
9090
<scope>compile</scope>
9191
<optional>true</optional>
9292
</dependency>

src/main/java/plugily/projects/buildbattle/Main.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import plugily.projects.buildbattle.boot.MessageInitializer;
3333
import plugily.projects.buildbattle.boot.PlaceholderInitializer;
3434
import plugily.projects.buildbattle.commands.arguments.ArgumentsRegistry;
35+
import plugily.projects.buildbattle.handlers.LanguageMigrator;
3536
import plugily.projects.buildbattle.handlers.menu.OptionsRegistry;
3637
import plugily.projects.buildbattle.handlers.misc.BlacklistManager;
3738
import plugily.projects.buildbattle.handlers.misc.HeadDatabaseManager;
@@ -70,6 +71,7 @@ public void onEnable() {
7071
MessageInitializer messageInitializer = new MessageInitializer(this);
7172
super.onEnable();
7273
getDebugger().debug("[System] [Plugin] Initialization start");
74+
new LanguageMigrator(this);
7375
arenaRegistry = new ArenaRegistry(this);
7476
new PlaceholderInitializer(this);
7577
messageInitializer.registerMessages();
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* MiniGamesBox - Library box with massive content that could be seen as minigames core.
3+
* Copyright (C) 2023 Plugily Projects - maintained by Tigerpanzer_02 and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
20+
package plugily.projects.buildbattle.handlers;
21+
22+
import org.bukkit.configuration.file.FileConfiguration;
23+
import plugily.projects.buildbattle.Main;
24+
import plugily.projects.minigamesbox.classic.PluginMain;
25+
import plugily.projects.minigamesbox.classic.utils.configuration.ConfigUtils;
26+
import plugily.projects.minigamesbox.classic.utils.migrator.MigratorUtils;
27+
28+
import java.io.File;
29+
import java.io.IOException;
30+
import java.nio.file.Files;
31+
import java.nio.file.Paths;
32+
import java.util.logging.Level;
33+
34+
/*
35+
NOTE FOR CONTRIBUTORS - Please do not touch this class if you don't know how it works! You can break migrator modifying these values!
36+
*/
37+
38+
/**
39+
* @author Tigerpanzer_02
40+
* <p>
41+
* Created at 21.09.2021
42+
*/
43+
@SuppressWarnings("deprecation")
44+
public class LanguageMigrator {
45+
46+
public enum PluginFileVersion {
47+
/*ARENA_SELECTOR(0),*/ BUNGEE(1), CONFIG(1), LANGUAGE(2),
48+
/*LEADERBOARDS(0),*/ MYSQL(1), PERMISSIONS(1), POWERUPS(1),
49+
/*SIGNS(0),*/ SPECIAL_ITEMS(1), SPECTATOR(1)/*, STATS(0)*/;
50+
51+
private final int version;
52+
53+
PluginFileVersion(int version) {
54+
this.version = version;
55+
}
56+
57+
public int getVersion() {
58+
return version;
59+
}
60+
}
61+
62+
private final Main plugin;
63+
64+
public LanguageMigrator(Main plugin) {
65+
this.plugin = plugin;
66+
updatePluginFiles();
67+
}
68+
69+
private void updatePluginFiles() {
70+
for(PluginFileVersion pluginFileVersion : PluginFileVersion.values()) {
71+
String fileName = pluginFileVersion.name().toLowerCase();
72+
int newVersion = pluginFileVersion.getVersion();
73+
File file = new File(plugin.getDataFolder() + "/" + fileName + ".yml");
74+
FileConfiguration configuration = ConfigUtils.getConfig(plugin, fileName, false);
75+
if(configuration == null) {
76+
continue;
77+
}
78+
int oldVersion = configuration.getInt("Do-Not-Edit.File-Version", 0);
79+
if(oldVersion == newVersion) {
80+
continue;
81+
}
82+
plugin.getDebugger().debug(Level.WARNING, "[System notify] The " + fileName + " file is outdated! Updating...");
83+
for(int i = oldVersion; i < newVersion; i++) {
84+
executeUpdate(file, pluginFileVersion, i);
85+
}
86+
87+
updatePluginFileVersion(file, configuration, oldVersion, newVersion);
88+
plugin.getDebugger().debug(Level.WARNING, "[System notify] " + fileName + " updated, no comments were removed :)");
89+
plugin.getDebugger().debug(Level.WARNING, "[System notify] You're using latest " + fileName + " file now! Nice!");
90+
91+
}
92+
}
93+
94+
private void executeUpdate(File file, PluginFileVersion pluginFileVersion, int version) {
95+
switch(pluginFileVersion) {
96+
case LANGUAGE:
97+
switch(version) {
98+
case 1:
99+
MigratorUtils.insertAfterLine(file, " Heads:", " Database:\n" +
100+
" Lore: \"Get Head %value%\"");
101+
break;
102+
default:
103+
break;
104+
}
105+
break;
106+
default:
107+
break;
108+
}
109+
}
110+
111+
public void updatePluginFileVersion(File file, FileConfiguration fileConfiguration, int oldVersion, int newVersion) {
112+
int coreVersion = fileConfiguration.getInt("Do-Not-Edit.Core-Version", 0);
113+
updateFileVersion(file, coreVersion, coreVersion, newVersion, oldVersion);
114+
}
115+
116+
private void updateFileVersion(File file, int coreVersion, int oldCoreVersion, int fileVersion, int oldFileVersion) {
117+
MigratorUtils.removeLineFromFile(file, "# Don't edit it. But who's stopping you? It's your server!");
118+
MigratorUtils.removeLineFromFile(file, "# Really, don't edit ;p");
119+
MigratorUtils.removeLineFromFile(file, "# You edited it, huh? Next time hurt yourself!");
120+
MigratorUtils.removeLineFromFile(file, "Do-Not-Edit:");
121+
MigratorUtils.removeLineFromFile(file, " File-Version: " + oldFileVersion + "");
122+
MigratorUtils.removeLineFromFile(file, " Core-Version: " + oldCoreVersion + "");
123+
MigratorUtils.addNewLines(file, "# Don't edit it. But who's stopping you? It's your server!\r\n" +
124+
"# Really, don't edit ;p\r\n" +
125+
"# You edited it, huh? Next time hurt yourself!\r\n" +
126+
"Do-Not-Edit:\r\n" +
127+
" File-Version: " + fileVersion + "\r\n" +
128+
" Core-Version: " + coreVersion + "\r\n");
129+
}
130+
131+
}

src/main/resources/locales/language_default.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ In-Game:
290290
Own: "&aYou became #%number%"
291291
Admin:
292292
Set-Starting-In-To-0: "%plugin_prefix% An admin set waiting time to 0. The game starts now!"
293-
Changed-Theme: "%plugin_prefix% Admin has changed theme to %THEME%"
293+
Changed-Theme: "%plugin_prefix% Admin has changed theme to %value%"
294294
Plot:
295295
Nobody: "%color_chat_issue%Nobody"
296296
Time-Left:
@@ -374,6 +374,8 @@ Menu:
374374
Item:
375375
Name: "&bGet player heads"
376376
Lore: "Click to get heads"
377+
Database:
378+
Lore: "Get Head %value%"
377379
Floor:
378380
Item:
379381
Name: "&bChange floor material"
@@ -390,7 +392,7 @@ Menu:
390392
Noon: "Noon (6000 ticks)"
391393
Sunset: "Sunset (12000 ticks)"
392394
Night: "Night (13000 ticks)"
393-
MidNight: "MidNight (18000 ticks)"
395+
MidNight: "Midnight (18000 ticks)"
394396
Sunrise: "Sunrise (23000 ticks)"
395397
Changed: "%plugin_prefix% Time has been changed to %value%"
396398
Biome:
@@ -568,5 +570,5 @@ Leaderboard:
568570
# Really, don't edit ;p
569571
# You edited it, huh? Next time hurt yourself!
570572
Do-Not-Edit:
571-
File-Version: 1
572-
Core-Version: 1
573+
File-Version: 2
574+
Core-Version: 2

0 commit comments

Comments
 (0)