Skip to content

Commit

Permalink
Add LazyEmbed#disabledDefaults
Browse files Browse the repository at this point in the history
  • Loading branch information
srnyx committed Oct 11, 2024
1 parent c865da7 commit a3f048d
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion src/main/java/xyz/srnyx/lazylibrary/LazyEmbed.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public class LazyEmbed {
* Replacements for all values that will be replaced when building the {@link MessageEmbed}
*/
@NotNull protected final Map<String, String> replacements = new HashMap<>();
/**
* The {@link Key keys} that are disabled from being set by the {@link LazySettings#embedDefaults}
*/
@NotNull protected final Set<Key> disabledDefaults = new HashSet<>();

/**
* The color of the embed
Expand Down Expand Up @@ -132,6 +136,10 @@ public LazyEmbed(@NotNull ConfigurationNode node) {
* @param lazyEmbed the {@link LazyEmbed} to copy
*/
public LazyEmbed(@NotNull LazyEmbed lazyEmbed) {
replacements.putAll(lazyEmbed.replacements);
disabledDefaults.addAll(lazyEmbed.disabledDefaults);

// Embed data
setColor(lazyEmbed.color);
setAuthor(lazyEmbed.authorName, lazyEmbed.authorUrl, lazyEmbed.authorIcon);
setTitle(lazyEmbed.titleText, lazyEmbed.titleUrl);
Expand Down Expand Up @@ -318,6 +326,44 @@ public LazyEmbed replace(@NotNull String key, @Nullable Object value) {
return this;
}

/**
* Replaces multiple keys with values in all parameters of the {@link LazyEmbed embed}
*
* @param replacements the replacements to make
*
* @return the {@link LazyEmbed} instance
*/
@NotNull
public LazyEmbed replace(@NotNull Map<String, Object> replacements) {
for (final Map.Entry<String, Object> entry : replacements.entrySet()) replace(entry.getKey(), entry.getValue());
return this;
}

/**
* Disables keys from being set by the {@link LazySettings#embedDefaults}
*
* @param keys the keys to disable
*
* @return the {@link LazyEmbed} instance
*/
@NotNull
public LazyEmbed disableDefaults(@NotNull Key... keys) {
return disableDefaults(Arrays.asList(keys));
}

/**
* Disables keys from being set by the {@link LazySettings#embedDefaults}
*
* @param keys the keys to disable
*
* @return the {@link LazyEmbed} instance
*/
@NotNull
public LazyEmbed disableDefaults(@NotNull Collection<Key> keys) {
disabledDefaults.addAll(keys);
return this;
}

/**
* Convenience method for {@link Factory#Factory(LazyEmbed) new Factory(LazyEmbed)} using this {@link LazyEmbed}
*
Expand Down Expand Up @@ -381,8 +427,10 @@ public MessageEmbed build(@NotNull LazyLibrary library) {

// Set defaults
for (final Map.Entry<Key, Object> entry : library.settings.embedDefaults.entrySet()) {
final Key key = entry.getKey();
if (disabledDefaults.contains(key)) continue;
final Object value = entry.getValue();
if (value != null) entry.getKey().setter.accept(this, value);
if (value != null) key.setter.accept(this, value);
}

return builder.build();
Expand Down

0 comments on commit a3f048d

Please sign in to comment.