1818@ ApiStatus .AvailableSince ("5.6" )
1919public final class BukkitCompatibility {
2020
21- private static final Option <Method > getLevelDirectoryMethod ;
22- private static final Option <Method > getWorldNamespacedKeyMethod ;
21+ private static final Option <Method > GET_LEVEL_DIRECTORY_METHOD ;
22+ private static final Option <Method > GET_WORLD_NAMESPACED_KEY_METHOD ;
2323
2424 static {
25- getLevelDirectoryMethod = Option .of (ReflectHelper .getMethod (Server .class , "getLevelDirectory" ));
26- getWorldNamespacedKeyMethod = Option .of (ReflectHelper .getMethod (Bukkit .class , "getWorld" , NamespacedKey .class ));
25+ GET_LEVEL_DIRECTORY_METHOD = Option .of (ReflectHelper .getMethod (Server .class , "getLevelDirectory" ));
26+ GET_WORLD_NAMESPACED_KEY_METHOD = Option .of (ReflectHelper .getMethod (Bukkit .class , "getWorld" , NamespacedKey .class ));
2727 }
2828
2929 /**
@@ -39,15 +39,19 @@ public final class BukkitCompatibility {
3939 @ NotNull
4040 public static Path getWorldFoldersDirectory () {
4141 Server server = Bukkit .getServer ();
42- return getLevelDirectoryMethod .map (method -> ReflectHelper .invokeMethod (server , method ))
42+ return GET_LEVEL_DIRECTORY_METHOD .map (method -> ReflectHelper .invokeMethod (server , method ))
4343 .filter (Path .class ::isInstance )
4444 .map (Path .class ::cast )
4545 .map (path -> path .resolve ("dimensions/minecraft" ))
4646 .getOrElse (() -> server .getWorldContainer ().toPath ());
4747 }
4848
4949 /**
50- * Check if the world with the given name or namespaced key (e.g. minecraft:overworld) exists, and return it if it does.
50+ * Check if the world with the given name or namespaced key (e.g. minecraft:overworld) exists,
51+ * and return it if it does.
52+ * <br />
53+ * Note that some default world names have different namespaced key matched with them.
54+ * E.g.: world -> minecraft:overworld, world_nether -> minecraft:the_nether, world_the_end -> minecraft:the_end.
5155 *
5256 * @param nameOrKey Either a name or namespaced key string representation.
5357 * @return The world if it exists
@@ -56,7 +60,7 @@ public static Path getWorldFoldersDirectory() {
5660 @ NotNull
5761 public static Option <World > getWorldByNameOrKey (@ NotNull String nameOrKey ) {
5862 return Option .of (Bukkit .getWorld (nameOrKey ))
59- .orElse (() -> getWorldNamespacedKeyMethod
63+ .orElse (() -> GET_WORLD_NAMESPACED_KEY_METHOD
6064 .map (method -> ReflectHelper .invokeMethod (null , method , NamespacedKey .fromString (nameOrKey )))
6165 .filter (World .class ::isInstance )
6266 .map (World .class ::cast ));
0 commit comments