From 0f5a3286d2f93da91d35def2a0a15ae6552e25de Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Fri, 25 Aug 2023 21:14:40 -0700 Subject: [PATCH 1/4] ignore list command --- .../carbon/api/users/CarbonPlayer.java | 9 + .../command/commands/IgnoreListCommand.java | 114 +++++++++++ .../common/messages/CarbonMessages.java | 3 + .../common/users/CarbonPlayerCommon.java | 3 +- .../common/users/ConsoleCarbonPlayer.java | 7 + .../common/users/WrappedCarbonPlayer.java | 6 + .../common/users/db/DatabaseUserManager.java | 2 +- .../carbon/common/util/CloudUtils.java | 4 +- .../carbon/common/util/Pagination.java | 179 ++++++++++++++++++ .../locale/messages-en_US.properties | 1 + 10 files changed, 325 insertions(+), 3 deletions(-) create mode 100644 common/src/main/java/net/draycia/carbon/common/command/commands/IgnoreListCommand.java create mode 100644 common/src/main/java/net/draycia/carbon/common/util/Pagination.java diff --git a/api/src/main/java/net/draycia/carbon/api/users/CarbonPlayer.java b/api/src/main/java/net/draycia/carbon/api/users/CarbonPlayer.java index b05421d9..ca0c2ac2 100644 --- a/api/src/main/java/net/draycia/carbon/api/users/CarbonPlayer.java +++ b/api/src/main/java/net/draycia/carbon/api/users/CarbonPlayer.java @@ -21,6 +21,7 @@ package net.draycia.carbon.api.users; import java.util.List; import java.util.Locale; +import java.util.Set; import java.util.UUID; import net.draycia.carbon.api.channels.ChatChannel; import net.draycia.carbon.api.util.InventorySlot; @@ -218,6 +219,14 @@ public interface CarbonPlayer extends Audience, Identified { */ void muted(boolean muted); + /** + * Gets the ids of the players this player is currently ignoring. + * + * @return the players currently ignored + * @since 2.1.0 + */ + Set ignoring(); + /** * Checks if the other player is being ignored by this player. * diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/IgnoreListCommand.java b/common/src/main/java/net/draycia/carbon/common/command/commands/IgnoreListCommand.java new file mode 100644 index 00000000..0660b76c --- /dev/null +++ b/common/src/main/java/net/draycia/carbon/common/command/commands/IgnoreListCommand.java @@ -0,0 +1,114 @@ +/* + * CarbonChat + * + * Copyright (c) 2023 Josua Parks (Vicarious) + * Contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package net.draycia.carbon.common.command.commands; + +import cloud.commandframework.CommandManager; +import cloud.commandframework.arguments.standard.IntegerArgument; +import cloud.commandframework.context.CommandContext; +import cloud.commandframework.minecraft.extras.MinecraftExtrasMetaKeys; +import com.google.inject.Inject; +import java.util.function.Supplier; +import net.draycia.carbon.api.users.CarbonPlayer; +import net.draycia.carbon.api.users.UserManager; +import net.draycia.carbon.common.command.CarbonCommand; +import net.draycia.carbon.common.command.CommandSettings; +import net.draycia.carbon.common.command.Commander; +import net.draycia.carbon.common.command.PlayerCommander; +import net.draycia.carbon.common.messages.CarbonMessages; +import net.draycia.carbon.common.util.Pagination; +import net.kyori.adventure.key.Key; +import net.kyori.adventure.text.Component; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.framework.qual.DefaultQualifier; + +@DefaultQualifier(NonNull.class) +public final class IgnoreListCommand extends CarbonCommand { + + private final UserManager users; + private final CommandManager commandManager; + private final CarbonMessages messages; + + @Inject + public IgnoreListCommand( + final UserManager userManager, + final CommandManager commandManager, + final CarbonMessages messages + ) { + this.users = userManager; + this.commandManager = commandManager; + this.messages = messages; + } + + @Override + protected CommandSettings _commandSettings() { + return new CommandSettings("ignorelist", "listignores"); + } + + @Override + public Key key() { + return Key.key("carbon", "ignorelist"); + } + + @Override + public void init() { + final var command = this.commandManager.commandBuilder(this.commandSettings().name(), this.commandSettings().aliases()) + .permission("carbon.ignore") + .senderType(PlayerCommander.class) + .argument(IntegerArgument.builder("page").withMin(1).asOptionalWithDefault(1)) + .meta(MinecraftExtrasMetaKeys.DESCRIPTION, this.messages.commandIgnoreListDescription()) + .handler(this::execute) + .build(); + + this.commandManager.command(command); + } + + private void execute(final CommandContext ctx) { + final Pagination> pagination = Pagination.>builder() + .header((currPage, pages) -> { + return Component.text("Ignores page " + currPage + "/" + pages); + }) + .item((e, lastOfPage) -> { + final CarbonPlayer ignoredPlayer = e.get(); + return Component.text(" - ").append( + ignoredPlayer.displayName().hoverEvent( + Component.text("Username: " + ignoredPlayer.username() + ", UUID: " + ignoredPlayer.uuid()) + ) + ); + }) + //.footer() // todo page buttons + .pageOutOfRange((currPage, pages) -> { + return Component.text("Page " + currPage + " out of range " + pages); + }) + .build(); + + final CarbonPlayer sender = ((PlayerCommander) ctx.getSender()).carbonPlayer(); + final int page = ctx.get("page"); + + pagination.render( + sender.ignoring().stream() + .sorted() // this way page numbers make sense + .map(id -> (Supplier) () -> this.users.user(id).join()) + .toList(), + page, + 6 + ).forEach(sender::sendMessage); + } + +} diff --git a/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java b/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java index e985cc55..9874ad67 100644 --- a/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java +++ b/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java @@ -298,6 +298,9 @@ public interface CarbonMessages { @Message("command.ignore.description") Component commandIgnoreDescription(); + @Message("command.ignorelist.description") + Component commandIgnoreListDescription(); + @Message("command.join.description") Component commandJoinDescription(); diff --git a/common/src/main/java/net/draycia/carbon/common/users/CarbonPlayerCommon.java b/common/src/main/java/net/draycia/carbon/common/users/CarbonPlayerCommon.java index 22b0e1c6..66f832cc 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/CarbonPlayerCommon.java +++ b/common/src/main/java/net/draycia/carbon/common/users/CarbonPlayerCommon.java @@ -210,7 +210,8 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing this.muted.set(muted); } - public Set ignoredPlayers() { + @Override + public Set ignoring() { return this.ignoredPlayers.get(); } diff --git a/common/src/main/java/net/draycia/carbon/common/users/ConsoleCarbonPlayer.java b/common/src/main/java/net/draycia/carbon/common/users/ConsoleCarbonPlayer.java index e119a3c6..24a3dad4 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/ConsoleCarbonPlayer.java +++ b/common/src/main/java/net/draycia/carbon/common/users/ConsoleCarbonPlayer.java @@ -19,8 +19,10 @@ */ package net.draycia.carbon.common.users; +import java.util.Collections; import java.util.List; import java.util.Locale; +import java.util.Set; import java.util.UUID; import net.draycia.carbon.api.channels.ChatChannel; import net.draycia.carbon.api.users.CarbonPlayer; @@ -134,6 +136,11 @@ public class ConsoleCarbonPlayer implements CarbonPlayer, ForwardingAudience.Sin } + @Override + public Set ignoring() { + return Collections.emptySet(); + } + @Override public boolean ignoring(final UUID player) { return false; diff --git a/common/src/main/java/net/draycia/carbon/common/users/WrappedCarbonPlayer.java b/common/src/main/java/net/draycia/carbon/common/users/WrappedCarbonPlayer.java index 5affe962..015f45fd 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/WrappedCarbonPlayer.java +++ b/common/src/main/java/net/draycia/carbon/common/users/WrappedCarbonPlayer.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.UUID; import java.util.function.Predicate; import net.draycia.carbon.api.channels.ChatChannel; @@ -141,6 +142,11 @@ public abstract class WrappedCarbonPlayer implements CarbonPlayer { return true; } + @Override + public Set ignoring() { + return this.carbonPlayerCommon.ignoring(); + } + @Override public boolean ignoring(final UUID player) { return this.carbonPlayerCommon.ignoring(player); diff --git a/common/src/main/java/net/draycia/carbon/common/users/db/DatabaseUserManager.java b/common/src/main/java/net/draycia/carbon/common/users/db/DatabaseUserManager.java index 6c89f492..9665bd1b 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/db/DatabaseUserManager.java +++ b/common/src/main/java/net/draycia/carbon/common/users/db/DatabaseUserManager.java @@ -136,7 +136,7 @@ public final class DatabaseUserManager extends CachingUserManager { .bind("id", player.uuid()) .execute(); - final Set ignored = player.ignoredPlayers(); + final Set ignored = player.ignoring(); if (!ignored.isEmpty()) { final PreparedBatch batch = handle.prepareBatch(this.locator.query("save-ignores")); for (final UUID ignoredPlayer : ignored) { diff --git a/common/src/main/java/net/draycia/carbon/common/util/CloudUtils.java b/common/src/main/java/net/draycia/carbon/common/util/CloudUtils.java index 7a94feef..774da264 100644 --- a/common/src/main/java/net/draycia/carbon/common/util/CloudUtils.java +++ b/common/src/main/java/net/draycia/carbon/common/util/CloudUtils.java @@ -45,6 +45,7 @@ import net.draycia.carbon.common.command.commands.ContinueCommand; import net.draycia.carbon.common.command.commands.DebugCommand; import net.draycia.carbon.common.command.commands.HelpCommand; import net.draycia.carbon.common.command.commands.IgnoreCommand; +import net.draycia.carbon.common.command.commands.IgnoreListCommand; import net.draycia.carbon.common.command.commands.JoinCommand; import net.draycia.carbon.common.command.commands.LeaveCommand; import net.draycia.carbon.common.command.commands.MuteCommand; @@ -76,7 +77,8 @@ public final class CloudUtils { public static final List> COMMAND_CLASSES = List.of(ClearChatCommand.class, ContinueCommand.class, DebugCommand.class, HelpCommand.class, IgnoreCommand.class, MuteCommand.class, MuteInfoCommand.class, NicknameCommand.class, ReloadCommand.class, ReplyCommand.class, ToggleMessagesCommand.class, - UnignoreCommand.class, UnmuteCommand.class, UpdateUsernameCommand.class, WhisperCommand.class, JoinCommand.class, LeaveCommand.class); + UnignoreCommand.class, UnmuteCommand.class, UpdateUsernameCommand.class, WhisperCommand.class, JoinCommand.class, LeaveCommand.class, + IgnoreListCommand.class); private static final List CONSTRUCTED_COMMANDS = new ArrayList<>(); diff --git a/common/src/main/java/net/draycia/carbon/common/util/Pagination.java b/common/src/main/java/net/draycia/carbon/common/util/Pagination.java new file mode 100644 index 00000000..0544c824 --- /dev/null +++ b/common/src/main/java/net/draycia/carbon/common/util/Pagination.java @@ -0,0 +1,179 @@ +/* + * CarbonChat + * + * Copyright (c) 2023 Josua Parks (Vicarious) + * Contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package net.draycia.carbon.common.util; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.RandomAccess; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.ComponentLike; +import org.checkerframework.checker.nullness.qual.MonotonicNonNull; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.framework.qual.DefaultQualifier; + +import static java.util.Objects.requireNonNull; +import static net.kyori.adventure.text.Component.empty; + +@DefaultQualifier(NonNull.class) +public interface Pagination { + + ComponentLike header(int page, int pages); + + ComponentLike footer(int page, int pages); + + ComponentLike pageOutOfRange(int page, int pages); + + ComponentLike item(T item, boolean lastOfPage); + + default List render( + final Collection content, + final int page, + final int itemsPerPage + ) { + if (content.isEmpty()) { + throw new IllegalArgumentException("Cannot paginate an empty collection."); + } + + final int pages = (int) Math.ceil(content.size() / (itemsPerPage * 1.00)); + if (page < 1 || page > pages) { + return Collections.singletonList(this.pageOutOfRange(page, pages).asComponent()); + } + + final List renderedContent = new ArrayList<>(); + + final Component header = this.header(page, pages).asComponent(); + if (header != empty()) { + renderedContent.add(header); + } + + final int start = itemsPerPage * (page - 1); + final int maxIndex = start + itemsPerPage; + + if (content instanceof RandomAccess && content instanceof final List contentList) { + for (int i = start; i < maxIndex; i++) { + if (i > content.size() - 1) { + break; + } + renderedContent.add(this.item(contentList.get(i), i == maxIndex - 1).asComponent()); + } + } else { + final Iterator iterator = content.iterator(); + for (int i = 0; i < start && iterator.hasNext(); i++) { + iterator.next(); + } + for (int i = start; i < maxIndex && iterator.hasNext(); ++i) { + renderedContent.add(this.item(iterator.next(), i == maxIndex - 1).asComponent()); + } + } + + final Component footer = this.footer(page, pages).asComponent(); + if (footer != empty()) { + renderedContent.add(footer); + } + + return Collections.unmodifiableList(renderedContent); + } + + static Builder builder() { + return new Builder<>(); + } + + final class Builder { + private BiIntFunction headerRenderer = ($, $$) -> empty(); + private BiIntFunction footerRenderer = ($, $$) -> empty(); + private @MonotonicNonNull BiIntFunction pageOutOfRangeRenderer = null; + private @MonotonicNonNull ItemRenderer itemRenderer = null; + + private Builder() { + } + + public Builder header(final BiIntFunction headerRenderer) { + this.headerRenderer = headerRenderer; + return this; + } + + public Builder footer(final BiIntFunction footerRenderer) { + this.footerRenderer = footerRenderer; + return this; + } + + public Builder pageOutOfRange(final BiIntFunction pageOutOfRangeRenderer) { + this.pageOutOfRangeRenderer = pageOutOfRangeRenderer; + return this; + } + + public Builder item(final ItemRenderer itemRenderer) { + this.itemRenderer = itemRenderer; + return this; + } + + public Pagination build() { + return new DelegatingPaginationImpl<>( + requireNonNull(this.headerRenderer, "Must provide a header renderer!"), + requireNonNull(this.footerRenderer, "Must provide a footer renderer!"), + requireNonNull(this.pageOutOfRangeRenderer, "Must provide a page out of range renderer!"), + requireNonNull(this.itemRenderer, "Must provide an item renderer!") + ); + } + + @FunctionalInterface + public interface ItemRenderer { + ComponentLike render(T item, boolean lastOfPage); + } + + private record DelegatingPaginationImpl( + BiIntFunction headerRenderer, + BiIntFunction footerRenderer, + BiIntFunction pageOutOfRangeRenderer, + ItemRenderer itemRenderer + ) implements Pagination { + @Override + public ComponentLike header(final int page, final int pages) { + return this.headerRenderer.apply(page, pages); + } + + @Override + public ComponentLike footer(final int page, final int pages) { + return this.footerRenderer.apply(page, pages); + } + + @Override + public ComponentLike pageOutOfRange(final int page, final int pages) { + return this.pageOutOfRangeRenderer.apply(page, pages); + } + + @Override + public ComponentLike item(final T item, final boolean lastOfPage) { + return this.itemRenderer.render(item, lastOfPage); + } + } + } + + @FunctionalInterface + interface BiIntFunction { + + T apply(int i, int i1); + + } + +} diff --git a/common/src/main/resources/locale/messages-en_US.properties b/common/src/main/resources/locale/messages-en_US.properties index 886d4190..697bb5cc 100644 --- a/common/src/main/resources/locale/messages-en_US.properties +++ b/common/src/main/resources/locale/messages-en_US.properties @@ -22,6 +22,7 @@ command.help.misc.showing_results_for_query=Showing search results for query command.ignore.argument.player=The name of the player to ignore. command.ignore.argument.uuid=The UUID of the player to ignore. command.ignore.description=Hides all incoming messages from ignored players. +command.ignorelist.description=Displays a paginated list of who you are ignoring. command.join.description=Join a channel you have previously left. command.leave.description=Leave a channel that you currently have access to. command.mute.argument.player=The name of the player to mute. From 178863db6cc8fbea07ce2920fc1f0b630bb9912e Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Sat, 26 Aug 2023 12:43:05 -0700 Subject: [PATCH 2/4] ignorelist messages --- .../carbon/common/CarbonCommonModule.java | 6 +- .../common/channels/ConfigChatChannel.java | 2 + .../command/commands/IgnoreListCommand.java | 82 ++++++++++++++----- .../common/messages/CarbonMessages.java | 22 +++++ ...aceholderResolverStrategyButDifferent.java | 38 ++++++--- .../placeholders/IntPlaceholderResolver.java | 46 +++++++++++ .../locale/messages-en_US.properties | 8 +- 7 files changed, 170 insertions(+), 34 deletions(-) create mode 100644 common/src/main/java/net/draycia/carbon/common/messages/placeholders/IntPlaceholderResolver.java diff --git a/common/src/main/java/net/draycia/carbon/common/CarbonCommonModule.java b/common/src/main/java/net/draycia/carbon/common/CarbonCommonModule.java index a955ae45..47f5aa84 100644 --- a/common/src/main/java/net/draycia/carbon/common/CarbonCommonModule.java +++ b/common/src/main/java/net/draycia/carbon/common/CarbonCommonModule.java @@ -62,6 +62,7 @@ import net.draycia.carbon.common.messages.SourcedReceiverResolver; import net.draycia.carbon.common.messages.StandardPlaceholderResolverStrategyButDifferent; import net.draycia.carbon.common.messages.placeholders.BooleanPlaceholderResolver; import net.draycia.carbon.common.messages.placeholders.ComponentPlaceholderResolver; +import net.draycia.carbon.common.messages.placeholders.IntPlaceholderResolver; import net.draycia.carbon.common.messages.placeholders.KeyPlaceholderResolver; import net.draycia.carbon.common.messages.placeholders.StringPlaceholderResolver; import net.draycia.carbon.common.messages.placeholders.UUIDPlaceholderResolver; @@ -90,6 +91,7 @@ import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.framework.qual.DefaultQualifier; import org.jdbi.v3.core.h2.H2DatabasePlugin; import org.jdbi.v3.postgres.PostgresPlugin; +import org.spongepowered.configurate.util.NamingSchemes; @DefaultQualifier(NonNull.class) public final class CarbonCommonModule extends AbstractModule { @@ -136,6 +138,7 @@ public final class CarbonCommonModule extends AbstractModule { final ComponentPlaceholderResolver componentPlaceholderResolver, final UUIDPlaceholderResolver uuidPlaceholderResolver, final StringPlaceholderResolver stringPlaceholderResolver, + final IntPlaceholderResolver intPlaceholderResolver, final KeyPlaceholderResolver keyPlaceholderResolver, final BooleanPlaceholderResolver booleanPlaceholderResolver, final CarbonMessageSource carbonMessageSource, @@ -147,10 +150,11 @@ public final class CarbonCommonModule extends AbstractModule { .sourced(carbonMessageSource) .rendered(messageRenderer) .sent(carbonMessageSender) - .resolvingWithStrategy(new StandardPlaceholderResolverStrategyButDifferent<>()) + .resolvingWithStrategy(new StandardPlaceholderResolverStrategyButDifferent<>(NamingSchemes.SNAKE_CASE)) .weightedPlaceholderResolver(Component.class, componentPlaceholderResolver, 0) .weightedPlaceholderResolver(UUID.class, uuidPlaceholderResolver, 0) .weightedPlaceholderResolver(String.class, stringPlaceholderResolver, 0) + .weightedPlaceholderResolver(Integer.class, intPlaceholderResolver, 0) .weightedPlaceholderResolver(Key.class, keyPlaceholderResolver, 0) .weightedPlaceholderResolver(Boolean.class, booleanPlaceholderResolver, 0) .create(this.getClass().getClassLoader()); diff --git a/common/src/main/java/net/draycia/carbon/common/channels/ConfigChatChannel.java b/common/src/main/java/net/draycia/carbon/common/channels/ConfigChatChannel.java index 452fdb89..d0b70412 100644 --- a/common/src/main/java/net/draycia/carbon/common/channels/ConfigChatChannel.java +++ b/common/src/main/java/net/draycia/carbon/common/channels/ConfigChatChannel.java @@ -37,6 +37,7 @@ import net.draycia.carbon.common.messages.SourcedMessageSender; import net.draycia.carbon.common.messages.SourcedReceiverResolver; import net.draycia.carbon.common.messages.placeholders.BooleanPlaceholderResolver; import net.draycia.carbon.common.messages.placeholders.ComponentPlaceholderResolver; +import net.draycia.carbon.common.messages.placeholders.IntPlaceholderResolver; import net.draycia.carbon.common.messages.placeholders.KeyPlaceholderResolver; import net.draycia.carbon.common.messages.placeholders.StringPlaceholderResolver; import net.draycia.carbon.common.messages.placeholders.UUIDPlaceholderResolver; @@ -209,6 +210,7 @@ public final class ConfigChatChannel implements ChatChannel { .weightedPlaceholderResolver(Component.class, componentPlaceholderResolver, 0) .weightedPlaceholderResolver(UUID.class, uuidPlaceholderResolver, 0) .weightedPlaceholderResolver(String.class, stringPlaceholderResolver, 0) + .weightedPlaceholderResolver(Integer.class, new IntPlaceholderResolver<>(), 0) .weightedPlaceholderResolver(Key.class, keyPlaceholderResolver, 0) .weightedPlaceholderResolver(Boolean.class, booleanPlaceholderResolver, 0) .create(this.getClass().getClassLoader()); diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/IgnoreListCommand.java b/common/src/main/java/net/draycia/carbon/common/command/commands/IgnoreListCommand.java index 0660b76c..8b4f503e 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/commands/IgnoreListCommand.java +++ b/common/src/main/java/net/draycia/carbon/common/command/commands/IgnoreListCommand.java @@ -24,6 +24,7 @@ import cloud.commandframework.arguments.standard.IntegerArgument; import cloud.commandframework.context.CommandContext; import cloud.commandframework.minecraft.extras.MinecraftExtrasMetaKeys; import com.google.inject.Inject; +import java.util.function.IntFunction; import java.util.function.Supplier; import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.api.users.UserManager; @@ -35,9 +36,16 @@ import net.draycia.carbon.common.messages.CarbonMessages; import net.draycia.carbon.common.util.Pagination; import net.kyori.adventure.key.Key; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.ComponentLike; +import net.kyori.adventure.text.TextComponent; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.framework.qual.DefaultQualifier; +import static net.kyori.adventure.text.Component.empty; +import static net.kyori.adventure.text.Component.space; +import static net.kyori.adventure.text.Component.text; +import static net.kyori.adventure.text.event.ClickEvent.runCommand; + @DefaultQualifier(NonNull.class) public final class IgnoreListCommand extends CarbonCommand { @@ -80,35 +88,65 @@ public final class IgnoreListCommand extends CarbonCommand { } private void execute(final CommandContext ctx) { + final CarbonPlayer sender = ((PlayerCommander) ctx.getSender()).carbonPlayer(); + final var elements = sender.ignoring().stream() + .sorted() // this way page numbers make sense + .map(id -> (Supplier) () -> this.users.user(id).join()) + .toList(); + + if (elements.isEmpty()) { + this.messages.commandIgnoreListNoneIgnored(sender); + return; + } + final Pagination> pagination = Pagination.>builder() - .header((currPage, pages) -> { - return Component.text("Ignores page " + currPage + "/" + pages); - }) + .header(this.messages::commandIgnoreListPaginationHeader) .item((e, lastOfPage) -> { - final CarbonPlayer ignoredPlayer = e.get(); - return Component.text(" - ").append( - ignoredPlayer.displayName().hoverEvent( - Component.text("Username: " + ignoredPlayer.username() + ", UUID: " + ignoredPlayer.uuid()) - ) - ); - }) - //.footer() // todo page buttons - .pageOutOfRange((currPage, pages) -> { - return Component.text("Page " + currPage + " out of range " + pages); + final CarbonPlayer p = e.get(); + return this.messages.commandIgnoreListPaginationElement(p.displayName(), p.username()); }) + .footer(this.footerRenderer(p -> "/" + this.commandSettings().name() + " " + p)) + .pageOutOfRange(this.messages::paginationOutOfRange) .build(); - final CarbonPlayer sender = ((PlayerCommander) ctx.getSender()).carbonPlayer(); final int page = ctx.get("page"); - pagination.render( - sender.ignoring().stream() - .sorted() // this way page numbers make sense - .map(id -> (Supplier) () -> this.users.user(id).join()) - .toList(), - page, - 6 - ).forEach(sender::sendMessage); + pagination.render(elements, page, 6).forEach(sender::sendMessage); + } + + private Pagination.BiIntFunction footerRenderer(final IntFunction commandFunction) { + return (currentPage, pages) -> { + if (pages == 1) { + return empty(); // we don't need to see 'Page 1/1' + } + final TextComponent.Builder buttons = text(); + if (currentPage > 1) { + buttons.append(this.previousPageButton(currentPage, commandFunction)); + } + if (currentPage > 1 && currentPage < pages) { + buttons.append(space()); + } + if (currentPage < pages) { + buttons.append(this.nextPageButton(currentPage, commandFunction)); + } + return this.messages.paginationFooter(currentPage, pages, buttons.build()); + }; + } + + private Component previousPageButton(final int currentPage, final IntFunction commandFunction) { + return text() + .content("←") + .clickEvent(runCommand(commandFunction.apply(currentPage - 1))) + .hoverEvent(this.messages.paginationClickForPreviousPage()) + .build(); + } + + private Component nextPageButton(final int currentPage, final IntFunction commandFunction) { + return text() + .content("→") + .clickEvent(runCommand(commandFunction.apply(currentPage + 1))) + .hoverEvent(this.messages.paginationClickForNextPage()) + .build(); } } diff --git a/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java b/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java index 9874ad67..9e9dceb2 100644 --- a/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java +++ b/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java @@ -301,6 +301,16 @@ public interface CarbonMessages { @Message("command.ignorelist.description") Component commandIgnoreListDescription(); + @Message("command.ignorelist.none_ignored") + void commandIgnoreListNoneIgnored(Audience audience); + + @Message("command.ignorelist.pagination_header") + Component commandIgnoreListPaginationHeader(int page, int pages); + + // todo make username placeholder work in click event commands (change placeholder type to TagResolver) + @Message("command.ignorelist.pagination_element") + Component commandIgnoreListPaginationElement(Component displayName, String username); + @Message("command.join.description") Component commandJoinDescription(); @@ -409,4 +419,16 @@ public interface CarbonMessages { @Message("deletemessage.prefix") Component deleteMessagePrefix(); + @Message("pagination.page_out_of_range") + Component paginationOutOfRange(int page, int pages); + + @Message("pagination.click_for_next_page") + Component paginationClickForNextPage(); + + @Message("pagination.click_for_previous_page") + Component paginationClickForPreviousPage(); + + @Message("pagination.footer") + Component paginationFooter(int page, int pages, Component buttons); + } diff --git a/common/src/main/java/net/draycia/carbon/common/messages/StandardPlaceholderResolverStrategyButDifferent.java b/common/src/main/java/net/draycia/carbon/common/messages/StandardPlaceholderResolverStrategyButDifferent.java index 195a8a1b..cf92cd55 100644 --- a/common/src/main/java/net/draycia/carbon/common/messages/StandardPlaceholderResolverStrategyButDifferent.java +++ b/common/src/main/java/net/draycia/carbon/common/messages/StandardPlaceholderResolverStrategyButDifferent.java @@ -36,6 +36,7 @@ import net.kyori.moonshine.strategy.IPlaceholderResolverStrategy; import net.kyori.moonshine.strategy.supertype.ISupertypeStrategy; import net.kyori.moonshine.strategy.supertype.StandardSupertypeThenInterfaceSupertypeStrategy; import org.checkerframework.checker.nullness.qual.Nullable; +import org.spongepowered.configurate.util.NamingScheme; import static java.util.Collections.emptyNavigableSet; @@ -43,12 +44,24 @@ import static java.util.Collections.emptyNavigableSet; public final class StandardPlaceholderResolverStrategyButDifferent implements IPlaceholderResolverStrategy { private final ISupertypeStrategy supertypeStrategy = new StandardSupertypeThenInterfaceSupertypeStrategy(false); + private final NamingScheme namingScheme; + + /** + * Create a new {@link StandardPlaceholderResolverStrategyButDifferent}. + * + * @param placeholderParamaterDefaultNamingScheme the default naming scheme for placeholders that do not specify a name explicitly, applied to parameter names + */ + public StandardPlaceholderResolverStrategyButDifferent(final NamingScheme placeholderParamaterDefaultNamingScheme) { + this.namingScheme = placeholderParamaterDefaultNamingScheme; + } @Override - public Map resolvePlaceholders(final Moonshine moonshine, - final R receiver, final I intermediateText, - final MoonshineMethod moonshineMethod, - final @Nullable Object[] parameters) + public Map resolvePlaceholders( + final Moonshine moonshine, + final R receiver, final I intermediateText, + final MoonshineMethod moonshineMethod, + final @Nullable Object[] parameters + ) throws PlaceholderResolvingException { if (parameters.length == 0) { return Collections.emptyMap(); @@ -61,7 +74,8 @@ public final class StandardPlaceholderResolverStrategyButDifferent impl moonshineMethod.reflectMethod(), moonshine.proxiedType()); // Don't resolve recipients - for (int idx = 1; idx < parameters.length; ++idx) { + final int start = moonshineMethod.reflectMethod().getReturnType() != Void.TYPE ? 0 : 1; + for (int idx = start; idx < parameters.length; ++idx) { final Parameter parameter = methodParameters[idx]; final @Nullable Object value = parameters[idx]; @@ -77,7 +91,7 @@ public final class StandardPlaceholderResolverStrategyButDifferent impl final String placeholderName = (placeholder != null && !placeholder.value().isEmpty()) ? placeholder.value() - : parameter.getName(); + : this.namingScheme.coerce(parameter.getName()); resolvingPlaceholders .put(placeholderName, ContinuanceValue.continuanceValue(value, parameterType)); @@ -98,10 +112,14 @@ public final class StandardPlaceholderResolverStrategyButDifferent impl * @param resolvingPlaceholders the placeholders to resolve * @param moonshineMethod the method we are resolving a placeholder for */ - private void resolvePlaceholder(final Moonshine moonshine, final R receiver, - final Map finalisedPlaceholders, - final Map> resolvingPlaceholders, - final MoonshineMethod moonshineMethod, final @Nullable Object[] parameters) + private void resolvePlaceholder( + final Moonshine moonshine, + final R receiver, + final Map finalisedPlaceholders, + final Map> resolvingPlaceholders, + final MoonshineMethod moonshineMethod, + final @Nullable Object[] parameters + ) throws UnfinishedPlaceholderException { final var weightedPlaceholderResolvers = moonshine.weightedPlaceholderResolvers(); diff --git a/common/src/main/java/net/draycia/carbon/common/messages/placeholders/IntPlaceholderResolver.java b/common/src/main/java/net/draycia/carbon/common/messages/placeholders/IntPlaceholderResolver.java new file mode 100644 index 00000000..f5d1efb3 --- /dev/null +++ b/common/src/main/java/net/draycia/carbon/common/messages/placeholders/IntPlaceholderResolver.java @@ -0,0 +1,46 @@ +/* + * CarbonChat + * + * Copyright (c) 2023 Josua Parks (Vicarious) + * Contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package net.draycia.carbon.common.messages.placeholders; + +import java.lang.reflect.Method; +import java.lang.reflect.Type; +import java.util.Map; +import net.kyori.adventure.text.Component; +import net.kyori.moonshine.placeholder.ConclusionValue; +import net.kyori.moonshine.placeholder.ContinuanceValue; +import net.kyori.moonshine.placeholder.IPlaceholderResolver; +import net.kyori.moonshine.util.Either; +import org.checkerframework.checker.nullness.qual.Nullable; + +public class IntPlaceholderResolver implements IPlaceholderResolver { + + @Override + public @Nullable Map, ContinuanceValue>> resolve( + final String placeholderName, + final Integer value, + final R receiver, + final Type owner, + final Method method, + final @Nullable Object[] parameters + ) { + return Map.of(placeholderName, Either.left(ConclusionValue.conclusionValue(Component.text(value)))); + } + +} diff --git a/common/src/main/resources/locale/messages-en_US.properties b/common/src/main/resources/locale/messages-en_US.properties index 697bb5cc..5f63530f 100644 --- a/common/src/main/resources/locale/messages-en_US.properties +++ b/common/src/main/resources/locale/messages-en_US.properties @@ -23,6 +23,9 @@ command.ignore.argument.player=The name of the player to ignore. command.ignore.argument.uuid=The UUID of the player to ignore. command.ignore.description=Hides all incoming messages from ignored players. command.ignorelist.description=Displays a paginated list of who you are ignoring. +command.ignorelist.none_ignored=You are not ignoring any players. +command.ignorelist.pagination_header=Ignored players +command.ignorelist.pagination_element= - '> '>'>[unignore] command.join.description=Join a channel you have previously left. command.leave.description=Leave a channel that you currently have access to. command.mute.argument.player=The name of the player to mute. @@ -112,4 +115,7 @@ channel.no_permission=You do not have permission to use this channel channel.already_left=You have already left this channel channel.not_left=You have not left this channel channel.not_found=Channel not found - +pagination.page_out_of_range=Page is out of range! There are only pages. +pagination.click_for_next_page=Click for next page +pagination.click_for_previous_page=Click for previous page +pagination.footer=Page / From 8574fb950376a068bbbfc5c3cd5153627e3d0587 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Sat, 26 Aug 2023 13:04:15 -0700 Subject: [PATCH 3/4] fix (un)ignore feedback messages --- .../carbon/common/command/commands/IgnoreCommand.java | 2 +- .../carbon/common/command/commands/UnignoreCommand.java | 5 +++++ .../net/draycia/carbon/common/messages/CarbonMessages.java | 3 +++ common/src/main/resources/locale/messages-en_US.properties | 1 + 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/IgnoreCommand.java b/common/src/main/java/net/draycia/carbon/common/command/commands/IgnoreCommand.java index 9715f9d4..310ef261 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/commands/IgnoreCommand.java +++ b/common/src/main/java/net/draycia/carbon/common/command/commands/IgnoreCommand.java @@ -98,7 +98,7 @@ public final class IgnoreCommand extends CarbonCommand { return; } - if (target.ignoring(target)) { + if (sender.ignoring(target)) { this.carbonMessages.alreadyIgnored(sender, target.displayName()); return; } diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/UnignoreCommand.java b/common/src/main/java/net/draycia/carbon/common/command/commands/UnignoreCommand.java index 9ba2a675..8436d527 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/commands/UnignoreCommand.java +++ b/common/src/main/java/net/draycia/carbon/common/command/commands/UnignoreCommand.java @@ -94,6 +94,11 @@ public final class UnignoreCommand extends CarbonCommand { return; } + if (!sender.ignoring(target)) { + this.carbonMessages.notIgnored(sender, target.displayName()); + return; + } + sender.ignoring(target, false); this.carbonMessages.noLongerIgnoring(sender, target.displayName()); }) diff --git a/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java b/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java index 9e9dceb2..9ac226f3 100644 --- a/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java +++ b/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java @@ -207,6 +207,9 @@ public interface CarbonMessages { @Message("ignore.already_ignored") void alreadyIgnored(final Audience audience, final Component target); + @Message("ignore.not_ignored") + void notIgnored(Audience audience, Component target); + @Message("ignore.exempt") void ignoreExempt(final Audience audience, final Component target); diff --git a/common/src/main/resources/locale/messages-en_US.properties b/common/src/main/resources/locale/messages-en_US.properties index 5f63530f..276135fb 100644 --- a/common/src/main/resources/locale/messages-en_US.properties +++ b/common/src/main/resources/locale/messages-en_US.properties @@ -71,6 +71,7 @@ error.command.invalid_syntax=Invalid command syntax. Correct command syntax error.command.no_permission=I'm sorry, but you do not have permission to perform this command.\nPlease contact the server administrators if you believe that this is in error. error.command.command_needs_player=Non-players must provide the player argument to execute this command. ignore.already_ignored=You are already ignoring +ignore.not_ignored=You are not ignoring ignore.exempt=You cannot ignore ignore.invalid_target=No target found ignore.now_ignoring=You are now ignoring From 73f78f75730be059a3e1b07b0370a32e4609967a Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Sat, 26 Aug 2023 13:12:00 -0700 Subject: [PATCH 4/4] adjust message --- common/src/main/resources/locale/messages-en_US.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/main/resources/locale/messages-en_US.properties b/common/src/main/resources/locale/messages-en_US.properties index 276135fb..34332a3c 100644 --- a/common/src/main/resources/locale/messages-en_US.properties +++ b/common/src/main/resources/locale/messages-en_US.properties @@ -25,7 +25,7 @@ command.ignore.description=Hides all incoming messages from ignored players. command.ignorelist.description=Displays a paginated list of who you are ignoring. command.ignorelist.none_ignored=You are not ignoring any players. command.ignorelist.pagination_header=Ignored players -command.ignorelist.pagination_element= - '> '>'>[unignore] +command.ignorelist.pagination_element= - '> '>'>[unignore] command.join.description=Join a channel you have previously left. command.leave.description=Leave a channel that you currently have access to. command.mute.argument.player=The name of the player to mute.