Splits the blacksmith command into two commands, and much more

This commit is contained in:
2022-08-19 19:08:54 +02:00
parent 755db8c497
commit e1191dad7d
21 changed files with 1077 additions and 235 deletions

View File

@ -0,0 +1,62 @@
package net.knarcraft.blacksmith.util;
/**
* A helper class for getting an object value as the correct type
*/
public final class ConfigHelper {
private ConfigHelper() {
}
/**
* Gets the given value as a double
*
* <p>This will throw an exception if used for a non-double setting</p>
*
* @param value <p>The object value to get</p>
* @return <p>The value of the given object as a double</p>
*/
public static double asDouble(Object value) {
if (value instanceof String) {
return Double.parseDouble((String) value);
} else if (value instanceof Integer) {
return (Integer) value;
} else {
return (Double) value;
}
}
/**
* Gets the given value as a boolean
*
* <p>This will throw an exception if used for a non-boolean value</p>
*
* @param value <p>The object value to get</p>
* @return <p>The value of the given object as a boolean</p>
*/
public static boolean asBoolean(Object value) {
if (value instanceof String) {
return Boolean.parseBoolean((String) value);
} else {
return (Boolean) value;
}
}
/**
* Gets the given value as an integer
*
* <p>This will throw an exception if used for a non-integer setting</p>
*
* @param value <p>The object value to get</p>
* @return <p>The value of the given object as an integer</p>
*/
public static int asInt(Object value) {
if (value instanceof String) {
return Integer.parseInt((String) value);
} else {
return (Integer) value;
}
}
}

View File

@ -0,0 +1,121 @@
package net.knarcraft.blacksmith.util;
import net.knarcraft.blacksmith.config.SettingValueType;
import java.util.ArrayList;
import java.util.List;
/**
* A helper class for getting valid values for tab-completion
*/
public final class TabCompleteValuesHelper {
private TabCompleteValuesHelper() {
}
/**
* Gets tab-completion values for the given value type
*
* @param valueType <p>The value type to get possible values for</p>
* @return <p>The values to show the user</p>
*/
public static List<String> getTabCompletions(SettingValueType valueType) {
return switch (valueType) {
case POSITIVE_INTEGER -> getPositiveIntegers();
case BOOLEAN -> getBooleans();
case POSITIVE_DOUBLE -> getPositiveDoubles();
case STRING -> getStrings();
case PERCENTAGE -> getPercentages();
case STRING_LIST -> getReforgeAbleMaterials();
};
}
/**
* Gets some example possible values for reforge-able materials
*
* @return <p>Some example possible values for reforge-able materials</p>
*/
private static List<String> getReforgeAbleMaterials() {
List<String> stringLists = new ArrayList<>();
stringLists.add("preset:sword-smith");
stringLists.add("preset:weapon-smith");
stringLists.add("preset:armor-smith");
stringLists.add("preset:tool-smith");
stringLists.add("preset:ranged-smith");
stringLists.add("bow,crossbow,elytra");
return stringLists;
}
/**
* Gets some example string values
*
* @return <p>Some example string values</p>
*/
private static List<String> getStrings() {
List<String> strings = new ArrayList<>(1);
strings.add("&aExample message. Use & for color tags.");
return strings;
}
/**
* Gets some example percentage values
*
* @return <p>Some example percentage values</p>
*/
private static List<String> getPercentages() {
List<String> percentages = new ArrayList<>(6);
percentages.add("0");
percentages.add("10");
percentages.add("25");
percentages.add("45");
percentages.add("75");
percentages.add("100");
return percentages;
}
/**
* Gets some possible positive doubles
*
* @return <p>Some possible positive doubles</p>
*/
private static List<String> getPositiveDoubles() {
List<String> positiveDoubles = new ArrayList<>(4);
positiveDoubles.add("0.0");
positiveDoubles.add("0.0001");
positiveDoubles.add("5.0");
positiveDoubles.add("7.34");
positiveDoubles.add("5674.34534");
return positiveDoubles;
}
/**
* Gets some example positive integers
*
* @return <p>Some example positive integers</p>
*/
private static List<String> getPositiveIntegers() {
List<String> positiveIntegers = new ArrayList<>(6);
positiveIntegers.add("0");
positiveIntegers.add("5");
positiveIntegers.add("10");
positiveIntegers.add("25");
positiveIntegers.add("50");
positiveIntegers.add("100");
positiveIntegers.add("4565");
return positiveIntegers;
}
/**
* Gets the possible boolean values
*
* @return <p>The possible boolean values</p>
*/
private static List<String> getBooleans() {
List<String> booleans = new ArrayList<>(2);
booleans.add("True");
booleans.add("False");
return booleans;
}
}

