Uses KnarLib for common tasks

This commit is contained in:
2022-11-07 00:07:32 +01:00
parent 3c4394d6fa
commit 3c805ee284
21 changed files with 159 additions and 501 deletions

View File

@ -1,32 +0,0 @@
package net.knarcraft.blacksmith.util;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
/**
* A helper class for dealing with files
*/
public final class FileHelper {
private FileHelper() {
}
/**
* Gets a buffered reader for
*
* @return <p>A buffered read for reading the file</p>
* @throws FileNotFoundException <p>If unable to get an input stream for the given file</p>
*/
public static BufferedReader getBufferedReaderForInternalFile(String file) throws FileNotFoundException {
InputStream inputStream = FileHelper.class.getResourceAsStream(file);
if (inputStream == null) {
throw new FileNotFoundException("Unable to read the given file");
}
return new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
}
}

View File

@ -1,32 +0,0 @@
package net.knarcraft.blacksmith.util;
import java.util.ArrayList;
import java.util.List;
/**
* A helper class for tab-completion
*/
public final class TabCompletionHelper {
private TabCompletionHelper() {
}
/**
* Finds tab complete values that contain the typed text
*
* @param values <p>The values to filter</p>
* @param typedText <p>The text the player has started typing</p>
* @return <p>The given string values that contain the player's typed text</p>
*/
public static List<String> filterMatchingContains(List<String> values, String typedText) {
List<String> configValues = new ArrayList<>();
for (String value : values) {
if (value.toLowerCase().contains(typedText.toLowerCase())) {
configValues.add(value);
}
}
return configValues;
}
}

View File

@ -1,13 +1,12 @@
package net.knarcraft.blacksmith.util;
import net.knarcraft.blacksmith.config.SettingValueType;
import net.knarcraft.blacksmith.formatting.TranslatableMessage;
import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage;
import net.knarcraft.knarlib.formatting.StringFormatter;
import org.bukkit.command.CommandSender;
import java.util.List;
import static net.knarcraft.blacksmith.formatting.StringFormatter.displayErrorMessage;
/**
* A helper class for validating a value's type
*/
@ -52,7 +51,7 @@ public final class TypeValidationHelper {
private static boolean isStringList(Object value, CommandSender sender) {
boolean isStringList = value instanceof String[] || value instanceof List<?> || value instanceof String;
if (!isStringList && sender != null) {
displayErrorMessage(sender, TranslatableMessage.INPUT_STRING_LIST_REQUIRED);
StringFormatter.displayErrorMessage(sender, BlacksmithTranslatableMessage.INPUT_STRING_LIST_REQUIRED);
}
return isStringList;
}
@ -70,7 +69,7 @@ public final class TypeValidationHelper {
return intValue >= 0 && intValue <= 100;
} catch (NumberFormatException | NullPointerException exception) {
if (sender != null) {
displayErrorMessage(sender, TranslatableMessage.INPUT_PERCENTAGE_REQUIRED);
StringFormatter.displayErrorMessage(sender, BlacksmithTranslatableMessage.INPUT_PERCENTAGE_REQUIRED);
}
return false;
}
@ -86,7 +85,7 @@ public final class TypeValidationHelper {
private static boolean isNonEmptyString(Object value, CommandSender sender) {
boolean isString = value instanceof String string && !string.strip().isEmpty();
if (!isString && sender != null) {
displayErrorMessage(sender, TranslatableMessage.INPUT_STRING_REQUIRED);
StringFormatter.displayErrorMessage(sender, BlacksmithTranslatableMessage.INPUT_STRING_REQUIRED);
}
return isString;
}
@ -103,7 +102,7 @@ public final class TypeValidationHelper {
return ConfigHelper.asDouble(value) >= 0.0;
} catch (NumberFormatException | NullPointerException exception) {
if (sender != null) {
displayErrorMessage(sender, TranslatableMessage.INPUT_POSITIVE_DOUBLE_REQUIRED);
StringFormatter.displayErrorMessage(sender, BlacksmithTranslatableMessage.INPUT_POSITIVE_DOUBLE_REQUIRED);
}
return false;
}
@ -121,7 +120,7 @@ public final class TypeValidationHelper {
return ConfigHelper.asInt(value) >= 0;
} catch (NumberFormatException | NullPointerException exception) {
if (sender != null) {
displayErrorMessage(sender, TranslatableMessage.INPUT_POSITIVE_INTEGER_REQUIRED);
StringFormatter.displayErrorMessage(sender, BlacksmithTranslatableMessage.INPUT_POSITIVE_INTEGER_REQUIRED);
}
return false;
}