19
19
package net .onelitefeather .bettergopaint ;
20
20
21
21
import com .fastasyncworldedit .core .Fawe ;
22
+ import net .kyori .adventure .key .Key ;
23
+ import net .kyori .adventure .text .Component ;
22
24
import net .kyori .adventure .text .minimessage .MiniMessage ;
25
+ import net .kyori .adventure .translation .GlobalTranslator ;
26
+ import net .kyori .adventure .translation .TranslationRegistry ;
27
+ import net .kyori .adventure .util .UTF8ResourceBundleControl ;
23
28
import net .onelitefeather .bettergopaint .brush .PlayerBrushManager ;
24
29
import net .onelitefeather .bettergopaint .command .GoPaintCommand ;
25
30
import net .onelitefeather .bettergopaint .command .ReloadCommand ;
26
31
import net .onelitefeather .bettergopaint .listeners .ConnectListener ;
27
32
import net .onelitefeather .bettergopaint .listeners .InteractListener ;
28
33
import net .onelitefeather .bettergopaint .listeners .InventoryListener ;
29
34
import net .onelitefeather .bettergopaint .objects .other .Settings ;
35
+ import net .onelitefeather .bettergopaint .service .UpdateService ;
36
+ import net .onelitefeather .bettergopaint .translations .PluginTranslationRegistry ;
30
37
import org .bstats .bukkit .Metrics ;
31
38
import org .bstats .charts .SimplePie ;
32
39
import org .bukkit .Bukkit ;
44
51
import org .jetbrains .annotations .Nullable ;
45
52
46
53
import java .io .File ;
54
+ import java .io .IOException ;
55
+ import java .net .URL ;
56
+ import java .net .URLClassLoader ;
57
+ import java .nio .file .Files ;
58
+ import java .nio .file .Path ;
59
+ import java .util .HashSet ;
60
+ import java .util .Locale ;
47
61
import java .util .Objects ;
62
+ import java .util .ResourceBundle ;
48
63
import java .util .logging .Level ;
49
64
50
65
public class BetterGoPaint extends JavaPlugin implements Listener {
51
66
52
67
public static final String PAPER_DOCS = "https://jd.papermc.io/paper/1.20.6/org/bukkit/Material.html#enum-constant-summary" ;
53
- public static final String USE_PERMISSION = "bettergopaint.use" ;
54
- public static final String ADMIN_PERMISSION = "bettergopaint.admin" ;
55
- public static final String RELOAD_PERMISSION = "bettergopaint.command.admin.reload" ;
56
- public static final String WORLD_BYPASS_PERMISSION = "bettergopaint.world.bypass" ;
57
68
58
69
private final PlayerBrushManager brushManager = new PlayerBrushManager ();
59
70
private final Metrics metrics = new Metrics (this , 18734 );
71
+ private UpdateService updateService ;
60
72
61
73
@ Override
62
74
public void onLoad () {
@@ -81,6 +93,30 @@ public void onEnable() {
81
93
82
94
reloadConfig ();
83
95
96
+ final TranslationRegistry translationRegistry = new PluginTranslationRegistry (TranslationRegistry .create (Key .key ("bettergopaint" , "translations" )));
97
+ translationRegistry .defaultLocale (Locale .US );
98
+ Path langFolder = getDataFolder ().toPath ().resolve ("lang" );
99
+ var languages = new HashSet <>(Settings .settings ().generic .LANGUAGES );
100
+ languages .add ("en-US" );
101
+ if (Files .exists (langFolder )) {
102
+ try (var urlClassLoader = new URLClassLoader (new URL []{langFolder .toUri ().toURL ()})) {
103
+ languages .stream ().map (Locale ::forLanguageTag ).forEach (r -> {
104
+ var bundle = ResourceBundle .getBundle ("bettergopaint" , r , urlClassLoader , UTF8ResourceBundleControl .get ());
105
+ translationRegistry .registerAll (r , bundle , false );
106
+ });
107
+ } catch (IOException e ) {
108
+ throw new RuntimeException (e );
109
+ }
110
+ } else {
111
+ languages .stream ().map (Locale ::forLanguageTag ).forEach (r -> {
112
+ var bundle = ResourceBundle .getBundle ("bettergopaint" , r , UTF8ResourceBundleControl .get ());
113
+ translationRegistry .registerAll (r , bundle , false );
114
+ });
115
+ }
116
+ GlobalTranslator .translator ().addSource (translationRegistry );
117
+ donationInformation ();
118
+
119
+
84
120
Material brush = Settings .settings ().generic .DEFAULT_BRUSH ;
85
121
if (!brush .isItem ()) {
86
122
getComponentLogger ().error ("{} is not a valid default brush, it has to be an item" , brush .name ());
@@ -95,17 +131,34 @@ public void onEnable() {
95
131
96
132
registerListeners ();
97
133
registerCommands ();
134
+ updateService ();
98
135
}
99
136
100
137
@ Override
101
138
public void onDisable () {
102
139
metrics .shutdown ();
140
+ this .updateService .shutdown ();
103
141
}
104
142
105
143
public void reloadConfig () {
106
- Settings .settings ().reload (this , new File (getDataFolder (), "config.yml" ));
144
+ try {
145
+ Files .createDirectories (getDataFolder ().toPath ());
146
+ final Path resolve = getDataFolder ().toPath ().resolve ("config.yml" );
147
+ Settings .settings ().save (resolve .toFile ());
148
+ Settings .settings ().load (resolve .toFile ());
149
+ } catch (IOException e ) {
150
+ getLogger ().log (Level .SEVERE , "Cannot init config" , e );
151
+ }
152
+
107
153
}
108
154
155
+ private void updateService () {
156
+ this .updateService = new UpdateService (this );
157
+ this .updateService .run ();
158
+ this .updateService .notifyConsole (getComponentLogger ());
159
+ }
160
+
161
+
109
162
@ SuppressWarnings ("UnstableApiUsage" )
110
163
private void registerCommands () {
111
164
Bukkit .getCommandMap ().register ("gopaint" , getPluginMeta ().getName (), new GoPaintCommand (this ));
@@ -121,13 +174,17 @@ private void registerListeners() {
121
174
PluginManager pm = getServer ().getPluginManager ();
122
175
pm .registerEvents (new InventoryListener (getBrushManager ()), this );
123
176
pm .registerEvents (new InteractListener (this ), this );
124
- pm .registerEvents (new ConnectListener (getBrushManager ()), this );
177
+ pm .registerEvents (new ConnectListener (getBrushManager (), this ), this );
125
178
}
126
179
127
180
private boolean hasOriginalGoPaint () {
128
181
return getServer ().getPluginManager ().getPlugin ("goPaint" ) != this ;
129
182
}
130
183
184
+ private void donationInformation () {
185
+ getComponentLogger ().info (Component .translatable ("bettergopaint.notify.donation.console" ));
186
+ }
187
+
131
188
private @ Nullable AnnotationParser <CommandSender > enableCommandSystem () {
132
189
try {
133
190
LegacyPaperCommandManager <CommandSender > commandManager = LegacyPaperCommandManager .createNative (
@@ -150,4 +207,8 @@ private boolean hasOriginalGoPaint() {
150
207
return brushManager ;
151
208
}
152
209
210
+ public UpdateService getUpdateService () {
211
+ return updateService ;
212
+ }
213
+
153
214
}
0 commit comments