Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bukkit player api usage #232

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion api/src/main/java/at/helpch/chatchat/api/user/ChatUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ public interface ChatUser extends User {
*
* @return The player that this user is backed by.
*/
@NotNull Player player();
@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
5 changes: 3 additions & 2 deletions plugin/src/main/java/at/helpch/chatchat/ChatChatPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@BukkitMain
Expand Down Expand Up @@ -225,8 +226,8 @@ private void registerSuggestions() {
.filter(ChatUser.class::isInstance)
.map(ChatUser.class::cast)
.filter(sender::canSee)
.map(ChatUser::player)
.map(Player::getName)
.map(user -> user.player().map(Player::getName).orElse(null))
.filter(Objects::nonNull)
.collect(Collectors.toUnmodifiableList())
);
commandManager.registerSuggestion(SuggestionKey.of("files"), (sender, context) -> DumpUtils.FILES);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public void testFormat(
sender.sendMessage(
FormatUtils.parseFormat(
format,
sender.player(),
sender.player(),
sender.playerNotNull(),
sender.playerNotNull(),
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 @@ -28,12 +28,12 @@ public void ignore(ChatUser sender, ChatUser target) {

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

sender.ignoreUser(target);
sender.sendMessage(plugin.configManager().messages().ignoredPlayer()
.replaceText(builder -> builder.matchLiteral("<player>").replacement(target.player().getDisplayName())));
.replaceText(builder -> builder.matchLiteral("<player>").replacement(target.playerNotNull().getDisplayName())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public UnignoreCommand(final ChatChatPlugin plugin) {
public void unignore(ChatUser sender, ChatUser target) {
if (!sender.ignoredUsers().contains(target.uuid())) {
sender.sendMessage(plugin.configManager().messages().notIgnored()
.replaceText(builder -> builder.matchLiteral("<player>").replacement(target.player().getDisplayName())));
.replaceText(builder -> builder.matchLiteral("<player>").replacement(target.playerNotNull().getDisplayName())));
return;
}

sender.unignoreUser(target);
sender.sendMessage(plugin.configManager().messages().unignoredPlayer()
.replaceText(builder -> builder.matchLiteral("<player>").replacement(target.player().getDisplayName())));
.replaceText(builder -> builder.matchLiteral("<player>").replacement(target.playerNotNull().getDisplayName())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ public void whisperCommand(
formats.forEach((Audience audience, Format format) ->
audience.sendMessage(FormatUtils.parseFormat(
format,
sender.player(),
recipient.player(),
sender.playerNotNull(),
recipient.playerNotNull(),
pmSendEvent.message()
))
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void onChat(ChatChatEvent event) {
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().player(), message,
DiscordSRV.getPlugin().processChatMessage(event.user().playerNotNull(), message,
event.channel().name(), event.isCancelled());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public void enable() {

@Override
public boolean canSee(@NotNull final ChatUser user, @NotNull final ChatUser target) {
return user.player().canSee(target.player());
return user.player()
.map(player -> target.player().map(player::canSee).orElse(true))
.orElse(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public void enable() {

@Override
public boolean canSee(@NotNull final ChatUser user, @NotNull final ChatUser target) {
return VanishAPI.canSee(user.player(), target.player());
return user.player()
.map(player -> target.player().map(targetPlayer -> VanishAPI.canSee(player, targetPlayer))
.orElse(true))
.orElse(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public void enable() {

@Override
public boolean canSee(@NotNull final ChatUser user, @NotNull final ChatUser target) {
return user.player().canSee(target.player());
return user.player()
.map(player -> target.player().map(player::canSee).orElse(true))
.orElse(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Setting;
Expand Down Expand Up @@ -59,7 +60,7 @@ public MiniPlaceholderImpl(
return TagResolver.empty();
}

if (context.inMessage() && !sender.get().player().hasPermission(MINI_PLACEHOLDER_PERMISSION + tagName)) {
if (context.inMessage() && !sender.get().hasPermission(MINI_PLACEHOLDER_PERMISSION + tagName)) {
return TagResolver.empty();
}

Expand All @@ -78,14 +79,20 @@ public MiniPlaceholderImpl(
}

final boolean recipientIsChatUser = recipient.isPresent() && recipient.get() instanceof ChatUser;
final Player senderPlayer = sender.get().player().orElse(null);
final Player recipientPlayer = recipientIsChatUser ? ((ChatUser) recipient.get()).player().orElse(null) : null;

if (senderPlayer == null) {
return TagResolver.empty();
}

final TagResolver papiTag = isRelationalTag && recipientIsChatUser
? TagResolver.resolver(
PapiTagUtils.createPlaceholderAPITag(sender.get().player()),
PapiTagUtils.createRelPlaceholderAPITag(sender.get().player(), ((ChatUser) recipient.get()).player()),
PapiTagUtils.createRecipientTag(((ChatUser) recipient.get()).player())
PapiTagUtils.createPlaceholderAPITag(senderPlayer),
PapiTagUtils.createRelPlaceholderAPITag(senderPlayer, recipientPlayer),
PapiTagUtils.createRecipientTag(recipientPlayer)
)
: PapiTagUtils.createPlaceholderAPITag(sender.get().player());
: PapiTagUtils.createPlaceholderAPITag(senderPlayer);

return shouldAutoCloseTags
? Placeholder.component(tagName, MessageUtils.parseToMiniMessage(message, papiTag))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,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.player().getName()).orElse("");
return chatUser.lastMessagedUser().map(value -> value.playerNotNull().getName()).orElse("");
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public InvalidCharsRule(@NotNull final ChatChatPlugin plugin) {
}

public boolean isAllowedPublic(@NotNull final ChatUser sender, @NotNull final String message) {
return message.chars().noneMatch(it -> it > 127 && it != 248) || sender.player().hasPermission(UTF_PERMISSION);
return message.chars().noneMatch(it -> it > 127 && it != 248) || sender.hasPermission(UTF_PERMISSION);
}

public boolean isAllowedPrivate(@NotNull ChatUser sender, @NotNull User recipient, @NotNull String message) {
return message.chars().noneMatch(it -> it > 127 && it != 248) || sender.player().hasPermission(UTF_PERMISSION);
return message.chars().noneMatch(it -> it > 127 && it != 248) || sender.hasPermission(UTF_PERMISSION);
}

public @NotNull Optional<@NotNull Component> publicDeniedMessage() {
Expand Down
15 changes: 11 additions & 4 deletions plugin/src/main/java/at/helpch/chatchat/user/ChatUserImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void format(@NotNull final Format format) {

@Override
public boolean hasPermission(@NotNull final String node) {
return player().hasPermission(node);
return player().map(value -> value.hasPermission(node)).orElse(false);
}

@Override
Expand Down Expand Up @@ -168,13 +168,20 @@ public boolean canSee(@NotNull final User target) {
}

@Override
public @NotNull Player player() {
return Objects.requireNonNull(Bukkit.getPlayer(uuid)); // this will never be null
public @NotNull Optional<Player> player() {
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() {
return ChatChatPlugin.audiences().player(uuid);
try (var audiences = ChatChatPlugin.audiences()) {
return audiences.player(uuid);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public static boolean isTargetWithinRadius(
}

if (radius != -1 && source instanceof ChatUser) {
final Location sourceLocation = ((ChatUser) source).player().getLocation();
final Location targetLocation = ((ChatUser) target).player().getLocation();
final Location sourceLocation = ((ChatUser) source).playerNotNull().getLocation();
final Location targetLocation = ((ChatUser) target).playerNotNull().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 @@ -73,7 +73,7 @@ public static MentionReplaceResult replaceMention(
@NotNull final Component component,
@NotNull final Format format) {
return replaceMention(username, component, (r) -> user instanceof ChatUser
? FormatUtils.parseFormat(format, ((ChatUser) user).player(), component)
? FormatUtils.parseFormat(format, ((ChatUser) user).player().get(), component)
: FormatUtils.parseFormat(format, component));
}

Expand All @@ -84,8 +84,8 @@ public static MentionReplaceResult replaceMention(
@NotNull final Component component,
@NotNull final Format format
) {
return replaceMention(prefix + user.player().getName(), component,
r -> FormatUtils.parseFormat(format, user.player(), component));
return replaceMention(prefix + user.playerNotNull(), component,
r -> FormatUtils.parseFormat(format, user.playerNotNull(), component));
}

public static @NotNull Map.Entry<@NotNull Boolean, @NotNull Component> processChannelMentions(
Expand Down
25 changes: 14 additions & 11 deletions plugin/src/main/java/at/helpch/chatchat/util/MessageProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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 @@ -96,7 +97,7 @@ public static boolean process(
async,
user,
FormatUtils.findFormat(
user.player(),
user.playerNotNull(),
channel,
plugin.configManager().formats(),
plugin.configManager().extensions().addons().deluxeChatInversePriorities()),
Expand Down Expand Up @@ -146,8 +147,8 @@ public static boolean process(

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

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

final var component = FormatUtils.parseFormat(
chatEvent.format(),
user.player(),
user.player(),
user.playerNotNull(),
user.playerNotNull(),
mentionResult.message(),
plugin.miniPlaceholdersManager().compileTags(MiniPlaceholderContext.builder().inMessage(false).sender(user).recipient(user).build())
);
Expand Down Expand Up @@ -238,11 +239,13 @@ public static boolean process(
}

if (user.hasPermission(ITEM_TAG_PERMISSION)) {
resolver.resolver(
ItemUtils.createItemPlaceholder(
plugin.configManager().settings().itemFormat(),
plugin.configManager().settings().itemFormatInfo(),
user.player().getInventory().getItemInMainHand()
user.player().ifPresent( player ->
resolver.resolver(
ItemUtils.createItemPlaceholder(
plugin.configManager().settings().itemFormat(),
plugin.configManager().settings().itemFormatInfo(),
player.getInventory().getItemInMainHand()
)
)
);
}
Expand Down
12 changes: 10 additions & 2 deletions plugin/src/main/java/at/helpch/chatchat/util/PapiTagUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private PapiTagUtils() {

public static @NotNull TagResolver createRelPlaceholderAPITag(
final @NotNull Player player,
final @NotNull Player target
final @Nullable Player target
) {
return TagResolver.resolver("papi-rel", (argumentQueue, context) -> {
if (!argumentQueue.hasNext()) {
Expand Down Expand Up @@ -118,6 +118,10 @@ private PapiTagUtils() {
return null;
}

if (target == null) {
return null;
}

final var parsedPlaceholder = PlaceholderAPI.setRelationalPlaceholders(
player,
target,
Expand All @@ -134,7 +138,11 @@ private PapiTagUtils() {
});
}

public static @NotNull TagResolver createRecipientTag(@NotNull final Player player) {
public static @NotNull TagResolver createRecipientTag(@Nullable final Player player) {
if (player == null) {
return TagResolver.empty();
}

return createPlaceholderAPITag("recipient", player);
}
}
Loading