Adds possibility for message customization and translation #5
This commit is contained in:
32
src/main/java/net/knarcraft/blacksmith/util/FileHelper.java
Normal file
32
src/main/java/net/knarcraft/blacksmith/util/FileHelper.java
Normal file
@ -0,0 +1,32 @@
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
@ -1,72 +0,0 @@
|
||||
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));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
}
|
@ -1,9 +1,11 @@
|
||||
package net.knarcraft.blacksmith.util;
|
||||
|
||||
import net.knarcraft.blacksmith.config.SettingValueType;
|
||||
import net.knarcraft.blacksmith.formatting.TranslatableMessage;
|
||||
import net.knarcraft.blacksmith.formatting.Translator;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import static net.knarcraft.blacksmith.util.MessageFormatter.displayErrorMessage;
|
||||
import static net.knarcraft.blacksmith.formatting.StringFormatter.displayErrorMessage;
|
||||
|
||||
/**
|
||||
* A helper class for validating a value's type
|
||||
@ -49,7 +51,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) {
|
||||
displayErrorMessage(sender, "A string list is required!");
|
||||
displayErrorMessage(sender, Translator.getTranslatedMessage(TranslatableMessage.INPUT_STRING_LIST_REQUIRED));
|
||||
}
|
||||
return isStringList;
|
||||
}
|
||||
@ -67,7 +69,7 @@ public final class TypeValidationHelper {
|
||||
return intValue > 0 && intValue <= 100;
|
||||
} catch (NumberFormatException | NullPointerException exception) {
|
||||
if (sender != null) {
|
||||
displayErrorMessage(sender, "You specified a value which isn't between 0 and 100!");
|
||||
displayErrorMessage(sender, Translator.getTranslatedMessage(TranslatableMessage.INPUT_PERCENTAGE_REQUIRED));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -83,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, "A non-empty string is required!");
|
||||
displayErrorMessage(sender, Translator.getTranslatedMessage(TranslatableMessage.INPUT_STRING_REQUIRED));
|
||||
}
|
||||
return isString;
|
||||
}
|
||||
@ -100,7 +102,8 @@ public final class TypeValidationHelper {
|
||||
return ConfigHelper.asDouble(value) > 0.0;
|
||||
} catch (NumberFormatException | NullPointerException exception) {
|
||||
if (sender != null) {
|
||||
displayErrorMessage(sender, "You specified a value which isn't a positive double!");
|
||||
displayErrorMessage(sender,
|
||||
Translator.getTranslatedMessage(TranslatableMessage.INPUT_POSITIVE_DOUBLE_REQUIRED));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -118,7 +121,8 @@ public final class TypeValidationHelper {
|
||||
return ConfigHelper.asInt(value) > 0;
|
||||
} catch (NumberFormatException | NullPointerException exception) {
|
||||
if (sender != null) {
|
||||
displayErrorMessage(sender, "You specified a value which isn't a positive integer!");
|
||||
displayErrorMessage(sender,
|
||||
Translator.getTranslatedMessage(TranslatableMessage.INPUT_POSITIVE_INTEGER_REQUIRED));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
Reference in New Issue
Block a user