26
26
import org .spongepowered .api .event .game .state .GameInitializationEvent ;
27
27
import org .spongepowered .api .event .game .state .GamePostInitializationEvent ;
28
28
import org .spongepowered .api .event .game .state .GamePreInitializationEvent ;
29
+ import org .spongepowered .api .event .game .state .GameStoppingServerEvent ;
29
30
import org .spongepowered .api .event .message .MessageChannelEvent ;
30
31
import org .spongepowered .api .event .network .ClientConnectionEvent ;
31
32
import org .spongepowered .api .plugin .Plugin ;
@@ -56,6 +57,8 @@ public class ChatUI {
56
57
57
58
private static ChatUI instance ;
58
59
60
+ private boolean enabledAsService = false ;
61
+
59
62
@ Inject
60
63
@ DefaultConfig (sharedRoot = true )
61
64
private ConfigurationLoader <CommentedConfigurationNode > confLoader ;
@@ -80,6 +83,14 @@ public static PlayerChatView getView(UUID uuid) {
80
83
return instance .playerViewMap .get (uuid );
81
84
}
82
85
86
+ public static ActivePlayerChatView getActiveView (CommandSource source ) {
87
+ return (ActivePlayerChatView ) getView (source );
88
+ }
89
+
90
+ public static ActivePlayerChatView getActiveView (UUID uuid ) {
91
+ return (ActivePlayerChatView ) getView (uuid );
92
+ }
93
+
83
94
public static ClickAction <?> command (String subcommand ) {
84
95
return TextActions .runCommand ("/chatui " + subcommand );
85
96
}
@@ -102,12 +113,22 @@ public void onInit(GameInitializationEvent event) {
102
113
Sponge .getGame ().getCommandManager ().register (this , new ChatUICommand (), "chatui" );
103
114
Config .init (this .confLoader , this .logger );
104
115
116
+ this .enabledAsService = !Config .getRootNode ().getNode ("interfaceEnabled" ).getBoolean (true );
117
+
118
+ if (this .enabledAsService ) {
119
+ return ;
120
+ }
121
+
105
122
this .featuresToLoad = Maps .newHashMap ();
106
123
107
124
this .registerFeature (this , "privmsg" , PrivateMessageFeature ::new );
108
125
this .registerFeature (this , "chatgroup" , ChatGroupFeature ::new );
109
126
}
110
127
128
+ public boolean isServiceOnlyMode () {
129
+ return this .enabledAsService ;
130
+ }
131
+
111
132
public void registerFeature (Object plugin , String id , Supplier <AbstractFeature > featureLoader ) {
112
133
checkState (this .featuresToLoad != null , "Not accepting new features to be registered" );
113
134
String featureId = Sponge .getPluginManager ().fromInstance (plugin ).get ().getId () + ":" + id ;
@@ -116,6 +137,9 @@ public void registerFeature(Object plugin, String id, Supplier<AbstractFeature>
116
137
117
138
@ Listener (order = Order .POST )
118
139
public void onPostInit (GamePostInitializationEvent event ) {
140
+ if (this .enabledAsService ) {
141
+ return ;
142
+ }
119
143
Optional <ProviderRegistration <PaginationService >> optService = Sponge .getGame ().getServiceManager ().getRegistration (PaginationService .class );
120
144
if (!optService .isPresent ()) {
121
145
return ;
@@ -157,6 +181,10 @@ public void onGameReload(GameReloadEvent event) {
157
181
158
182
@ Listener
159
183
public void onPlayerJoin (ClientConnectionEvent .Join event ) {
184
+ if (this .enabledAsService ) {
185
+ this .playerViewMap .put (event .getTargetEntity ().getUniqueId (), new ExternalServiceView (event .getTargetEntity ()));
186
+ return ;
187
+ }
160
188
initialize (event .getTargetEntity ());
161
189
}
162
190
@@ -169,6 +197,9 @@ void initialize(Player player) {
169
197
view = new ActivePlayerChatView (player , playerSettings );
170
198
}
171
199
PlayerChatView oldView = this .playerViewMap .put (player .getUniqueId (), view );
200
+ if (oldView != null ) {
201
+ oldView .onRemove ();
202
+ }
172
203
for (AbstractFeature feature : this .features ) {
173
204
if (oldView != null ) {
174
205
feature .onViewClose (oldView );
@@ -182,10 +213,12 @@ void initialize(Player player) {
182
213
public void onPlayerQuit (ClientConnectionEvent .Disconnect event ) {
183
214
PlayerChatView view = this .playerViewMap .remove (event .getTargetEntity ().getUniqueId ());
184
215
Config .saveConfig ();
185
- for (AbstractFeature feature : this .features ) {
186
- feature .onViewClose (view );
216
+ if (!this .enabledAsService ) {
217
+ for (AbstractFeature feature : this .features ) {
218
+ feature .onViewClose (view );
219
+ }
187
220
}
188
- view .getWindow (). closeAll ();
221
+ view .onRemove ();
189
222
// TODO Offline message buffering?
190
223
}
191
224
@@ -201,6 +234,11 @@ public void onIncomingMessage(MessageChannelEvent.Chat event, @Root Player playe
201
234
}
202
235
}
203
236
237
+ @ Listener
238
+ public void onServerStop (GameStoppingServerEvent event ) {
239
+ Config .saveConfig ();
240
+ }
241
+
204
242
// Try to be last so we can wrap around any messages sent
205
243
@ Listener (order = Order .POST )
206
244
public void onOutgoingMessage (MessageChannelEvent event ) {
0 commit comments