NotificationManager.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package com.gmail.nossr50.util.player;
  2. import com.gmail.nossr50.config.hocon.notifications.PlayerNotificationSettings;
  3. import com.gmail.nossr50.datatypes.interactions.NotificationType;
  4. import com.gmail.nossr50.datatypes.notifications.SensitiveCommandType;
  5. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  6. import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
  7. import com.gmail.nossr50.datatypes.skills.SubSkillType;
  8. import com.gmail.nossr50.events.skills.McMMOPlayerNotificationEvent;
  9. import com.gmail.nossr50.mcMMO;
  10. import com.gmail.nossr50.util.Permissions;
  11. import com.gmail.nossr50.util.sounds.SoundManager;
  12. import com.gmail.nossr50.util.sounds.SoundType;
  13. import net.md_5.bungee.api.ChatMessageType;
  14. import net.md_5.bungee.api.chat.TextComponent;
  15. import org.bukkit.Bukkit;
  16. import org.bukkit.ChatColor;
  17. import org.bukkit.Server;
  18. import org.bukkit.SoundCategory;
  19. import org.bukkit.command.CommandSender;
  20. import org.bukkit.entity.Player;
  21. import java.util.HashMap;
  22. /**
  23. * Handles all messages sent to the player from mcMMO
  24. */
  25. public class NotificationManager {
  26. private HashMap<NotificationType, PlayerNotificationSettings> playerNotificationHashMap;
  27. private final mcMMO pluginRef;
  28. public NotificationManager(mcMMO pluginRef) {
  29. this.pluginRef = pluginRef;
  30. playerNotificationHashMap = new HashMap<>();
  31. initMaps();
  32. }
  33. private void initMaps() {
  34. //Copy the map
  35. playerNotificationHashMap = new HashMap<>(pluginRef.getConfigManager().getConfigNotifications().getNotificationSettingHashMap());
  36. }
  37. public void setPlayerNotificationSettings(NotificationType notificationType, PlayerNotificationSettings playerNotificationSettings) {
  38. playerNotificationHashMap.put(notificationType, playerNotificationSettings);
  39. }
  40. /**
  41. * Grab the settings for a NotificationType
  42. * @param notificationType target notification type
  43. * @return the notification settings for this type
  44. */
  45. public PlayerNotificationSettings getPlayerNotificationSettings(NotificationType notificationType) {
  46. return playerNotificationHashMap.get(notificationType);
  47. }
  48. /**
  49. * Sends players notifications from mcMMO
  50. * Does so by sending out an event so other plugins can cancel it
  51. * @param player target player
  52. * @param notificationType notifications defined type
  53. * @param key the locale key for the notifications defined message
  54. */
  55. public void sendPlayerInformation(Player player, NotificationType notificationType, String key) {
  56. if (pluginRef.getUserManager().getPlayer(player) == null || !pluginRef.getUserManager().getPlayer(player).useChatNotifications())
  57. return;
  58. TextComponent textComponent = pluginRef.getTextComponentFactory().getNotificationTextComponentFromLocale(key);
  59. McMMOPlayerNotificationEvent customEvent = pluginRef.getEventManager().createAndCallNotificationEvent(player, notificationType, textComponent);
  60. sendNotification(customEvent);
  61. }
  62. /**
  63. * Builds a text component with one or more parameters
  64. * @param key locale key
  65. * @param values parameters
  66. * @return TextComponent for this message
  67. */
  68. public TextComponent buildTextComponent(String key, String... values) {
  69. return pluginRef.getTextComponentFactory().getNotificationMultipleValues(key, values);
  70. }
  71. /**
  72. * Builds a text component without any parameters
  73. * @param key locale key
  74. * @return TextComponent for this message
  75. */
  76. public TextComponent buildTextComponent(String key) {
  77. return pluginRef.getTextComponentFactory().getNotificationTextComponentFromLocale(key);
  78. }
  79. /**
  80. * Sends players notifications from mcMMO
  81. * This does this by sending out an event so other plugins can cancel it
  82. * This event in particular is provided with a source player, and players near the source player are sent the information
  83. * @param targetPlayer the recipient player for this message
  84. * @param notificationType type of notification
  85. * @param key LocaleManager Key for the string to use with this event
  86. * @param values values to be injected into the locale string
  87. */
  88. public void sendNearbyPlayersInformation(Player targetPlayer, NotificationType notificationType, String key, String... values)
  89. {
  90. sendPlayerInformation(targetPlayer, notificationType, key, values);
  91. }
  92. public void sendPlayerInformationChatOnly(Player player, String key, String... values)
  93. {
  94. if(pluginRef.getUserManager().getPlayer(player) == null || !pluginRef.getUserManager().getPlayer(player).useChatNotifications())
  95. return;
  96. String preColoredString = pluginRef.getLocaleManager().getString(key, (Object[]) values);
  97. player.sendMessage(preColoredString);
  98. }
  99. public void sendPlayerInformation(Player player, NotificationType notificationType, String key, String... values)
  100. {
  101. if(pluginRef.getUserManager().getPlayer(player) == null || !pluginRef.getUserManager().getPlayer(player).useChatNotifications())
  102. return;
  103. TextComponent textComponent = buildTextComponent(key, values);
  104. McMMOPlayerNotificationEvent customEvent = pluginRef.getEventManager().createAndCallNotificationEvent(player, notificationType, textComponent);
  105. sendNotification(customEvent);
  106. }
  107. /**
  108. * Handles sending level up notifications to a mcMMOPlayer
  109. *
  110. * @param mcMMOPlayer target mcMMOPlayer
  111. * @param skillName skill that leveled up
  112. * @param newLevel new level of that skill
  113. */
  114. public void sendPlayerLevelUpNotification(McMMOPlayer mcMMOPlayer, PrimarySkillType skillName, int levelsGained, int newLevel) {
  115. if (!mcMMOPlayer.useChatNotifications())
  116. return;
  117. TextComponent levelUpTextComponent = pluginRef.getTextComponentFactory().getNotificationLevelUpTextComponent(skillName, levelsGained, newLevel);
  118. McMMOPlayerNotificationEvent customEvent = pluginRef.getEventManager().createAndCallNotificationEvent(mcMMOPlayer.getPlayer(), NotificationType.LEVEL_UP_MESSAGE, levelUpTextComponent);
  119. sendNotification(customEvent);
  120. }
  121. private void sendNotification(McMMOPlayerNotificationEvent customEvent) {
  122. if (customEvent.isCancelled())
  123. return;
  124. Player player = customEvent.getRecipient();
  125. PlayerNotificationSettings playerNotificationSettings = customEvent.getPlayerNotificationSettings();
  126. //Text Component found
  127. if(customEvent.hasTextComponent()) {
  128. if(playerNotificationSettings.isSendToActionBar()) {
  129. player.spigot().sendMessage(ChatMessageType.ACTION_BAR, customEvent.getNotificationTextComponent());
  130. }
  131. //Chat (System)
  132. if(playerNotificationSettings.isSendToChat()) {
  133. if(customEvent.hasTextComponent()) {
  134. player.spigot().sendMessage(ChatMessageType.SYSTEM, customEvent.getNotificationTextComponent());
  135. }
  136. }
  137. } else {
  138. //Chat but without a text component
  139. if(playerNotificationSettings.isSendToChat()) {
  140. player.sendMessage(customEvent.getNotificationText());
  141. }
  142. }
  143. }
  144. public void broadcastTitle(Server server, String title, String subtitle, int i1, int i2, int i3) {
  145. for (Player player : server.getOnlinePlayers()) {
  146. player.sendTitle(title, subtitle, i1, i2, i3);
  147. }
  148. }
  149. public void sendPlayerUnlockNotification(McMMOPlayer mcMMOPlayer, SubSkillType subSkillType) {
  150. if (!mcMMOPlayer.useChatNotifications())
  151. return;
  152. //CHAT MESSAGE
  153. mcMMOPlayer.getPlayer().spigot().sendMessage(pluginRef.getTextComponentFactory().getSubSkillUnlockedNotificationComponents(mcMMOPlayer.getPlayer(), subSkillType));
  154. //Unlock Sound Effect
  155. SoundManager.sendCategorizedSound(mcMMOPlayer.getPlayer(), mcMMOPlayer.getPlayer().getLocation(), SoundType.SKILL_UNLOCKED, SoundCategory.MASTER);
  156. }
  157. /**
  158. * Convenience method to report info about a command sender using a sensitive command
  159. *
  160. * @param commandSender the command user
  161. * @param sensitiveCommandType type of command issued
  162. */
  163. public void processSensitiveCommandNotification(CommandSender commandSender, SensitiveCommandType sensitiveCommandType, String... args) {
  164. /*
  165. * Determine the 'identity' of the one who executed the command to pass as a parameters
  166. */
  167. String senderName = pluginRef.getLocaleManager().getString("Server.ConsoleName");
  168. if (commandSender instanceof Player) {
  169. senderName = ((Player) commandSender).getDisplayName() + ChatColor.RESET + "-" + ((Player) commandSender).getUniqueId();
  170. }
  171. //Send the notification
  172. switch (sensitiveCommandType) {
  173. case XPRATE_MODIFY:
  174. sendAdminNotification(pluginRef.getLocaleManager().getString("Notifications.Admin.XPRate.Start.Others", addItemToFirstPositionOfArray(senderName, args)));
  175. sendAdminCommandConfirmation(commandSender, pluginRef.getLocaleManager().getString("Notifications.Admin.XPRate.Start.Self", args));
  176. break;
  177. case XPRATE_END:
  178. sendAdminNotification(pluginRef.getLocaleManager().getString("Notifications.Admin.XPRate.End.Others", addItemToFirstPositionOfArray(senderName, args)));
  179. sendAdminCommandConfirmation(commandSender, pluginRef.getLocaleManager().getString("Notifications.Admin.XPRate.End.Self", args));
  180. break;
  181. }
  182. }
  183. /**
  184. * Sends a message to all admins with the admin notification formatting from the locale
  185. * Admins are currently players with either Operator status or Admin Chat permission
  186. *
  187. * @param msg message fetched from locale
  188. */
  189. private void sendAdminNotification(String msg) {
  190. //If its not enabled exit
  191. if (!pluginRef.getConfigManager().getConfigAdmin().isSendAdminNotifications())
  192. return;
  193. for (Player player : Bukkit.getServer().getOnlinePlayers()) {
  194. if (player.isOp() || Permissions.adminChat(player)) {
  195. player.sendMessage(pluginRef.getLocaleManager().getString("Notifications.Admin.Format.Others", msg));
  196. }
  197. }
  198. //Copy it out to Console too
  199. pluginRef.getLogger().info(pluginRef.getLocaleManager().getString("Notifications.Admin.Format.Others", msg));
  200. }
  201. /**
  202. * Sends a confirmation message to the CommandSender who just executed an admin command
  203. *
  204. * @param commandSender target command sender
  205. * @param msg message fetched from locale
  206. */
  207. private void sendAdminCommandConfirmation(CommandSender commandSender, String msg) {
  208. commandSender.sendMessage(pluginRef.getLocaleManager().getString("Notifications.Admin.Format.Self", msg));
  209. }
  210. /**
  211. * Takes an array and an object, makes a new array with object in the first position of the new array,
  212. * and the following elements in this new array being a copy of the existing array retaining their order
  213. *
  214. * @param itemToAdd the string to put at the beginning of the new array
  215. * @param existingArray the existing array to be copied to the new array at position [0]+1 relative to their original index
  216. * @return the new array combining itemToAdd at the start and existing array elements following while retaining their order
  217. */
  218. public String[] addItemToFirstPositionOfArray(String itemToAdd, String... existingArray) {
  219. String[] newArray = new String[existingArray.length + 1];
  220. newArray[0] = itemToAdd;
  221. System.arraycopy(existingArray, 0, newArray, 1, existingArray.length);
  222. return newArray;
  223. }
  224. public boolean doesPlayerUseNotifications(Player player) {
  225. if (pluginRef.getUserManager().getPlayer(player) == null)
  226. return false;
  227. else
  228. return pluginRef.getUserManager().getPlayer(player).useChatNotifications();
  229. }
  230. }