Adds tons of changes to messages

This commit is contained in:
2022-09-29 01:49:12 +02:00
parent 3cfa7a2a0a
commit a6e9163dbd
18 changed files with 458 additions and 95 deletions

View File

@ -0,0 +1,36 @@
package net.knarcraft.blacksmith.util;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment;
/**
* A helper class for parsing input into proper object types
*/
public final class InputParsingHelper {
private InputParsingHelper() {
}
/**
* Tries to find the material matching the given input string
*
* @param input <p>The string to match to a material</p>
* @return <p>The material matching the string, or null if not found</p>
*/
public static Material matchMaterial(String input) {
return Material.matchMaterial(input.replace("-", "_"));
}
/**
* Tries to find the enchantment matching the given input string
*
* @param input <p>The string to match to an enchantment</p>
* @return <p>The enchantment matching the string, or null if not found</p>
*/
public static Enchantment matchEnchantment(String input) {
return Enchantment.getByKey(NamespacedKey.minecraft(input.replace("-", "_")));
}
}

View File

@ -0,0 +1,83 @@
package net.knarcraft.blacksmith.util;
import net.citizensnpcs.api.npc.NPC;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
/**
* A message formatter to ensure uniform colors and format of chat messages
*/
public final class MessageFormatter {
private final static String pluginName = BlacksmithPlugin.getInstance().getDescription().getName();
private MessageFormatter() {
}
/**
* Sends a message from a blacksmith NPC to a player
*
* @param npc <p>The NPC sending the message</p>
* @param player <p>The player to send the message to</p>
* @param message <p>The message to send</p>
*/
public static void sendNPCMessage(NPC npc, Player player, String message) {
player.sendMessage(ChatColor.GREEN + "[" + npc.getName() + "] -> You:" + ChatColor.RESET + " " +
translateColors(message));
}
/**
* Displays a message signifying a successful action
*
* @param sender <p>The command sender to display the message to</p>
* @param message <p>The raw message to display</p>
*/
public static void displaySuccessMessage(CommandSender sender, String message) {
sender.sendMessage(ChatColor.GREEN + getFormattedMessage(message));
}
/**
* Displays a message signifying an unsuccessful action
*
* @param sender <p>The command sender to display the message to</p>
* @param message <p>The raw message to display</p>
*/
public static void displayErrorMessage(CommandSender sender, String message) {
sender.sendMessage(ChatColor.DARK_RED + getFormattedMessage(message));
}
/**
* Escapes color codes to prevent them from being shown
*
* @param input <p>The input string to escape color codes for</p>
* @return <p>The input string with color codes escaped</p>
*/
public static String escapeColorCodes(String input) {
//TODO: Find a working way of escaping color codes
return translateColors(input).replace(String.valueOf(ChatColor.COLOR_CHAR), "&&&");
}
/**
* Gets the formatted version of any chat message
*
* @param message <p>The message to format</p>
* @return <p>The formatted message</p>
*/
private static String getFormattedMessage(String message) {
return "[" + pluginName + "] " + ChatColor.RESET + translateColors(message);
}
/**
* Translates & color codes to proper colors
*
* @param input <p>The input string to translate colors for</p>
* @return <p>The input with color codes translated</p>
*/
private static String translateColors(String input) {
return ChatColor.translateAlternateColorCodes('&', input);
}
}

View File

@ -1,6 +1,11 @@
package net.knarcraft.blacksmith.util;
import net.knarcraft.blacksmith.config.SettingValueType;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.enchantments.EnchantmentTarget;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import java.util.ArrayList;
import java.util.List;
@ -28,9 +33,40 @@ public final class TabCompleteValuesHelper {
case STRING -> getStrings();
case PERCENTAGE -> getPercentages();
case STRING_LIST -> getReforgeAbleMaterials();
case MATERIAL -> getAllReforgeAbleMaterials();
case ENCHANTMENT -> getAllEnchantments();
};
}
/**
* Gets a complete list of all reforge-able materials
*
* @return <p>A complete list of reforge-able materials</p>
*/
private static List<String> getAllReforgeAbleMaterials() {
List<String> reforgeAbleMaterials = new ArrayList<>();
for (Material material : Material.values()) {
ItemStack item = new ItemStack(material);
if (item.getItemMeta() instanceof Damageable && EnchantmentTarget.BREAKABLE.includes(item)) {
reforgeAbleMaterials.add(material.name());
}
}
return reforgeAbleMaterials;
}
/**
* Gets a complete list of enchantments
*
* @return <p>A complete list of enchantments</p>
*/
private static List<String> getAllEnchantments() {
List<String> enchantments = new ArrayList<>();
for (Enchantment enchantment : Enchantment.values()) {
enchantments.add(enchantment.toString());
}
return enchantments;
}
/**
* Gets some example possible values for reforge-able materials
*

View File

@ -4,7 +4,7 @@ import java.util.ArrayList;
import java.util.List;
/**
* A helper class for tab complation
* A helper class for tab-completion
*/
public final class TabCompletionHelper {

View File

@ -1,9 +1,10 @@
package net.knarcraft.blacksmith.util;
import net.knarcraft.blacksmith.config.SettingValueType;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import static net.knarcraft.blacksmith.util.MessageFormatter.displayErrorMessage;
/**
* A helper class for validating a value's type
*/
@ -30,6 +31,7 @@ public final class TypeValidationHelper {
case PERCENTAGE -> isPercentage(value, sender);
case BOOLEAN -> true;
case STRING_LIST -> isStringList(value, sender);
case MATERIAL, ENCHANTMENT -> false;
};
} catch (ClassCastException exception) {
//This error signifies that an object is not a string, and of the wrong class
@ -47,7 +49,7 @@ public final class TypeValidationHelper {
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!");
displayErrorMessage(sender, "A string list is required!");
}
return isStringList;
}
@ -65,7 +67,7 @@ public final class TypeValidationHelper {
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!");
displayErrorMessage(sender, "You specified a value which isn't between 0 and 100!");
}
return false;
}
@ -81,7 +83,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) {
sender.sendMessage(ChatColor.DARK_RED + "A non-empty string is required!");
displayErrorMessage(sender, "A non-empty string is required!");
}
return isString;
}
@ -98,7 +100,7 @@ public final class TypeValidationHelper {
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!");
displayErrorMessage(sender, "You specified a value which isn't a positive double!");
}
return false;
}
@ -116,7 +118,7 @@ public final class TypeValidationHelper {
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!");
displayErrorMessage(sender, "You specified a value which isn't a positive integer!");
}
return false;
}