Including WorldUtil and PrimaryWorlds classes (from Pandacube private code)

This commit is contained in:
Marc Baloup 2022-12-12 17:32:03 +01:00
parent edea5835ad
commit b427d23dd6
Signed by: marcbal
GPG Key ID: BBC0FE3ABC30B893
2 changed files with 115 additions and 0 deletions

View File

@ -0,0 +1,40 @@
package fr.pandacube.lib.paper.reflect.util;
import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.CraftServer;
import fr.pandacube.lib.reflect.wrapper.ReflectWrapper;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Utility method to get the list of the primary world of the Bukkit/Paper server.
* The primary worlds are the world that are loaded by the server at the startup, that the plugins cannot unload.
*/
public class PrimaryWorlds {
/**
* An unmodifiable list containing the names of the primary worlds of this server instance, in the order they are
* loaded. This list can be accessed even if the corresponding worlds are not yet loaded, for instance in the
* {@link Plugin#onLoad()} method.
*/
public static final List<String> PRIMARY_WORLDS;
static {
List<String> primaryWorlds = new ArrayList<>(3);
String world = ReflectWrapper.wrapTyped(Bukkit.getServer(), CraftServer.class).getServer().getLevelIdName();
primaryWorlds.add(world);
if (Bukkit.getAllowNether()) primaryWorlds.add(world + "_nether");
if (Bukkit.getAllowEnd()) primaryWorlds.add(world + "_the_end");
PRIMARY_WORLDS = Collections.unmodifiableList(primaryWorlds);
}
}

View File

@ -0,0 +1,75 @@
package fr.pandacube.lib.paper.util;
import org.bukkit.Bukkit;
import org.bukkit.World.Environment;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
public class WorldUtil {
public static Environment determineEnvironment(String world) {
if (Bukkit.getWorld(world) != null) {
return Bukkit.getWorld(world).getEnvironment();
}
File worldFolder = worldDir(world);
if (!worldFolder.isDirectory())
throw new IllegalStateException("The world " + world + " is not a valid world (directory not found).");
if (!new File(worldFolder, "level.dat").isFile())
throw new IllegalStateException("The world " + world + " is not a valid world (level.dat not found).");
if (new File(worldFolder, "region").isDirectory())
return Environment.NORMAL;
if (new File(worldFolder, "DIM-1" + File.pathSeparator + "region").isDirectory())
return Environment.NETHER;
if (new File(worldFolder, "DIM1" + File.pathSeparator + "region").isDirectory())
return Environment.THE_END;
throw new IllegalStateException("Unable to determine the type of the world " + world + ".");
}
private static final List<String> REGION_DATA_FILES = Arrays.asList("entities", "poi", "region", "DIM-1", "DIM1");
public static List<File> regionDataFiles(String world) {
return onlyExistents(worldDir(world), REGION_DATA_FILES);
}
public static List<File> mapFiles(String world) {
Pattern mapFilePattern = Pattern.compile("map_\\d+.dat");
return List.of(dataDir(world).listFiles((dir, name) -> mapFilePattern.matcher(name).find() || "idcounts.dat".equals(name)));
}
private static List<File> onlyExistents(File worldDir, List<String> searchList) {
return searchList.stream()
.map(f -> new File(worldDir, f))
.filter(File::exists)
.toList();
}
public static File worldDir(String world) {
return new File(Bukkit.getWorldContainer(), world);
}
public static File dataDir(String world) {
return new File(worldDir(world), "data");
}
public static boolean isValidWorld(String world) {
File d = worldDir(world);
return d.isDirectory() && new File(d, "level.dat").isFile();
}
}