11package org .mvplugins .multiverse .core ;
22
33import com .google .common .collect .Lists ;
4+ import io .vavr .control .Option ;
45import jakarta .annotation .PostConstruct ;
56import jakarta .inject .Inject ;
67import me .clip .placeholderapi .expansion .PlaceholderExpansion ;
1213import org .jetbrains .annotations .Nullable ;
1314import org .jvnet .hk2 .annotations .Service ;
1415
16+ import org .mvplugins .multiverse .core .config .CoreConfig ;
1517import org .mvplugins .multiverse .core .economy .MVEconomist ;
1618import org .mvplugins .multiverse .core .utils .MinecraftTimeFormatter ;
1719import org .mvplugins .multiverse .core .utils .REPatterns ;
1820import org .mvplugins .multiverse .core .utils .StringFormatter ;
1921import org .mvplugins .multiverse .core .world .LoadedMultiverseWorld ;
22+ import org .mvplugins .multiverse .core .world .MultiverseWorld ;
2023import org .mvplugins .multiverse .core .world .WorldManager ;
2124
2225import java .util .List ;
2629final class PlaceholderExpansionHook extends PlaceholderExpansion {
2730
2831 private final MultiverseCore plugin ;
32+ private final CoreConfig coreConfig ;
2933 private final WorldManager worldManager ;
3034 private final MVEconomist economist ;
3135
3236 @ Inject
33- public PlaceholderExpansionHook (MultiverseCore plugin , WorldManager worldManager , MVEconomist economist ) {
37+ public PlaceholderExpansionHook (@ NotNull MultiverseCore plugin ,
38+ @ NotNull CoreConfig coreConfig ,
39+ @ NotNull WorldManager worldManager ,
40+ @ NotNull MVEconomist economist ) {
3441 this .plugin = plugin ;
42+ this .coreConfig = coreConfig ;
3543 this .worldManager = worldManager ;
3644 this .economist = economist ;
3745 }
@@ -42,6 +50,13 @@ public boolean register() {
4250 return super .register ();
4351 }
4452
53+ @ Override
54+ public void warning (String msg ) {
55+ if (coreConfig .getWarnInvalidPapiFormat ()) {
56+ super .warning (msg );
57+ }
58+ }
59+
4560 @ Override
4661 public @ NotNull String getIdentifier () {
4762 return "multiverse-core" ;
@@ -80,22 +95,22 @@ public boolean persist() {
8095 return null ;
8196 }
8297
83- final var placeholder = paramsArray .remove (0 );
98+ final var placeholder = paramsArray .remove (0 ). toLowerCase ( Locale . ENGLISH ) ;
8499
85- String worldName = parseWorldName (offlinePlayer , paramsArray );
86- if (worldName == null ) return null ;
87-
88- return worldManager . getLoadedWorld ( worldName )
89- . onEmpty (() -> warning ( "Multiverse World not found: " + worldName ))
90- . map ( world -> getWorldPlaceHolderValue (placeholder , paramsArray , world ))
91- .getOrNull ( );
100+ return parseWorldName (offlinePlayer , paramsArray )
101+ . flatMap (worldName -> worldManager . getWorld ( worldName )
102+ . onEmpty (() -> warning ( "Multiverse World not found: " + worldName )))
103+ . flatMap ( world -> world . asLoadedWorld ( )
104+ . flatMap ( loadedWorld -> getLoadedWorldPlaceHolderValue ( placeholder , paramsArray , loadedWorld ))
105+ . orElse (() -> getWorldPlaceHolderValue (placeholder , paramsArray , world ) ))
106+ .getOrElse (() -> coreConfig . getInvalidPapiFormatReturnsBlank () ? "" : null );
92107 }
93108
94- private @ Nullable String parseWorldName (OfflinePlayer offlinePlayer , List <String > paramsArray ) {
109+ private @ NotNull Option < String > parseWorldName (OfflinePlayer offlinePlayer , List <String > paramsArray ) {
95110 // No world defined, get from player
96111 if (paramsArray .isEmpty ()) {
97112 if (offlinePlayer instanceof Player player ) {
98- return player .getWorld ().getName ();
113+ return Option . of ( player .getWorld ().getName () );
99114 } else {
100115 warning ("You must specify a world name for non-player placeholders" );
101116 return null ;
@@ -104,112 +119,77 @@ public boolean persist() {
104119
105120 // Try get from params
106121 String paramWorldName = paramsArray .get (paramsArray .size () - 1 );
107- if (worldManager .isLoadedWorld (paramWorldName )) {
122+ if (worldManager .isWorld (paramWorldName )) {
108123 paramsArray .remove (paramsArray .size () - 1 );
109- return paramWorldName ;
124+ return Option . of ( paramWorldName ) ;
110125 }
111126
112127 // Param not a world, fallback to player
113128 if (offlinePlayer instanceof Player player ) {
114- return player .getWorld ().getName ();
129+ return Option . of ( player .getWorld ().getName () );
115130 }
116131 warning ("Multiverse World not found: " + paramWorldName );
117- return null ;
132+ return Option . none () ;
118133 }
119134
120- private @ Nullable String getWorldPlaceHolderValue (@ NotNull String placeholder ,
121- @ NotNull List <String > placeholderParams ,
122- @ NotNull LoadedMultiverseWorld world ) {
123- // Switch to find what specific placeholder we want
124- switch (placeholder .toLowerCase (Locale .ENGLISH )) {
125- case "alias" -> {
126- return world .getAliasOrName ();
127- }
128- case "animalspawn" -> {
129- return String .valueOf (world .getEntitySpawnConfig ().getSpawnCategoryConfig (SpawnCategory .ANIMAL ).isSpawn ());
130- }
131- case "autoheal" -> {
132- return String .valueOf (world .getAutoHeal ());
133- }
134- case "blacklist" -> {
135- return String .join (", " , world .getWorldBlacklist ());
136- }
137- case "currency" -> {
138- return String .valueOf (world .getCurrency ());
139- }
140- case "difficulty" -> {
141- return world .getDifficulty ().toString ();
142- }
143- case "entryfee" -> {
144- return economist .formatPrice (world .getPrice (), world .getCurrency ());
145- }
146- case "environment" -> {
147- return world .getEnvironment ().toString ().toLowerCase ();
148- }
149- case "flight" -> {
150- return String .valueOf (world .isAllowFlight ());
151- }
152- case "gamemode" -> {
153- return world .getGameMode ().toString ().toLowerCase ();
154- }
155- case "generator" -> {
156- return world .getGenerator ();
157- }
158- case "hunger" -> {
159- return String .valueOf (world .isHunger ());
135+ private @ NotNull Option <String > getWorldPlaceHolderValue (@ NotNull String placeholder ,
136+ @ NotNull List <String > placeholderParams ,
137+ @ NotNull MultiverseWorld world ) {
138+ return Option .of (switch (placeholder ) {
139+ case "alias" -> world .getAliasOrName ();
140+ case "animalspawn" -> String .valueOf (world .getEntitySpawnConfig ()
141+ .getSpawnCategoryConfig (SpawnCategory .ANIMAL )
142+ .isSpawn ());
143+ case "autoheal" -> String .valueOf (world .getAutoHeal ());
144+ case "blacklist" -> String .join (", " , world .getWorldBlacklist ());
145+ case "currency" -> String .valueOf (world .getCurrency ());
146+ case "difficulty" -> world .getDifficulty ().toString ();
147+ case "entryfee" -> economist .formatPrice (world .getPrice (), world .getCurrency ());
148+ case "environment" -> world .getEnvironment ().toString ().toLowerCase ();
149+ case "flight" -> String .valueOf (world .isAllowFlight ());
150+ case "gamemode" -> world .getGameMode ().toString ().toLowerCase ();
151+ case "generator" -> world .getGenerator ();
152+ case "hunger" -> String .valueOf (world .isHunger ());
153+ case "monstersspawn" -> String .valueOf (world .getEntitySpawnConfig ()
154+ .getSpawnCategoryConfig (SpawnCategory .MONSTER )
155+ .isSpawn ());
156+ case "name" -> world .getName ();
157+ case "playerlimit" -> String .valueOf (world .getPlayerLimit ());
158+ case "price" -> String .valueOf (world .getPrice ());
159+ case "pvp" -> String .valueOf (world .getPvp ());
160+ case "seed" -> String .valueOf (world .getSeed ());
161+ case "weather" -> String .valueOf (world .isAllowWeather ());
162+ case "playercount" , "time" , "type" -> {
163+ warning ("Placeholder '" + placeholder + "' is only available for loaded worlds." );
164+ yield null ;
160165 }
161- case "monstersspawn" -> {
162- return String .valueOf (world .getEntitySpawnConfig ().getSpawnCategoryConfig (SpawnCategory .MONSTER ).isSpawn ());
163- }
164- case "name" -> {
165- return world .getName ();
166- }
167- case "playercount" -> {
168- return String .valueOf (world .getBukkitWorld ()
169- .map (World ::getPlayers )
170- .map (List ::size )
171- .getOrElse (-1 ));
172- }
173- case "playerlimit" -> {
174- return String .valueOf (world .getPlayerLimit ());
175- }
176- case "price" -> {
177- return String .valueOf (world .getPrice ());
178- }
179- case "pvp" -> {
180- return String .valueOf (world .getPvp ());
181- }
182- case "seed" -> {
183- return String .valueOf (world .getSeed ());
166+ default -> {
167+ warning ("Unknown Placeholder: " + placeholder );
168+ yield null ;
184169 }
170+ });
171+ }
172+
173+ private @ NotNull Option <String > getLoadedWorldPlaceHolderValue (@ NotNull String placeholder ,
174+ @ NotNull List <String > placeholderParams ,
175+ @ NotNull LoadedMultiverseWorld world ) {
176+ return Option .of (switch (placeholder ) {
177+ case "playercount" -> String .valueOf (world .getBukkitWorld ()
178+ .map (World ::getPlayers )
179+ .map (List ::size )
180+ .getOrElse (-1 ));
185181 case "time" -> {
186182 String timeFormat = !placeholderParams .isEmpty () ? placeholderParams .get (0 ) : "" ;
187183 long time = world .getBukkitWorld ().map (World ::getTime ).getOrElse (0L );
188- switch (timeFormat ) {
189- case "" -> {
190- return String .valueOf (time );
191- }
192- case "12h" -> {
193- return MinecraftTimeFormatter .format12h (time );
194- }
195- case "24h" -> {
196- return MinecraftTimeFormatter .format24h (time );
197- }
198- default -> {
199- return MinecraftTimeFormatter .formatTime (time , timeFormat );
200- }
201- }
202- }
203- case "type" -> {
204- return world .getBukkitWorld ().map (World ::getWorldType ).map (Enum ::name ).getOrElse ("null" );
205- }
206- case "weather" -> {
207- return String .valueOf (world .isAllowWeather ());
208- }
209- default -> {
210- warning ("Unknown placeholder: " + placeholder );
211- return null ;
212- }
213- }
184+ yield switch (timeFormat ) {
185+ case "" -> String .valueOf (time );
186+ case "12h" -> MinecraftTimeFormatter .format12h (time );
187+ case "24h" -> MinecraftTimeFormatter .format24h (time );
188+ default -> MinecraftTimeFormatter .formatTime (time , timeFormat );
189+ };
190+ }
191+ case "type" -> world .getBukkitWorld ().map (World ::getWorldType ).map (Enum ::name ).getOrElse ("null" );
192+ default -> null ;
193+ });
214194 }
215195}
0 commit comments