Refactoring

This commit is contained in:
filoghost 2021-08-03 18:51:08 +02:00
parent d94cb681a8
commit e161dd18f9
5 changed files with 36 additions and 40 deletions

View File

@ -86,7 +86,7 @@ public class ReadtextCommand extends LineEditingCommand {
hologram.setLines(newLines);
hologramEditor.saveChanges(hologram, ChangeType.EDIT_LINES);
if (isSupportedImageExtension(FileUtils.getExtension(fileToRead))) {
if (FileUtils.hasFileExtension(fileToRead, "jpg", "png", "jpeg", "gif")) {
DisplayFormat.sendWarning(sender, "The read file has an image's extension."
+ " If it is an image, you should use /" + context.getRootLabel() + " readimage.");
}
@ -94,8 +94,4 @@ public class ReadtextCommand extends LineEditingCommand {
sender.sendMessage(ColorScheme.PRIMARY + "The lines were pasted into the hologram.");
}
private boolean isSupportedImageExtension(String extension) {
return Arrays.asList("jpg", "png", "jpeg", "gif").contains(extension.toLowerCase());
}
}

View File

@ -19,6 +19,7 @@ import me.filoghost.fcommons.logging.Log;
import me.filoghost.holographicdisplays.plugin.hologram.internal.InternalHologramManager;
import me.filoghost.holographicdisplays.plugin.placeholder.internal.AnimationPlaceholder;
import me.filoghost.holographicdisplays.plugin.placeholder.internal.AnimationPlaceholderFactory;
import me.filoghost.holographicdisplays.plugin.util.FileUtils;
import java.io.IOException;
import java.nio.file.Files;
@ -136,8 +137,8 @@ public class ConfigManager extends BaseConfigManager {
if (!Files.isRegularFile(file)) {
return false;
}
String fileName = file.getFileName().toString().toLowerCase();
return fileName.endsWith(".yml") || fileName.endsWith(".yaml");
return FileUtils.hasFileExtension(file, "yml", "yaml");
}
private void logConfigInitException(ErrorCollector errorCollector, Path file, ConfigException e) {

View File

@ -8,6 +8,7 @@ package me.filoghost.holographicdisplays.plugin.config.upgrade;
import me.filoghost.fcommons.config.Config;
import me.filoghost.fcommons.logging.ErrorCollector;
import me.filoghost.holographicdisplays.plugin.config.ConfigManager;
import me.filoghost.holographicdisplays.plugin.util.FileUtils;
import java.io.IOException;
import java.nio.file.Files;
@ -49,8 +50,8 @@ public class LegacyAnimationsUpgrade {
}
String newFileName = oldFile.getFileName().toString();
if (LegacyUpgradeUtils.hasFileExtension(newFileName, "txt")) {
newFileName = LegacyUpgradeUtils.removeFileExtension(newFileName);
if (FileUtils.hasFileExtension(newFileName, "txt")) {
newFileName = FileUtils.removeFileExtension(newFileName);
}
newFileName += ".yml";
Path newFile = oldFile.resolveSibling(newFileName);

View File

@ -5,38 +5,18 @@
*/
package me.filoghost.holographicdisplays.plugin.config.upgrade;
import me.filoghost.holographicdisplays.plugin.util.FileUtils;
import java.nio.file.Path;
class LegacyUpgradeUtils {
private static final String BACKUP_FILE_EXTENSION = ".backup";
static Path getBackupFile(Path file) {
return file.resolveSibling(file.getFileName() + BACKUP_FILE_EXTENSION);
return file.resolveSibling(file.getFileName() + ".backup");
}
static boolean isBackupFile(Path file) {
return file.getFileName().toString().toLowerCase().endsWith(BACKUP_FILE_EXTENSION);
}
static boolean hasFileExtension(String fileName, String extension) {
int extensionBeginIndex = fileName.lastIndexOf('.');
if (extensionBeginIndex < 0) {
return false;
}
return fileName.substring(extensionBeginIndex + 1).equalsIgnoreCase(extension);
}
static String removeFileExtension(String fileName) {
int extensionBeginIndex = fileName.lastIndexOf('.');
if (extensionBeginIndex < 0) {
return fileName;
}
return fileName.substring(0, extensionBeginIndex);
return FileUtils.hasFileExtension(file, "backup");
}
}

View File

@ -5,19 +5,37 @@
*/
package me.filoghost.holographicdisplays.plugin.util;
import me.filoghost.fcommons.Strings;
import org.jetbrains.annotations.NotNull;
import java.nio.file.Path;
public class FileUtils {
public static String getExtension(Path file) {
String fileName = file.getFileName().toString();
int lastFullStop = fileName.lastIndexOf('.');
if (lastFullStop >= 0) {
return fileName.substring(lastFullStop + 1);
} else {
return "";
public static boolean hasFileExtension(@NotNull Path file, @NotNull String... validExtensions) {
return hasFileExtension(file.getFileName().toString(), validExtensions);
}
public static boolean hasFileExtension(@NotNull String fileName, @NotNull String... validExtensions) {
int extensionBeginIndex = fileName.lastIndexOf('.');
if (extensionBeginIndex < 0) {
return false;
}
String extension = fileName.substring(extensionBeginIndex + 1);
return Strings.containsIgnoreCase(validExtensions, extension);
}
public static String removeFileExtension(@NotNull String fileName) {
int extensionBeginIndex = fileName.lastIndexOf('.');
if (extensionBeginIndex < 0) {
return fileName;
}
return fileName.substring(0, extensionBeginIndex);
}
public static boolean isInsideDirectory(Path file, Path directory) {