Complete rewrite of Admin & Party chat code

There are some API breaks as a result of these rewrites, I tried to keep it minimal, but I'm sure some plugins will need to update.
This commit is contained in:
nossr50
2020-10-26 16:31:02 -07:00
parent cf6a2e9e97
commit 749c83ac59
44 changed files with 1419 additions and 713 deletions

View File

@ -1,21 +0,0 @@
package com.gmail.nossr50.chat;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.events.chat.McMMOAdminChatEvent;
import org.bukkit.plugin.Plugin;
public class AdminChatManager extends ChatManager {
protected AdminChatManager(Plugin plugin) {
super(plugin, Config.getInstance().getAdminDisplayNames(), Config.getInstance().getAdminChatPrefix());
}
@Override
public void handleChat(String senderName, String displayName, String message, boolean isAsync) {
handleChat(new McMMOAdminChatEvent(plugin, senderName, displayName, message, isAsync));
}
@Override
protected void sendMessage() {
plugin.getServer().broadcast(message, "mcmmo.chat.adminchat");
}
}

View File

@ -1,88 +1,184 @@
package com.gmail.nossr50.chat;
import com.gmail.nossr50.chat.author.Author;
import com.gmail.nossr50.chat.author.ConsoleAuthor;
import com.gmail.nossr50.chat.mailer.AdminChatMailer;
import com.gmail.nossr50.chat.mailer.PartyChatMailer;
import com.gmail.nossr50.datatypes.chat.ChatChannel;
import com.gmail.nossr50.datatypes.party.Party;
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.events.chat.McMMOChatEvent;
import com.gmail.nossr50.events.chat.McMMOPartyChatEvent;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.util.player.UserManager;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public abstract class ChatManager {
protected Plugin plugin;
protected boolean useDisplayNames;
protected String chatPrefix;
//TODO: Micro optimization - Cache audiences and update cache when needed
public class ChatManager {
protected String senderName;
protected String displayName;
protected String message;
private final @NotNull AdminChatMailer adminChatMailer;
private final @NotNull PartyChatMailer partyChatMailer;
protected ChatManager(Plugin plugin, boolean useDisplayNames, String chatPrefix) {
this.plugin = plugin;
this.useDisplayNames = useDisplayNames;
this.chatPrefix = chatPrefix;
private @Nullable ConsoleAuthor consoleAuthor;
public ChatManager(@NotNull mcMMO pluginRef) {
adminChatMailer = new AdminChatMailer(pluginRef);
partyChatMailer = new PartyChatMailer(pluginRef);
}
protected void handleChat(McMMOChatEvent event) {
plugin.getServer().getPluginManager().callEvent(event);
/**
* Handles player messaging when they are either in party chat or admin chat modes
* @param mmoPlayer target player
* @param rawMessage the raw message from the player as it was typed
* @param isAsync whether or not this is getting processed via async
*/
public void processPlayerMessage(@NotNull McMMOPlayer mmoPlayer, @NotNull String rawMessage, boolean isAsync) {
processPlayerMessage(mmoPlayer, mmoPlayer.getChatChannel(), rawMessage, isAsync);
}
if (event.isCancelled()) {
return;
/**
* Handles player messaging for a specific chat channel
* @param mmoPlayer target player
* @param args the raw command arguments from the player
* @param chatChannel target channel
*/
public void processPlayerMessage(@NotNull McMMOPlayer mmoPlayer, @NotNull String[] args, @NotNull ChatChannel chatChannel) {
String chatMessageWithoutCommand = buildChatMessage(args);
//Commands are never async
processPlayerMessage(mmoPlayer, chatChannel, chatMessageWithoutCommand, false);
}
/**
* Handles player messaging for a specific chat channel
* @param mmoPlayer target player
* @param chatChannel target chat channel
* @param rawMessage raw chat message as it was typed
* @param isAsync whether or not this is getting processed via async
*/
private void processPlayerMessage(@NotNull McMMOPlayer mmoPlayer, @NotNull ChatChannel chatChannel, @NotNull String rawMessage, boolean isAsync) {
switch (chatChannel) {
case ADMIN:
adminChatMailer.processChatMessage(mmoPlayer.getAdminAuthor(), rawMessage, isAsync);
break;
case PARTY:
partyChatMailer.processChatMessage(mmoPlayer.getPartyAuthor(), rawMessage, mmoPlayer.getParty(), isAsync);
break;
case PARTY_OFFICER:
case NONE:
break;
}
}
/**
* Handles console messaging to admins
* @param rawMessage raw message from the console
*/
public void processConsoleMessage(@NotNull String rawMessage) {
adminChatMailer.processChatMessage(getConsoleAuthor(), rawMessage, false);
}
/**
* Handles console messaging to admins
* @param args raw command args from the console
*/
public void processConsoleMessage(@NotNull String[] args) {
processConsoleMessage(buildChatMessage(args));
}
/**
* Handles console messaging to a specific party
* @param rawMessage raw message from the console
* @param party target party
*/
public void processConsoleMessage(@NotNull String rawMessage, @NotNull Party party) {
partyChatMailer.processChatMessage(getConsoleAuthor(), rawMessage, party, false);
}
/**
* Handles console messaging to a specific party
* @param args raw command args from the console
* @param party target party
*/
public void processConsoleMessage(@NotNull String[] args, @NotNull Party party) {
String chatMessageWithoutCommand = buildChatMessage(args);
processConsoleMessage(chatMessageWithoutCommand, party);
}
/**
* Gets a console author
* Constructs one if it doesn't already exist
* @return a {@link ConsoleAuthor}
*/
private @NotNull Author getConsoleAuthor() {
if (consoleAuthor == null) {
consoleAuthor = new ConsoleAuthor(LocaleLoader.getString("Chat.Identity.Console"));
}
senderName = event.getSender();
displayName = useDisplayNames ? event.getDisplayName() : senderName;
message = LocaleLoader.formatString(chatPrefix, displayName) + " " + event.getMessage();
return consoleAuthor;
}
sendMessage();
/**
* Change the chat channel of a {@link McMMOPlayer}
* Targeting the channel a player is already in will remove that player from the chat channel
* @param mmoPlayer target player
* @param targetChatChannel target chat channel
*/
public void setOrToggleChatChannel(@NotNull McMMOPlayer mmoPlayer, @NotNull ChatChannel targetChatChannel) {
if(targetChatChannel == mmoPlayer.getChatChannel()) {
//Disabled message
mmoPlayer.getPlayer().sendMessage(LocaleLoader.getString("Chat.Channel.Off", StringUtils.getCapitalized(targetChatChannel.toString())));
mmoPlayer.setChatMode(ChatChannel.NONE);
} else {
mmoPlayer.setChatMode(targetChatChannel);
mmoPlayer.getPlayer().sendMessage(LocaleLoader.getString("Chat.Channel.On", StringUtils.getCapitalized(targetChatChannel.toString())));
}
}
/*
* Party Chat Spying
* Party messages will be copied to people with the mcmmo.admin.chatspy permission node
*/
if(event instanceof McMMOPartyChatEvent)
{
//We need to grab the party chat name
McMMOPartyChatEvent partyChatEvent = (McMMOPartyChatEvent) event;
/**
* Create a chat message from an array of {@link String}
* @param args array of {@link String}
* @return a String built from the array
*/
private @NotNull String buildChatMessage(@NotNull String[] args) {
StringBuilder stringBuilder = new StringBuilder();
//Find the people with permissions
for(McMMOPlayer mcMMOPlayer : UserManager.getPlayers())
{
Player player = mcMMOPlayer.getPlayer();
//Check for toggled players
if(mcMMOPlayer.isPartyChatSpying())
{
Party adminParty = mcMMOPlayer.getParty();
//Only message admins not part of this party
if(adminParty != null)
{
//TODO: Incorporate JSON
if(!adminParty.getName().equalsIgnoreCase(partyChatEvent.getParty()))
player.sendMessage(LocaleLoader.getString("Commands.AdminChatSpy.Chat", partyChatEvent.getParty(), message));
} else {
player.sendMessage(LocaleLoader.getString("Commands.AdminChatSpy.Chat", partyChatEvent.getParty(), message));
}
}
for(int i = 0; i < args.length; i++) {
if(i + 1 >= args.length) {
stringBuilder.append(args[i]);
} else {
stringBuilder.append(args[i]).append(" ");
}
}
return stringBuilder.toString();
}
public void handleChat(String senderName, String message) {
handleChat(senderName, senderName, message, false);
/**
* Whether or not the player is allowed to send a message to the chat channel they are targeting
* @param mmoPlayer target player
* @return true if the player can send messages to that chat channel
*/
public boolean isMessageAllowed(@NotNull McMMOPlayer mmoPlayer) {
switch (mmoPlayer.getChatChannel()) {
case ADMIN:
if(mmoPlayer.getPlayer().isOp() || Permissions.adminChat(mmoPlayer.getPlayer())) {
return true;
}
break;
case PARTY:
if(mmoPlayer.getParty() != null) {
return true;
}
break;
case PARTY_OFFICER:
case NONE:
return false;
}
return false;
}
public void handleChat(Player player, String message, boolean isAsync) {
handleChat(player.getName(), player.getDisplayName(), message, isAsync);
}
public void handleChat(String senderName, String displayName, String message) {
handleChat(senderName, displayName, message, false);
}
public abstract void handleChat(String senderName, String displayName, String message, boolean isAsync);
protected abstract void sendMessage();
}

View File

@ -1,30 +0,0 @@
package com.gmail.nossr50.chat;
import com.gmail.nossr50.datatypes.chat.ChatMode;
import org.bukkit.plugin.Plugin;
import java.util.HashMap;
public class ChatManagerFactory {
private static final HashMap<Plugin, AdminChatManager> adminChatManagers = new HashMap<>();
private static final HashMap<Plugin, PartyChatManager> partyChatManagers = new HashMap<>();
public static ChatManager getChatManager(Plugin plugin, ChatMode mode) {
switch (mode) {
case ADMIN:
if (!adminChatManagers.containsKey(plugin)) {
adminChatManagers.put(plugin, new AdminChatManager(plugin));
}
return adminChatManagers.get(plugin);
case PARTY:
if (!partyChatManagers.containsKey(plugin)) {
partyChatManagers.put(plugin, new PartyChatManager(plugin));
}
return partyChatManagers.get(plugin);
default:
return null;
}
}
}

View File

@ -1,29 +0,0 @@
package com.gmail.nossr50.chat;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.party.Party;
import com.gmail.nossr50.events.chat.McMMOPartyChatEvent;
import com.gmail.nossr50.runnables.party.PartyChatTask;
import org.bukkit.plugin.Plugin;
public class PartyChatManager extends ChatManager {
private Party party;
protected PartyChatManager(Plugin plugin) {
super(plugin, Config.getInstance().getPartyDisplayNames(), Config.getInstance().getPartyChatPrefix());
}
public void setParty(Party party) {
this.party = party;
}
@Override
public void handleChat(String senderName, String displayName, String message, boolean isAsync) {
handleChat(new McMMOPartyChatEvent(plugin, senderName, displayName, party.getName(), message, isAsync));
}
@Override
protected void sendMessage() {
new PartyChatTask(plugin, party, senderName, displayName, message).runTask(plugin);
}
}

View File

@ -0,0 +1,36 @@
package com.gmail.nossr50.chat;
import com.gmail.nossr50.datatypes.party.Party;
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.util.player.UserManager;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import java.util.function.Predicate;
public class SamePartyPredicate<T extends CommandSender> implements Predicate<T> {
final Party party;
public SamePartyPredicate(Party party) {
this.party = party;
}
@Override
public boolean test(T t) {
//Include the console in the audience
if(t instanceof ConsoleCommandSender) {
return true;
} else {
if(t instanceof Player) {
Player player = (Player) t;
McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player);
if(mcMMOPlayer != null) {
return mcMMOPlayer.getParty() == party;
}
}
}
return false;
}
}

View File

@ -0,0 +1,52 @@
package com.gmail.nossr50.chat.author;
import com.gmail.nossr50.config.Config;
import org.bukkit.entity.Player;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.UUID;
public class AdminAuthor implements Author {
private final @NotNull Player player;
private @Nullable String overrideName;
public AdminAuthor(@NotNull Player player) {
this.player = player;
}
@Override
public @NotNull String getAuthoredName() {
if(overrideName != null) {
return overrideName;
} else {
if(Config.getInstance().getAdminDisplayNames()) {
return player.getDisplayName();
} else {
return player.getName();
}
}
}
@Override
public void setName(@NotNull String newName) {
overrideName = newName;
}
@Override
public boolean isConsole() {
return false;
}
@Override
public boolean isPlayer() {
return true;
}
@Override
public @NonNull UUID uuid() {
return player.getUniqueId();
}
}

View File

@ -0,0 +1,32 @@
package com.gmail.nossr50.chat.author;
import net.kyori.adventure.identity.Identity;
import org.jetbrains.annotations.NotNull;
public interface Author extends Identity {
/**
* The name of this author
* @return the name of this author
*/
@NotNull String getAuthoredName();
/**
* Set the name of this author
* @param newName value of the new name
*/
void setName(@NotNull String newName);
/**
* Whether or not this author is a {@link org.bukkit.command.ConsoleCommandSender}
*
* @return true if this author is the console
*/
boolean isConsole();
/**
* Whether or not this author is a {@link org.bukkit.entity.Player}
* @return true if this author is a player
*/
boolean isPlayer();
}

View File

@ -0,0 +1,41 @@
package com.gmail.nossr50.chat.author;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
public class ConsoleAuthor implements Author {
private final UUID uuid;
private @NotNull String name;
public ConsoleAuthor(@NotNull String name) {
this.name = name;
this.uuid = new UUID(0, 0);
}
@Override
public @NotNull String getAuthoredName() {
return name;
}
@Override
public void setName(@NotNull String newName) {
this.name = newName;
}
@Override
public boolean isConsole() {
return true;
}
@Override
public boolean isPlayer() {
return false;
}
@Override
public @NonNull UUID uuid() {
return uuid;
}
}

View File

@ -0,0 +1,52 @@
package com.gmail.nossr50.chat.author;
import com.gmail.nossr50.config.Config;
import org.bukkit.entity.Player;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.UUID;
public class PartyAuthor implements Author {
private final @NotNull Player player;
private @Nullable String overrideName;
public PartyAuthor(@NotNull Player player) {
this.player = player;
}
@Override
public @NotNull String getAuthoredName() {
if(overrideName != null) {
return overrideName;
} else {
if(Config.getInstance().getPartyDisplayNames()) {
return player.getDisplayName();
} else {
return player.getName();
}
}
}
@Override
public void setName(@NotNull String newName) {
overrideName = newName;
}
@Override
public boolean isConsole() {
return false;
}
@Override
public boolean isPlayer() {
return true;
}
@Override
public @NonNull UUID uuid() {
return player.getUniqueId();
}
}

View File

@ -0,0 +1,13 @@
package com.gmail.nossr50.chat.mailer;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
public abstract class AbstractChatMailer implements ChatMailer {
protected final @NotNull Plugin pluginRef;
public AbstractChatMailer(@NotNull Plugin pluginRef) {
this.pluginRef = pluginRef;
}
}

View File

@ -0,0 +1,72 @@
package com.gmail.nossr50.chat.mailer;
import com.gmail.nossr50.chat.author.Author;
import com.gmail.nossr50.chat.message.AdminChatMessage;
import com.gmail.nossr50.chat.message.ChatMessage;
import com.gmail.nossr50.events.chat.McMMOAdminChatEvent;
import com.gmail.nossr50.events.chat.McMMOChatEvent;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.mcMMO;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import java.util.function.Predicate;
public class AdminChatMailer extends AbstractChatMailer {
public AdminChatMailer(Plugin pluginRef) {
super(pluginRef);
}
public static final @NotNull String MCMMO_CHAT_ADMINCHAT_PERMISSION = "mcmmo.chat.adminchat";
/**
* Constructs an audience of admins
* @return an audience of admins
*/
public @NotNull Audience constructAudience() {
return mcMMO.getAudiences().filter(predicate());
}
/**
* Predicate used to filter the audience
* @return admin chat audience predicate
*/
public @NotNull Predicate<CommandSender> predicate() {
return (commandSender) -> commandSender.isOp()
|| commandSender.hasPermission(MCMMO_CHAT_ADMINCHAT_PERMISSION)
|| commandSender instanceof ConsoleCommandSender;
}
/**
* Styles a string using a locale entry
* @param author message author
* @param message message contents
* @return the styled string, based on a locale entry
*/
public @NotNull TextComponent addStyle(@NotNull Author author, @NotNull String message) {
return Component.text(LocaleLoader.getString("Chat.Style.Admin", author.getAuthoredName(), message));
}
@Override
public void sendMail(@NotNull ChatMessage chatMessage) {
chatMessage.sendMessage();
}
public void processChatMessage(@NotNull Author author, @NotNull String rawString, boolean isAsync) {
AdminChatMessage chatMessage = new AdminChatMessage(pluginRef, author, constructAudience(), rawString, addStyle(author, rawString));
McMMOChatEvent chatEvent = new McMMOAdminChatEvent(pluginRef, chatMessage, isAsync);
Bukkit.getPluginManager().callEvent(chatEvent);
if(!chatEvent.isCancelled()) {
sendMail(chatMessage);
}
}
}

View File

@ -0,0 +1,12 @@
package com.gmail.nossr50.chat.mailer;
import com.gmail.nossr50.chat.message.ChatMessage;
import org.jetbrains.annotations.NotNull;
public interface ChatMailer {
/**
* Send out a chat message
* @param chatMessage the {@link ChatMessage}
*/
void sendMail(@NotNull ChatMessage chatMessage);
}

View File

@ -0,0 +1,47 @@
package com.gmail.nossr50.chat.mailer;
import com.gmail.nossr50.chat.author.Author;
import com.gmail.nossr50.chat.message.ChatMessage;
import com.gmail.nossr50.chat.message.PartyChatMessage;
import com.gmail.nossr50.datatypes.party.Party;
import com.gmail.nossr50.events.chat.McMMOChatEvent;
import com.gmail.nossr50.events.chat.McMMOPartyChatEvent;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.mcMMO;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
public class PartyChatMailer extends AbstractChatMailer {
public PartyChatMailer(@NotNull Plugin pluginRef) {
super(pluginRef);
}
public void processChatMessage(@NotNull Author author, @NotNull String rawString, @NotNull Party party, boolean isAsync) {
PartyChatMessage chatMessage = new PartyChatMessage(pluginRef, author, constructPartyAudience(party), rawString, addStyle(author, rawString), party);
McMMOChatEvent chatEvent = new McMMOPartyChatEvent(pluginRef, chatMessage, party, isAsync);
Bukkit.getPluginManager().callEvent(chatEvent);
if(!chatEvent.isCancelled()) {
sendMail(chatMessage);
}
}
public @NotNull Audience constructPartyAudience(@NotNull Party party) {
return mcMMO.getAudiences().filter(party.getSamePartyPredicate());
}
public @NotNull TextComponent addStyle(@NotNull Author author, @NotNull String message) {
return Component.text(LocaleLoader.getString("Chat.Style.Party", author.getAuthoredName(), message));
}
@Override
public void sendMail(@NotNull ChatMessage chatMessage) {
chatMessage.sendMessage();
}
}

View File

@ -0,0 +1,59 @@
package com.gmail.nossr50.chat.message;
import com.gmail.nossr50.chat.author.Author;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
public abstract class AbstractChatMessage implements ChatMessage {
protected final @NotNull Plugin pluginRef;
protected final @NotNull Author author;
protected final @NotNull String rawMessage;
protected @NotNull TextComponent componentMessage;
protected @NotNull Audience audience;
public AbstractChatMessage(@NotNull Plugin pluginRef, @NotNull Author author, @NotNull Audience audience, @NotNull String rawMessage, @NotNull TextComponent componentMessage) {
this.pluginRef = pluginRef;
this.author = author;
this.audience = audience;
this.rawMessage = rawMessage;
this.componentMessage = componentMessage;
}
@Override
public @NotNull String rawMessage() {
return rawMessage;
}
@Override
public @NotNull Author getAuthor() {
return author;
}
@Override
public @NotNull String getAuthorDisplayName() {
return author.getAuthoredName();
}
@Override
public @NotNull Audience getAudience() {
return audience;
}
@Override
public @NotNull TextComponent getChatMessage() {
return componentMessage;
}
@Override
public void setChatMessage(@NotNull TextComponent textComponent) {
this.componentMessage = textComponent;
}
@Override
public void setAudience(@NotNull Audience newAudience) {
audience = newAudience;
}
}

View File

@ -0,0 +1,18 @@
package com.gmail.nossr50.chat.message;
import com.gmail.nossr50.chat.author.Author;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
public class AdminChatMessage extends AbstractChatMessage {
public AdminChatMessage(@NotNull Plugin pluginRef, @NotNull Author author, @NotNull Audience audience, @NotNull String rawMessage, @NotNull TextComponent componentMessage) {
super(pluginRef, author, audience, rawMessage, componentMessage);
}
@Override
public void sendMessage() {
audience.sendMessage(author, componentMessage);
}
}

View File

@ -0,0 +1,73 @@
package com.gmail.nossr50.chat.message;
import com.gmail.nossr50.chat.author.Author;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.TextComponent;
import org.jetbrains.annotations.NotNull;
public interface ChatMessage {
/**
* The original message from the {@link Author}
* This is formatted and styled before being sent out to players by mcMMO
*
* @return the original message without any formatting or alterations
* @see #getChatMessage()
*/
@NotNull String rawMessage();
/**
* The {@link Author} from which this payload originated
*
* @see #getChatMessage()
* @return the source of the chat message
*/
@NotNull Author getAuthor();
/**
* The authors display name which is used in the initial creation of the message payload, it is provided for convenience.
*
* This is a name generated by mcMMO during the creation of the {@link ChatMessage}
*
* This is used by mcMMO when generating the message payload
*
* This method provides the display name for the convenience of plugins constructing their own {@link TextComponent payloads}
*
* @see #getChatMessage()
* @return the author display name as generated by mcMMO
*/
@NotNull String getAuthorDisplayName();
/**
* The target audience of this chat message
* Unless modified, this will include the {@link Author}
*
* @return target audience
*/
@NotNull Audience getAudience();
/**
* The {@link TextComponent message} being sent to the audience
*
* @return the {@link TextComponent message} that will be sent to the audience
*/
@NotNull TextComponent getChatMessage();
/**
* Change the value of the {@link TextComponent message}
*
* @param textComponent new message value
*/
void setChatMessage(@NotNull TextComponent textComponent);
/**
* Changes the audience
*
* @param newAudience the replacement audience
*/
void setAudience(@NotNull Audience newAudience);
/**
* Deliver the message to the audience
*/
void sendMessage();
}

View File

@ -0,0 +1,63 @@
package com.gmail.nossr50.chat.message;
import com.gmail.nossr50.chat.author.Author;
import com.gmail.nossr50.datatypes.party.Party;
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.player.UserManager;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
public class PartyChatMessage extends AbstractChatMessage {
private final @NotNull Party party;
public PartyChatMessage(@NotNull Plugin pluginRef, @NotNull Author author, @NotNull Audience audience, @NotNull String rawMessage, @NotNull TextComponent componentMessage, @NotNull Party party) {
super(pluginRef, author, audience, rawMessage, componentMessage);
this.party = party;
}
/**
* The party that this chat message was intended for
* @return the party that this message was intended for
*/
public @NotNull Party getParty() {
return party;
}
@Override
public void sendMessage() {
audience.sendMessage(author, componentMessage);
//Relay to spies
TextComponent textComponent = Component.text("[" + getParty().getName() + "] ->" ).append(getChatMessage());
relayChatToSpies(textComponent);
}
/**
* Party Chat Spies will get a copy of the message as well
* @param spyMessage the message to copy to spies
*/
private void relayChatToSpies(@NotNull TextComponent spyMessage) {
//Find the people with permissions
for(McMMOPlayer mcMMOPlayer : UserManager.getPlayers()) {
Player player = mcMMOPlayer.getPlayer();
//Check for toggled players
if(mcMMOPlayer.isPartyChatSpying()) {
Party adminParty = mcMMOPlayer.getParty();
//Only message admins not part of this party
if(adminParty == null || adminParty != getParty()) {
//TODO: Hacky, rewrite later
Audience audience = mcMMO.getAudiences().player(player);
audience.sendMessage(spyMessage);
}
}
}
}
}