Skip to content

Commit

Permalink
Merge pull request #233 from BlitzOffline/fix/messages-and-mentions
Browse files Browse the repository at this point in the history
Fix issues introduced in PR #232
  • Loading branch information
BlitzOffline authored May 11, 2024
2 parents 09e14f2 + 69bf385 commit 583073d
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 42 deletions.
8 changes: 0 additions & 8 deletions api/src/main/java/at/helpch/chatchat/api/user/ChatUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ public interface ChatUser extends User {
*/
@NotNull Optional<Player> player();

/**
* Gets the player that this user is backed by. If the player is not present, an exception is thrown.
*
* @return The player that this user is backed by.
* @throws NullPointerException If the player is not present.
*/
@NotNull Player playerNotNull() throws NullPointerException;

/**
* Gets the user that this user has last sent a private message to.
*
Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
spigot = "1.20.1-R0.1-SNAPSHOT"

# Adventure
minimessage = "4.15.0"
adventure-platform = "4.3.2"
minimessage = "4.16.0"
adventure-platform = "4.3.3-SNAPSHOT"

# Other
configurate = "4.1.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public void testFormat(
@NotNull final PriorityFormat format,
@Join @NotNull final String message
) {
var player = sender.player();
if (player.isEmpty()) {
sender.sendMessage(plugin.configManager().messages().genericError());
return;
}

if (message.isBlank()) {
sender.sendMessage(plugin.configManager().messages().emptyMessage());
return;
Expand All @@ -37,8 +43,8 @@ public void testFormat(
sender.sendMessage(
FormatUtils.parseFormat(
format,
sender.playerNotNull(),
sender.playerNotNull(),
player.get(),
player.get(),
MessageProcessor.processMessage(plugin, sender, ConsoleUser.INSTANCE, message),
plugin.miniPlaceholdersManager().compileTags(MiniPlaceholderContext.builder().inMessage(false).sender(sender).recipient(sender).build())
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,20 @@ public void ignore(ChatUser sender, ChatUser target) {
return;
}

var targetPlayer = target.player();
if (targetPlayer.isEmpty()) {
sender.sendMessage(plugin.configManager().messages().userOffline());
return;
}

if (sender.ignoredUsers().contains(target.uuid())) {
sender.sendMessage(plugin.configManager().messages().alreadyIgnored()
.replaceText(builder -> builder.matchLiteral("<player>").replacement(target.playerNotNull().getDisplayName())));
.replaceText(builder -> builder.matchLiteral("<player>").replacement(targetPlayer.get().getDisplayName())));
return;
}

sender.ignoreUser(target);
sender.sendMessage(plugin.configManager().messages().ignoredPlayer()
.replaceText(builder -> builder.matchLiteral("<player>").replacement(target.playerNotNull().getDisplayName())));
.replaceText(builder -> builder.matchLiteral("<player>").replacement(targetPlayer.get().getDisplayName())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ public UnignoreCommand(final ChatChatPlugin plugin) {
@Permission(IGNORE_PERMISSION)
@Default
public void unignore(ChatUser sender, ChatUser target) {
var targetPlayer = target.player();
if (targetPlayer.isEmpty()) {
sender.sendMessage(plugin.configManager().messages().userOffline());
return;
}

if (!sender.ignoredUsers().contains(target.uuid())) {
sender.sendMessage(plugin.configManager().messages().notIgnored()
.replaceText(builder -> builder.matchLiteral("<player>").replacement(target.playerNotNull().getDisplayName())));
.replaceText(builder -> builder.matchLiteral("<player>").replacement(targetPlayer.get().getDisplayName())));
return;
}

sender.unignoreUser(target);
sender.sendMessage(plugin.configManager().messages().unignoredPlayer()
.replaceText(builder -> builder.matchLiteral("<player>").replacement(target.playerNotNull().getDisplayName())));
.replaceText(builder -> builder.matchLiteral("<player>").replacement(targetPlayer.get().getDisplayName())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public void whisperCommand(
@Suggestion(value = "recipients") final ChatUser recipient,
@Join final String message
) {
var senderPlayer = sender.player();
var recipientPlayer = recipient.player();
if (senderPlayer.isEmpty() || recipientPlayer.isEmpty()) {
sender.sendMessage(plugin.configManager().messages().userOffline());
return;
}

if (!plugin.configManager().settings().privateMessagesSettings().enabled()) {
sender.sendMessage(plugin.configManager().messages().unknownCommand());
return;
Expand Down Expand Up @@ -123,8 +130,8 @@ public void whisperCommand(
formats.forEach((Audience audience, Format format) ->
audience.sendMessage(FormatUtils.parseFormat(
format,
sender.playerNotNull(),
recipient.playerNotNull(),
senderPlayer.get(),
recipientPlayer.get(),
pmSendEvent.message()
))
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ public final class MessagesHolder {
// format related
private Component invalidFormat = text("Invalid format.", RED);

// generic
private Component genericError = text("An unexpected error occurred!", RED);

public @NotNull Component consoleOnly() {
return consoleOnly;
}
Expand Down Expand Up @@ -237,4 +240,8 @@ public final class MessagesHolder {
return chatDisabled;
}

public @NotNull Component genericError() {
return genericError;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ public void broadcastMessageToChannel(final String channelName, final Component

@EventHandler(priority = EventPriority.MONITOR)
public void onChat(ChatChatEvent event) {
var player = event.user().player();
if (player.isEmpty()) {
return;
}

final var message = github.scarsz.discordsrv.dependencies.kyori.adventure.text.serializer.gson.GsonComponentSerializer.gson().deserialize(
GsonComponentSerializer.gson().serialize(event.message())
);
DiscordSRV.getPlugin().processChatMessage(event.user().playerNotNull(), message,
event.channel().name(), event.isCancelled());
DiscordSRV.getPlugin().processChatMessage(player.get(), message, event.channel().name(), event.isCancelled());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,12 @@ public void onChat(final AsyncPlayerChatEvent event) {
"format are installed and work properly.");
}

var sent = MessageProcessor.process(plugin, user, channel, message, event.isAsynchronous());
// Cancel the event if the message doesn't end up being sent
// This only happens if the message contains illegal characters or if the ChatChatEvent is canceled.
event.setCancelled(!MessageProcessor.process(plugin, user, channel, message, event.isAsynchronous()));
if (!event.isCancelled() && !sent) {
event.setCancelled(false);
}
user.channel(oldChannel);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import net.kyori.adventure.identity.Identity;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Optional;

public final class PlaceholderAPIPlaceholders extends PlaceholderExpansion {
private final ChatChatPlugin plugin;
Expand Down Expand Up @@ -85,7 +87,7 @@ public String onRequest(final OfflinePlayer offlinePlayer, @NotNull final String
case "private_messages_enabled":
return formatBoolean(chatUser.privateMessages());
case "private_messages_recipient":
return chatUser.lastMessagedUser().map(value -> value.playerNotNull().getName()).orElse("");
return chatUser.lastMessagedUser().map(value -> value.player().map(Player::getName).orElse("")).orElse("");
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,9 @@ public boolean canSee(@NotNull final User target) {
return Optional.ofNullable(Bukkit.getPlayer(uuid));
}

@Override
public @NotNull Player playerNotNull() throws NullPointerException {
return player().orElseThrow(() -> new NullPointerException("Player is not present!"));
}

@Override
public @NotNull Audience audience() {
try (var audiences = ChatChatPlugin.audiences()) {
return audiences.player(uuid);
}
return ChatChatPlugin.audiences().player(uuid);
}

@Override
Expand Down
11 changes: 9 additions & 2 deletions plugin/src/main/java/at/helpch/chatchat/util/ChannelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,15 @@ public static boolean isTargetWithinRadius(
}

if (radius != -1 && source instanceof ChatUser) {
final Location sourceLocation = ((ChatUser) source).playerNotNull().getLocation();
final Location targetLocation = ((ChatUser) target).playerNotNull().getLocation();
var sourcePlayer = ((ChatUser) source).player();
var targetPlayer = ((ChatUser) target).player();

if (sourcePlayer.isEmpty() || targetPlayer.isEmpty()) {
return false;
}

final Location sourceLocation = sourcePlayer.get().getLocation();
final Location targetLocation = targetPlayer.get().getLocation();

final World sourceWorld = sourceLocation.getWorld();
final World targetWorld = targetLocation.getWorld();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ public static MentionReplaceResult replaceMention(
@NotNull final Component component,
@NotNull final Format format
) {
return replaceMention(prefix + user.playerNotNull(), component,
r -> FormatUtils.parseFormat(format, user.playerNotNull(), component));
return user.player()
.map(value -> replaceMention(prefix + value.getName(), component, r -> FormatUtils.parseFormat(format, value, component)))
.orElseGet(() -> new MentionReplaceResult(false, component));
}

public static @NotNull Map.Entry<@NotNull Boolean, @NotNull Component> processChannelMentions(
Expand Down
27 changes: 19 additions & 8 deletions plugin/src/main/java/at/helpch/chatchat/util/MessageProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.minimessage.tag.standard.StandardTags;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.Map;
Expand Down Expand Up @@ -77,6 +76,11 @@ public static boolean process(
@NotNull final String message,
final boolean async
) {
final var player = user.player();
if (player.isEmpty()) {
return false;
}

final var isMuted = plugin.hookManager()
.muteHooks()
.stream()
Expand All @@ -97,7 +101,7 @@ public static boolean process(
async,
user,
FormatUtils.findFormat(
user.playerNotNull(),
player.get(),
channel,
plugin.configManager().formats(),
plugin.configManager().extensions().addons().deluxeChatInversePriorities()),
Expand Down Expand Up @@ -130,7 +134,9 @@ public static boolean process(
}

// Console Users have their own format we set in ChatListener.java
if (target instanceof ConsoleUser) continue;
if (target instanceof ConsoleUser) {
continue;
}

// Process mentions and get the result.
final var mentionResult = plugin.mentionsManager().processMentions(
Expand All @@ -144,11 +150,16 @@ public static boolean process(

if (target instanceof ChatUser) {
final var chatTarget = (ChatUser) target;
final var playerTarget = chatTarget.player();

if (playerTarget.isEmpty()) {
continue;
}

final var component = FormatUtils.parseFormat(
chatEvent.format(),
user.playerNotNull(),
chatTarget.playerNotNull(),
player.get(),
playerTarget.get(),
mentionResult.message(),
plugin.miniPlaceholdersManager().compileTags(MiniPlaceholderContext.builder().inMessage(false).sender(user).recipient(target).build())
);
Expand All @@ -172,7 +183,7 @@ public static boolean process(

final var component = FormatUtils.parseFormat(
chatEvent.format(),
user.playerNotNull(),
player.get(),
mentionResult.message(),
plugin.miniPlaceholdersManager().compileTags(MiniPlaceholderContext.builder().inMessage(false).sender(user).recipient(target).build())
);
Expand All @@ -199,8 +210,8 @@ public static boolean process(

final var component = FormatUtils.parseFormat(
chatEvent.format(),
user.playerNotNull(),
user.playerNotNull(),
player.get(),
player.get(),
mentionResult.message(),
plugin.miniPlaceholdersManager().compileTags(MiniPlaceholderContext.builder().inMessage(false).sender(user).recipient(user).build())
);
Expand Down
2 changes: 2 additions & 0 deletions plugin/src/main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ channel-mentions-enabled: "<green>Successfully enabled channel mentions!"
channel-mentions-disabled: "<green>Successfully disabled channel mentions!"

invalid-format: "<red>Invalid format!"

generic-error: "<red>An unexpected error occurred!"

0 comments on commit 583073d

Please sign in to comment.