Skip to content

Commit

Permalink
Make file utils a service and optimise server folder file search
Browse files Browse the repository at this point in the history
  • Loading branch information
benwoo1110 committed Jan 8, 2025
1 parent 5dddfbe commit 9e38142
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,20 @@
import org.mvplugins.multiverse.core.commandtools.flags.ParsedCommandFlags;
import org.mvplugins.multiverse.core.event.MVDumpsDebugInfoEvent;
import org.mvplugins.multiverse.core.utils.MVCorei18n;
import org.mvplugins.multiverse.core.utils.file.FileUtils;
import org.mvplugins.multiverse.core.utils.webpaste.PasteFailedException;
import org.mvplugins.multiverse.core.utils.webpaste.PasteService;
import org.mvplugins.multiverse.core.utils.webpaste.PasteServiceFactory;
import org.mvplugins.multiverse.core.utils.webpaste.PasteServiceType;
import org.mvplugins.multiverse.core.world.WorldManager;

import static org.mvplugins.multiverse.core.utils.file.FileUtils.getBukkitConfig;
import static org.mvplugins.multiverse.core.utils.file.FileUtils.getServerProperties;

@Service
@CommandAlias("mv")
class DumpsCommand extends CoreCommand {

private final MultiverseCore plugin;
private final WorldManager worldManager;
private final FileUtils fileUtils;

private final CommandValueFlag<LogsTypeOption> LOGS_FLAG = flag(CommandValueFlag
.enumBuilder("--logs", LogsTypeOption.class)
Expand All @@ -64,10 +63,12 @@ class DumpsCommand extends CoreCommand {
@Inject
DumpsCommand(@NotNull MVCommandManager commandManager,
@NotNull MultiverseCore plugin,
@NotNull WorldManager worldManager) {
@NotNull WorldManager worldManager,
@NotNull FileUtils fileUtils) {
super(commandManager);
this.plugin = plugin;
this.worldManager = worldManager;
this.fileUtils = fileUtils;
}

private enum ServiceTypeOption {
Expand Down Expand Up @@ -200,15 +201,15 @@ private void addDebugInfoToEvent(MVDumpsDebugInfoEvent event) {
event.putDetailedDebugInfo("multiverse-core/worlds.yml", worldsFile);

// Add bukkit.yml if we found it
if (getBukkitConfig() != null) {
event.putDetailedDebugInfo(getBukkitConfig().getPath(), getBukkitConfig());
if (fileUtils.getBukkitConfig() != null) {
event.putDetailedDebugInfo(fileUtils.getBukkitConfig().getPath(), fileUtils.getBukkitConfig());
} else {
Logging.warning("/mv version could not find bukkit.yml. Not including file");
}

// Add server.properties if we found it
if (getServerProperties() != null) {
event.putDetailedDebugInfo(getServerProperties().getPath(), getServerProperties());
if (fileUtils.getServerProperties() != null) {
event.putDetailedDebugInfo(fileUtils.getServerProperties().getPath(), fileUtils.getServerProperties());
} else {
Logging.warning("/mv version could not find server.properties. Not including file");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,72 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;

import com.dumptruckman.minecraft.util.Logging;
import jakarta.inject.Inject;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.core.MultiverseCore;

import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
import static org.bukkit.Bukkit.getServer;

/**
* File-utilities.
*/
@Service
//todo: Review duplicated methods in FileManipulator
public class FileUtils {
protected FileUtils() {
throw new UnsupportedOperationException();

private final File serverFolder;
private final File bukkitYml;
private final File serverProperties;

@Inject
protected FileUtils(@NotNull MultiverseCore plugin) {
this.serverFolder = getServerFolder(plugin);
Logging.fine("Server folder: " + this.serverFolder);
this.bukkitYml = findFileFromServerDirectory("bukkit.yml");
this.serverProperties = findFileFromServerDirectory("server.properties");
}

private File getServerFolder(@NotNull Plugin plugin) {
return new File(System.getProperty("user.dir"));
}

@Nullable
private File findFileFromServerDirectory(String fileName) {
File[] files;
try {
files = this.serverFolder.listFiles((file, s) -> s.equalsIgnoreCase(fileName));
} catch (Exception e) {
Logging.severe("Could not read from server directory. Unable to locate file: %s", fileName);
Logging.severe(e.getMessage());
return null;
}

// TODO: Implement binary search to find file, config option or use reflections to get it from configuration on CraftServer
if (files != null && files.length == 1) {
return files[0];
}
Logging.warning("Unable to locate file from server directory: %s", fileName);
return null;
}


/**
* Used to delete a folder.
*
* @param file The folder to delete.
* @return true if the folder was successfully deleted.
*/
public static boolean deleteFolder(File file) {
public boolean deleteFolder(File file) {
try (Stream<Path> files = Files.walk(file.toPath())) {
files.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
return true;
Expand All @@ -54,7 +90,7 @@ public static boolean deleteFolder(File file) {
* @param file The folder whose contents to delete.
* @return true if the contents were successfully deleted
*/
public static boolean deleteFolderContents(File file) {
public boolean deleteFolderContents(File file) {
try (Stream<Path> files = Files.walk(file.toPath())){
files.sorted(Comparator.reverseOrder())
.map(Path::toFile)
Expand All @@ -74,7 +110,7 @@ public static boolean deleteFolderContents(File file) {
*
* @return true if it had success
*/
public static boolean copyFolder(File source, File target) {
public boolean copyFolder(File source, File target) {
return copyFolder(source, target, null);
}

Expand All @@ -86,7 +122,7 @@ public static boolean copyFolder(File source, File target) {
*
* @return true if it had success
*/
public static boolean copyFolder(File source, File target, List<String> excludeFiles) {
public boolean copyFolder(File source, File target, List<String> excludeFiles) {
Path sourceDir = source.toPath();
Path targetDir = target.toPath();

Expand All @@ -100,33 +136,13 @@ public static boolean copyFolder(File source, File target, List<String> excludeF
}

@Nullable
public static File getBukkitConfig() {
return findFileFromServerDirectory("bukkit.yml");
}

@Nullable
public static File getServerProperties() {
return findFileFromServerDirectory("server.properties");
public File getBukkitConfig() {
return this.bukkitYml;
}

@Nullable
private static File findFileFromServerDirectory(String fileName) {
File[] files;
try {
// TODO: getWorldContainer may throw error for MockBukkit during test
files = getServer().getWorldContainer().listFiles((file, s) -> s.equalsIgnoreCase(fileName));
} catch (Exception e) {
Logging.severe("Could not read from server directory. Unable to locate file: %s", fileName);
Logging.severe(e.getMessage());
return null;
}

// TODO: Implement binary search to find file, config option or use reflections to get it from configuration on CraftServer
if (files != null && files.length == 1) {
return files[0];
}
Logging.warning("Unable to locate file from server directory: %s", fileName);
return null;
public File getServerProperties() {
return this.serverProperties;
}

private static class CopyDirFileVisitor extends SimpleFileVisitor<Path> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
import org.jvnet.hk2.annotations.Service;

import org.mvplugins.multiverse.core.MultiverseCore;

import static org.mvplugins.multiverse.core.utils.file.FileUtils.getBukkitConfig;
import org.mvplugins.multiverse.core.utils.file.FileUtils;

/**
* Parse the default world generators from the bukkit config and load any generator plugins.
Expand All @@ -38,9 +37,11 @@
public class GeneratorProvider implements Listener {
private final Map<String, String> defaultGenerators;
private final Map<String, GeneratorPlugin> generatorPlugins;
private final FileUtils fileUtils;

@Inject
GeneratorProvider(@NotNull MultiverseCore multiverseCore) {
GeneratorProvider(@NotNull MultiverseCore multiverseCore, @NotNull FileUtils fileUtils) {
this.fileUtils = fileUtils;
defaultGenerators = new HashMap<>();
generatorPlugins = new HashMap<>();

Expand All @@ -53,7 +54,7 @@ public class GeneratorProvider implements Listener {
* Load the default world generators string from the bukkit config.
*/
private void loadDefaultWorldGenerators() {
File bukkitConfigFile = getBukkitConfig();
File bukkitConfigFile = fileUtils.getBukkitConfig();
if (bukkitConfigFile == null) {
Logging.warning("Any default world generators will not be loaded!");
return;
Expand Down

0 comments on commit 9e38142

Please sign in to comment.