NotificationManager.java 11 KB

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