3
3
import com .github .siroshun09 .configapi .api .util .ResourceUtils ;
4
4
import com .github .siroshun09 .configapi .yaml .YamlConfiguration ;
5
5
import com .github .siroshun09 .translationloader .directory .TranslationDirectory ;
6
+ import com .google .inject .Inject ;
7
+ import com .velocitypowered .api .event .PostOrder ;
8
+ import com .velocitypowered .api .event .Subscribe ;
9
+ import com .velocitypowered .api .event .proxy .ProxyInitializeEvent ;
10
+ import com .velocitypowered .api .event .proxy .ProxyReloadEvent ;
11
+ import com .velocitypowered .api .event .proxy .ProxyShutdownEvent ;
12
+ import com .velocitypowered .api .plugin .annotation .DataDirectory ;
13
+ import com .velocitypowered .api .proxy .ProxyServer ;
14
+ import com .velocitypowered .api .proxy .server .RegisteredServer ;
6
15
import net .kyori .adventure .key .Key ;
7
- import net .md_5 .bungee .api .plugin .Plugin ;
8
16
import net .okocraft .serverconnector .command .SlashServerCommand ;
9
17
import net .okocraft .serverconnector .config .ConfigValues ;
10
18
import net .okocraft .serverconnector .listener .FirstJoinListener ;
11
19
import net .okocraft .serverconnector .listener .PlayerListener ;
12
- import net .okocraft .serverconnector .listener .ServerListener ;
13
- import net .okocraft .serverconnector .listener .SnapshotClientListener ;
14
- import net .okocraft .serverconnector .util .AudienceUtil ;
15
20
import org .jetbrains .annotations .NotNull ;
21
+ import org .slf4j .Logger ;
16
22
17
23
import java .io .IOException ;
18
24
import java .nio .file .Path ;
25
+ import java .util .ArrayList ;
26
+ import java .util .List ;
19
27
import java .util .Locale ;
20
28
21
- public final class ServerConnectorPlugin extends Plugin {
29
+ public final class ServerConnectorPlugin {
22
30
23
- private final YamlConfiguration config = YamlConfiguration .create (getDataFolder ().toPath ().resolve ("config.yml" ));
24
- private final TranslationDirectory translationDirectory =
25
- TranslationDirectory .newBuilder ()
26
- .setDirectory (getDataFolder ().toPath ().resolve ("languages" ))
27
- .setKey (Key .key ("serverconnector" , "language" ))
28
- .setDefaultLocale (Locale .ENGLISH )
29
- .onDirectoryCreated (this ::saveDefaultLanguages )
30
- .build ();
31
+ private final ProxyServer proxy ;
32
+ private final Logger logger ;
33
+ private final YamlConfiguration config ;
34
+ private final TranslationDirectory translationDirectory ;
35
+ private final List <SlashServerCommand > registeredSlashServerCommands = new ArrayList <>();
31
36
32
37
private FirstJoinListener firstJoinListener ;
33
38
34
- @ Override
35
- public void onLoad () {
39
+ @ Inject
40
+ public ServerConnectorPlugin (@ NotNull ProxyServer proxy , @ NotNull Logger logger ,
41
+ @ DataDirectory Path dataDirectory ) {
42
+ this .proxy = proxy ;
43
+ this .logger = logger ;
44
+
45
+ this .config = YamlConfiguration .create (dataDirectory .resolve ("config.yml" ));
46
+ this .translationDirectory =
47
+ TranslationDirectory .newBuilder ()
48
+ .setDirectory (dataDirectory .resolve ("languages" ))
49
+ .setKey (Key .key ("serverconnector" , "language" ))
50
+ .setDefaultLocale (Locale .ENGLISH )
51
+ .onDirectoryCreated (this ::saveDefaultLanguages )
52
+ .build ();
53
+ }
54
+
55
+ @ Subscribe (order = PostOrder .FIRST )
56
+ public void onEnable (ProxyInitializeEvent ignored ) {
36
57
try {
37
58
ResourceUtils .copyFromClassLoaderIfNotExists (getClass ().getClassLoader (), "config.yml" , config .getPath ());
38
59
config .load ();
@@ -45,74 +66,74 @@ public void onLoad() {
45
66
} catch (IOException e ) {
46
67
throw new IllegalStateException ("Failed to load languages" , e );
47
68
}
48
- }
49
-
50
- @ Override
51
- public void onEnable () {
52
- AudienceUtil .init (this );
53
69
54
70
enablePlayerListener ();
55
- enableServerListener ();
56
71
enableSlashServer ();
57
72
enableFirstJoinDetector ();
58
- enableSnapshotListenerIfConfigured ();
59
73
}
60
74
61
- @ Override
62
- public void onDisable () {
75
+ @ Subscribe ( order = PostOrder . LAST )
76
+ public void onDisable (ProxyShutdownEvent ignored ) {
63
77
if (firstJoinListener != null ) {
64
78
firstJoinListener .unsubscribe ();
65
79
}
66
80
67
- getProxy ().getPluginManager ().unregisterListeners (this );
68
- getProxy ().getPluginManager ().unregisterCommands (this );
81
+ getProxy ().getEventManager ().unregisterListeners (this );
82
+
83
+ registeredSlashServerCommands .forEach (SlashServerCommand ::unregister );
84
+
69
85
translationDirectory .unload ();
70
86
}
71
87
88
+ @ Subscribe
89
+ public void onReload (ProxyReloadEvent ignored ) {
90
+ if (!registeredSlashServerCommands .isEmpty ()) {
91
+ registeredSlashServerCommands .forEach (SlashServerCommand ::unregister );
92
+ enableSlashServer ();
93
+ }
94
+ }
95
+
96
+ public @ NotNull ProxyServer getProxy () {
97
+ return proxy ;
98
+ }
99
+
100
+ public @ NotNull Logger getLogger () {
101
+ return logger ;
102
+ }
103
+
72
104
public @ NotNull YamlConfiguration getConfig () {
73
105
return config ;
74
106
}
75
107
76
108
private void enablePlayerListener () {
77
109
var playerListener = new PlayerListener (this );
78
- getProxy ().getPluginManager ().registerListener (this , playerListener );
79
- }
80
-
81
- private void enableServerListener () {
82
- var serverListener = new ServerListener (this );
83
- getProxy ().getPluginManager ().registerListener (this , serverListener );
110
+ getProxy ().getEventManager ().register (this , playerListener );
84
111
}
85
112
86
113
public void enableSlashServer () {
87
- getProxy ().getServers ().values ().stream ()
88
- .map (SlashServerCommand ::new )
89
- .forEach (cmd -> getProxy ().getPluginManager ().registerCommand (this , cmd ));
114
+ getProxy ().getAllServers ().stream ()
115
+ .map (server -> new SlashServerCommand (this , server ))
116
+ .forEach (command -> {
117
+ command .register ();
118
+ registeredSlashServerCommands .add (command );
119
+ });
90
120
}
91
121
92
122
private void enableFirstJoinDetector () {
93
- if (getProxy ().getPluginManager ().getPlugin ("LuckPerms" ) != null && config .get (ConfigValues .SEND_FIRST_JOIN_MESSAGE )) {
123
+ if (getProxy ().getPluginManager ().getPlugin ("LuckPerms" ). isPresent () && config .get (ConfigValues .SEND_FIRST_JOIN_MESSAGE )) {
94
124
firstJoinListener = new FirstJoinListener (this );
95
125
}
96
126
}
97
127
98
- private void enableSnapshotListenerIfConfigured () {
99
- if (config .get (ConfigValues .ENABLE_SNAPSHOT_SERVER )) {
100
- var snapshotListener = new SnapshotClientListener (this );
101
- getProxy ().getPluginManager ().registerListener (this , snapshotListener );
102
- }
103
- }
104
-
105
128
private void saveDefaultLanguages (@ NotNull Path directory ) throws IOException {
106
- var jarPath = getFile ().toPath ();
107
-
108
129
var defaultFileName = "en.yml" ;
109
130
var defaultFile = directory .resolve (defaultFileName );
110
131
111
- ResourceUtils .copyFromJarIfNotExists ( jarPath , defaultFileName , defaultFile );
132
+ ResourceUtils .copyFromClassLoaderIfNotExists ( getClass (). getClassLoader () , defaultFileName , defaultFile );
112
133
113
134
var japaneseFileName = "ja_JP.yml" ;
114
135
var japaneseFile = directory .resolve (japaneseFileName );
115
136
116
- ResourceUtils .copyFromJarIfNotExists ( jarPath , japaneseFileName , japaneseFile );
137
+ ResourceUtils .copyFromClassLoaderIfNotExists ( getClass (). getClassLoader () , japaneseFileName , japaneseFile );
117
138
}
118
139
}
0 commit comments