mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 06:28:54 +00:00
Merge pull request #14 from Draycia/refactor/channels
Refactor channels
This commit is contained in:
32 files changed
+356
-242
No files matched your search
@@ -86,13 +86,14 @@ public final class CarbonChat extends JavaPlugin {
|
||||
|
||||
// Register chat listeners
|
||||
pluginManager.registerEvents(new BukkitChatListener(this), this);
|
||||
pluginManager.registerEvents(new EmptyChatHandler(this), this);
|
||||
pluginManager.registerEvents(new IgnoredPlayerHandler(), this);
|
||||
pluginManager.registerEvents(new ItemLinkHandler(this), this);
|
||||
pluginManager.registerEvents(new LegacyFormatHandler(), this);
|
||||
pluginManager.registerEvents(new OfflineNameHandler(), this);
|
||||
pluginManager.registerEvents(new PingHandler(this), this);
|
||||
pluginManager.registerEvents(new PlaceholderHandler(), this);
|
||||
pluginManager.registerEvents(new PlayerJoinListener(this), this);
|
||||
pluginManager.registerEvents(new RelationalPlaceholderHandler(), this);
|
||||
pluginManager.registerEvents(new UserFormattingListener(), this);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package net.draycia.carbon.channels;
|
||||
|
||||
import net.draycia.carbon.events.ChatFormatEvent;
|
||||
import net.draycia.carbon.events.PreChatFormatEvent;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.kyori.adventure.audience.ForwardingAudience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -9,7 +10,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public abstract class ChatChannel {
|
||||
public abstract class ChatChannel implements ForwardingAudience {
|
||||
|
||||
/**
|
||||
* @return The color that represents this channel. Optionally used in formatting.
|
||||
@@ -52,6 +53,8 @@ public abstract class ChatChannel {
|
||||
|
||||
public abstract String getSwitchOtherMessage();
|
||||
|
||||
public abstract String getSwitchFailureMessage();
|
||||
|
||||
/**
|
||||
* @return The message to be send to the player when toggling this channel off.
|
||||
*/
|
||||
@@ -83,8 +86,6 @@ public abstract class ChatChannel {
|
||||
|
||||
public abstract Boolean canPlayerSee(ChatUser sender, ChatUser target, boolean checkSpying);
|
||||
|
||||
public abstract List<ChatUser> getAudience(ChatUser user);
|
||||
|
||||
/**
|
||||
* @return If the channel should forward its formatting / formatted message to other servers
|
||||
*/
|
||||
@@ -95,7 +96,7 @@ public abstract class ChatChannel {
|
||||
public abstract List<Pattern> getItemLinkPatterns();
|
||||
|
||||
/**
|
||||
* Parses the specified message, calls a {@link ChatFormatEvent}, and sends the message to everyone who can view this channel.
|
||||
* Parses the specified message, calls a {@link PreChatFormatEvent}, and sends the message to everyone who can view this channel.
|
||||
* @param user The player who is saying the message.
|
||||
* @param message The message to be sent.
|
||||
*/
|
||||
|
||||
@@ -4,10 +4,12 @@ import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.events.ChatComponentEvent;
|
||||
import net.draycia.carbon.events.ChatFormatEvent;
|
||||
import net.draycia.carbon.events.PreChatFormatEvent;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -44,108 +46,109 @@ public class CarbonChatChannel extends ChatChannel {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ChatUser> getAudience(ChatUser user) {
|
||||
public List<ChatUser> audiences() {
|
||||
List<ChatUser> audience = new ArrayList<>();
|
||||
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
if (canPlayerSee(user, carbonChat.getUserService().wrap(player), false)) {
|
||||
audience.add(carbonChat.getUserService().wrap(player));
|
||||
ChatUser playerUser = carbonChat.getUserService().wrap(player);
|
||||
|
||||
if (canPlayerSee(playerUser, true)) {
|
||||
audience.add(playerUser);
|
||||
}
|
||||
}
|
||||
|
||||
return audience;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(ChatUser user, String message, boolean fromBungee) {
|
||||
private void updateUserNickname(ChatUser user) {
|
||||
if (user.isOnline()) {
|
||||
if (user.getNickname() != null) {
|
||||
user.asPlayer().setDisplayName(user.getNickname());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private TextColor getColor(ChatUser user) {
|
||||
TextColor userColor = user.getChannelSettings(this).getColor();
|
||||
|
||||
if (userColor != null) {
|
||||
return userColor;
|
||||
} else {
|
||||
return getColor();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(ChatUser user, String message, boolean fromRemote) {
|
||||
updateUserNickname(user);
|
||||
|
||||
// Get player's formatting
|
||||
String messageFormat = getFormat(user);
|
||||
|
||||
// Call custom chat event
|
||||
ChatFormatEvent formatEvent = new ChatFormatEvent(user, this, messageFormat, message);
|
||||
PreChatFormatEvent preFormatEvent = new PreChatFormatEvent(user, this, messageFormat, message);
|
||||
Bukkit.getPluginManager().callEvent(preFormatEvent);
|
||||
|
||||
Bukkit.getPluginManager().callEvent(formatEvent);
|
||||
|
||||
if (formatEvent.isCancelled() || formatEvent.getMessage().isEmpty()) {
|
||||
// Return if cancelled or message is emptied
|
||||
if (preFormatEvent.isCancelled() || preFormatEvent.getMessage().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get formatted message
|
||||
TextComponent formattedMessage = (TextComponent) carbonChat.getAdventureManager().processMessage(formatEvent.getFormat(),
|
||||
// Iterate through players who should receive messages in this channel
|
||||
for (ChatUser target : audiences()) {
|
||||
// Call second format event. Used for relational stuff (placeholders etc)
|
||||
ChatFormatEvent formatEvent = new ChatFormatEvent(user, target, this, preFormatEvent.getFormat(), preFormatEvent.getMessage());
|
||||
Bukkit.getPluginManager().callEvent(formatEvent);
|
||||
|
||||
// Afain, return if cancelled or message is emptied
|
||||
if (formatEvent.isCancelled() || formatEvent.getMessage().isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
TextComponent formattedMessage = (TextComponent) carbonChat.getAdventureManager().processMessage(formatEvent.getFormat(),
|
||||
"br", "\n",
|
||||
"color", "<" + getColor(target).asHexString() + ">",
|
||||
"phase", Long.toString(System.currentTimeMillis() % 25),
|
||||
"server", carbonChat.getConfig().getString("server-name", "Server"),
|
||||
"message", formatEvent.getMessage());
|
||||
|
||||
if (isUserSpying(user, target)) {
|
||||
String prefix = processPlaceholders(user, carbonChat.getConfig().getString("spy-prefix"));
|
||||
|
||||
formattedMessage = (TextComponent) MiniMessage.get().parse(prefix, "color",
|
||||
getColor(target).asHexString()).append(formattedMessage);
|
||||
}
|
||||
|
||||
ChatComponentEvent newEvent = new ChatComponentEvent(user, target, this, formattedMessage,
|
||||
formatEvent.getMessage());
|
||||
|
||||
Bukkit.getPluginManager().callEvent(newEvent);
|
||||
|
||||
target.sendMessage(newEvent.getComponent());
|
||||
}
|
||||
|
||||
TextComponent consoleMessage = (TextComponent) carbonChat.getAdventureManager().processMessage(preFormatEvent.getFormat(),
|
||||
"br", "\n",
|
||||
"color", "<color:" + getColor().toString() + ">",
|
||||
"color", "<" + getColor().asHexString() + ">",
|
||||
"phase", Long.toString(System.currentTimeMillis() % 25),
|
||||
"server", carbonChat.getConfig().getString("server-name", "Server"),
|
||||
"message", formatEvent.getMessage());
|
||||
"message", preFormatEvent.getMessage());
|
||||
|
||||
// Call custom chat event
|
||||
ChatComponentEvent componentEvent = new ChatComponentEvent(user, this, formattedMessage,
|
||||
formatEvent.getMessage(), getAudience(user));
|
||||
ChatComponentEvent consoleEvent = new ChatComponentEvent(user, null, this, consoleMessage,
|
||||
preFormatEvent.getMessage());
|
||||
|
||||
Bukkit.getPluginManager().callEvent(componentEvent);
|
||||
|
||||
// Send message as normal
|
||||
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
|
||||
ChatUser chatUser = carbonChat.getUserService().wrap(onlinePlayer);
|
||||
|
||||
TextColor userColor = chatUser.getChannelSettings(this).getColor();
|
||||
|
||||
String prefix = processPlaceholders(user, carbonChat.getConfig().getString("spy-prefix"));
|
||||
|
||||
// TODO: restructure plugin to always format messages per player
|
||||
|
||||
if (userColor == null) {
|
||||
if (isUserSpying(user, chatUser)) {
|
||||
prefix = prefix.replace("<color>", "<color:" + getColor() + ">");
|
||||
|
||||
chatUser.sendMessage(carbonChat.getAdventureManager().processMessage(prefix).append(componentEvent.getComponent()));
|
||||
} else if (componentEvent.getRecipients().contains(chatUser)) {
|
||||
chatUser.sendMessage(componentEvent.getComponent());
|
||||
}
|
||||
} else {
|
||||
prefix = prefix.replace("<color>", "<color:" +
|
||||
chatUser.getChannelSettings(this).getColor().asHexString() + ">");
|
||||
String format = formatEvent.getFormat();
|
||||
|
||||
TextComponent newFormat = (TextComponent) carbonChat.getAdventureManager().processMessage(format,
|
||||
"br", "\n",
|
||||
"color", "<color:" + userColor.toString() + ">",
|
||||
"phase", Long.toString(System.currentTimeMillis() % 25),
|
||||
"server", carbonChat.getConfig().getString("server-name", "Server"),
|
||||
"message", formatEvent.getMessage());
|
||||
|
||||
if (isUserSpying(user, chatUser)) {
|
||||
newFormat = (TextComponent) carbonChat.getAdventureManager().processMessage(prefix, "br", "\n")
|
||||
.append(newFormat);
|
||||
} else if (!componentEvent.getRecipients().contains(chatUser)) {
|
||||
return;
|
||||
}
|
||||
|
||||
ChatComponentEvent newEvent = new ChatComponentEvent(user, this, newFormat,
|
||||
formatEvent.getMessage(), Collections.singletonList(chatUser), true);
|
||||
|
||||
Bukkit.getPluginManager().callEvent(newEvent);
|
||||
|
||||
chatUser.sendMessage(newEvent.getComponent());
|
||||
}
|
||||
}
|
||||
Bukkit.getPluginManager().callEvent(consoleEvent);
|
||||
|
||||
// Log message to console
|
||||
String sm = user.isShadowMuted() ? "[SM] " : "";
|
||||
System.out.println(sm + CarbonChat.LEGACY.serialize(componentEvent.getComponent()));
|
||||
System.out.println(sm + CarbonChat.LEGACY.serialize(consoleEvent.getComponent()));
|
||||
|
||||
// Route message to bungee / discord (if message originates from this server)
|
||||
// Use instanceof and not isOnline, if this message originates from another then the instanceof will
|
||||
// fail, but isOnline may succeed if the player is online on both servers (somehow).
|
||||
if (user.isOnline() && !fromBungee && shouldBungee()) {
|
||||
if (user.isOnline() && !fromRemote && shouldBungee()) {
|
||||
if (shouldForwardFormatting()) {
|
||||
sendMessageToBungee(user.asPlayer(), componentEvent.getComponent());
|
||||
sendMessageToBungee(user.asPlayer(), consoleEvent.getComponent());
|
||||
} else {
|
||||
sendMessageToBungee(user.asPlayer(), message);
|
||||
}
|
||||
@@ -217,7 +220,7 @@ public class CarbonChatChannel extends ChatChannel {
|
||||
|
||||
@Override
|
||||
public void sendComponent(ChatUser player, Component component) {
|
||||
for (ChatUser user : getAudience(player)) {
|
||||
for (ChatUser user : audiences()) {
|
||||
if (!user.isIgnoringUser(player)) {
|
||||
user.sendMessage(component);
|
||||
}
|
||||
@@ -234,6 +237,26 @@ public class CarbonChatChannel extends ChatChannel {
|
||||
carbonChat.getPluginMessageManager().sendMessage(this, player, message);
|
||||
}
|
||||
|
||||
public Boolean canPlayerSee(ChatUser target, boolean checkSpying) {
|
||||
Player targetPlayer = target.asPlayer();
|
||||
|
||||
if (checkSpying && targetPlayer.hasPermission("carbonchat.spy." + getName())) {
|
||||
if (target.getChannelSettings(this).isSpying()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!targetPlayer.hasPermission("carbonchat.channels." + getName() + ".see")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isIgnorable()) {
|
||||
return !target.getChannelSettings(this).isIgnored();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public Boolean canPlayerSee(ChatUser sender, ChatUser target, boolean checkSpying) {
|
||||
Player targetPlayer = target.asPlayer();
|
||||
|
||||
@@ -310,6 +333,11 @@ public class CarbonChatChannel extends ChatChannel {
|
||||
return getString("switch-other-message");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSwitchFailureMessage() {
|
||||
return getString("switch-failure-message");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getToggleOffMessage() {
|
||||
return getString("toggle-off-message");
|
||||
|
||||
@@ -24,8 +24,8 @@ public class ChannelRegisterEvent extends Event {
|
||||
}
|
||||
|
||||
/** Relevant stuff **/
|
||||
private List<ChatChannel> registeredChannels;
|
||||
private Registry<ChatChannel> registry;
|
||||
private final List<ChatChannel> registeredChannels;
|
||||
private final Registry<ChatChannel> registry;
|
||||
|
||||
public ChannelRegisterEvent(List<ChatChannel> registeredChannels, Registry<ChatChannel> registry) {
|
||||
super(!Bukkit.isPrimaryThread());
|
||||
|
||||
@@ -34,8 +34,8 @@ public class ChannelSwitchEvent extends Event implements Cancellable {
|
||||
}
|
||||
|
||||
/** Relevant stuff **/
|
||||
private ChatChannel channel;
|
||||
private ChatUser user;
|
||||
private final ChatChannel channel;
|
||||
private final ChatUser user;
|
||||
private String failureMessage;
|
||||
|
||||
public ChannelSwitchEvent(ChatChannel channel, ChatUser user, String failureMessage) {
|
||||
|
||||
@@ -8,34 +8,27 @@ import org.bukkit.event.Cancellable;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class ChatComponentEvent extends Event implements Cancellable {
|
||||
|
||||
private static final HandlerList HANDLERS_LIST = new HandlerList();
|
||||
private boolean isCancelled = false;
|
||||
|
||||
private ChatUser user;
|
||||
private @NotNull final ChatUser sender;
|
||||
private @Nullable final ChatUser target;
|
||||
private ChatChannel chatChannel;
|
||||
private TextComponent component;
|
||||
private String originalMessage;
|
||||
private List<ChatUser> recipients;
|
||||
private boolean customPlayerFormat;
|
||||
private final String originalMessage;
|
||||
|
||||
public ChatComponentEvent(ChatUser user, ChatChannel chatChannel, TextComponent component, String originalMessage, List<ChatUser> recipients) {
|
||||
this(user, chatChannel, component, originalMessage, recipients, false);
|
||||
}
|
||||
|
||||
public ChatComponentEvent(ChatUser user, ChatChannel chatChannel, TextComponent component, String originalMessage, List<ChatUser> recipients, boolean customPlayerFormat) {
|
||||
public ChatComponentEvent(ChatUser sender, ChatUser target, ChatChannel chatChannel, TextComponent component, String originalMessage) {
|
||||
super(!Bukkit.isPrimaryThread());
|
||||
|
||||
this.user = user;
|
||||
this.sender = sender;
|
||||
this.target = target;
|
||||
this.chatChannel = chatChannel;
|
||||
this.component = component;
|
||||
this.originalMessage = originalMessage;
|
||||
this.recipients = recipients;
|
||||
this.customPlayerFormat = customPlayerFormat;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -58,8 +51,14 @@ public class ChatComponentEvent extends Event implements Cancellable {
|
||||
return HANDLERS_LIST;
|
||||
}
|
||||
|
||||
public ChatUser getUser() {
|
||||
return user;
|
||||
@NotNull
|
||||
public ChatUser getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public ChatUser getTarget() {
|
||||
return target;
|
||||
}
|
||||
|
||||
public ChatChannel getChannel() {
|
||||
@@ -78,15 +77,8 @@ public class ChatComponentEvent extends Event implements Cancellable {
|
||||
this.component = component;
|
||||
}
|
||||
|
||||
public List<ChatUser> getRecipients() {
|
||||
return recipients;
|
||||
}
|
||||
|
||||
public String getOriginalMessage() {
|
||||
return originalMessage;
|
||||
}
|
||||
|
||||
public boolean isCustomPlayerFormat() {
|
||||
return customPlayerFormat;
|
||||
}
|
||||
}
|
||||
@@ -13,15 +13,19 @@ public class ChatFormatEvent extends Event implements Cancellable {
|
||||
private static final HandlerList HANDLERS_LIST = new HandlerList();
|
||||
private boolean isCancelled = false;
|
||||
|
||||
private final ChatUser user;
|
||||
private final ChatUser sender;
|
||||
private final ChatUser target;
|
||||
|
||||
private ChatChannel chatChannel;
|
||||
private String format;
|
||||
private String message;
|
||||
|
||||
public ChatFormatEvent(ChatUser user, ChatChannel chatChannel, String format, String message) {
|
||||
public ChatFormatEvent(ChatUser sender, ChatUser target, ChatChannel chatChannel, String format, String message) {
|
||||
super(!Bukkit.isPrimaryThread());
|
||||
|
||||
this.user = user;
|
||||
this.sender = sender;
|
||||
this.target = target;
|
||||
|
||||
this.chatChannel = chatChannel;
|
||||
this.format = format;
|
||||
this.message = message;
|
||||
@@ -47,8 +51,12 @@ public class ChatFormatEvent extends Event implements Cancellable {
|
||||
return HANDLERS_LIST;
|
||||
}
|
||||
|
||||
public ChatUser getUser() {
|
||||
return user;
|
||||
public ChatUser getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
public ChatUser getTarget() {
|
||||
return target;
|
||||
}
|
||||
|
||||
public ChatChannel getChannel() {
|
||||
@@ -74,5 +82,4 @@ public class ChatFormatEvent extends Event implements Cancellable {
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package net.draycia.carbon.events;
|
||||
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.Cancellable;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class PreChatFormatEvent extends Event implements Cancellable {
|
||||
|
||||
private static final HandlerList HANDLERS_LIST = new HandlerList();
|
||||
private boolean isCancelled = false;
|
||||
|
||||
private final ChatUser user;
|
||||
private ChatChannel chatChannel;
|
||||
private String format;
|
||||
private String message;
|
||||
|
||||
public PreChatFormatEvent(ChatUser user, ChatChannel chatChannel, String format, String message) {
|
||||
super(!Bukkit.isPrimaryThread());
|
||||
|
||||
this.user = user;
|
||||
this.chatChannel = chatChannel;
|
||||
this.format = format;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@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 getChannel() {
|
||||
return chatChannel;
|
||||
}
|
||||
|
||||
public void setChannel(ChatChannel chatChannel) {
|
||||
this.chatChannel = chatChannel;
|
||||
}
|
||||
|
||||
public String getFormat() {
|
||||
return format;
|
||||
}
|
||||
|
||||
public void setFormat(String format) {
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.events.ChatComponentEvent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class EmptyChatHandler implements Listener {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
|
||||
public EmptyChatHandler(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onComponent(ChatComponentEvent event) {
|
||||
if (event.isCustomPlayerFormat()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.getRecipients().size() == 1 && event.getUser().equals(event.getRecipients().get(0))) {
|
||||
String message = carbonChat.getConfig().getString("language.empty-channel");
|
||||
|
||||
if (message == null || message.isEmpty() || message.equalsIgnoreCase("NONE")) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.getUser().sendMessage(carbonChat.getAdventureManager().processMessage(message));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import net.draycia.carbon.events.ChatFormatEvent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class IgnoredPlayerHandler implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onPapiPlaceholder(ChatFormatEvent event) {
|
||||
if (event.getTarget().isIgnoringUser(event.getSender())) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,13 +20,13 @@ public class ItemLinkHandler implements Listener {
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onItemLink(ChatComponentEvent event) {
|
||||
// Handle item linking placeholders
|
||||
if (event.getUser().isOnline()) {
|
||||
if (event.getSender().isOnline()) {
|
||||
for (Pattern pattern : event.getChannel().getItemLinkPatterns()) {
|
||||
String patternContent = pattern.toString().replace("\\Q", "").replace("\\E", "");
|
||||
|
||||
if (event.getOriginalMessage().contains(patternContent)) {
|
||||
TextComponent component = event.getComponent().replaceFirst(pattern, (input) -> {
|
||||
return TextComponent.builder().append(carbonChat.getItemStackUtils().createComponent(event.getUser().asPlayer()));
|
||||
return TextComponent.builder().append(carbonChat.getItemStackUtils().createComponent(event.getSender().asPlayer()));
|
||||
});
|
||||
|
||||
event.setComponent(component);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.events.ChatFormatEvent;
|
||||
import net.draycia.carbon.events.PreChatFormatEvent;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.event.EventHandler;
|
||||
@@ -11,7 +11,7 @@ import org.bukkit.event.Listener;
|
||||
public class LegacyFormatHandler implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onLegacyFormat(ChatFormatEvent event) {
|
||||
public void onLegacyFormat(PreChatFormatEvent event) {
|
||||
Component component = CarbonChat.LEGACY.deserialize(event.getFormat());
|
||||
event.setFormat(MiniMessage.get().serialize(component));
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import net.draycia.carbon.events.ChatFormatEvent;
|
||||
import net.draycia.carbon.events.PreChatFormatEvent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
@@ -8,7 +8,7 @@ import org.bukkit.event.Listener;
|
||||
public class OfflineNameHandler implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onOfflineMessage(ChatFormatEvent event) {
|
||||
public void onOfflineMessage(PreChatFormatEvent event) {
|
||||
// If the player isn't online (cross server message), use their normal name
|
||||
if (!event.getUser().isOnline()) {
|
||||
event.setFormat(event.getFormat().replace("%player_displayname%", "%player_name%"));
|
||||
|
||||
@@ -2,7 +2,6 @@ package net.draycia.carbon.listeners;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.events.ChatComponentEvent;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.sound.Sound;
|
||||
import org.bukkit.event.EventHandler;
|
||||
@@ -11,7 +10,7 @@ import org.bukkit.event.Listener;
|
||||
|
||||
public class PingHandler implements Listener {
|
||||
|
||||
private CarbonChat carbonChat;
|
||||
private final CarbonChat carbonChat;
|
||||
|
||||
public PingHandler(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
@@ -19,20 +18,22 @@ public class PingHandler implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onPing(ChatComponentEvent event) {
|
||||
for (ChatUser user : event.getRecipients()) {
|
||||
if (user.getUUID().equals(event.getUser().getUUID())) {
|
||||
continue;
|
||||
}
|
||||
if (event.getTarget() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.getOriginalMessage().contains(user.asPlayer().getName())) {
|
||||
if (carbonChat.getConfig().getBoolean("pings.enabled")) {
|
||||
Key key = Key.of(carbonChat.getConfig().getString("pings.sound"));
|
||||
Sound.Source source = Sound.Source.valueOf(carbonChat.getConfig().getString("pings.source"));
|
||||
float volume = (float) carbonChat.getConfig().getDouble("pings.volume");
|
||||
float pitch = (float) carbonChat.getConfig().getDouble("pings.pitch");
|
||||
if (event.getSender().getUUID().equals(event.getTarget().getUUID())) {
|
||||
return;
|
||||
}
|
||||
|
||||
user.playSound(Sound.of(key, source, volume, pitch));
|
||||
}
|
||||
if (event.getOriginalMessage().contains(event.getSender().asOfflinePlayer().getName())) {
|
||||
if (carbonChat.getConfig().getBoolean("pings.enabled")) {
|
||||
Key key = Key.of(carbonChat.getConfig().getString("pings.sound"));
|
||||
Sound.Source source = Sound.Source.valueOf(carbonChat.getConfig().getString("pings.source"));
|
||||
float volume = (float) carbonChat.getConfig().getDouble("pings.volume");
|
||||
float pitch = (float) carbonChat.getConfig().getDouble("pings.pitch");
|
||||
|
||||
event.getSender().playSound(Sound.of(key, source, volume, pitch));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import net.draycia.carbon.events.ChatFormatEvent;
|
||||
import net.draycia.carbon.events.PreChatFormatEvent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
@@ -9,7 +9,7 @@ import org.bukkit.event.Listener;
|
||||
public class PlaceholderHandler implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onPapiPlaceholder(ChatFormatEvent event) {
|
||||
public void onPapiPlaceholder(PreChatFormatEvent event) {
|
||||
event.setFormat(PlaceholderAPI.setPlaceholders(event.getUser().asOfflinePlayer(), event.getFormat()));
|
||||
}
|
||||
|
||||
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import net.draycia.carbon.events.ChatFormatEvent;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class RelationalPlaceholderHandler implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onPapiPlaceholder(ChatFormatEvent event) {
|
||||
if (!event.getSender().isOnline() || !event.getTarget().isOnline()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Player sender = event.getSender().asPlayer();
|
||||
Player target = event.getTarget().asPlayer();
|
||||
|
||||
event.setFormat(PlaceholderAPI.setRelationalPlaceholders(sender, target, event.getFormat()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.events.ChatFormatEvent;
|
||||
import net.draycia.carbon.managers.AdventureManager;
|
||||
import net.draycia.carbon.events.PreChatFormatEvent;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -12,29 +10,26 @@ import org.bukkit.event.Listener;
|
||||
public class UserFormattingListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onFormat(ChatFormatEvent event) {
|
||||
|
||||
public void onFormat(PreChatFormatEvent event) {
|
||||
if (!event.getUser().isOnline()) {
|
||||
suppressFormatting(event);
|
||||
return;
|
||||
}
|
||||
|
||||
Player p = event.getUser().asPlayer();
|
||||
if (!(p.hasPermission("carbonchat.formatting") ||
|
||||
p.hasPermission("carbonchat.channels." + event.getChannel().getKey() + ".formatting"))) {
|
||||
Player player = event.getUser().asPlayer();
|
||||
|
||||
if (!(player.hasPermission("carbonchat.formatting") ||
|
||||
player.hasPermission("carbonchat.channels." + event.getChannel().getKey() + ".formatting"))) {
|
||||
suppressFormatting(event);
|
||||
return;
|
||||
} else {
|
||||
// Swap the &-style codes for minimessage-compatible strings
|
||||
event.setMessage(MiniMessage.get().serialize(
|
||||
LegacyComponentSerializer.legacyAmpersand().deserialize(event.getMessage())
|
||||
));
|
||||
LegacyComponentSerializer.legacyAmpersand().deserialize(event.getMessage())));
|
||||
}
|
||||
}
|
||||
|
||||
private void suppressFormatting(ChatFormatEvent event) {
|
||||
event.setFormat(event.getFormat().replace("<message>", "<pre><message></pre>")
|
||||
);
|
||||
private void suppressFormatting(PreChatFormatEvent event) {
|
||||
event.setFormat(event.getFormat().replace("<message>", "<pre><message></pre>"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.commands.*;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -5,7 +5,6 @@ import io.lettuce.core.RedisURI;
|
||||
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
|
||||
import io.lettuce.core.pubsub.api.sync.RedisPubSubCommands;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.draycia.carbon.util.RedisListener;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
@@ -24,7 +23,7 @@ public class RedisManager {
|
||||
private final StatefulRedisPubSubConnection<String, String> publishConnection;
|
||||
private final RedisPubSubCommands<String, String> publishSync;
|
||||
|
||||
private UUID serverUUID = UUID.randomUUID();
|
||||
private final UUID serverUUID = UUID.randomUUID();
|
||||
|
||||
public RedisManager(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
|
||||
@@ -104,7 +104,8 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
|
||||
|
||||
@Override
|
||||
public void setSelectedChannel(ChatChannel chatChannel, boolean fromRemote) {
|
||||
String failureMessage = carbonChat.getConfig().getString("language.channel-switch-failure");
|
||||
String failureMessage = chatChannel.getSwitchFailureMessage();
|
||||
|
||||
ChannelSwitchEvent event = new ChannelSwitchEvent(chatChannel, this, failureMessage);
|
||||
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
|
||||
+2
-4
@@ -1,8 +1,6 @@
|
||||
package net.draycia.carbon.storage.impl;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.draycia.carbon.storage.UserChannelSettings;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import org.bukkit.Bukkit;
|
||||
@@ -16,8 +14,8 @@ public class SimpleUserChannelSettings implements UserChannelSettings {
|
||||
private boolean ignored;
|
||||
private String color;
|
||||
|
||||
private UUID uuid;
|
||||
private String channel;
|
||||
private final UUID uuid;
|
||||
private final String channel;
|
||||
|
||||
public SimpleUserChannelSettings(UUID uuid, String channel) {
|
||||
this.uuid = uuid;
|
||||
|
||||
@@ -7,6 +7,7 @@ default:
|
||||
# switch-message, toggle-off-message and toggle-on-message support PlaceholderAPI
|
||||
switch-message: '<gray>You are now in <color><channel> <gray>chat!'
|
||||
switch-other-message: '<gray><player> </rainbow><gray>is now in <color><channel> <gray>chat!'
|
||||
switch-failure-message: '<red>You cannot use channel <channel>!'
|
||||
toggle-on-message: '<gray>You can now see <color><channel> <gray>chat!'
|
||||
toggle-off-message: '<gray>You can no longer see <color><channel> <gray>chat!'
|
||||
toggle-other-on: '<gray><player> </rainbow><gray>can now see <color><channel> <gray>chat!'
|
||||
@@ -85,7 +86,6 @@ language:
|
||||
spy-whispers: '<yellow>Spy [<gray><sender></rainbow> <gold>-> <gray><target></rainbow><yellow>] <message>'
|
||||
no-reply-target: '<red>You have no one to reply to!'
|
||||
empty-channel: 'NONE' #<channel>, set to "NONE" to disable
|
||||
channel-switch-failure: '<red>You cannot use channel <channel>!'
|
||||
other-nickname-set: '<green><user></rainbow>''s nickname was set to <nickname><green>!'
|
||||
other-nickname-reset: '<green><user></rainbow>''s nickname was reset!'
|
||||
|
||||
|
||||
@@ -4,14 +4,15 @@ import com.gmail.nossr50.api.PartyAPI;
|
||||
import com.gmail.nossr50.events.party.McMMOPartyChangeEvent;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.events.ChannelSwitchEvent;
|
||||
import net.draycia.carbon.events.ChatFormatEvent;
|
||||
import net.draycia.carbon.events.PreChatFormatEvent;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public final class CarbonChatMCMMO extends JavaPlugin {
|
||||
public final class CarbonChatMCMMO extends JavaPlugin implements Listener {
|
||||
|
||||
private CarbonChat carbonChat;
|
||||
|
||||
@@ -28,6 +29,8 @@ public final class CarbonChatMCMMO extends JavaPlugin {
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
Bukkit.getPluginManager().registerEvents(this, this);
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
@@ -37,13 +40,12 @@ public final class CarbonChatMCMMO extends JavaPlugin {
|
||||
if ((party instanceof Boolean) && ((Boolean) party)) {
|
||||
if (!isInParty(event.getUser())) {
|
||||
event.setCancelled(true);
|
||||
event.setFailureMessage(getConfig().getString("cancellation-message"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onChannelMessage(ChatFormatEvent event) {
|
||||
public void onChannelMessage(PreChatFormatEvent event) {
|
||||
Object party = event.getChannel().getContext("mcmmo-party");
|
||||
|
||||
if ((party instanceof Boolean) && ((Boolean) party)) {
|
||||
|
||||
+1
-1
@@ -15,7 +15,7 @@ import org.bukkit.entity.Player;
|
||||
@CommandPermission("carbonchat.clearchat.clear")
|
||||
public class ClearChatCommand extends BaseCommand {
|
||||
|
||||
private CarbonChatModeration moderation;
|
||||
private final CarbonChatModeration moderation;
|
||||
|
||||
public ClearChatCommand(CarbonChatModeration moderation) {
|
||||
this.moderation = moderation;
|
||||
|
||||
+1
-1
@@ -17,7 +17,7 @@ import org.bukkit.command.CommandSender;
|
||||
@CommandPermission("carbonchat.mute")
|
||||
public class MuteCommand extends BaseCommand {
|
||||
|
||||
private CarbonChatModeration moderation;
|
||||
private final CarbonChatModeration moderation;
|
||||
|
||||
public MuteCommand(CarbonChatModeration moderation) {
|
||||
this.moderation = moderation;
|
||||
|
||||
+1
-1
@@ -17,7 +17,7 @@ import org.bukkit.command.CommandSender;
|
||||
@CommandPermission("carbonchat.shadowmute")
|
||||
public class ShadowMuteCommand extends BaseCommand {
|
||||
|
||||
private CarbonChatModeration moderation;
|
||||
private final CarbonChatModeration moderation;
|
||||
|
||||
public ShadowMuteCommand(CarbonChatModeration moderation) {
|
||||
this.moderation = moderation;
|
||||
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
package net.draycia.carbonmoderation.listeners;
|
||||
|
||||
import net.draycia.carbon.events.ChatFormatEvent;
|
||||
import net.draycia.carbon.events.PreChatFormatEvent;
|
||||
import net.draycia.carbonmoderation.CarbonChatModeration;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
@@ -8,14 +8,14 @@ import org.bukkit.event.Listener;
|
||||
|
||||
public class CapsHandler implements Listener {
|
||||
|
||||
private CarbonChatModeration moderation;
|
||||
private final CarbonChatModeration moderation;
|
||||
|
||||
public CapsHandler(CarbonChatModeration moderation) {
|
||||
this.moderation = moderation;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOW)
|
||||
public void onMessage(ChatFormatEvent event) {
|
||||
public void onMessage(PreChatFormatEvent event) {
|
||||
if (!moderation.getConfig().getBoolean("caps-protection.enabled")) {
|
||||
return;
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
package net.draycia.carbonmoderation.listeners;
|
||||
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.events.ChatFormatEvent;
|
||||
import net.draycia.carbon.events.PreChatFormatEvent;
|
||||
import net.draycia.carbonmoderation.CarbonChatModeration;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
@@ -15,14 +15,14 @@ import java.util.regex.Pattern;
|
||||
|
||||
public class FilterHandler implements Listener {
|
||||
|
||||
private CarbonChatModeration moderation;
|
||||
private final CarbonChatModeration moderation;
|
||||
|
||||
public FilterHandler(CarbonChatModeration moderation) {
|
||||
this.moderation = moderation;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onFilter(ChatFormatEvent event) {
|
||||
public void onFilter(PreChatFormatEvent event) {
|
||||
if (!moderation.getConfig().getBoolean("filters.enabled")) {
|
||||
return;
|
||||
}
|
||||
|
||||
+2
-2
@@ -1,13 +1,13 @@
|
||||
package net.draycia.carbonmoderation.listeners;
|
||||
|
||||
import net.draycia.carbon.events.ChatFormatEvent;
|
||||
import net.draycia.carbon.events.PreChatFormatEvent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class MuteHandler implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onMute(ChatFormatEvent event) {
|
||||
public void onMute(PreChatFormatEvent event) {
|
||||
if (event.getUser().isMuted()) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
|
||||
+3
-8
@@ -9,15 +9,10 @@ public class ShadowMuteHandler implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onComponent(ChatComponentEvent event) {
|
||||
if (event.getUser().isShadowMuted()) {
|
||||
if (event.getRecipients().size() == 1 && event.getUser().equals(event.getRecipients().get(0))) {
|
||||
return;
|
||||
if (event.getSender().isShadowMuted()) {
|
||||
if (!event.getSender().equals(event.getTarget())) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
|
||||
try {
|
||||
event.getRecipients().clear();
|
||||
event.getRecipients().add(event.getUser());
|
||||
} catch (UnsupportedOperationException e) {}
|
||||
}
|
||||
|
||||
// TODO: figure out a way to add the console "[SM]" prefix for shadow muted players
|
||||
|
||||
@@ -6,14 +6,15 @@ import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
|
||||
import com.palmergames.bukkit.towny.object.Resident;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.events.ChannelSwitchEvent;
|
||||
import net.draycia.carbon.events.ChatFormatEvent;
|
||||
import net.draycia.carbon.events.PreChatFormatEvent;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public final class CarbonChatTowny extends JavaPlugin {
|
||||
public final class CarbonChatTowny extends JavaPlugin implements Listener {
|
||||
|
||||
private CarbonChat carbonChat;
|
||||
private static final String KEY = "towny-town";
|
||||
@@ -31,6 +32,8 @@ public final class CarbonChatTowny extends JavaPlugin {
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
Bukkit.getPluginManager().registerEvents(this, this);
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
@@ -46,7 +49,7 @@ public final class CarbonChatTowny extends JavaPlugin {
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onChannelMessage(ChatFormatEvent event) {
|
||||
public void onChannelMessage(PreChatFormatEvent event) {
|
||||
// TODO: event.setFailureMessage
|
||||
Object town = event.getChannel().getContext(KEY);
|
||||
|
||||
|
||||
+38
-31
@@ -10,15 +10,16 @@ import com.sk89q.worldguard.protection.regions.RegionContainer;
|
||||
import com.sk89q.worldguard.protection.regions.RegionQuery;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.events.ChannelSwitchEvent;
|
||||
import net.draycia.carbon.events.ChatFormatEvent;
|
||||
import net.draycia.carbon.events.PreChatFormatEvent;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public final class CarbonChatWorldGuard extends JavaPlugin {
|
||||
public final class CarbonChatWorldGuard extends JavaPlugin implements Listener {
|
||||
|
||||
private static final String KEY = "worldguard-region";
|
||||
|
||||
@@ -28,49 +29,29 @@ public final class CarbonChatWorldGuard extends JavaPlugin {
|
||||
CarbonChat carbonChat = (CarbonChat) Bukkit.getPluginManager().getPlugin("CarbonChat");
|
||||
|
||||
carbonChat.getContextManager().register(KEY, (context) -> {
|
||||
if ((context.getValue() instanceof Boolean) && ((Boolean) context.getValue())) {
|
||||
return isInSameRegion(context.getSender(), context.getTarget());
|
||||
}
|
||||
|
||||
boolean user1InRegion = false;
|
||||
boolean user2InRegion = false;
|
||||
|
||||
if (context.getValue() instanceof String) {
|
||||
user1InRegion = isInRegion((String)context.getValue(), context.getSender());
|
||||
user2InRegion = isInRegion((String)context.getValue(), context.getTarget());
|
||||
} else if (context.getValue() instanceof List) {
|
||||
for (String item : (List<String>) context.getValue()) {
|
||||
if (!user1InRegion) {
|
||||
user1InRegion = isInRegion(item, context.getSender());
|
||||
}
|
||||
|
||||
if (!user2InRegion) {
|
||||
user2InRegion = isInRegion(item, context.getTarget());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return user1InRegion && user2InRegion;
|
||||
return this.testContext(context.getSender(), context.getTarget(), context.getValue());
|
||||
});
|
||||
|
||||
Bukkit.getPluginManager().registerEvents(this, this);
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onChannelSwitch(ChannelSwitchEvent event) {
|
||||
// TODO: cancellation message
|
||||
Object value = event.getChannel().getContext(KEY);
|
||||
|
||||
if (!isInRegionOrRegions(value, event.getUser())) {
|
||||
if ((value instanceof String || value instanceof List) && !isInRegionOrRegions(value, event.getUser())) {
|
||||
event.setCancelled(true);
|
||||
// TODO: placeholders (regions, etc)
|
||||
event.setFailureMessage(getConfig().getString("cancellation-message"));
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onChannelMessage(ChatFormatEvent event) {
|
||||
public void onChannelMessage(PreChatFormatEvent event) {
|
||||
// TODO: cancellation message
|
||||
Object value = event.getChannel().getContext(KEY);
|
||||
|
||||
if (!isInRegionOrRegions(value, event.getUser())) {
|
||||
event.setCancelled(true);
|
||||
if ((value instanceof String || value instanceof List) && !isInRegionOrRegions(value, event.getUser())) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,6 +71,32 @@ public final class CarbonChatWorldGuard extends JavaPlugin {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean testContext(ChatUser sender, ChatUser target, Object value) {
|
||||
boolean user1InRegion = false;
|
||||
boolean user2InRegion = false;
|
||||
|
||||
if ((value instanceof Boolean) && ((Boolean) value)) {
|
||||
return isInSameRegion(sender, target);
|
||||
} else if (value instanceof String) {
|
||||
user1InRegion = isInRegion((String)value, sender);
|
||||
user2InRegion = isInRegion((String)value, target);
|
||||
} else if (value instanceof List) {
|
||||
for (String item : (List<String>)value) {
|
||||
if (!user1InRegion) {
|
||||
user1InRegion = isInRegion(item, sender);
|
||||
}
|
||||
|
||||
if (!user2InRegion) {
|
||||
user2InRegion = isInRegion(item, target);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
|
||||
return user1InRegion && user2InRegion;
|
||||
}
|
||||
|
||||
public boolean isInSameRegion(ChatUser user1, ChatUser user2) {
|
||||
if (!user1.isOnline() || !user2.isOnline()) {
|
||||
return false;
|
||||
|
||||
Reference in new issue
Block a user