2019-01-13 04:56:54 +01:00
|
|
|
package com.gmail.nossr50.util.player;
|
|
|
|
|
|
|
|
import com.gmail.nossr50.config.AdvancedConfig;
|
2019-05-18 19:09:45 +02:00
|
|
|
import com.gmail.nossr50.config.Config;
|
2019-01-13 04:56:54 +01:00
|
|
|
import com.gmail.nossr50.datatypes.interactions.NotificationType;
|
2019-05-18 19:09:45 +02:00
|
|
|
import com.gmail.nossr50.datatypes.notifications.SensitiveCommandType;
|
2019-01-13 04:56:54 +01:00
|
|
|
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
|
2019-01-13 08:54:53 +01:00
|
|
|
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
|
2019-01-20 03:51:18 +01:00
|
|
|
import com.gmail.nossr50.datatypes.skills.SubSkillType;
|
2019-01-13 04:56:54 +01:00
|
|
|
import com.gmail.nossr50.events.skills.McMMOPlayerNotificationEvent;
|
2019-05-18 17:12:32 +02:00
|
|
|
import com.gmail.nossr50.locale.LocaleLoader;
|
|
|
|
import com.gmail.nossr50.mcMMO;
|
2020-09-24 16:08:24 +02:00
|
|
|
import com.gmail.nossr50.util.McMMOMessageType;
|
2019-05-18 17:12:32 +02:00
|
|
|
import com.gmail.nossr50.util.Permissions;
|
2019-01-13 04:56:54 +01:00
|
|
|
import com.gmail.nossr50.util.TextComponentFactory;
|
2019-01-20 10:21:33 +01:00
|
|
|
import com.gmail.nossr50.util.sounds.SoundManager;
|
|
|
|
import com.gmail.nossr50.util.sounds.SoundType;
|
2020-09-24 16:08:24 +02:00
|
|
|
import net.kyori.adventure.audience.Audience;
|
|
|
|
import net.kyori.adventure.audience.MessageType;
|
|
|
|
import net.kyori.adventure.text.Component;
|
2019-06-03 07:42:03 +02:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.Server;
|
|
|
|
import org.bukkit.SoundCategory;
|
2019-05-18 19:09:45 +02:00
|
|
|
import org.bukkit.command.CommandSender;
|
2019-01-13 04:56:54 +01:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
|
|
|
public class NotificationManager {
|
|
|
|
/**
|
|
|
|
* Sends players notifications from mcMMO
|
2019-06-16 06:22:21 +02:00
|
|
|
* Does so by sending out an event so other plugins can cancel it
|
2019-01-13 04:56:54 +01:00
|
|
|
* @param player target player
|
|
|
|
* @param notificationType notifications defined type
|
|
|
|
* @param key the locale key for the notifications defined message
|
|
|
|
*/
|
|
|
|
public static void sendPlayerInformation(Player player, NotificationType notificationType, String key)
|
|
|
|
{
|
2019-04-13 00:17:05 +02:00
|
|
|
if(UserManager.getPlayer(player) == null || !UserManager.getPlayer(player).useChatNotifications())
|
2019-04-05 02:27:17 +02:00
|
|
|
return;
|
|
|
|
|
2020-09-24 16:08:24 +02:00
|
|
|
McMMOMessageType destination = AdvancedConfig.getInstance().doesNotificationUseActionBar(notificationType) ? McMMOMessageType.ACTION_BAR : McMMOMessageType.SYSTEM;
|
2019-01-13 04:56:54 +01:00
|
|
|
|
2020-09-24 16:08:24 +02:00
|
|
|
Component message = TextComponentFactory.getNotificationTextComponentFromLocale(key);
|
2019-01-13 04:56:54 +01:00
|
|
|
McMMOPlayerNotificationEvent customEvent = checkNotificationEvent(player, notificationType, destination, message);
|
|
|
|
|
|
|
|
sendNotification(player, customEvent);
|
|
|
|
}
|
|
|
|
|
2019-04-13 00:17:05 +02:00
|
|
|
|
|
|
|
public static boolean doesPlayerUseNotifications(Player player)
|
|
|
|
{
|
|
|
|
if(UserManager.getPlayer(player) == null)
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return UserManager.getPlayer(player).useChatNotifications();
|
|
|
|
}
|
|
|
|
|
2019-01-13 06:51:31 +01:00
|
|
|
/**
|
|
|
|
* Sends players notifications from mcMMO
|
|
|
|
* This does this by sending out an event so other plugins can cancel it
|
|
|
|
* This event in particular is provided with a source player, and players near the source player are sent the information
|
2019-05-28 04:25:36 +02:00
|
|
|
* @param targetPlayer the recipient player for this message
|
2019-01-13 06:51:31 +01:00
|
|
|
* @param notificationType type of notification
|
|
|
|
* @param key Locale Key for the string to use with this event
|
|
|
|
* @param values values to be injected into the locale string
|
|
|
|
*/
|
2019-05-28 04:25:36 +02:00
|
|
|
public static void sendNearbyPlayersInformation(Player targetPlayer, NotificationType notificationType, String key, String... values)
|
2019-01-13 04:56:54 +01:00
|
|
|
{
|
2019-05-28 04:25:36 +02:00
|
|
|
sendPlayerInformation(targetPlayer, notificationType, key, values);
|
2019-01-13 04:56:54 +01:00
|
|
|
}
|
|
|
|
|
2019-06-11 07:37:20 +02:00
|
|
|
public static void sendPlayerInformationChatOnly(Player player, String key, String... values)
|
|
|
|
{
|
|
|
|
if(UserManager.getPlayer(player) == null || !UserManager.getPlayer(player).useChatNotifications())
|
|
|
|
return;
|
|
|
|
|
|
|
|
String preColoredString = LocaleLoader.getString(key, (Object[]) values);
|
|
|
|
player.sendMessage(preColoredString);
|
|
|
|
}
|
|
|
|
|
2019-07-03 02:29:33 +02:00
|
|
|
public static void sendPlayerInformationChatOnlyPrefixed(Player player, String key, String... values)
|
|
|
|
{
|
|
|
|
if(UserManager.getPlayer(player) == null || !UserManager.getPlayer(player).useChatNotifications())
|
|
|
|
return;
|
|
|
|
|
|
|
|
String preColoredString = LocaleLoader.getString(key, (Object[]) values);
|
|
|
|
String prefixFormattedMessage = LocaleLoader.getString("mcMMO.Template.Prefix", preColoredString);
|
|
|
|
player.sendMessage(prefixFormattedMessage);
|
|
|
|
}
|
|
|
|
|
2019-01-13 04:56:54 +01:00
|
|
|
public static void sendPlayerInformation(Player player, NotificationType notificationType, String key, String... values)
|
|
|
|
{
|
2019-04-13 00:17:05 +02:00
|
|
|
if(UserManager.getPlayer(player) == null || !UserManager.getPlayer(player).useChatNotifications())
|
2019-04-05 02:27:17 +02:00
|
|
|
return;
|
2019-01-13 04:56:54 +01:00
|
|
|
|
2020-09-24 16:08:24 +02:00
|
|
|
McMMOMessageType destination = AdvancedConfig.getInstance().doesNotificationUseActionBar(notificationType) ? McMMOMessageType.ACTION_BAR : McMMOMessageType.SYSTEM;
|
2019-01-13 04:56:54 +01:00
|
|
|
|
2020-09-24 16:08:24 +02:00
|
|
|
Component message = TextComponentFactory.getNotificationMultipleValues(key, values);
|
2019-01-13 04:56:54 +01:00
|
|
|
McMMOPlayerNotificationEvent customEvent = checkNotificationEvent(player, notificationType, destination, message);
|
|
|
|
|
|
|
|
sendNotification(player, customEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void sendNotification(Player player, McMMOPlayerNotificationEvent customEvent) {
|
|
|
|
if (customEvent.isCancelled())
|
|
|
|
return;
|
|
|
|
|
2020-09-30 20:34:01 +02:00
|
|
|
final Audience audience = mcMMO.getAudiences().player(player);
|
2020-09-24 16:08:24 +02:00
|
|
|
|
2019-01-14 10:22:14 +01:00
|
|
|
//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
|
2020-09-24 16:08:24 +02:00
|
|
|
if(customEvent.getChatMessageType() == McMMOMessageType.ACTION_BAR)
|
2019-01-14 10:22:14 +01:00
|
|
|
{
|
2020-09-24 16:08:24 +02:00
|
|
|
audience.sendActionBar(customEvent.getNotificationTextComponent());
|
2019-01-14 10:22:14 +01:00
|
|
|
|
|
|
|
if(customEvent.isMessageAlsoBeingSentToChat())
|
|
|
|
{
|
|
|
|
//Send copy to chat system
|
2020-09-24 16:08:24 +02:00
|
|
|
audience.sendMessage(customEvent.getNotificationTextComponent(), MessageType.SYSTEM);
|
2019-01-14 10:22:14 +01:00
|
|
|
}
|
|
|
|
} else {
|
2020-09-24 16:08:24 +02:00
|
|
|
audience.sendMessage(customEvent.getNotificationTextComponent(), MessageType.SYSTEM);
|
2019-01-14 10:22:14 +01:00
|
|
|
}
|
2019-01-13 04:56:54 +01:00
|
|
|
}
|
|
|
|
|
2020-09-24 16:08:24 +02:00
|
|
|
private static McMMOPlayerNotificationEvent checkNotificationEvent(Player player, NotificationType notificationType, McMMOMessageType destination, Component message) {
|
2019-01-13 04:56:54 +01:00
|
|
|
//Init event
|
|
|
|
McMMOPlayerNotificationEvent customEvent = new McMMOPlayerNotificationEvent(player,
|
2019-01-14 10:22:14 +01:00
|
|
|
notificationType, message, destination, AdvancedConfig.getInstance().doesNotificationSendCopyToChat(notificationType));
|
2019-01-13 04:56:54 +01:00
|
|
|
|
|
|
|
//Call event
|
|
|
|
Bukkit.getServer().getPluginManager().callEvent(customEvent);
|
|
|
|
return customEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles sending level up notifications to a mcMMOPlayer
|
|
|
|
* @param mcMMOPlayer target mcMMOPlayer
|
|
|
|
* @param skillName skill that leveled up
|
|
|
|
* @param newLevel new level of that skill
|
|
|
|
*/
|
2019-01-21 16:27:09 +01:00
|
|
|
public static void sendPlayerLevelUpNotification(McMMOPlayer mcMMOPlayer, PrimarySkillType skillName, int levelsGained, int newLevel)
|
2019-01-13 04:56:54 +01:00
|
|
|
{
|
2019-04-13 00:17:05 +02:00
|
|
|
if(!mcMMOPlayer.useChatNotifications())
|
2019-04-05 02:27:17 +02:00
|
|
|
return;
|
|
|
|
|
2020-09-24 16:08:24 +02:00
|
|
|
McMMOMessageType destination = AdvancedConfig.getInstance().doesNotificationUseActionBar(NotificationType.LEVEL_UP_MESSAGE) ? McMMOMessageType.ACTION_BAR : McMMOMessageType.SYSTEM;
|
2019-01-13 04:56:54 +01:00
|
|
|
|
2020-09-24 16:08:24 +02:00
|
|
|
Component levelUpTextComponent = TextComponentFactory.getNotificationLevelUpTextComponent(skillName, levelsGained, newLevel);
|
2019-01-13 04:56:54 +01:00
|
|
|
McMMOPlayerNotificationEvent customEvent = checkNotificationEvent(mcMMOPlayer.getPlayer(), NotificationType.LEVEL_UP_MESSAGE, destination, levelUpTextComponent);
|
|
|
|
|
|
|
|
sendNotification(mcMMOPlayer.getPlayer(), customEvent);
|
|
|
|
}
|
2019-01-14 11:32:02 +01:00
|
|
|
|
|
|
|
public static void broadcastTitle(Server server, String title, String subtitle, int i1, int i2, int i3)
|
|
|
|
{
|
|
|
|
for(Player player : server.getOnlinePlayers())
|
|
|
|
{
|
|
|
|
player.sendTitle(title, subtitle, i1, i2, i3);
|
|
|
|
}
|
|
|
|
}
|
2019-01-20 03:51:18 +01:00
|
|
|
|
|
|
|
public static void sendPlayerUnlockNotification(McMMOPlayer mcMMOPlayer, SubSkillType subSkillType)
|
|
|
|
{
|
2019-04-13 00:17:05 +02:00
|
|
|
if(!mcMMOPlayer.useChatNotifications())
|
2019-04-05 02:27:17 +02:00
|
|
|
return;
|
|
|
|
|
2019-01-20 03:51:18 +01:00
|
|
|
//CHAT MESSAGE
|
2020-09-30 20:34:01 +02:00
|
|
|
mcMMO.getAudiences().player(mcMMOPlayer.getPlayer()).sendMessage(TextComponentFactory.getSubSkillUnlockedNotificationComponents(mcMMOPlayer.getPlayer(), subSkillType));
|
2019-01-20 03:51:18 +01:00
|
|
|
|
2019-01-20 10:21:33 +01:00
|
|
|
//Unlock Sound Effect
|
|
|
|
SoundManager.sendCategorizedSound(mcMMOPlayer.getPlayer(), mcMMOPlayer.getPlayer().getLocation(), SoundType.SKILL_UNLOCKED, SoundCategory.MASTER);
|
|
|
|
|
2019-01-20 03:51:18 +01:00
|
|
|
//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)))));*/
|
|
|
|
}
|
2019-05-18 17:12:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a message to all admins with the admin notification formatting from the locale
|
|
|
|
* Admins are currently players with either Operator status or Admin Chat permission
|
2019-05-18 19:40:06 +02:00
|
|
|
* @param msg message fetched from locale
|
2019-05-18 17:12:32 +02:00
|
|
|
*/
|
2019-05-18 19:09:45 +02:00
|
|
|
private static void sendAdminNotification(String msg) {
|
2019-05-18 19:47:37 +02:00
|
|
|
//If its not enabled exit
|
|
|
|
if(!Config.getInstance().adminNotifications())
|
|
|
|
return;
|
|
|
|
|
2019-05-18 17:12:32 +02:00
|
|
|
for(Player player : Bukkit.getServer().getOnlinePlayers())
|
|
|
|
{
|
|
|
|
if(player.isOp() || Permissions.adminChat(player))
|
|
|
|
{
|
2019-05-18 19:40:06 +02:00
|
|
|
player.sendMessage(LocaleLoader.getString("Notifications.Admin.Format.Others", msg));
|
2019-05-18 17:12:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Copy it out to Console too
|
2019-05-18 19:40:06 +02:00
|
|
|
mcMMO.p.getLogger().info(LocaleLoader.getString("Notifications.Admin.Format.Others", msg));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a confirmation message to the CommandSender who just executed an admin command
|
|
|
|
* @param commandSender target command sender
|
|
|
|
* @param msg message fetched from locale
|
|
|
|
*/
|
|
|
|
private static void sendAdminCommandConfirmation(CommandSender commandSender, String msg) {
|
|
|
|
commandSender.sendMessage(LocaleLoader.getString("Notifications.Admin.Format.Self", msg));
|
2019-05-18 17:12:32 +02:00
|
|
|
}
|
2019-05-18 19:09:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 static 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)
|
|
|
|
{
|
2019-05-18 19:59:52 +02:00
|
|
|
senderName = ((Player) commandSender).getDisplayName() + ChatColor.RESET + "-" + ((Player) commandSender).getUniqueId();
|
2019-05-18 19:09:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Send the notification
|
|
|
|
switch(sensitiveCommandType)
|
|
|
|
{
|
|
|
|
case XPRATE_MODIFY:
|
2019-05-18 19:40:06 +02:00
|
|
|
sendAdminNotification(LocaleLoader.getString("Notifications.Admin.XPRate.Start.Others", addItemToFirstPositionOfArray(senderName, args)));
|
|
|
|
sendAdminCommandConfirmation(commandSender, LocaleLoader.getString("Notifications.Admin.XPRate.Start.Self", args));
|
2019-05-18 19:09:45 +02:00
|
|
|
break;
|
|
|
|
case XPRATE_END:
|
2019-05-18 19:40:06 +02:00
|
|
|
sendAdminNotification(LocaleLoader.getString("Notifications.Admin.XPRate.End.Others", addItemToFirstPositionOfArray(senderName, args)));
|
|
|
|
sendAdminCommandConfirmation(commandSender, LocaleLoader.getString("Notifications.Admin.XPRate.End.Self", args));
|
2019-05-18 19:09:45 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-05-18 19:40:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @param itemToAdd the string to put at the beginning of the new array
|
|
|
|
* @param existingArray the existing array to be copied to the new array at position [0]+1 relative to their original index
|
|
|
|
* @return the new array combining itemToAdd at the start and existing array elements following while retaining their order
|
|
|
|
*/
|
|
|
|
public static String[] addItemToFirstPositionOfArray(String itemToAdd, String... existingArray) {
|
|
|
|
String[] newArray = new String[existingArray.length + 1];
|
|
|
|
newArray[0] = itemToAdd;
|
|
|
|
|
|
|
|
System.arraycopy(existingArray, 0, newArray, 1, existingArray.length);
|
|
|
|
|
|
|
|
return newArray;
|
|
|
|
}
|
|
|
|
|
2019-01-13 04:56:54 +01:00
|
|
|
}
|