mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 14:38:48 +00:00
ignorelist messages
This commit is contained in:
7 files changed
+170
-34
No files matched your search
@@ -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<Audience> componentPlaceholderResolver,
|
||||
final UUIDPlaceholderResolver<Audience> uuidPlaceholderResolver,
|
||||
final StringPlaceholderResolver<Audience> stringPlaceholderResolver,
|
||||
final IntPlaceholderResolver<Audience> intPlaceholderResolver,
|
||||
final KeyPlaceholderResolver<Audience> keyPlaceholderResolver,
|
||||
final BooleanPlaceholderResolver<Audience> 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());
|
||||
|
||||
@@ -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());
|
||||
|
||||
+60
-22
@@ -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<Commander> ctx) {
|
||||
final CarbonPlayer sender = ((PlayerCommander) ctx.getSender()).carbonPlayer();
|
||||
final var elements = sender.ignoring().stream()
|
||||
.sorted() // this way page numbers make sense
|
||||
.map(id -> (Supplier<CarbonPlayer>) () -> this.users.user(id).join())
|
||||
.toList();
|
||||
|
||||
if (elements.isEmpty()) {
|
||||
this.messages.commandIgnoreListNoneIgnored(sender);
|
||||
return;
|
||||
}
|
||||
|
||||
final Pagination<Supplier<CarbonPlayer>> pagination = Pagination.<Supplier<CarbonPlayer>>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<CarbonPlayer>) () -> this.users.user(id).join())
|
||||
.toList(),
|
||||
page,
|
||||
6
|
||||
).forEach(sender::sendMessage);
|
||||
pagination.render(elements, page, 6).forEach(sender::sendMessage);
|
||||
}
|
||||
|
||||
private Pagination.BiIntFunction<ComponentLike> footerRenderer(final IntFunction<String> 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<String> commandFunction) {
|
||||
return text()
|
||||
.content("←")
|
||||
.clickEvent(runCommand(commandFunction.apply(currentPage - 1)))
|
||||
.hoverEvent(this.messages.paginationClickForPreviousPage())
|
||||
.build();
|
||||
}
|
||||
|
||||
private Component nextPageButton(final int currentPage, final IntFunction<String> commandFunction) {
|
||||
return text()
|
||||
.content("→")
|
||||
.clickEvent(runCommand(commandFunction.apply(currentPage + 1)))
|
||||
.hoverEvent(this.messages.paginationClickForNextPage())
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
+28
-10
@@ -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<R, I, F> implements IPlaceholderResolverStrategy<R, I, F> {
|
||||
|
||||
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<String, ? extends F> resolvePlaceholders(final Moonshine<R, I, ?, F> moonshine,
|
||||
final R receiver, final I intermediateText,
|
||||
final MoonshineMethod<? extends R> moonshineMethod,
|
||||
final @Nullable Object[] parameters)
|
||||
public Map<String, ? extends F> resolvePlaceholders(
|
||||
final Moonshine<R, I, ?, F> moonshine,
|
||||
final R receiver, final I intermediateText,
|
||||
final MoonshineMethod<? extends R> moonshineMethod,
|
||||
final @Nullable Object[] parameters
|
||||
)
|
||||
throws PlaceholderResolvingException {
|
||||
if (parameters.length == 0) {
|
||||
return Collections.emptyMap();
|
||||
@@ -61,7 +74,8 @@ public final class StandardPlaceholderResolverStrategyButDifferent<R, I, F> 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<R, I, F> 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<R, I, F> impl
|
||||
* @param resolvingPlaceholders the placeholders to resolve
|
||||
* @param moonshineMethod the method we are resolving a placeholder for
|
||||
*/
|
||||
private void resolvePlaceholder(final Moonshine<R, I, ?, F> moonshine, final R receiver,
|
||||
final Map<String, F> finalisedPlaceholders,
|
||||
final Map<String, ContinuanceValue<?>> resolvingPlaceholders,
|
||||
final MoonshineMethod<? extends R> moonshineMethod, final @Nullable Object[] parameters)
|
||||
private void resolvePlaceholder(
|
||||
final Moonshine<R, I, ?, F> moonshine,
|
||||
final R receiver,
|
||||
final Map<String, F> finalisedPlaceholders,
|
||||
final Map<String, ContinuanceValue<?>> resolvingPlaceholders,
|
||||
final MoonshineMethod<? extends R> moonshineMethod,
|
||||
final @Nullable Object[] parameters
|
||||
)
|
||||
throws UnfinishedPlaceholderException {
|
||||
final var weightedPlaceholderResolvers = moonshine.weightedPlaceholderResolvers();
|
||||
|
||||
|
||||
+46
@@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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<R> implements IPlaceholderResolver<R, Integer, Component> {
|
||||
|
||||
@Override
|
||||
public @Nullable Map<String, Either<ConclusionValue<? extends Component>, 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))));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user