Add component event

This commit is contained in:
Draycia committed 2020-07-01 14:23:31 -07:00
1 parent 2d14d42f64
commit 0483720640
3 files changed
+104 -22

No files matched your search

@@ -5,7 +5,6 @@ import net.draycia.simplechat.events.ChatFormatEvent;
import net.draycia.simplechat.storage.ChatUser;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.entity.Player;
import org.javacord.api.event.message.MessageCreateEvent;
import java.util.List;
@@ -109,7 +108,7 @@ public abstract class ChatChannel {
public abstract boolean canPlayerSee(ChatUser sender, ChatUser target);
public abstract List<Player> getAudience(ChatUser user);
public abstract List<ChatUser> getAudience(ChatUser user);
/**
* @return If the channel should forward its formatting / formatted message to other servers
@@ -3,6 +3,7 @@ package net.draycia.simplechat.channels.impls;
import me.clip.placeholderapi.PlaceholderAPI;
import net.draycia.simplechat.SimpleChat;
import net.draycia.simplechat.channels.ChatChannel;
import net.draycia.simplechat.events.ChatComponentEvent;
import net.draycia.simplechat.events.ChatFormatEvent;
import net.draycia.simplechat.storage.ChatUser;
import net.draycia.simplechat.util.DiscordWebhook;
@@ -99,12 +100,12 @@ public class SimpleChatChannel extends ChatChannel {
}
@Override
public List<Player> getAudience(ChatUser user) {
List<Player> audience = new ArrayList<>();
public List<ChatUser> getAudience(ChatUser user) {
List<ChatUser> audience = new ArrayList<>();
for (Player player : Bukkit.getOnlinePlayers()) {
if (canPlayerSee(user, simpleChat.getUserService().wrap(player))) {
audience.add(player);
audience.add(simpleChat.getUserService().wrap(player));
}
}
@@ -138,8 +139,8 @@ public class SimpleChatChannel extends ChatChannel {
"username", author.getName(), "displayname", author.getDisplayName(), "channel", channel.getName(),
"server", event.getServer().get().getName(), "primaryrole", role);
for (Player player : getAudience(null)) {
simpleChat.getAudiences().player(player).sendMessage(component);
for (ChatUser user : getAudience(null)) {
user.asAudience().sendMessage(component);
}
}
@@ -193,45 +194,52 @@ public class SimpleChatChannel extends ChatChannel {
Component component = LegacyComponentSerializer.legacy('&').deserialize(messageFormat);
messageFormat = MiniMessage.instance().serialize(component);
// Send message to players who can see it
Component formattedMessage = MiniMessage.instance().parse(messageFormat, "color", "<" + color.toString() + ">",
// Get formatted message
TextComponent formattedMessage = (TextComponent)MiniMessage.instance().parse(messageFormat, "color", "<" + color.toString() + ">",
"phase", Long.toString(System.currentTimeMillis() % 25), "server",
simpleChat.getConfig().getString("server-name", "Server"),
"message", formatEvent.getMessage());
// Handle item linking placeholders
if (formattedMessage instanceof TextComponent && user.isOnline()) {
if (user.isOnline()) {
// TODO: move this into a ChatComponentEvent listener
for (Pattern pattern : itemPatterns) {
formattedMessage = ((TextComponent) formattedMessage).replace(pattern, (input) -> {
formattedMessage = (formattedMessage).replace(pattern, (input) -> {
return TextComponent.builder().append(simpleChat.getItemStackUtils().createComponent(user.asPlayer()));
});
}
}
// Call custom chat event
ChatComponentEvent componentEvent = new ChatComponentEvent(user, this, formattedMessage, getAudience(user));
Bukkit.getPluginManager().callEvent(componentEvent);
if (componentEvent.isCancelled()) {
return;
}
// Handle shadow mutes
if (user.isShadowMuted()) {
// TODO: move this into a ChatComponentEvent listener
if (user.isOnline()) {
user.asAudience().sendMessage(formattedMessage);
user.asAudience().sendMessage(componentEvent.getComponent());
}
} else {
// Send message as normal
List<Player> receivers = getAudience(user);
for (Player onlinePlayer : receivers) {
Audience audience = simpleChat.getAudiences().player(onlinePlayer);
if (message.contains(onlinePlayer.getName())) {
for (ChatUser chatUser : componentEvent.getRecipients()) {
if (message.contains(chatUser.asPlayer().getName())) {
if (simpleChat.getConfig().getBoolean("pings.enabled")) {
Key key = Key.of(simpleChat.getConfig().getString("pings.sound"));
Sound.Source source = Sound.Source.valueOf(simpleChat.getConfig().getString("pings.source"));
float volume = (float)simpleChat.getConfig().getDouble("pings.volume");
float pitch = (float)simpleChat.getConfig().getDouble("pings.pitch");
audience.playSound(Sound.of(key, source, volume, pitch));
chatUser.asAudience().playSound(Sound.of(key, source, volume, pitch));
}
}
audience.sendMessage(formattedMessage);
chatUser.asAudience().sendMessage(componentEvent.getComponent());
}
}
@@ -255,8 +263,8 @@ public class SimpleChatChannel extends ChatChannel {
@Override
public void sendComponent(ChatUser player, Component component) {
for (Player onlinePlayer : getAudience(player)) {
simpleChat.getAudiences().player(onlinePlayer).sendMessage(component);
for (ChatUser user : getAudience(player)) {
user.asAudience().sendMessage(component);
}
System.out.println(LegacyComponentSerializer.legacy().serialize(component));
@@ -0,0 +1,75 @@
package net.draycia.simplechat.events;
import net.draycia.simplechat.channels.ChatChannel;
import net.draycia.simplechat.storage.ChatUser;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class ChatComponentEvent extends Event implements Cancellable {
private static final HandlerList HANDLERS_LIST = new HandlerList();
private boolean isCancelled = false;
private ChatUser user;
private ChatChannel chatChannel;
private TextComponent component;
private List<ChatUser> recipients;
public ChatComponentEvent(ChatUser user, ChatChannel chatChannel, TextComponent component, List<ChatUser> recipients) {
super(true);
this.user = user;
this.chatChannel = chatChannel;
this.component = component;
this.recipients = recipients;
}
@Override
public boolean isCancelled() {
return isCancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.isCancelled = cancelled;
}
@Override
@NotNull
public HandlerList getHandlers() {
return HANDLERS_LIST;
}
public static HandlerList getHandlerList() {
return HANDLERS_LIST;
}
public ChatUser getUser() {
return user;
}
public ChatChannel getChatChannel() {
return chatChannel;
}
public void setChatChannel(ChatChannel chatChannel) {
this.chatChannel = chatChannel;
}
public TextComponent getComponent() {
return component;
}
public void setComponent(TextComponent component) {
this.component = component;
}
public List<ChatUser> getRecipients() {
return recipients;
}
}