mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2025-06-30 04:34:43 +02:00
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:
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
12
src/main/java/com/gmail/nossr50/chat/mailer/ChatMailer.java
Normal file
12
src/main/java/com/gmail/nossr50/chat/mailer/ChatMailer.java
Normal 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);
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user