NotificationManager.java 12 KB

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