73 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			73 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
| 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);
 | |
|     }
 | |
| 
 | |
| }
 |