-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
385 additions
and
287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 44 additions & 1 deletion
45
src/main/java/io/ix0rai/rainglow/config/DeferredSaveOption.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,50 @@ | ||
package io.ix0rai.rainglow.config; | ||
|
||
import net.minecraft.client.MinecraftClient; | ||
import net.minecraft.client.option.GameOptions; | ||
import net.minecraft.client.option.Option; | ||
import net.minecraft.text.Text; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Consumer; | ||
|
||
public class DeferredSaveOption<T> extends Option<T> { | ||
private final T deferredValue; | ||
private T deferredValue; | ||
|
||
public DeferredSaveOption(String key, TooltipSupplier<T> tooltipSupplier, OptionTextGetter<T> textGetter, Option.ValueSet<T> values, T defaultValue, Consumer<T> updateCallback) { | ||
super(key, tooltipSupplier, textGetter, values, defaultValue, updateCallback); | ||
this.deferredValue = this.value; | ||
} | ||
|
||
@Override | ||
public void set(T value) { | ||
T object = (T) this.getValues().validate(value).orElseGet(() -> { | ||
System.out.println("Illegal option value " + value + " for " + this.text); | ||
return this.defaultValue; | ||
}); | ||
if (!MinecraftClient.getInstance().isRunning()) { | ||
this.deferredValue = object; | ||
} else { | ||
if (!Objects.equals(this.value, object)) { | ||
this.deferredValue = object; | ||
this.updateCallback.accept(this.deferredValue); | ||
} | ||
} | ||
} | ||
|
||
public static DeferredSaveOption<Boolean> createBoolean(boolean defaultValue, String key) { | ||
return new DeferredSaveOption<>( | ||
"rainglow.config." + key, | ||
Option.constantTooltip(Text.translatable("rainglow.config.tooltip." + key)), | ||
(text, value) -> GameOptions.getGenericValueText(text, Text.translatable("ramel.config.value." + key, value)), | ||
Option.BOOLEAN_VALUES, | ||
defaultValue, | ||
value -> { | ||
} | ||
); | ||
} | ||
|
||
public void save() { | ||
this.value = this.deferredValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.