Fixes & improvements

This commit is contained in:
Vankka 2021-07-29 16:15:21 +03:00
parent 2f5a429fb5
commit b149acb36f
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
21 changed files with 221 additions and 91 deletions

View File

@ -24,6 +24,7 @@
package com.discordsrv.api.channel;
import com.discordsrv.api.component.MinecraftComponent;
import org.jetbrains.annotations.NotNull;
/**
* An in-game channel for sending Minecraft messages.
@ -34,12 +35,14 @@ public interface GameChannel {
* Gets the name of the plugin/mod/extension that 'owns' this game channel.
* @return the name of the owner of this game channel
*/
@NotNull
String getOwnerName();
/**
* Gets the name of this channel.
* @return the channel name
*/
@NotNull
String getChannelName();
/**
@ -52,5 +55,5 @@ public interface GameChannel {
* Send a message to this {@link GameChannel}'s participants.
* @param component the message
*/
void sendMessage(MinecraftComponent component);
void sendMessage(@NotNull MinecraftComponent component);
}

View File

@ -31,7 +31,6 @@ import java.util.concurrent.CompletableFuture;
public interface DiscordTextChannel {
String getId();
long getIdLong();
String getName();
String getTopic();

View File

@ -58,6 +58,7 @@ public interface AllowedMention {
}
class Snowflake implements AllowedMention {
private final String id;
private final boolean user;

View File

@ -23,6 +23,7 @@
package com.discordsrv.api.discord.api.message;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -145,22 +146,40 @@ public class DiscordMessageEmbed {
return footerImageUrl;
}
public MessageEmbed toJDA() {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setColor(color);
embedBuilder.setAuthor(authorName, authorUrl, authorImageUrl);
embedBuilder.setTitle(title, titleUrl);
embedBuilder.setDescription(description);
for (Field field : fields) {
embedBuilder.addField(new MessageEmbed.Field(field.getTitle(), field.getValue(), field.isInline(), false));
}
embedBuilder.setThumbnail(thumbnailUrl);
embedBuilder.setImage(imageUrl);
embedBuilder.setTimestamp(timestamp);
embedBuilder.setFooter(footer, footerImageUrl);
return embedBuilder.build();
}
public static class Field {
private final String title;
private final String value;
private final boolean inline;
public Field(String title, String value, boolean inline) {
this.title = title;
this.value = value;
public Field(@Nullable CharSequence title, @Nullable CharSequence value, boolean inline) {
this.title = title != null ? title.toString() : null;
this.value = value != null ? value.toString() : null;
this.inline = inline;
}
@Nullable
public String getTitle() {
return title;
}
@Nullable
public String getValue() {
return value;
}
@ -217,14 +236,14 @@ public class DiscordMessageEmbed {
return this;
}
public Builder setAuthor(@Nullable String authorName, @Nullable String authorUrl) {
public Builder setAuthor(@Nullable CharSequence authorName, @Nullable CharSequence authorUrl) {
return setAuthor(authorName, authorUrl, null);
}
public Builder setAuthor(@Nullable String authorName, @Nullable String authorUrl, @Nullable String authorImageUrl) {
this.authorName = authorName;
this.authorUrl = authorUrl;
this.authorImageUrl = authorImageUrl;
public Builder setAuthor(@Nullable CharSequence authorName, @Nullable CharSequence authorUrl, @Nullable CharSequence authorImageUrl) {
this.authorName = authorName != null ? authorName.toString() : null;
this.authorUrl = authorUrl != null ? authorUrl.toString() : null;
this.authorImageUrl = authorImageUrl != null ? authorImageUrl.toString() : null;
return this;
}
@ -232,8 +251,8 @@ public class DiscordMessageEmbed {
return authorName;
}
public Builder setAuthorName(@Nullable String authorName) {
this.authorName = authorName;
public Builder setAuthorName(@Nullable CharSequence authorName) {
this.authorName = authorName != null ? authorName.toString() : null;
return this;
}
@ -241,8 +260,8 @@ public class DiscordMessageEmbed {
return authorUrl;
}
public Builder setAuthorUrl(@Nullable String authorUrl) {
this.authorUrl = authorUrl;
public Builder setAuthorUrl(@Nullable CharSequence authorUrl) {
this.authorUrl = authorUrl != null ? authorUrl.toString() : null;
return this;
}
@ -250,14 +269,14 @@ public class DiscordMessageEmbed {
return authorImageUrl;
}
public Builder setAuthorImageUrl(String authorImageUrl) {
this.authorImageUrl = authorImageUrl;
public Builder setAuthorImageUrl(@Nullable CharSequence authorImageUrl) {
this.authorImageUrl = authorImageUrl != null ? authorImageUrl.toString() : null;
return this;
}
public Builder setTitle(@Nullable String title, @Nullable String titleUrl) {
this.title = title;
this.titleUrl = titleUrl;
public Builder setTitle(@Nullable CharSequence title, @Nullable CharSequence titleUrl) {
this.title = title != null ? title.toString() : null;
this.titleUrl = titleUrl != null ? titleUrl.toString() : null;
return this;
}
@ -265,8 +284,8 @@ public class DiscordMessageEmbed {
return title;
}
public Builder setTitle(@Nullable String title) {
this.title = title;
public Builder setTitle(@Nullable CharSequence title) {
this.title = title != null ? title.toString() : null;
return this;
}
@ -274,8 +293,8 @@ public class DiscordMessageEmbed {
return titleUrl;
}
public Builder setTitleUrl(@Nullable String titleUrl) {
this.titleUrl = titleUrl;
public Builder setTitleUrl(@Nullable CharSequence titleUrl) {
this.titleUrl = titleUrl != null ? titleUrl.toString() : null;
return this;
}
@ -283,8 +302,8 @@ public class DiscordMessageEmbed {
return description;
}
public Builder setDescription(@Nullable String description) {
this.description = description;
public Builder setDescription(@Nullable CharSequence description) {
this.description = description != null ? description.toString() : null;
return this;
}
@ -292,7 +311,7 @@ public class DiscordMessageEmbed {
return fields;
}
public Builder addField(@NotNull String title, @NotNull String value, boolean inline) {
public Builder addField(@NotNull CharSequence title, @NotNull CharSequence value, boolean inline) {
return addField(new Field(title, value, inline));
}
@ -310,8 +329,8 @@ public class DiscordMessageEmbed {
return thumbnailUrl;
}
public Builder setThumbnailUrl(@Nullable String thumbnailUrl) {
this.thumbnailUrl = thumbnailUrl;
public Builder setThumbnailUrl(@Nullable CharSequence thumbnailUrl) {
this.thumbnailUrl = thumbnailUrl != null ? thumbnailUrl.toString() : null;
return this;
}
@ -319,8 +338,8 @@ public class DiscordMessageEmbed {
return imageUrl;
}
public Builder setImageUrl(@Nullable String imageUrl) {
this.imageUrl = imageUrl;
public Builder setImageUrl(@Nullable CharSequence imageUrl) {
this.imageUrl = imageUrl != null ? imageUrl.toString() : null;
return this;
}
@ -333,9 +352,9 @@ public class DiscordMessageEmbed {
return this;
}
public Builder setFooter(@Nullable String footer, @Nullable String footerImageUrl) {
this.footer = footer;
this.footerImageUrl = footerImageUrl;
public Builder setFooter(@Nullable CharSequence footer, @Nullable CharSequence footerImageUrl) {
this.footer = footer != null ? footer.toString() : null;
this.footerImageUrl = footerImageUrl != null ? footerImageUrl.toString() : null;
return this;
}
@ -343,8 +362,8 @@ public class DiscordMessageEmbed {
return footer;
}
public Builder setFooter(@Nullable String footer) {
this.footer = footer;
public Builder setFooter(@Nullable CharSequence footer) {
this.footer = footer != null ? footer.toString() : null;
return this;
}
@ -352,8 +371,8 @@ public class DiscordMessageEmbed {
return footerImageUrl;
}
public Builder setFooterImageUrl(@Nullable String footerImageUrl) {
this.footerImageUrl = footerImageUrl;
public Builder setFooterImageUrl(@Nullable CharSequence footerImageUrl) {
this.footerImageUrl = footerImageUrl != null ? footerImageUrl.toString() : null;
return this;
}

View File

@ -26,6 +26,7 @@ package com.discordsrv.api.discord.api.message;
import com.discordsrv.api.discord.api.message.impl.SendableDiscordMessageImpl;
import java.util.List;
import java.util.Set;
@SuppressWarnings("unused") // API
public interface SendableDiscordMessage {
@ -46,6 +47,12 @@ public interface SendableDiscordMessage {
*/
List<DiscordMessageEmbed> getEmbeds();
/**
* Gets the allowed mentions of the message.
* @return the allowed mentions in this message
*/
Set<AllowedMention> getAllowedMentions();
/**
* Gets the webhook username.
* @return the webhook username or {@code null} if this isn't a webhook message
@ -101,6 +108,26 @@ public interface SendableDiscordMessage {
*/
Builder removeEmbed(DiscordMessageEmbed embed);
/**
* Gets the allowed mentions in this builder.
* @return the builder's current allowed mentions
*/
Set<AllowedMention> getAllowedMentions();
/**
* Adds an allowed mention to this builder.
* @param allowedMention the allowed mention to add
* @return the builder, useful for chaining
*/
Builder addAllowedMention(AllowedMention allowedMention);
/**
* Removes an allowed mention from this builder.
* @param allowedMention the allowed mention to remove
* @return the builder, useful for chaining
*/
Builder removeAllowedMention(AllowedMention allowedMention);
/**
* Gets the webhook username for this builder or {@code null} if webhooks are not being used.
* @return the webhook username

View File

@ -23,23 +23,31 @@
package com.discordsrv.api.discord.api.message.impl;
import com.discordsrv.api.discord.api.message.AllowedMention;
import com.discordsrv.api.discord.api.message.DiscordMessageEmbed;
import com.discordsrv.api.discord.api.message.SendableDiscordMessage;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class SendableDiscordMessageImpl implements SendableDiscordMessage {
private final String content;
private final List<DiscordMessageEmbed> embeds;
private final Set<AllowedMention> allowedMentions;
private final String webhookUsername;
private final String webhookAvatarUrl;
public SendableDiscordMessageImpl(String content, List<DiscordMessageEmbed> embeds,
String webhookUsername, String webhookAvatarUrl) {
public SendableDiscordMessageImpl(String content,
List<DiscordMessageEmbed> embeds,
Set<AllowedMention> allowedMentions,
String webhookUsername,
String webhookAvatarUrl) {
this.content = content;
this.embeds = embeds;
this.allowedMentions = allowedMentions;
this.webhookUsername = webhookUsername;
this.webhookAvatarUrl = webhookAvatarUrl;
}
@ -54,6 +62,11 @@ public class SendableDiscordMessageImpl implements SendableDiscordMessage {
return embeds;
}
@Override
public Set<AllowedMention> getAllowedMentions() {
return allowedMentions;
}
@Override
public String getWebhookUsername() {
return webhookUsername;
@ -68,6 +81,7 @@ public class SendableDiscordMessageImpl implements SendableDiscordMessage {
private String content;
private final List<DiscordMessageEmbed> embeds = new ArrayList<>();
private final Set<AllowedMention> allowedMentions = new HashSet<>();
private String webhookUsername;
private String webhookAvatarUrl;
@ -99,6 +113,23 @@ public class SendableDiscordMessageImpl implements SendableDiscordMessage {
return this;
}
@Override
public Set<AllowedMention> getAllowedMentions() {
return allowedMentions;
}
@Override
public Builder addAllowedMention(AllowedMention allowedMention) {
this.allowedMentions.add(allowedMention);
return this;
}
@Override
public Builder removeAllowedMention(AllowedMention allowedMention) {
this.allowedMentions.remove(allowedMention);
return this;
}
@Override
public String getWebhookUsername() {
return webhookUsername;
@ -123,7 +154,7 @@ public class SendableDiscordMessageImpl implements SendableDiscordMessage {
@Override
public SendableDiscordMessage build() {
return new SendableDiscordMessageImpl(content, embeds, webhookUsername, webhookAvatarUrl);
return new SendableDiscordMessageImpl(content, embeds, allowedMentions, webhookUsername, webhookAvatarUrl);
}
}
}

View File

@ -29,9 +29,10 @@ dependencies {
shadowJar {
archiveFileName = 'bukkit.jarinjar'
// [
// 'net.kyori.adventure'
// ].each {
// relocate it, 'com.discordsrv.dependencies.' + it
// }
[
'net.kyori.adventure'
].each {
relocate it, 'com.discordsrv.dependencies.' + it
}
// More relocations in buildscript/relocations.gradle
}

View File

@ -34,4 +34,5 @@ shadowJar {
].each {
relocate it, 'com.discordsrv.dependencies.' + it
}
// More relocations in buildscript/relocations.gradle
}

View File

@ -46,7 +46,7 @@ dependencies {
runtimeDownloadApi 'dev.vankka:mcdiscordreserializer:4.2.4-SNAPSHOT'
runtimeDownloadApi 'dev.vankka:enhancedlegacytext:1.0.0-SNAPSHOT'
// Database Drivers & SLF4J
// Database Drivers
h2Driver 'com.h2database:h2:1.4.200'
mysqlDriver 'mysql:mysql-connector-java:8.0.25'

View File

@ -18,7 +18,6 @@
package com.discordsrv.common;
import com.discordsrv.api.discord.api.DiscordAPI;
import com.discordsrv.api.discord.connection.DiscordConnectionDetails;
import com.discordsrv.api.event.bus.EventBus;
import com.discordsrv.api.event.events.lifecycle.DiscordSRVShuttingDownEvent;
@ -55,7 +54,7 @@ public abstract class AbstractDiscordSRV<C extends MainConfig, CC extends Connec
// DiscordSRVApi
private final EventBus eventBus;
private final ComponentFactory componentFactory;
private final DiscordAPI discordAPI;
private final DiscordAPIImpl discordAPI;
private final DiscordConnectionDetails discordConnectionDetails;
// DiscordSRV
@ -92,7 +91,7 @@ public abstract class AbstractDiscordSRV<C extends MainConfig, CC extends Connec
}
@Override
public @NotNull DiscordAPI discordAPI() {
public @NotNull DiscordAPIImpl discordAPI() {
return discordAPI;
}

View File

@ -27,6 +27,7 @@ import com.discordsrv.common.config.main.MainConfig;
import com.discordsrv.common.config.manager.ConnectionConfigManager;
import com.discordsrv.common.config.manager.MainConfigManager;
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.player.provider.AbstractPlayerProvider;
@ -55,6 +56,10 @@ public interface DiscordSRV extends DiscordSRVApi {
@NotNull
ComponentFactory componentFactory();
@Override
@NotNull
DiscordAPIImpl discordAPI();
// Config
ConnectionConfigManager<? extends ConnectionConfig> connectionConfigManager();
ConnectionConfig connectionConfig();

View File

@ -63,12 +63,12 @@ public class ChannelConfig {
return orDefault(gameChannel.getOwnerName(), gameChannel.getChannelName());
}
public OrDefault<BaseChannelConfig> orDefault(String pluginName, String channelName) {
public OrDefault<BaseChannelConfig> orDefault(String ownerName, String channelName) {
ChannelConfigHolder defaultConfig = channels().computeIfAbsent(
"default", key -> new ChannelConfigHolder(new BaseChannelConfig()));
return new OrDefault<>(
get(pluginName, channelName),
get(ownerName, channelName),
defaultConfig.get()
);
}
@ -77,10 +77,19 @@ public class ChannelConfig {
return get(gameChannel.getOwnerName(), gameChannel.getChannelName());
}
public BaseChannelConfig get(String pluginName, String channelName) {
if (pluginName != null) {
ChannelConfigHolder config = channels().get(pluginName + ":" + channelName);
return config != null ? config.get() : null;
public BaseChannelConfig get(String ownerName, String channelName) {
if (ownerName != null) {
ChannelConfigHolder config = channels().get(ownerName + ":" + channelName);
if (config != null) {
return config.get();
}
GameChannel gameChannel = CHANNELS.get(channelName);
if (gameChannel != null && gameChannel.getOwnerName().equals(ownerName)) {
config = channels().get(channelName);
return config != null ? config.get() : null;
}
return null;
}
GameChannel gameChannel = CHANNELS.get(channelName);

View File

@ -24,6 +24,7 @@ import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.component.util.ComponentUtil;
import com.discordsrv.common.player.IPlayer;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;
public class DefaultGlobalChannel implements GameChannel {
@ -34,12 +35,12 @@ public class DefaultGlobalChannel implements GameChannel {
}
@Override
public String getOwnerName() {
public @NotNull String getOwnerName() {
return "DiscordSRV";
}
@Override
public String getChannelName() {
public @NotNull String getChannelName() {
return "global";
}
@ -49,7 +50,7 @@ public class DefaultGlobalChannel implements GameChannel {
}
@Override
public void sendMessage(MinecraftComponent minecraftComponent) {
public void sendMessage(@NotNull MinecraftComponent minecraftComponent) {
Component component = ComponentUtil.fromAPI(minecraftComponent);
for (IPlayer player : discordSRV.playerProvider().allPlayers()) {
player.sendMessage(component);

View File

@ -41,11 +41,6 @@ public class DiscordTextChannelImpl implements DiscordTextChannel {
return textChannel.getId();
}
@Override
public long getIdLong() {
return textChannel.getIdLong();
}
@Override
public String getName() {
return textChannel.getName();

View File

@ -19,6 +19,7 @@
package com.discordsrv.common.discord.api.message;
import com.discordsrv.api.discord.api.channel.DiscordTextChannel;
import com.discordsrv.api.discord.api.message.AllowedMention;
import com.discordsrv.api.discord.api.message.DiscordMessageEmbed;
import com.discordsrv.api.discord.api.message.ReceivedDiscordMessage;
import com.discordsrv.api.discord.api.message.SendableDiscordMessage;
@ -26,9 +27,13 @@ import com.discordsrv.api.discord.api.message.impl.SendableDiscordMessageImpl;
import com.discordsrv.common.DiscordSRV;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.User;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
public class ReceivedDiscordMessageImpl extends SendableDiscordMessageImpl implements ReceivedDiscordMessage {
@ -41,7 +46,21 @@ public class ReceivedDiscordMessageImpl extends SendableDiscordMessageImpl imple
return mappedEmbeds;
}
private static String getWebhookUsername(Message message) {
private static Set<AllowedMention> allowedMentions(Message message) {
Set<AllowedMention> allowedMentions = new HashSet<>();
if (message.mentionsEveryone()) {
allowedMentions.add(AllowedMention.EVERYONE);
}
for (User user : message.getMentionedUsers()) {
allowedMentions.add(AllowedMention.user(user.getId()));
}
for (Role role : message.getMentionedRoles()) {
allowedMentions.add(AllowedMention.role(role.getId()));
}
return allowedMentions;
}
private static String webhookUsername(Message message) {
if (!message.isWebhookMessage()) {
return null;
}
@ -49,7 +68,7 @@ public class ReceivedDiscordMessageImpl extends SendableDiscordMessageImpl imple
return message.getAuthor().getName();
}
private static String getWebhookAvatarUrl(Message message) {
private static String webhookAvatarUrl(Message message) {
if (!message.isWebhookMessage()) {
return null;
}
@ -64,8 +83,9 @@ public class ReceivedDiscordMessageImpl extends SendableDiscordMessageImpl imple
super(
message.getContentRaw(),
mapEmbeds(message.getEmbeds()),
getWebhookUsername(message),
getWebhookAvatarUrl(message)
allowedMentions(message),
webhookUsername(message),
webhookAvatarUrl(message)
);
this.discordSRV = discordSRV;
this.message = message;

View File

@ -21,12 +21,13 @@ package com.discordsrv.common.discord.api.message.util;
import club.minnced.discord.webhook.send.WebhookMessage;
import club.minnced.discord.webhook.send.WebhookMessageBuilder;
import com.discordsrv.api.discord.api.message.AllowedMention;
import com.discordsrv.api.discord.api.message.DiscordMessageEmbed;
import com.discordsrv.api.discord.api.message.SendableDiscordMessage;
import net.dv8tion.jda.api.MessageBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public final class SendableDiscordMessageUtil {
@ -38,7 +39,7 @@ public final class SendableDiscordMessageUtil {
List<String> allowedUsers = new ArrayList<>();
List<String> allowedRoles = new ArrayList<>();
for (AllowedMention allowedMention : Collections.singletonList(AllowedMention.ALL_USERS)) { // TODO
for (AllowedMention allowedMention : message.getAllowedMentions()) {
if (allowedMention instanceof AllowedMention.Snowflake) {
String id = ((AllowedMention.Snowflake) allowedMention).getId();
if (((AllowedMention.Snowflake) allowedMention).isUser()) {
@ -51,18 +52,18 @@ public final class SendableDiscordMessageUtil {
}
}
// Always allow these
allowedTypes.add(Message.MentionType.EMOTE);
allowedTypes.add(Message.MentionType.CHANNEL);
List<MessageEmbed> embeds = new ArrayList<>();
for (DiscordMessageEmbed embed : message.getEmbeds()) {
embeds.add(embed.toJDA());
}
MessageBuilder messageBuilder = new MessageBuilder()
return new MessageBuilder()
.setContent(message.getContent())
//.setEmbeds() // TODO
.setEmbeds(embeds)
.setAllowedMentions(allowedTypes)
.mentionUsers(allowedUsers.toArray(new String[0]))
.mentionRoles(allowedRoles.toArray(new String[0]));
return messageBuilder.build();
.mentionRoles(allowedRoles.toArray(new String[0]))
.build();
}
public static WebhookMessage toWebhook(SendableDiscordMessage message) {

View File

@ -37,12 +37,14 @@ import net.dv8tion.jda.api.events.StatusChangeEvent;
import net.dv8tion.jda.api.requests.CloseCode;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.utils.AllowedMentions;
import net.dv8tion.jda.api.utils.MemberCachePolicy;
import net.dv8tion.jda.internal.hooks.EventManagerProxy;
import net.dv8tion.jda.internal.utils.IOUtil;
import okhttp3.OkHttpClient;
import javax.security.auth.login.LoginException;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.*;
@ -67,6 +69,7 @@ public class JDAConnectionManager implements DiscordConnectionManager {
new CountingThreadFactory(Scheduler.THREAD_NAME_PREFIX + "JDA RateLimit #%s")
);
RestAction.setDefaultFailure(t -> discordSRV.logger().error("Callback failed", t));
AllowedMentions.setDefaultMentions(Collections.emptyList());
discordSRV.eventBus().subscribe(this);
}
@ -132,7 +135,9 @@ public class JDAConnectionManager implements DiscordConnectionManager {
}
private boolean checkCode(CloseCode closeCode) {
if (closeCode == CloseCode.DISALLOWED_INTENTS) {
if (closeCode == null) {
return false;
} else if (closeCode == CloseCode.DISALLOWED_INTENTS) {
// TODO
return true;
} else if (closeCode.isReconnect()) {

View File

@ -18,6 +18,8 @@
package com.discordsrv.common.function;
import org.jetbrains.annotations.Nullable;
import java.util.function.Function;
public class OrDefault<T> {
@ -30,6 +32,12 @@ public class OrDefault<T> {
this.secondary = secondary;
}
public <R> R get(Function<T, R> function, R otherwise) {
R value = get(function);
return value != null ? value : otherwise;
}
@Nullable
public <R> R get(Function<T, R> function) {
if (primary != null) {
R primaryValue = function.apply(primary);
@ -40,4 +48,16 @@ public class OrDefault<T> {
return function.apply(secondary);
}
public <R> OrDefault<R> map(Function<T, R> mappingFunction) {
R primaryValue = null;
R secondaryValue = null;
if (primary != null) {
primaryValue = mappingFunction.apply(primary);
}
if (secondary != null) {
secondaryValue = mappingFunction.apply(secondary);
}
return new OrDefault<>(primaryValue, secondaryValue);
}
}

View File

@ -43,7 +43,6 @@ import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
public class DefaultChatListener extends AbstractListener {
@ -63,22 +62,12 @@ public class DefaultChatListener extends AbstractListener {
OrDefault<BaseChannelConfig> channelConfig = discordSRV.channelConfig().orDefault(gameChannel);
Component discordMessage = EnhancedLegacyText.get().buildComponent(
channelConfig.get(cfg ->
Optional.ofNullable(cfg.minecraftToDiscord)
.map(config -> config.messageFormat)
.orElse(null)
))
Component discordMessage = EnhancedLegacyText.get().buildComponent(channelConfig.map(cfg -> cfg.minecraftToDiscord).get(cfg -> cfg.messageFormat))
.replace("%message%", message)
.replace("%player_display_name%", displayName)
.build();
String username = new Placeholders(
channelConfig.get(cfg ->
Optional.ofNullable(cfg.minecraftToDiscord)
.map(config -> config.usernameFormat)
.orElse(null)
))
String username = new Placeholders(channelConfig.map(cfg -> cfg.minecraftToDiscord).get(cfg -> cfg.usernameFormat))
.replace("%player_display_name%", () -> PlainTextComponentSerializer.plainText().serialize(displayName))
.get();

View File

@ -27,4 +27,6 @@ dependencies {
shadowJar {
archiveFileName = 'sponge.jarinjar'
// Relocations in buildscript/relocations.gradle
}

View File

@ -28,6 +28,8 @@ jar {
shadowJar {
archiveBaseName = 'DiscordSRV-Velocity'
// Relocations in buildscript/relocations.gradle
}
blossom {