mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 22:15:45 +00:00
Add component event
This commit is contained in:
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.draycia.simplechat.storage.ChatUser;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.format.TextColor;
|
import net.kyori.adventure.text.format.TextColor;
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.javacord.api.event.message.MessageCreateEvent;
|
import org.javacord.api.event.message.MessageCreateEvent;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -109,7 +108,7 @@ public abstract class ChatChannel {
|
|||||||
|
|
||||||
public abstract boolean canPlayerSee(ChatUser sender, ChatUser target);
|
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
|
* @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 me.clip.placeholderapi.PlaceholderAPI;
|
||||||
import net.draycia.simplechat.SimpleChat;
|
import net.draycia.simplechat.SimpleChat;
|
||||||
import net.draycia.simplechat.channels.ChatChannel;
|
import net.draycia.simplechat.channels.ChatChannel;
|
||||||
|
import net.draycia.simplechat.events.ChatComponentEvent;
|
||||||
import net.draycia.simplechat.events.ChatFormatEvent;
|
import net.draycia.simplechat.events.ChatFormatEvent;
|
||||||
import net.draycia.simplechat.storage.ChatUser;
|
import net.draycia.simplechat.storage.ChatUser;
|
||||||
import net.draycia.simplechat.util.DiscordWebhook;
|
import net.draycia.simplechat.util.DiscordWebhook;
|
||||||
@@ -99,12 +100,12 @@ public class SimpleChatChannel extends ChatChannel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Player> getAudience(ChatUser user) {
|
public List<ChatUser> getAudience(ChatUser user) {
|
||||||
List<Player> audience = new ArrayList<>();
|
List<ChatUser> audience = new ArrayList<>();
|
||||||
|
|
||||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||||
if (canPlayerSee(user, simpleChat.getUserService().wrap(player))) {
|
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(),
|
"username", author.getName(), "displayname", author.getDisplayName(), "channel", channel.getName(),
|
||||||
"server", event.getServer().get().getName(), "primaryrole", role);
|
"server", event.getServer().get().getName(), "primaryrole", role);
|
||||||
|
|
||||||
for (Player player : getAudience(null)) {
|
for (ChatUser user : getAudience(null)) {
|
||||||
simpleChat.getAudiences().player(player).sendMessage(component);
|
user.asAudience().sendMessage(component);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,45 +194,52 @@ public class SimpleChatChannel extends ChatChannel {
|
|||||||
Component component = LegacyComponentSerializer.legacy('&').deserialize(messageFormat);
|
Component component = LegacyComponentSerializer.legacy('&').deserialize(messageFormat);
|
||||||
messageFormat = MiniMessage.instance().serialize(component);
|
messageFormat = MiniMessage.instance().serialize(component);
|
||||||
|
|
||||||
// Send message to players who can see it
|
// Get formatted message
|
||||||
Component formattedMessage = MiniMessage.instance().parse(messageFormat, "color", "<" + color.toString() + ">",
|
TextComponent formattedMessage = (TextComponent)MiniMessage.instance().parse(messageFormat, "color", "<" + color.toString() + ">",
|
||||||
"phase", Long.toString(System.currentTimeMillis() % 25), "server",
|
"phase", Long.toString(System.currentTimeMillis() % 25), "server",
|
||||||
simpleChat.getConfig().getString("server-name", "Server"),
|
simpleChat.getConfig().getString("server-name", "Server"),
|
||||||
"message", formatEvent.getMessage());
|
"message", formatEvent.getMessage());
|
||||||
|
|
||||||
// Handle item linking placeholders
|
// Handle item linking placeholders
|
||||||
if (formattedMessage instanceof TextComponent && user.isOnline()) {
|
if (user.isOnline()) {
|
||||||
|
// TODO: move this into a ChatComponentEvent listener
|
||||||
for (Pattern pattern : itemPatterns) {
|
for (Pattern pattern : itemPatterns) {
|
||||||
formattedMessage = ((TextComponent) formattedMessage).replace(pattern, (input) -> {
|
formattedMessage = (formattedMessage).replace(pattern, (input) -> {
|
||||||
return TextComponent.builder().append(simpleChat.getItemStackUtils().createComponent(user.asPlayer()));
|
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
|
// Handle shadow mutes
|
||||||
if (user.isShadowMuted()) {
|
if (user.isShadowMuted()) {
|
||||||
|
// TODO: move this into a ChatComponentEvent listener
|
||||||
if (user.isOnline()) {
|
if (user.isOnline()) {
|
||||||
user.asAudience().sendMessage(formattedMessage);
|
user.asAudience().sendMessage(componentEvent.getComponent());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Send message as normal
|
// Send message as normal
|
||||||
List<Player> receivers = getAudience(user);
|
for (ChatUser chatUser : componentEvent.getRecipients()) {
|
||||||
|
if (message.contains(chatUser.asPlayer().getName())) {
|
||||||
for (Player onlinePlayer : receivers) {
|
|
||||||
Audience audience = simpleChat.getAudiences().player(onlinePlayer);
|
|
||||||
|
|
||||||
if (message.contains(onlinePlayer.getName())) {
|
|
||||||
if (simpleChat.getConfig().getBoolean("pings.enabled")) {
|
if (simpleChat.getConfig().getBoolean("pings.enabled")) {
|
||||||
Key key = Key.of(simpleChat.getConfig().getString("pings.sound"));
|
Key key = Key.of(simpleChat.getConfig().getString("pings.sound"));
|
||||||
Sound.Source source = Sound.Source.valueOf(simpleChat.getConfig().getString("pings.source"));
|
Sound.Source source = Sound.Source.valueOf(simpleChat.getConfig().getString("pings.source"));
|
||||||
float volume = (float)simpleChat.getConfig().getDouble("pings.volume");
|
float volume = (float)simpleChat.getConfig().getDouble("pings.volume");
|
||||||
float pitch = (float)simpleChat.getConfig().getDouble("pings.pitch");
|
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
|
@Override
|
||||||
public void sendComponent(ChatUser player, Component component) {
|
public void sendComponent(ChatUser player, Component component) {
|
||||||
for (Player onlinePlayer : getAudience(player)) {
|
for (ChatUser user : getAudience(player)) {
|
||||||
simpleChat.getAudiences().player(onlinePlayer).sendMessage(component);
|
user.asAudience().sendMessage(component);
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println(LegacyComponentSerializer.legacy().serialize(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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user