Add default value for user not having any roles

This commit is contained in:
Vankka 2021-10-23 02:20:15 +03:00
parent f35d8d67ee
commit e7d93a4176
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
14 changed files with 103 additions and 44 deletions

View File

@ -62,6 +62,7 @@ public interface DiscordSRVApi {
* DiscordSRV's own placeholder service.
* @return the {@link PlaceholderService} instance.
*/
@NotNull
PlaceholderService placeholderService();
/**

View File

@ -29,7 +29,7 @@ import java.util.function.Supplier;
public final class ResultMappers {
private static final ThreadLocal<Boolean> PLAIN_COMPONENTS = new ThreadLocal<>();
private static final ThreadLocal<Boolean> PLAIN_COMPONENTS = ThreadLocal.withInitial(() -> false);
private ResultMappers() {}

View File

@ -47,7 +47,7 @@ allprojects {
}
generateRuntimeDownloadResourceForRuntimeDownloadOnly {
file = 'runtimeDownload-' + project.name + '.txt'
file = 'dependencies/runtimeDownload-' + project.name + '.txt'
}
repositories {

View File

@ -9,12 +9,12 @@ configurations {
task generateResourceForH2Driver(type: GenerateDependencyDownloadResourceTask) {
var conf = configurations.h2Driver
configuration = conf
file = conf.name + '.txt'
file = 'dependencies/' + conf.name + '.txt'
}
task generateResourceForMySQLDriver(type: GenerateDependencyDownloadResourceTask) {
var conf = configurations.mysqlDriver
configuration = conf
file = conf.name + '.txt'
file = 'dependencies/' + conf.name + '.txt'
}
dependencies {

View File

@ -22,7 +22,6 @@ import com.discordsrv.api.discord.connection.DiscordConnectionDetails;
import com.discordsrv.api.event.bus.EventBus;
import com.discordsrv.api.event.events.lifecycle.DiscordSRVReloadEvent;
import com.discordsrv.api.event.events.lifecycle.DiscordSRVShuttingDownEvent;
import com.discordsrv.api.placeholder.PlaceholderService;
import com.discordsrv.common.api.util.ApiInstanceUtil;
import com.discordsrv.common.channel.ChannelConfigHelper;
import com.discordsrv.common.channel.DefaultGlobalChannel;
@ -43,8 +42,9 @@ import com.discordsrv.common.listener.DiscordChatListener;
import com.discordsrv.common.listener.GameChatListener;
import com.discordsrv.common.logging.DependencyLoggingFilter;
import com.discordsrv.common.logging.logger.backend.LoggingBackend;
import com.discordsrv.common.placeholder.PlaceholderServiceImpl;
import com.discordsrv.common.placeholder.ComponentResultStringifier;
import com.discordsrv.common.placeholder.PlaceholderServiceImpl;
import com.discordsrv.common.placeholder.context.GlobalTextHandlingContext;
import net.dv8tion.jda.api.JDA;
import org.jetbrains.annotations.NotNull;
@ -67,7 +67,7 @@ public abstract class AbstractDiscordSRV<C extends MainConfig, CC extends Connec
// DiscordSRVApi
private EventBus eventBus;
private PlaceholderService placeholderService;
private PlaceholderServiceImpl placeholderService;
private ComponentFactory componentFactory;
private DiscordAPIImpl discordAPI;
private DiscordConnectionDetails discordConnectionDetails;
@ -106,7 +106,7 @@ public abstract class AbstractDiscordSRV<C extends MainConfig, CC extends Connec
}
@Override
public @NotNull PlaceholderService placeholderService() {
public @NotNull PlaceholderServiceImpl placeholderService() {
return placeholderService;
}
@ -245,8 +245,9 @@ public abstract class AbstractDiscordSRV<C extends MainConfig, CC extends Connec
discordConnectionManager = new JDAConnectionManager(this);
discordConnectionManager.connect().join();
// Placeholder result stringifiers
// Placeholder result stringifiers & global contexts
placeholderService().addResultMapper(new ComponentResultStringifier(this));
placeholderService().addGlobalContext(new GlobalTextHandlingContext(this));
// Register PlayerProvider listeners
playerProvider().subscribe();

View File

@ -30,6 +30,7 @@ import com.discordsrv.common.console.Console;
import com.discordsrv.common.discord.api.DiscordAPIImpl;
import com.discordsrv.common.discord.connection.DiscordConnectionManager;
import com.discordsrv.common.logging.logger.Logger;
import com.discordsrv.common.placeholder.PlaceholderServiceImpl;
import com.discordsrv.common.player.provider.AbstractPlayerProvider;
import com.discordsrv.common.scheduler.Scheduler;
import com.github.benmanes.caffeine.cache.Caffeine;
@ -54,6 +55,10 @@ public interface DiscordSRV extends DiscordSRVApi {
@NotNull
ComponentFactory componentFactory();
@Override
@NotNull
PlaceholderServiceImpl placeholderService();
@Override
@NotNull
AbstractPlayerProvider<?> playerProvider();

View File

@ -56,21 +56,29 @@ public class EnhancedTextBuilderImpl implements EnhancedTextBuilder {
@Override
public @NotNull EnhancedTextBuilder addReplacement(Pattern target, Function<Matcher, Object> replacement) {
this.replacements.put(target, replacement);
this.replacements.put(target, wrapFunction(replacement));
return this;
}
@Override
public @NotNull EnhancedTextBuilder applyPlaceholderService() {
this.replacements.put(PlaceholderService.PATTERN, matcher -> {
Object result = discordSRV.placeholderService().getResult(matcher, context);
this.replacements.put(PlaceholderService.PATTERN, wrapFunction(
matcher -> discordSRV.placeholderService().getResult(matcher, context)));
return this;
}
private Function<Matcher, Object> wrapFunction(Function<Matcher, Object> function) {
return matcher -> {
Object result = function.apply(matcher);
if (result instanceof Color) {
// Convert Color to something it'll understand
return TextColor.color(((Color) result).rgb());
} else if (result instanceof MinecraftComponent) {
// Convert to adventure component from API
return ComponentUtil.fromAPI((MinecraftComponent) result);
}
return result;
});
return this;
};
}
@Override

View File

@ -29,20 +29,29 @@ import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.Optional;
import java.util.function.Supplier;
public class DiscordSRVMinecraftRenderer extends DefaultMinecraftRenderer {
private static final ThreadLocal<Long> GUILD_CONTEXT = new ThreadLocal<>();
private static final ThreadLocal<Long> GUILD_CONTEXT = ThreadLocal.withInitial(() -> 0L);
private final DiscordSRV discordSRV;
public DiscordSRVMinecraftRenderer(DiscordSRV discordSRV) {
this.discordSRV = discordSRV;
}
public static void inGuildContext(long guildId, Runnable runnable) {
GUILD_CONTEXT.set(guildId);
public static void runInGuildContext(long guildId, Runnable runnable) {
getWithGuildContext(guildId, () -> {
runnable.run();
return null;
});
}
public static <T> T getWithGuildContext(long guildId, Supplier<T> supplier) {
GUILD_CONTEXT.set(guildId);
T output = supplier.get();
GUILD_CONTEXT.set(0L);
return output;
}
@Override

View File

@ -18,8 +18,6 @@
package com.discordsrv.common.config.main.channels;
import com.discordsrv.common.config.main.channels.discordtominecraft.DiscordToMinecraftChatConfig;
import com.discordsrv.common.config.main.channels.minecraftodiscord.MinecraftToDiscordChatConfig;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;

View File

@ -16,7 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.discordsrv.common.config.main.channels.discordtominecraft;
package com.discordsrv.common.config.main.channels;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Comment;
@ -31,10 +31,10 @@ import java.util.regex.Pattern;
public class DiscordToMinecraftChatConfig {
@Comment("The Discord to Minecraft message format for regular users")
public String format = "[&#5865F2Discord&r] [hover:show_text:Tag: %user_tag%&r\\nRoles: %user_roles_, %]%user_color%%user_effective_name%&r » %message%";
public String format = "[&#5865F2Discord&r] [hover:show_text:Tag: %user_tag%&r\\nRoles: %user_roles_, |text_&7&oNone%%]%user_color%%user_effective_name%&r » %message%";
@Comment("The Discord to Minecraft message format for webhook messages (if enabled)")
public String webhookFormat = "[&#5865F2Discord&r] [hover:show_text:Webhook message]%user_name% » %message%";
public String webhookFormat = "[&#5865F2Discord&r] [hover:show_text:Webhook message]%user_name%&r » %message%";
@Comment("Users, bots and webhooks to ignore")
public Ignores ignores = new Ignores();

View File

@ -16,7 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.discordsrv.common.config.main.channels.minecraftodiscord;
package com.discordsrv.common.config.main.channels;
import com.discordsrv.api.discord.api.entity.message.SendableDiscordMessage;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;

View File

@ -33,7 +33,7 @@ import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.component.renderer.DiscordSRVMinecraftRenderer;
import com.discordsrv.common.component.util.ComponentUtil;
import com.discordsrv.common.config.main.channels.BaseChannelConfig;
import com.discordsrv.common.config.main.channels.discordtominecraft.DiscordToMinecraftChatConfig;
import com.discordsrv.common.config.main.channels.DiscordToMinecraftChatConfig;
import com.discordsrv.common.function.OrDefault;
import net.kyori.adventure.text.Component;
import org.apache.commons.lang3.tuple.Pair;
@ -105,19 +105,19 @@ public class DiscordChatListener extends AbstractListener {
}
}
String format = chatConfig.opt(cfg -> webhookMessage ? cfg.format : cfg.webhookFormat)
String format = chatConfig.opt(cfg -> webhookMessage ? cfg.webhookFormat : cfg.format)
.map(option -> option.replace("\\n", "\n"))
.orElse(null);
if (format == null) {
return;
}
DiscordSRVMinecraftRenderer.inGuildContext(channel.getGuild().getId(), () -> {
Placeholders message = new Placeholders(event.getMessageContent());
chatConfig.opt(cfg -> cfg.contentRegexFilters)
.ifPresent(filters -> filters.forEach(message::replaceAll));
Component messageComponent = discordSRV.componentFactory().minecraftSerializer().serialize(message.toString());
Component messageComponent = DiscordSRVMinecraftRenderer.getWithGuildContext(channel.getGuild().getId(), () ->
discordSRV.componentFactory().minecraftSerializer().serialize(message.toString()));
EnhancedTextBuilder componentBuilder = discordSRV.componentFactory()
.enhancedBuilder(format)
@ -134,6 +134,5 @@ public class DiscordChatListener extends AbstractListener {
}
gameChannel.sendMessage(component);
});
}
}

View File

@ -30,7 +30,7 @@ import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.component.util.ComponentUtil;
import com.discordsrv.common.config.main.channels.BaseChannelConfig;
import com.discordsrv.common.config.main.channels.ChannelConfig;
import com.discordsrv.common.config.main.channels.minecraftodiscord.MinecraftToDiscordChatConfig;
import com.discordsrv.common.config.main.channels.MinecraftToDiscordChatConfig;
import com.discordsrv.common.discord.api.message.ReceivedDiscordMessageClusterImpl;
import com.discordsrv.common.function.OrDefault;
import net.kyori.adventure.text.Component;

View File

@ -0,0 +1,38 @@
/*
* This file is part of DiscordSRV, licensed under the GPLv3 License
* Copyright (c) 2016-2021 Austin "Scarsz" Shapiro, Henri "Vankka" Schubin and DiscordSRV 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 com.discordsrv.common.placeholder.context;
import com.discordsrv.api.component.MinecraftComponent;
import com.discordsrv.api.placeholder.annotation.Placeholder;
import com.discordsrv.api.placeholder.annotation.PlaceholderRemainder;
import com.discordsrv.common.DiscordSRV;
public class GlobalTextHandlingContext {
private final DiscordSRV discordSRV;
public GlobalTextHandlingContext(DiscordSRV discordSRV) {
this.discordSRV = discordSRV;
}
@Placeholder("text_")
public MinecraftComponent text(@PlaceholderRemainder String text) {
return discordSRV.componentFactory().enhancedBuilder(text).build();
}
}