mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 14:38:48 +00:00
ignore list command
This commit is contained in:
10 files changed
+325
-3
No files matched your search
+114
@@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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<Commander> commandManager;
|
||||
private final CarbonMessages messages;
|
||||
|
||||
@Inject
|
||||
public IgnoreListCommand(
|
||||
final UserManager<?> userManager,
|
||||
final CommandManager<Commander> 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.<Commander>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<Commander> ctx) {
|
||||
final Pagination<Supplier<CarbonPlayer>> pagination = Pagination.<Supplier<CarbonPlayer>>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<CarbonPlayer>) () -> this.users.user(id).join())
|
||||
.toList(),
|
||||
page,
|
||||
6
|
||||
).forEach(sender::sendMessage);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -210,7 +210,8 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
|
||||
this.muted.set(muted);
|
||||
}
|
||||
|
||||
public Set<UUID> ignoredPlayers() {
|
||||
@Override
|
||||
public Set<UUID> ignoring() {
|
||||
return this.ignoredPlayers.get();
|
||||
}
|
||||
|
||||
|
||||
@@ -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<UUID> ignoring() {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean ignoring(final UUID player) {
|
||||
return false;
|
||||
|
||||
@@ -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<UUID> ignoring() {
|
||||
return this.carbonPlayerCommon.ignoring();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean ignoring(final UUID player) {
|
||||
return this.carbonPlayerCommon.ignoring(player);
|
||||
|
||||
@@ -136,7 +136,7 @@ public final class DatabaseUserManager extends CachingUserManager {
|
||||
.bind("id", player.uuid())
|
||||
.execute();
|
||||
|
||||
final Set<UUID> ignored = player.ignoredPlayers();
|
||||
final Set<UUID> ignored = player.ignoring();
|
||||
if (!ignored.isEmpty()) {
|
||||
final PreparedBatch batch = handle.prepareBatch(this.locator.query("save-ignores"));
|
||||
for (final UUID ignoredPlayer : ignored) {
|
||||
|
||||
@@ -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<Class<? extends CarbonCommand>> 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<CarbonCommand> CONSTRUCTED_COMMANDS = new ArrayList<>();
|
||||
|
||||
|
||||
@@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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<T> {
|
||||
|
||||
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<Component> render(
|
||||
final Collection<T> 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<Component> 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<T> 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<T> 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 <T> Builder<T> builder() {
|
||||
return new Builder<>();
|
||||
}
|
||||
|
||||
final class Builder<T> {
|
||||
private BiIntFunction<ComponentLike> headerRenderer = ($, $$) -> empty();
|
||||
private BiIntFunction<ComponentLike> footerRenderer = ($, $$) -> empty();
|
||||
private @MonotonicNonNull BiIntFunction<ComponentLike> pageOutOfRangeRenderer = null;
|
||||
private @MonotonicNonNull ItemRenderer<T> itemRenderer = null;
|
||||
|
||||
private Builder() {
|
||||
}
|
||||
|
||||
public Builder<T> header(final BiIntFunction<ComponentLike> headerRenderer) {
|
||||
this.headerRenderer = headerRenderer;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder<T> footer(final BiIntFunction<ComponentLike> footerRenderer) {
|
||||
this.footerRenderer = footerRenderer;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder<T> pageOutOfRange(final BiIntFunction<ComponentLike> pageOutOfRangeRenderer) {
|
||||
this.pageOutOfRangeRenderer = pageOutOfRangeRenderer;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder<T> item(final ItemRenderer<T> itemRenderer) {
|
||||
this.itemRenderer = itemRenderer;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Pagination<T> 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<T> {
|
||||
ComponentLike render(T item, boolean lastOfPage);
|
||||
}
|
||||
|
||||
private record DelegatingPaginationImpl<T>(
|
||||
BiIntFunction<ComponentLike> headerRenderer,
|
||||
BiIntFunction<ComponentLike> footerRenderer,
|
||||
BiIntFunction<ComponentLike> pageOutOfRangeRenderer,
|
||||
ItemRenderer<T> itemRenderer
|
||||
) implements Pagination<T> {
|
||||
@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> {
|
||||
|
||||
T apply(int i, int i1);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user