Skip to content

Commit

Permalink
Changed OptionalEntry to be more versatile
Browse files Browse the repository at this point in the history
  • Loading branch information
MehradN committed Jul 4, 2023
1 parent 9a0af39 commit 4ce0bd4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ parchment_version=1.19.4:2023.06.26
loader_version=0.14.21

# Mod Properties
mod_version=1.1.0
mod_version=1.2.0
maven_group=ir.mehradn
archives_base_name=mehrad-config

Expand Down
35 changes: 31 additions & 4 deletions src/main/java/ir/mehradn/mehradconfig/entry/OptionalEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* OptionalEntry is an implementation of {@link ConfigEntry}, Its default value is based on another {@code ConfigEntry}.
Expand All @@ -13,9 +14,23 @@
*/
public class OptionalEntry <T> implements ConfigEntry<T> {
private final ConfigEntry<T> optionalEntry;
private final ConfigEntry<T> fallbackEntry;
private @Nullable ConfigEntry<T> fallbackEntry;
private boolean hasValue;

/**
* The lazy constructor. If you use this constructor, you must call {@link #setFallbackEntry} as soon as possible before using any of the other
* methods, doing otherwise will cause unexpected behaviours.
* <p>
* <b>Note:</b> Always at the initial state after the constructor, the value of the optional entry is ignored.
*
* @param optionalEntry the config entry that should hold the optional value
*/
public OptionalEntry(ConfigEntry<T> optionalEntry) {
this.optionalEntry = optionalEntry;
this.fallbackEntry = null;
this.hasValue = false;
}

/**
* The main constructor.
* <p>
Expand All @@ -24,10 +39,20 @@ public class OptionalEntry <T> implements ConfigEntry<T> {
* @param optionalEntry the config entry that should hold the optional value
* @param fallbackEntry the config entry that should provide the default value, avoid using a config entry from the same config
*/
public OptionalEntry(ConfigEntry<T> optionalEntry, ConfigEntry<T> fallbackEntry) {
this.optionalEntry = optionalEntry;
public OptionalEntry(ConfigEntry<T> optionalEntry, @NotNull ConfigEntry<T> fallbackEntry) {
this(optionalEntry);
setFallbackEntry(fallbackEntry);
}

/**
* Sets the fallback entry. <b>This method must be called after the lazy constructor and only once.</b>
*
* @param fallbackEntry the config entry that should provide the default value, avoid using a config entry from the same config
*/
public void setFallbackEntry(@NotNull ConfigEntry<T> fallbackEntry) {
if (this.fallbackEntry != null)
throw new IllegalStateException("This method must be called after the lazy constructor and only once!");
this.fallbackEntry = fallbackEntry;
this.hasValue = false;
}

/**
Expand All @@ -48,6 +73,8 @@ public String getName() {

@Override
public T get() {
if (this.fallbackEntry == null)
throw new IllegalStateException("setFallbackEntry must be called before this method!");
if (this.hasValue)
return this.optionalEntry.get();
else
Expand Down

0 comments on commit 4ce0bd4

Please sign in to comment.