@@ -27,12 +27,17 @@ of this software and associated documentation files (the "Software"), to deal
27
27
import java .io .IOException ;
28
28
import java .nio .file .Files ;
29
29
import java .nio .file .Path ;
30
+ import java .time .format .DateTimeFormatter ;
31
+ import java .util .ArrayList ;
30
32
import java .util .HashMap ;
33
+ import java .util .HashSet ;
34
+ import java .util .List ;
31
35
import java .util .Set ;
32
36
import java .util .stream .Collectors ;
33
37
34
38
import org .slf4j .Logger ;
35
39
import org .spongepowered .api .Game ;
40
+ import org .spongepowered .api .Sponge ;
36
41
import org .spongepowered .api .asset .Asset ;
37
42
import org .spongepowered .api .command .CommandException ;
38
43
import org .spongepowered .api .command .CommandResult ;
@@ -64,15 +69,10 @@ of this software and associated documentation files (the "Software"), to deal
64
69
import me .rojo8399 .placeholderapi .configs .Config ;
65
70
import me .rojo8399 .placeholderapi .configs .JavascriptManager ;
66
71
import me .rojo8399 .placeholderapi .configs .Messages ;
67
- import me .rojo8399 .placeholderapi .expansions .CurrencyExpansion ;
68
- import me .rojo8399 .placeholderapi .expansions .DateTimeExpansion ;
69
- import me .rojo8399 .placeholderapi .expansions .Expansion ;
70
- import me .rojo8399 .placeholderapi .expansions .JavascriptExpansion ;
71
- import me .rojo8399 .placeholderapi .expansions .PlayerExpansion ;
72
- import me .rojo8399 .placeholderapi .expansions .RankExpansion ;
73
- import me .rojo8399 .placeholderapi .expansions .ServerExpansion ;
74
- import me .rojo8399 .placeholderapi .expansions .SoundExpansion ;
75
- import me .rojo8399 .placeholderapi .expansions .StatisticExpansion ;
72
+ import me .rojo8399 .placeholderapi .placeholder .Expansion ;
73
+ import me .rojo8399 .placeholderapi .placeholder .ExpansionBuilder ;
74
+ import me .rojo8399 .placeholderapi .placeholder .Store ;
75
+ import me .rojo8399 .placeholderapi .placeholder .impl .Defaults ;
76
76
import ninja .leaping .configurate .ConfigurationNode ;
77
77
import ninja .leaping .configurate .commented .CommentedConfigurationNode ;
78
78
import ninja .leaping .configurate .hocon .HoconConfigurationLoader ;
@@ -85,7 +85,7 @@ public class PlaceholderAPIPlugin {
85
85
86
86
public static final String PLUGIN_ID = "placeholderapi" ;
87
87
public static final String PLUGIN_NAME = "PlaceholderAPI" ;
88
- public static final String PLUGIN_VERSION = "3.12 " ;
88
+ public static final String PLUGIN_VERSION = "4.0 " ;
89
89
private static PlaceholderAPIPlugin instance ;
90
90
91
91
@ Inject
@@ -116,9 +116,14 @@ public class PlaceholderAPIPlugin {
116
116
private ConfigurationNode msgRoot ;
117
117
private Messages msgs ;
118
118
private ConfigurationLoader <CommentedConfigurationNode > msgloader ;
119
+ private DateTimeFormatter formatter ;
119
120
120
121
private PlaceholderService s ;
121
122
123
+ public DateTimeFormatter formatter () {
124
+ return formatter ;
125
+ }
126
+
122
127
public ConfigurationNode getRootConfig () {
123
128
return root ;
124
129
}
@@ -201,6 +206,7 @@ public void onGamePreInitializationEvent(GamePreInitializationEvent event)
201
206
}
202
207
}
203
208
Messages .init (msgs );
209
+ this .formatter = DateTimeFormatter .ofPattern (config .dateFormat );
204
210
}
205
211
206
212
@ Listener
@@ -236,36 +242,119 @@ public CommandResult execute(CommandSource src, CommandContext args) throws Comm
236
242
237
243
}
238
244
245
+ private Set <Object > alreadyRegistered = new HashSet <>();
246
+
247
+ public void registerListeners (Object object ) {
248
+ if (alreadyRegistered .contains (object )) {
249
+ return ;
250
+ }
251
+ Sponge .getEventManager ().registerListeners (this , object );
252
+ alreadyRegistered .add (object );
253
+ }
254
+
255
+ public void unregisterListeners (Object object ) {
256
+ Sponge .getEventManager ().unregisterListeners (object );
257
+ if (alreadyRegistered .contains (object )) {
258
+ alreadyRegistered .remove (object );
259
+ }
260
+ }
261
+
239
262
@ Listener
240
263
public void onGameStartingServerEvent (GameStartingServerEvent event ) {
241
264
registerPlaceholders ();
242
265
metrics .addCustomChart (new Metrics .SimpleBarChart ("placeholders" ) {
243
266
@ Override
244
267
public HashMap <String , Integer > getValues (HashMap <String , Integer > valueMap ) {
245
- Set <Expansion > exp = s .getExpansions ();
246
- if (exp .isEmpty ()) {
268
+ List <String > rids = Store .get ().ids (true );
269
+ List <String > ids = Store .get ().ids (false );
270
+ if (rids .isEmpty () && ids .isEmpty ()) {
247
271
HashMap <String , Integer > out = new HashMap <>();
248
272
out .put ("none" , 1 );
249
273
return out ;
250
274
}
251
- return (HashMap <String , Integer >) exp .stream ()
252
- .collect (Collectors .toMap (Expansion ::getIdentifier , e -> 1 ));
275
+ List <String > exp = new ArrayList <>();
276
+ rids .forEach (e -> exp .add ("rel_" + e ));
277
+ ids .forEach (exp ::add );
278
+ return (HashMap <String , Integer >) exp .stream ().collect (Collectors .toMap (e -> e , e -> 1 ));
253
279
}
254
280
});
255
281
}
256
282
257
283
public void registerPlaceholders () {
258
- s .registerPlaceholder (new JavascriptExpansion (jsm ));
259
- s .registerPlaceholder (new PlayerExpansion ());
260
- s .registerPlaceholder (new ServerExpansion ());
261
- s .registerPlaceholder (new SoundExpansion ());
262
- s .registerPlaceholder (new RankExpansion ());
263
- if (game .getServiceManager ().provide (EconomyService .class ).isPresent ()) {
264
- s .registerPlaceholder (
265
- new CurrencyExpansion (game .getServiceManager ().provideUnchecked (EconomyService .class )));
284
+ EconomyService ex = game .getServiceManager ().provide (EconomyService .class ).orElse (null );
285
+ Defaults handle = new Defaults (ex , this .jsm );
286
+ ExpansionBuilder .loadAll (handle , this ).stream ().map (builder -> {
287
+ switch (builder .getId ()) {
288
+ case "player" : {
289
+ if (builder .isRelational ()) {
290
+ return builder .description (Messages .get ().placeholder .relplayerdesc .value )
291
+ .tokens ("distance" , "audible" , "visible" , "distance_x" , "distance_y" , "distance_z" )
292
+ .version ("2.0" );
293
+ } else {
294
+ return builder .description (Messages .get ().placeholder .playerdesc .value )
295
+ .tokens (null , "prefix" , "suffix" , "option_[option]" , "permission_[permission]" , "name" ,
296
+ "displayname" , "uuid" , "can_fly" , "world" , "ping" , "language" , "flying" , "health" ,
297
+ "max_health" , "food" , "saturation" , "gamemode" , "x" , "y" , "z" , "direction" , "exp" ,
298
+ "exp_total" , "exp_to_next" , "level" , "first_join" , "fly_speed" , "max_air" ,
299
+ "remaining_air" , "item_in_main_hand" , "item_in_off_hand" , "walk_speed" ,
300
+ "time_played" , "time_played_ticks" , "time_played_seconds" , "time_played_minutes" ,
301
+ "time_played_hours" , "time_played_days" )
302
+ .version ("2.0" );
303
+ }
304
+ }
305
+ case "rank" : {
306
+ if (builder .isRelational ()) {
307
+ return builder .description (Messages .get ().placeholder .relrankdesc .value )
308
+ .tokens ("greater_than" , "less_than" ).version ("1.0" );
309
+ } else {
310
+ return builder .description (Messages .get ().placeholder .rankdesc .value )
311
+ .tokens (null , "prefix" , "suffix" , "name" , "permission_[permission]" , "option_[option]" )
312
+ .version ("2.0" );
313
+ }
314
+ }
315
+ case "javascript" :
316
+ return builder .description (Messages .get ().placeholder .jsdesc .value ).tokens (jsm .getScriptNames ())
317
+ .reloadFunction (e -> {
318
+ try {
319
+ jsm .reloadScripts ();
320
+ } catch (Exception exc ) {
321
+ getLogger ().warn ("Error reloading JavaScript placeholders!" );
322
+ exc .printStackTrace ();
323
+ return false ;
324
+ }
325
+ e .setTokens (jsm .getScriptNames ());
326
+ return true ;
327
+ }).version ("2.0" );
328
+ case "economy" :
329
+ return builder .description (Messages .get ().placeholder .curdesc .value )
330
+ .tokens ("" , "[currency]" , "balance" , "balance_[currency]" , "bal_format_[currency]" ,
331
+ "bal_format" , "display" , "display_[currency]" , "plural_display_[currency]" ,
332
+ "symbol_[currency]" , "plural_display" , "symbol" )
333
+ .version ("2.0" );
334
+ case "server" :
335
+ return builder
336
+ .description (Messages .get ().placeholder .serverdesc .value ).tokens ("unique_players" , "online" ,
337
+ "max_players" , "motd" , "cores" , "tps" , "ram_used" , "ram_free" , "ram_total" , "ram_max" )
338
+ .version ("2.0" );
339
+ case "sound" :
340
+ return builder .description (Messages .get ().placeholder .sounddesc .value )
341
+ .tokens ("[sound]-[volume]-[pitch]" ).version ("2.0" );
342
+ case "statistic" :
343
+ return builder .description (Messages .get ().placeholder .statdesc .value ).version ("2.0" );
344
+ case "time" :
345
+ return builder .description (Messages .get ().placeholder .timedesc .value ).tokens ("" ).version ("2.0" );
346
+ }
347
+ return builder ;
348
+ }).forEach (t -> {
349
+ try {
350
+ t .author ("Wundero" ).plugin (this ).buildAndRegister ();
351
+ } catch (Exception e ) {
352
+ e .printStackTrace ();
353
+ }
354
+ });
355
+ if (ex == null ) {
356
+ Store .get ().get ("economy" , false ).ifPresent (Expansion ::disable );
266
357
}
267
- s .registerPlaceholder (new DateTimeExpansion ());
268
- s .registerPlaceholder (new StatisticExpansion ());
269
358
}
270
359
271
360
@ Listener
0 commit comments