NotificationManager.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.player.McMMOPlayer;
  5. import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
  6. import com.gmail.nossr50.datatypes.skills.SubSkillType;
  7. import com.gmail.nossr50.events.skills.McMMOPlayerNotificationEvent;
  8. import com.gmail.nossr50.util.Misc;
  9. import com.gmail.nossr50.util.TextComponentFactory;
  10. import com.gmail.nossr50.util.sounds.SoundManager;
  11. import com.gmail.nossr50.util.sounds.SoundType;
  12. import net.md_5.bungee.api.ChatMessageType;
  13. import net.md_5.bungee.api.chat.TextComponent;
  14. import org.bukkit.Bukkit;
  15. import org.bukkit.Location;
  16. import org.bukkit.Server;
  17. import org.bukkit.SoundCategory;
  18. import org.bukkit.entity.Player;
  19. public class NotificationManager {
  20. /**
  21. * Sends players notifications from mcMMO
  22. * This does this by sending out an event so other plugins can cancel it
  23. * @param player target player
  24. * @param notificationType notifications defined type
  25. * @param key the locale key for the notifications defined message
  26. */
  27. public static void sendPlayerInformation(Player player, NotificationType notificationType, String key)
  28. {
  29. ChatMessageType destination = AdvancedConfig.getInstance().doesNotificationUseActionBar(notificationType) ? ChatMessageType.ACTION_BAR : ChatMessageType.SYSTEM;
  30. TextComponent message = TextComponentFactory.getNotificationTextComponentFromLocale(key, notificationType);
  31. McMMOPlayerNotificationEvent customEvent = checkNotificationEvent(player, notificationType, destination, message);
  32. sendNotification(player, customEvent);
  33. }
  34. /**
  35. * Sends players notifications from mcMMO
  36. * This does this by sending out an event so other plugins can cancel it
  37. * This event in particular is provided with a source player, and players near the source player are sent the information
  38. * @param source the source player for this event
  39. * @param notificationType type of notification
  40. * @param key Locale Key for the string to use with this event
  41. * @param values values to be injected into the locale string
  42. */
  43. public static void sendNearbyPlayersInformation(Player source, NotificationType notificationType, String key, String... values)
  44. {
  45. Location location = source.getLocation();
  46. for (Player otherPlayer : source.getWorld().getPlayers()) {
  47. if (otherPlayer != source && Misc.isNear(location, otherPlayer.getLocation(), Misc.SKILL_MESSAGE_MAX_SENDING_DISTANCE)) {
  48. sendPlayerInformation(otherPlayer, notificationType, key, values);
  49. }
  50. }
  51. }
  52. public static void sendPlayerInformation(Player player, NotificationType notificationType, String key, String... values)
  53. {
  54. ChatMessageType destination = AdvancedConfig.getInstance().doesNotificationUseActionBar(notificationType) ? ChatMessageType.ACTION_BAR : ChatMessageType.SYSTEM;
  55. TextComponent message = TextComponentFactory.getNotificationMultipleValues(key, notificationType, values);
  56. McMMOPlayerNotificationEvent customEvent = checkNotificationEvent(player, notificationType, destination, message);
  57. sendNotification(player, customEvent);
  58. }
  59. private static void sendNotification(Player player, McMMOPlayerNotificationEvent customEvent) {
  60. if (customEvent.isCancelled())
  61. return;
  62. //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
  63. if(customEvent.getChatMessageType() == ChatMessageType.ACTION_BAR)
  64. {
  65. player.spigot().sendMessage(customEvent.getChatMessageType(), customEvent.getNotificationTextComponent());
  66. if(customEvent.isMessageAlsoBeingSentToChat())
  67. {
  68. //Send copy to chat system
  69. player.spigot().sendMessage(ChatMessageType.SYSTEM, customEvent.getNotificationTextComponent());
  70. }
  71. } else {
  72. player.spigot().sendMessage(customEvent.getChatMessageType(), customEvent.getNotificationTextComponent());
  73. }
  74. }
  75. private static McMMOPlayerNotificationEvent checkNotificationEvent(Player player, NotificationType notificationType, ChatMessageType destination, TextComponent message) {
  76. //Init event
  77. McMMOPlayerNotificationEvent customEvent = new McMMOPlayerNotificationEvent(player,
  78. notificationType, message, destination, AdvancedConfig.getInstance().doesNotificationSendCopyToChat(notificationType));
  79. //Call event
  80. Bukkit.getServer().getPluginManager().callEvent(customEvent);
  81. return customEvent;
  82. }
  83. /**
  84. * Handles sending level up notifications to a mcMMOPlayer
  85. * @param mcMMOPlayer target mcMMOPlayer
  86. * @param skillName skill that leveled up
  87. * @param newLevel new level of that skill
  88. */
  89. public static void sendPlayerLevelUpNotification(McMMOPlayer mcMMOPlayer, PrimarySkillType skillName, int newLevel)
  90. {
  91. ChatMessageType destination = AdvancedConfig.getInstance().doesNotificationUseActionBar(NotificationType.LEVEL_UP_MESSAGE) ? ChatMessageType.ACTION_BAR : ChatMessageType.SYSTEM;
  92. TextComponent levelUpTextComponent = TextComponentFactory.getNotificationLevelUpTextComponent(mcMMOPlayer, skillName, newLevel);
  93. McMMOPlayerNotificationEvent customEvent = checkNotificationEvent(mcMMOPlayer.getPlayer(), NotificationType.LEVEL_UP_MESSAGE, destination, levelUpTextComponent);
  94. sendNotification(mcMMOPlayer.getPlayer(), customEvent);
  95. }
  96. public static void broadcastTitle(Server server, String title, String subtitle, int i1, int i2, int i3)
  97. {
  98. for(Player player : server.getOnlinePlayers())
  99. {
  100. player.sendTitle(title, subtitle, i1, i2, i3);
  101. }
  102. }
  103. public static void sendPlayerUnlockNotification(McMMOPlayer mcMMOPlayer, SubSkillType subSkillType)
  104. {
  105. //CHAT MESSAGE
  106. mcMMOPlayer.getPlayer().spigot().sendMessage(TextComponentFactory.getSubSkillUnlockedNotificationComponents(mcMMOPlayer.getPlayer(), subSkillType));
  107. //Unlock Sound Effect
  108. SoundManager.sendCategorizedSound(mcMMOPlayer.getPlayer(), mcMMOPlayer.getPlayer().getLocation(), SoundType.SKILL_UNLOCKED, SoundCategory.MASTER);
  109. //ACTION BAR MESSAGE
  110. /*if(AdvancedConfig.getInstance().doesNotificationUseActionBar(NotificationType.SUBSKILL_UNLOCKED))
  111. mcMMOPlayer.getPlayer().spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(LocaleLoader.getString("JSON.SkillUnlockMessage",
  112. subSkillType.getLocaleName(),
  113. String.valueOf(RankUtils.getRank(mcMMOPlayer.getPlayer(),
  114. subSkillType)))));*/
  115. }
  116. }