From f0cce29d71068d9f22b51c3f4b804d9c0c948a90 Mon Sep 17 00:00:00 2001 From: nossr50 Date: Thu, 13 Jun 2019 12:43:51 -0700 Subject: [PATCH] Rewrite NotificationManager to handle more flexible config options --- Changelog.txt | 1 + .../gmail/nossr50/config/ConfigManager.java | 4 +- .../notifications/ConfigNotifications.java | 4 +- .../ConfigPlayerNotifications.java | 38 ++-- ...n.java => PlayerNotificationSettings.java} | 4 +- .../PlayerNotificationSerializer.java | 12 +- .../skills/McMMOPlayerNotificationEvent.java | 49 ++++- .../com/gmail/nossr50/util/EventUtils.java | 35 ++++ .../util/player/NotificationManager.java | 189 ++++++++++-------- 9 files changed, 210 insertions(+), 126 deletions(-) rename src/main/java/com/gmail/nossr50/config/hocon/notifications/{PlayerNotification.java => PlayerNotificationSettings.java} (86%) diff --git a/Changelog.txt b/Changelog.txt index d4b07cadf..defe28171 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -153,6 +153,7 @@ Version 2.2.0 Config_Update_Overwrite, Tool_Mods_Enabled, Armor_Mods_Enabled, Block_Mods_Enabled, Entity_Mods_Enabled, ExperienceConversionMultiplier API Changes + NotificationManager is now provided via an instance in mcMMOs main class Restructured McMMOPlayerNotificationEvent to accommodate for new changes in NotificationManager Now Notification events are no longer assumed to be sending to the action bar with an option to have a copy sent to chat Notifications are now sent to either chat, actionbar, both or neither. The PlayerNotification type holds information on this. You can modify the event to change this how you wish if you hook into mcMMO. diff --git a/src/main/java/com/gmail/nossr50/config/ConfigManager.java b/src/main/java/com/gmail/nossr50/config/ConfigManager.java index 4a728ea2d..ca030776e 100644 --- a/src/main/java/com/gmail/nossr50/config/ConfigManager.java +++ b/src/main/java/com/gmail/nossr50/config/ConfigManager.java @@ -16,7 +16,7 @@ import com.gmail.nossr50.config.hocon.metrics.ConfigMetrics; import com.gmail.nossr50.config.hocon.mobs.ConfigMobs; import com.gmail.nossr50.config.hocon.motd.ConfigMOTD; import com.gmail.nossr50.config.hocon.notifications.ConfigNotifications; -import com.gmail.nossr50.config.hocon.notifications.PlayerNotification; +import com.gmail.nossr50.config.hocon.notifications.PlayerNotificationSettings; import com.gmail.nossr50.config.hocon.particles.ConfigParticles; import com.gmail.nossr50.config.hocon.party.ConfigParty; import com.gmail.nossr50.config.hocon.party.data.ConfigPartyData; @@ -273,7 +273,7 @@ public final class ConfigManager { customSerializers.registerType(TypeToken.of(SkillCeiling.class), new SkillCeilingSerializer()); customSerializers.registerType(TypeToken.of(SkillRankProperty.class), new SkillRankPropertySerializer()); customSerializers.registerType(TypeToken.of(MaxBonusLevel.class), new MaxBonusLevelSerializer()); - customSerializers.registerType(TypeToken.of(PlayerNotification.class), new PlayerNotificationSerializer()); + customSerializers.registerType(TypeToken.of(PlayerNotificationSettings.class), new PlayerNotificationSerializer()); } /** diff --git a/src/main/java/com/gmail/nossr50/config/hocon/notifications/ConfigNotifications.java b/src/main/java/com/gmail/nossr50/config/hocon/notifications/ConfigNotifications.java index abce3a9bf..6404a9528 100644 --- a/src/main/java/com/gmail/nossr50/config/hocon/notifications/ConfigNotifications.java +++ b/src/main/java/com/gmail/nossr50/config/hocon/notifications/ConfigNotifications.java @@ -49,11 +49,11 @@ public class ConfigNotifications { return configNotificationGeneral; } - public HashMap getNotificationSettingHashMap() { + public HashMap getNotificationSettingHashMap() { return playerNotifications.getNotificationSettingHashMap(); } - public PlayerNotification getPlayerNotification(NotificationType notificationType) { + public PlayerNotificationSettings getPlayerNotification(NotificationType notificationType) { return playerNotifications.getPlayerNotification(notificationType); } } diff --git a/src/main/java/com/gmail/nossr50/config/hocon/notifications/ConfigPlayerNotifications.java b/src/main/java/com/gmail/nossr50/config/hocon/notifications/ConfigPlayerNotifications.java index f7a17e6fb..9553bbc1c 100644 --- a/src/main/java/com/gmail/nossr50/config/hocon/notifications/ConfigPlayerNotifications.java +++ b/src/main/java/com/gmail/nossr50/config/hocon/notifications/ConfigPlayerNotifications.java @@ -9,36 +9,36 @@ import java.util.HashMap; @ConfigSerializable public class ConfigPlayerNotifications { - private final static HashMap NOTIFICATION_MAP_DEFAULT; + private final static HashMap NOTIFICATION_MAP_DEFAULT; static { NOTIFICATION_MAP_DEFAULT = new HashMap<>(); - NOTIFICATION_MAP_DEFAULT.put(NotificationType.ABILITY_OFF, new PlayerNotification(true, false, true)); - NOTIFICATION_MAP_DEFAULT.put(NotificationType.HARDCORE_MODE, new PlayerNotification(true, true, true)); - NOTIFICATION_MAP_DEFAULT.put(NotificationType.REQUIREMENTS_NOT_MET, new PlayerNotification(true, false, true)); - NOTIFICATION_MAP_DEFAULT.put(NotificationType.ABILITY_COOLDOWN, new PlayerNotification(true, false, true)); - NOTIFICATION_MAP_DEFAULT.put(NotificationType.LEVEL_UP_MESSAGE, new PlayerNotification(true, true, true)); - NOTIFICATION_MAP_DEFAULT.put(NotificationType.HOLIDAY, new PlayerNotification(true, true, true)); - NOTIFICATION_MAP_DEFAULT.put(NotificationType.TOOL, new PlayerNotification(true, false, true)); - NOTIFICATION_MAP_DEFAULT.put(NotificationType.SUBSKILL_MESSAGE, new PlayerNotification(true, false, true)); - NOTIFICATION_MAP_DEFAULT.put(NotificationType.SUBSKILL_MESSAGE_FAILED, new PlayerNotification(true, true, false)); - NOTIFICATION_MAP_DEFAULT.put(NotificationType.SUBSKILL_UNLOCKED, new PlayerNotification(true, true, true)); - NOTIFICATION_MAP_DEFAULT.put(NotificationType.SUPER_ABILITY, new PlayerNotification(true, false, true)); - NOTIFICATION_MAP_DEFAULT.put(NotificationType.SUPER_ABILITY_ALERT_OTHERS, new PlayerNotification(true, true, false)); - NOTIFICATION_MAP_DEFAULT.put(NotificationType.ITEM_MESSAGE, new PlayerNotification(true, false, true)); - NOTIFICATION_MAP_DEFAULT.put(NotificationType.NO_PERMISSION, new PlayerNotification(true, true, false)); - NOTIFICATION_MAP_DEFAULT.put(NotificationType.PARTY_MESSAGE, new PlayerNotification(true, true, false)); + NOTIFICATION_MAP_DEFAULT.put(NotificationType.ABILITY_OFF, new PlayerNotificationSettings(true, false, true)); + NOTIFICATION_MAP_DEFAULT.put(NotificationType.HARDCORE_MODE, new PlayerNotificationSettings(true, true, true)); + NOTIFICATION_MAP_DEFAULT.put(NotificationType.REQUIREMENTS_NOT_MET, new PlayerNotificationSettings(true, false, true)); + NOTIFICATION_MAP_DEFAULT.put(NotificationType.ABILITY_COOLDOWN, new PlayerNotificationSettings(true, false, true)); + NOTIFICATION_MAP_DEFAULT.put(NotificationType.LEVEL_UP_MESSAGE, new PlayerNotificationSettings(true, true, true)); + NOTIFICATION_MAP_DEFAULT.put(NotificationType.HOLIDAY, new PlayerNotificationSettings(true, true, true)); + NOTIFICATION_MAP_DEFAULT.put(NotificationType.TOOL, new PlayerNotificationSettings(true, false, true)); + NOTIFICATION_MAP_DEFAULT.put(NotificationType.SUBSKILL_MESSAGE, new PlayerNotificationSettings(true, false, true)); + NOTIFICATION_MAP_DEFAULT.put(NotificationType.SUBSKILL_MESSAGE_FAILED, new PlayerNotificationSettings(true, true, false)); + NOTIFICATION_MAP_DEFAULT.put(NotificationType.SUBSKILL_UNLOCKED, new PlayerNotificationSettings(true, true, true)); + NOTIFICATION_MAP_DEFAULT.put(NotificationType.SUPER_ABILITY, new PlayerNotificationSettings(true, false, true)); + NOTIFICATION_MAP_DEFAULT.put(NotificationType.SUPER_ABILITY_ALERT_OTHERS, new PlayerNotificationSettings(true, true, false)); + NOTIFICATION_MAP_DEFAULT.put(NotificationType.ITEM_MESSAGE, new PlayerNotificationSettings(true, false, true)); + NOTIFICATION_MAP_DEFAULT.put(NotificationType.NO_PERMISSION, new PlayerNotificationSettings(true, true, false)); + NOTIFICATION_MAP_DEFAULT.put(NotificationType.PARTY_MESSAGE, new PlayerNotificationSettings(true, true, false)); } @Setting(value = "Notification-Settings") - private HashMap notificationSettingHashMap = NOTIFICATION_MAP_DEFAULT; + private HashMap notificationSettingHashMap = NOTIFICATION_MAP_DEFAULT; - public HashMap getNotificationSettingHashMap() { + public HashMap getNotificationSettingHashMap() { return notificationSettingHashMap; } - public PlayerNotification getPlayerNotification(NotificationType notificationType) { + public PlayerNotificationSettings getPlayerNotification(NotificationType notificationType) { return notificationSettingHashMap.get(notificationType); } } \ No newline at end of file diff --git a/src/main/java/com/gmail/nossr50/config/hocon/notifications/PlayerNotification.java b/src/main/java/com/gmail/nossr50/config/hocon/notifications/PlayerNotificationSettings.java similarity index 86% rename from src/main/java/com/gmail/nossr50/config/hocon/notifications/PlayerNotification.java rename to src/main/java/com/gmail/nossr50/config/hocon/notifications/PlayerNotificationSettings.java index b195f505a..02d1522d2 100644 --- a/src/main/java/com/gmail/nossr50/config/hocon/notifications/PlayerNotification.java +++ b/src/main/java/com/gmail/nossr50/config/hocon/notifications/PlayerNotificationSettings.java @@ -1,12 +1,12 @@ package com.gmail.nossr50.config.hocon.notifications; -public class PlayerNotification { +public class PlayerNotificationSettings { private boolean enabled; private boolean sendToChat; private boolean sendToActionBar; - public PlayerNotification(boolean enabled, boolean sendToChat, boolean sendToActionBar) { + public PlayerNotificationSettings(boolean enabled, boolean sendToChat, boolean sendToActionBar) { this.enabled = enabled; this.sendToChat = sendToChat; this.sendToActionBar = sendToActionBar; diff --git a/src/main/java/com/gmail/nossr50/config/hocon/serializers/PlayerNotificationSerializer.java b/src/main/java/com/gmail/nossr50/config/hocon/serializers/PlayerNotificationSerializer.java index 264862567..db6aaba6f 100644 --- a/src/main/java/com/gmail/nossr50/config/hocon/serializers/PlayerNotificationSerializer.java +++ b/src/main/java/com/gmail/nossr50/config/hocon/serializers/PlayerNotificationSerializer.java @@ -1,6 +1,6 @@ package com.gmail.nossr50.config.hocon.serializers; -import com.gmail.nossr50.config.hocon.notifications.PlayerNotification; +import com.gmail.nossr50.config.hocon.notifications.PlayerNotificationSettings; import com.google.common.reflect.TypeToken; import ninja.leaping.configurate.ConfigurationNode; import ninja.leaping.configurate.objectmapping.ObjectMappingException; @@ -8,24 +8,24 @@ import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; -public class PlayerNotificationSerializer implements TypeSerializer { +public class PlayerNotificationSerializer implements TypeSerializer { private static final String ENABLED_NODE = "Enabled"; private static final String SEND_TO_CHAT_NODE = "Send-To-Chat"; private static final String SEND_TO_ACTION_BAR_NODE = "Send-To-Action-Bar"; @Nullable @Override - public PlayerNotification deserialize(@NonNull TypeToken type, @NonNull ConfigurationNode value) throws ObjectMappingException { + public PlayerNotificationSettings deserialize(@NonNull TypeToken type, @NonNull ConfigurationNode value) throws ObjectMappingException { Boolean enabled = value.getNode(ENABLED_NODE).getValue(TypeToken.of(Boolean.class)); Boolean sendTochat = value.getNode(SEND_TO_CHAT_NODE).getValue(TypeToken.of(Boolean.class)); Boolean sendToActionBar = value.getNode(SEND_TO_ACTION_BAR_NODE).getValue(TypeToken.of(Boolean.class)); - PlayerNotification playerNotification = new PlayerNotification(enabled, sendTochat, sendToActionBar); - return playerNotification; + PlayerNotificationSettings playerNotificationSettings = new PlayerNotificationSettings(enabled, sendTochat, sendToActionBar); + return playerNotificationSettings; } @Override - public void serialize(@NonNull TypeToken type, @Nullable PlayerNotification obj, @NonNull ConfigurationNode value) throws ObjectMappingException { + public void serialize(@NonNull TypeToken type, @Nullable PlayerNotificationSettings obj, @NonNull ConfigurationNode value) throws ObjectMappingException { value.getNode(ENABLED_NODE).setValue(obj.isEnabled()); value.getNode(SEND_TO_CHAT_NODE).setValue(obj.isSendToChat()); value.getNode(SEND_TO_ACTION_BAR_NODE).setValue(obj.isSendToActionBar()); diff --git a/src/main/java/com/gmail/nossr50/events/skills/McMMOPlayerNotificationEvent.java b/src/main/java/com/gmail/nossr50/events/skills/McMMOPlayerNotificationEvent.java index f7b7333ef..1641d2f50 100644 --- a/src/main/java/com/gmail/nossr50/events/skills/McMMOPlayerNotificationEvent.java +++ b/src/main/java/com/gmail/nossr50/events/skills/McMMOPlayerNotificationEvent.java @@ -1,10 +1,8 @@ package com.gmail.nossr50.events.skills; -import com.gmail.nossr50.config.hocon.notifications.PlayerNotification; +import com.gmail.nossr50.config.hocon.notifications.PlayerNotificationSettings; import com.gmail.nossr50.datatypes.interactions.NotificationType; -import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.TextComponent; -import org.bukkit.craftbukkit.libs.jline.internal.Nullable; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.Event; @@ -16,19 +14,32 @@ import org.bukkit.event.HandlerList; * TextComponent is not guaranteed to exist, but often it does */ public class McMMOPlayerNotificationEvent extends Event implements Cancellable { + private static final HandlerList handlers = new HandlerList(); - private PlayerNotification playerNotification; + private PlayerNotificationSettings playerNotificationSettings; private Player recipient; private boolean isCancelled; private NotificationType notificationType; private TextComponent textComponent; + private String notificationText; - public McMMOPlayerNotificationEvent(NotificationType notificationType, Player recipient, PlayerNotification playerNotification, TextComponent textComponent) { + public McMMOPlayerNotificationEvent(NotificationType notificationType, Player recipient, PlayerNotificationSettings playerNotificationSettings, String notificationText) { super(false); this.notificationType = notificationType; this.recipient = recipient; - this.playerNotification = playerNotification; + this.playerNotificationSettings = playerNotificationSettings; + this.textComponent = null; + this.notificationText = notificationText; + isCancelled = false; + } + + public McMMOPlayerNotificationEvent(NotificationType notificationType, Player recipient, PlayerNotificationSettings playerNotificationSettings, TextComponent textComponent) { + super(false); + this.notificationType = notificationType; + this.recipient = recipient; + this.playerNotificationSettings = playerNotificationSettings; this.textComponent = textComponent; + this.notificationText = textComponent.getText(); isCancelled = false; } @@ -36,6 +47,23 @@ public class McMMOPlayerNotificationEvent extends Event implements Cancellable { * Getters & Setters */ + public PlayerNotificationSettings getPlayerNotificationSettings() { + return playerNotificationSettings; + } + + /** + * Notification text + * Note that most messages sent by mcMMO use Text Components instead, in which case this will be the text version of the text component + * @return the notification text of this event + */ + public String getNotificationText() { + return notificationText; + } + + public void setNotificationText(String notificationText) { + this.notificationText = notificationText; + } + /** * Whether or not this notification event uses a text component * @return true if this notification has a text component @@ -61,7 +89,7 @@ public class McMMOPlayerNotificationEvent extends Event implements Cancellable { * @return true if being sent to chat */ public boolean isBeingSentToChat() { - return playerNotification.isSendToChat(); + return playerNotificationSettings.isSendToChat(); } /** @@ -69,7 +97,7 @@ public class McMMOPlayerNotificationEvent extends Event implements Cancellable { * @return true if being sent to action bar */ public boolean isBeingSentToActionBar() { - return playerNotification.isSendToActionBar(); + return playerNotificationSettings.isSendToActionBar(); } /** @@ -77,7 +105,7 @@ public class McMMOPlayerNotificationEvent extends Event implements Cancellable { * @param sendToChat new value */ public void setSendToChat(boolean sendToChat) { - playerNotification.setSendToChat(sendToChat); + playerNotificationSettings.setSendToChat(sendToChat); } /** @@ -85,7 +113,7 @@ public class McMMOPlayerNotificationEvent extends Event implements Cancellable { * @param sendToActionBar new value */ public void setSendToActionBar(boolean sendToActionBar) { - playerNotification.setSendToActionBar(sendToActionBar); + playerNotificationSettings.setSendToActionBar(sendToActionBar); } public static HandlerList getHandlerList() { @@ -110,7 +138,6 @@ public class McMMOPlayerNotificationEvent extends Event implements Cancellable { this.textComponent = textComponent; } - /* * Custom Event Boilerplate */ diff --git a/src/main/java/com/gmail/nossr50/util/EventUtils.java b/src/main/java/com/gmail/nossr50/util/EventUtils.java index 884c26b4a..23c108eb9 100644 --- a/src/main/java/com/gmail/nossr50/util/EventUtils.java +++ b/src/main/java/com/gmail/nossr50/util/EventUtils.java @@ -2,6 +2,7 @@ package com.gmail.nossr50.util; import com.gmail.nossr50.datatypes.experience.XPGainReason; import com.gmail.nossr50.datatypes.experience.XPGainSource; +import com.gmail.nossr50.datatypes.interactions.NotificationType; import com.gmail.nossr50.datatypes.party.Party; import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.player.PlayerProfile; @@ -20,6 +21,7 @@ import com.gmail.nossr50.events.hardcore.McMMOPlayerVampirismEvent; import com.gmail.nossr50.events.party.McMMOPartyLevelUpEvent; import com.gmail.nossr50.events.party.McMMOPartyTeleportEvent; import com.gmail.nossr50.events.party.McMMOPartyXpGainEvent; +import com.gmail.nossr50.events.skills.McMMOPlayerNotificationEvent; import com.gmail.nossr50.events.skills.abilities.McMMOPlayerAbilityActivateEvent; import com.gmail.nossr50.events.skills.abilities.McMMOPlayerAbilityDeactivateEvent; import com.gmail.nossr50.events.skills.fishing.McMMOPlayerFishingTreasureEvent; @@ -32,6 +34,8 @@ import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.skills.CombatUtils; +import net.md_5.bungee.api.chat.TextComponent; +import org.bukkit.Bukkit; import org.bukkit.block.Block; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Entity; @@ -453,5 +457,36 @@ public class EventUtils { return event; } + /** + * Creates and calls a McMMOPlayerNotificationEvent + * @param player target player + * @param notificationType notification category + * @param textComponent text component used for the message + * @return the McMMOPlayerNotificationEvent after its been fired + */ + public static McMMOPlayerNotificationEvent createAndCallNotificationEvent(Player player, NotificationType notificationType, TextComponent textComponent) { + //Init event + McMMOPlayerNotificationEvent customEvent = new McMMOPlayerNotificationEvent(notificationType, player, mcMMO.getNotificationManager().getPlayerNotificationSettings(notificationType), textComponent); + + //Call event + Bukkit.getServer().getPluginManager().callEvent(customEvent); + return customEvent; + } + + /** + * Creates and calls a McMMOPlayerNotificationEvent + * @param player target player + * @param notificationType notification category + * @param message string used for the message + * @return the McMMOPlayerNotificationEvent after its been fired + */ + public static McMMOPlayerNotificationEvent createAndCallNotificationEvent(Player player, NotificationType notificationType, String message) { + //Init event + McMMOPlayerNotificationEvent customEvent = new McMMOPlayerNotificationEvent(notificationType, player, mcMMO.getNotificationManager().getPlayerNotificationSettings(notificationType), message); + + //Call event + Bukkit.getServer().getPluginManager().callEvent(customEvent); + return customEvent; + } } diff --git a/src/main/java/com/gmail/nossr50/util/player/NotificationManager.java b/src/main/java/com/gmail/nossr50/util/player/NotificationManager.java index 4b7da34ce..b67630d55 100644 --- a/src/main/java/com/gmail/nossr50/util/player/NotificationManager.java +++ b/src/main/java/com/gmail/nossr50/util/player/NotificationManager.java @@ -1,7 +1,6 @@ package com.gmail.nossr50.util.player; -import com.gmail.nossr50.config.AdvancedConfig; -import com.gmail.nossr50.config.hocon.notifications.PlayerNotification; +import com.gmail.nossr50.config.hocon.notifications.PlayerNotificationSettings; import com.gmail.nossr50.datatypes.interactions.NotificationType; import com.gmail.nossr50.datatypes.notifications.SensitiveCommandType; import com.gmail.nossr50.datatypes.player.McMMOPlayer; @@ -10,6 +9,7 @@ import com.gmail.nossr50.datatypes.skills.SubSkillType; import com.gmail.nossr50.events.skills.McMMOPlayerNotificationEvent; import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.mcMMO; +import com.gmail.nossr50.util.EventUtils; import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.TextComponentFactory; import com.gmail.nossr50.util.sounds.SoundManager; @@ -25,9 +25,12 @@ import org.bukkit.entity.Player; import java.util.HashMap; +/** + * Handles all messages sent to the player from mcMMO + */ public class NotificationManager { - private HashMap playerNotificationHashMap; + private HashMap playerNotificationHashMap; public NotificationManager() { playerNotificationHashMap = new HashMap<>(); @@ -40,6 +43,20 @@ public class NotificationManager { playerNotificationHashMap = new HashMap<>(mcMMO.getConfigManager().getConfigNotifications().getNotificationSettingHashMap()); } + + public void setPlayerNotificationSettings(NotificationType notificationType, PlayerNotificationSettings playerNotificationSettings) { + playerNotificationHashMap.put(notificationType, playerNotificationSettings); + } + + /** + * Grab the settings for a NotificationType + * @param notificationType target notification type + * @return the notification settings for this type + */ + public PlayerNotificationSettings getPlayerNotificationSettings(NotificationType notificationType) { + return playerNotificationHashMap.get(notificationType); + } + /** * Sends players notifications from mcMMO * This does this by sending out an event so other plugins can cancel it @@ -52,20 +69,29 @@ public class NotificationManager { if (UserManager.getPlayer(player) == null || !UserManager.getPlayer(player).useChatNotifications()) return; - ChatMessageType destination = AdvancedConfig.getInstance().doesNotificationUseActionBar(notificationType) ? ChatMessageType.ACTION_BAR : ChatMessageType.SYSTEM; + TextComponent textComponent = TextComponentFactory.getNotificationTextComponentFromLocale(key); + McMMOPlayerNotificationEvent customEvent = EventUtils.createAndCallNotificationEvent(player, notificationType, textComponent); - TextComponent message = TextComponentFactory.getNotificationTextComponentFromLocale(key); - McMMOPlayerNotificationEvent customEvent = checkNotificationEvent(player, notificationType, destination, message); - - sendNotification(player, customEvent); + sendNotification(customEvent); } + /** + * Builds a text component with one or more parameters + * @param key locale key + * @param values parameters + * @return TextComponent for this message + */ + public TextComponent buildTextComponent(String key, String... values) { + return TextComponentFactory.getNotificationMultipleValues(key, values); + } - public boolean doesPlayerUseNotifications(Player player) { - if (UserManager.getPlayer(player) == null) - return false; - else - return UserManager.getPlayer(player).useChatNotifications(); + /** + * Builds a text component without any parameters + * @param key locale key + * @return TextComponent for this message + */ + public TextComponent buildTextComponent(String key) { + return TextComponentFactory.getNotificationTextComponentFromLocale(key); } /** @@ -96,39 +122,10 @@ public class NotificationManager { if(UserManager.getPlayer(player) == null || !UserManager.getPlayer(player).useChatNotifications()) return; - ChatMessageType destination = AdvancedConfig.getInstance().doesNotificationUseActionBar(notificationType) ? ChatMessageType.ACTION_BAR : ChatMessageType.SYSTEM; + TextComponent textComponent = buildTextComponent(key, values); + McMMOPlayerNotificationEvent customEvent = EventUtils.createAndCallNotificationEvent(player, notificationType, textComponent); - TextComponent message = TextComponentFactory.getNotificationMultipleValues(key, values); - McMMOPlayerNotificationEvent customEvent = checkNotificationEvent(player, notificationType, destination, message); - - sendNotification(player, customEvent); - } - - private void sendNotification(Player player, McMMOPlayerNotificationEvent customEvent) { - if (customEvent.isCancelled()) - return; - - //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 - if (customEvent.getChatMessageType() == ChatMessageType.ACTION_BAR) { - player.spigot().sendMessage(customEvent.getChatMessageType(), customEvent.getNotificationTextComponent()); - - if (customEvent.isMessageAlsoBeingSentToChat()) { - //Send copy to chat system - player.spigot().sendMessage(ChatMessageType.SYSTEM, customEvent.getNotificationTextComponent()); - } - } else { - player.spigot().sendMessage(customEvent.getChatMessageType(), customEvent.getNotificationTextComponent()); - } - } - - private McMMOPlayerNotificationEvent checkNotificationEvent(Player player, NotificationType notificationType, ChatMessageType destination, TextComponent message) { - //Init event - McMMOPlayerNotificationEvent customEvent = new McMMOPlayerNotificationEvent(player, - notificationType, message, destination, AdvancedConfig.getInstance().doesNotificationSendCopyToChat(notificationType)); - - //Call event - Bukkit.getServer().getPluginManager().callEvent(customEvent); - return customEvent; + sendNotification(customEvent); } /** @@ -142,12 +139,37 @@ public class NotificationManager { if (!mcMMOPlayer.useChatNotifications()) return; - ChatMessageType destination = AdvancedConfig.getInstance().doesNotificationUseActionBar(NotificationType.LEVEL_UP_MESSAGE) ? ChatMessageType.ACTION_BAR : ChatMessageType.SYSTEM; - TextComponent levelUpTextComponent = TextComponentFactory.getNotificationLevelUpTextComponent(skillName, levelsGained, newLevel); - McMMOPlayerNotificationEvent customEvent = checkNotificationEvent(mcMMOPlayer.getPlayer(), NotificationType.LEVEL_UP_MESSAGE, destination, levelUpTextComponent); + McMMOPlayerNotificationEvent customEvent = EventUtils.createAndCallNotificationEvent(mcMMOPlayer.getPlayer(), NotificationType.LEVEL_UP_MESSAGE, levelUpTextComponent); - sendNotification(mcMMOPlayer.getPlayer(), customEvent); + sendNotification(customEvent); + } + + private void sendNotification(McMMOPlayerNotificationEvent customEvent) { + if (customEvent.isCancelled()) + return; + + Player player = customEvent.getRecipient(); + PlayerNotificationSettings playerNotificationSettings = customEvent.getPlayerNotificationSettings(); + + //Text Component found + if(customEvent.hasTextComponent()) { + if(playerNotificationSettings.isSendToActionBar()) { + player.spigot().sendMessage(ChatMessageType.ACTION_BAR, customEvent.getNotificationTextComponent()); + } + + //Chat (System) + if(playerNotificationSettings.isSendToChat()) { + if(customEvent.hasTextComponent()) { + player.spigot().sendMessage(ChatMessageType.SYSTEM, customEvent.getNotificationTextComponent()); + } + } + } else { + //Chat but without a text component + if(playerNotificationSettings.isSendToChat()) { + player.sendMessage(customEvent.getNotificationText()); + } + } } public void broadcastTitle(Server server, String title, String subtitle, int i1, int i2, int i3) { @@ -165,13 +187,35 @@ public class NotificationManager { //Unlock Sound Effect SoundManager.sendCategorizedSound(mcMMOPlayer.getPlayer(), mcMMOPlayer.getPlayer().getLocation(), SoundType.SKILL_UNLOCKED, SoundCategory.MASTER); + } - //ACTION BAR MESSAGE - /*if(AdvancedConfig.getInstance().doesNotificationUseActionBar(NotificationType.SUBSKILL_UNLOCKED)) - mcMMOPlayer.getPlayer().spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(LocaleLoader.getString("JSON.SkillUnlockMessage", - subSkillType.getLocaleName(), - String.valueOf(RankUtils.getRank(mcMMOPlayer.getPlayer(), - subSkillType)))));*/ + /** + * Convenience method to report info about a command sender using a sensitive command + * + * @param commandSender the command user + * @param sensitiveCommandType type of command issued + */ + public void processSensitiveCommandNotification(CommandSender commandSender, SensitiveCommandType sensitiveCommandType, String... args) { + /* + * Determine the 'identity' of the one who executed the command to pass as a parameters + */ + String senderName = LocaleLoader.getString("Server.ConsoleName"); + + if (commandSender instanceof Player) { + senderName = ((Player) commandSender).getDisplayName() + ChatColor.RESET + "-" + ((Player) commandSender).getUniqueId(); + } + + //Send the notification + switch (sensitiveCommandType) { + case XPRATE_MODIFY: + sendAdminNotification(LocaleLoader.getString("Notifications.Admin.XPRate.Start.Others", addItemToFirstPositionOfArray(senderName, args))); + sendAdminCommandConfirmation(commandSender, LocaleLoader.getString("Notifications.Admin.XPRate.Start.Self", args)); + break; + case XPRATE_END: + sendAdminNotification(LocaleLoader.getString("Notifications.Admin.XPRate.End.Others", addItemToFirstPositionOfArray(senderName, args))); + sendAdminCommandConfirmation(commandSender, LocaleLoader.getString("Notifications.Admin.XPRate.End.Self", args)); + break; + } } /** @@ -205,35 +249,6 @@ public class NotificationManager { commandSender.sendMessage(LocaleLoader.getString("Notifications.Admin.Format.Self", msg)); } - /** - * Convenience method to report info about a command sender using a sensitive command - * - * @param commandSender the command user - * @param sensitiveCommandType type of command issued - */ - public void processSensitiveCommandNotification(CommandSender commandSender, SensitiveCommandType sensitiveCommandType, String... args) { - /* - * Determine the 'identity' of the one who executed the command to pass as a parameters - */ - String senderName = LocaleLoader.getString("Server.ConsoleName"); - - if (commandSender instanceof Player) { - senderName = ((Player) commandSender).getDisplayName() + ChatColor.RESET + "-" + ((Player) commandSender).getUniqueId(); - } - - //Send the notification - switch (sensitiveCommandType) { - case XPRATE_MODIFY: - sendAdminNotification(LocaleLoader.getString("Notifications.Admin.XPRate.Start.Others", addItemToFirstPositionOfArray(senderName, args))); - sendAdminCommandConfirmation(commandSender, LocaleLoader.getString("Notifications.Admin.XPRate.Start.Self", args)); - break; - case XPRATE_END: - sendAdminNotification(LocaleLoader.getString("Notifications.Admin.XPRate.End.Others", addItemToFirstPositionOfArray(senderName, args))); - sendAdminCommandConfirmation(commandSender, LocaleLoader.getString("Notifications.Admin.XPRate.End.Self", args)); - break; - } - } - /** * Takes an array and an object, makes a new array with object in the first position of the new array, * and the following elements in this new array being a copy of the existing array retaining their order @@ -251,4 +266,10 @@ public class NotificationManager { return newArray; } + public boolean doesPlayerUseNotifications(Player player) { + if (UserManager.getPlayer(player) == null) + return false; + else + return UserManager.getPlayer(player).useChatNotifications(); + } }