View File

@ -0,0 +1,125 @@
package net.knarcraft.blacksmith.util;
import net.knarcraft.blacksmith.config.SettingValueType;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
/**
* A helper class for validating a value's type
*/
public final class TypeValidationHelper {
private TypeValidationHelper() {
}
/**
* Gets whether the given value is valid for the given value type
*
* @param valueType <p>The value type required</p>
* @param value <p>The value given</p>
* @param sender <p>The command sender to use for printing error messages</p>
* @return <p>True if the value is valid</p>
*/
public static boolean isValid(SettingValueType valueType, Object value, CommandSender sender) {
try {
return switch (valueType) {
case POSITIVE_DOUBLE -> isPositiveDouble(value, sender);
case STRING -> isNonEmptyString(value, sender);
case POSITIVE_INTEGER -> isPositiveInteger(value, sender);
case PERCENTAGE -> isPercentage(value, sender);
case BOOLEAN -> true;
case STRING_LIST -> isStringList(value, sender);
};
} catch (ClassCastException exception) {
//This error signifies that an object is not a string, and of the wrong class
return false;
}
}
/**
* Checks whether the given value is a string list
*
* @param value <p>The value to check</p>
* @param sender <p>The command sender to use for printing error messages</p>
* @return <p>True if the value is a string list</p>
*/
private static boolean isStringList(Object value, CommandSender sender) {
boolean isStringList = value instanceof String[] || value instanceof String;
if (!isStringList && sender != null) {
sender.sendMessage(ChatColor.DARK_RED + "A string list is required!");
}
return isStringList;
}
/**
* Checks whether the given value is a valid percentage
*
* @param value <p>The value to check</p>
* @param sender <p>The command sender to use for printing error messages</p>
* @return <p>True if the value is a percentage</p>
*/
private static boolean isPercentage(Object value, CommandSender sender) {
try {
int intValue = ConfigHelper.asInt(value);
return intValue > 0 && intValue <= 100;
} catch (NumberFormatException | NullPointerException exception) {
if (sender != null) {
sender.sendMessage(ChatColor.DARK_RED + "You specified a value which isn't between 0 and 100!");
}
return false;
}
}
/**
* Checks whether the given value is a non-empty string
*
* @param value <p>The value to check</p>
* @param sender <p>The command sender to use for printing error messages</p>
* @return <p>True if the value is a non-empty string</p>
*/
private static boolean isNonEmptyString(Object value, CommandSender sender) {
boolean isString = value instanceof String string && !string.strip().isEmpty();
if (!isString && sender != null) {
sender.sendMessage(ChatColor.DARK_RED + "A non-empty string is required!");
}
return isString;
}
/**
* Checks whether the given value is a positive double
*
* @param value <p>The value to check</p>
* @param sender <p>The command sender to use for printing error messages</p>
* @return <p>True if the value is a positive double</p>
*/
private static boolean isPositiveDouble(Object value, CommandSender sender) {
try {
return ConfigHelper.asDouble(value) > 0.0;
} catch (NumberFormatException | NullPointerException exception) {
if (sender != null) {
sender.sendMessage(ChatColor.DARK_RED + "You specified a value which isn't a positive double!");
}
return false;
}
}
/**
* Checks whether the given value is a positive integer
*
* @param value <p>The value to check</p>
* @param sender <p>The command sender to use for printing error messages</p>
* @return <p>True if the value is a positive integer</p>
*/
private static boolean isPositiveInteger(Object value, CommandSender sender) {
try {
return ConfigHelper.asInt(value) > 0;
} catch (NumberFormatException | NullPointerException exception) {
if (sender != null) {
sender.sendMessage(ChatColor.DARK_RED + "You specified a value which isn't a positive integer!");
}
return false;
}
}
}