Skip to content

Commit

Permalink
Merge pull request #1 from HenryLoenwind/configrestart
Browse files Browse the repository at this point in the history
Adapted Config UI
  • Loading branch information
Matyrobbrt authored Jul 26, 2024
2 parents ecff458 + 7693a7e commit bc4f5e6
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.google.common.collect.ImmutableList;
import com.mojang.realmsclient.RealmsMainScreen;
import com.mojang.serialization.Codec;
import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -68,6 +69,7 @@
import net.neoforged.neoforge.common.ModConfigSpec.ConfigValue;
import net.neoforged.neoforge.common.ModConfigSpec.ListValueSpec;
import net.neoforged.neoforge.common.ModConfigSpec.Range;
import net.neoforged.neoforge.common.ModConfigSpec.RestartType;
import net.neoforged.neoforge.common.ModConfigSpec.ValueSpec;
import net.neoforged.neoforge.common.TranslatableEnum;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -104,6 +106,30 @@
*
*/
public final class ConfigurationScreen extends OptionsSubScreen {
private static final class TooltipConfirmScreen extends ConfirmScreen {
boolean seenYes = false;

private TooltipConfirmScreen(BooleanConsumer p_95658_, Component p_95659_, Component p_95660_, Component p_95661_, Component p_95662_) {
super(p_95658_, p_95659_, p_95660_, p_95661_, p_95662_);
}

@Override
protected void init() {
seenYes = false;
super.init();
}

@Override
protected void addExitButton(Button button) {
if (seenYes) {
button.setTooltip(Tooltip.create(RESTART_NO_TOOLTIP));
} else {
seenYes = true;
}
super.addExitButton(button);
}
}

public static class TranslationChecker {
private static final Logger LOGGER = LogManager.getLogger();
private final Set<String> untranslatables = new HashSet<>();
Expand Down Expand Up @@ -157,14 +183,6 @@ public void finish() {
}
}

public enum RestartType {
NONE, SERVER, GAME;

RestartType with(RestartType other) {
return other == NONE ? this : (other == GAME || this == GAME) ? GAME : SERVER;
}
}

/**
* Prefix for static keys the configuration screens use internally.
*/
Expand Down Expand Up @@ -210,6 +228,7 @@ RestartType with(RestartType other) {
public static final Component RETURN_TO_MENU = Component.translatable("menu.returnToMenu"); // PauseScreen.RETURN_TO_MENU
public static final Component SAVING_LEVEL = Component.translatable("menu.savingLevel"); // PauseScreen.SAVING_LEVEL
public static final Component RESTART_NO = Component.translatable(LANG_PREFIX + "restart.return");
public static final Component RESTART_NO_TOOLTIP = Component.translatable(LANG_PREFIX + "restart.return.tooltip");
public static final Component UNDO = Component.translatable(LANG_PREFIX + "undo");
public static final Component UNDO_TOOLTIP = Component.translatable(LANG_PREFIX + "undo.tooltip");
public static final Component RESET = Component.translatable(LANG_PREFIX + "reset");
Expand Down Expand Up @@ -303,24 +322,24 @@ public void onClose() {
translationChecker.finish();
switch (needsRestart) {
case GAME -> {
minecraft.setScreen(new ConfirmScreen(b -> {
minecraft.setScreen(new TooltipConfirmScreen(b -> {
if (b) {
minecraft.stop();
} else {
minecraft.setScreen(this);
super.onClose();
}
}, GAME_RESTART_TITLE, GAME_RESTART_MESSAGE, GAME_RESTART_YES, RESTART_NO));
return;
}
case SERVER -> {
case WORLD -> {
if (minecraft.level != null) {
minecraft.setScreen(new ConfirmScreen(b -> {
minecraft.setScreen(new TooltipConfirmScreen(b -> {
if (b) {
// when changing server configs from the client is added, this is where we tell the server to restart and activate the new config.
// also needs a different text in MP ("server will restart/exit, yada yada") than in SP
onDisconnect();
} else {
minecraft.setScreen(this);
super.onClose();
}
}, SERVER_RESTART_TITLE, SERVER_RESTART_MESSAGE, minecraft.isLocalServer() ? RETURN_TO_MENU : CommonComponents.GUI_DISCONNECT, RESTART_NO));
return;
Expand Down Expand Up @@ -547,8 +566,8 @@ protected Component getTooltipComponent(final String key, @Nullable Range<?> ran
protected void onChanged(final String key) {
changed = true;
final ValueSpec valueSpec = getValueSpec(key);
if (valueSpec != null && valueSpec.restartType() == ModConfigSpec.RestartType.WORLD) {
needsRestart = needsRestart.with(RestartType.SERVER);
if (valueSpec != null) {
needsRestart = needsRestart.with(valueSpec.restartType());
}
}

Expand All @@ -572,11 +591,11 @@ protected ConfigurationSectionScreen rebuild() {
var valueSpec = getValueSpec(key);
var element = switch (valueSpec) {
case ListValueSpec listValueSpec -> createList(key, listValueSpec, cv);
case ValueSpec spec when cv.getClass() == ConfigValue.class && spec.getDefault() instanceof String -> createStringValue(key, valueSpec::test, cv, cv::set);
case ValueSpec spec when cv.getClass() == ConfigValue.class && spec.getDefault() instanceof Integer -> createIntegerValue(key, valueSpec, cv, cv::set);
case ValueSpec spec when cv.getClass() == ConfigValue.class && spec.getDefault() instanceof Long -> createLongValue(key, valueSpec, cv, cv::set);
case ValueSpec spec when cv.getClass() == ConfigValue.class && spec.getDefault() instanceof Double -> createDoubleValue(key, valueSpec, cv, cv::set);
case ValueSpec spec when cv.getClass() == ConfigValue.class && spec.getDefault() instanceof Enum<?> -> createEnumValue(key, valueSpec, cv, cv::set);
case ValueSpec spec when cv.getClass() == ConfigValue.class && spec.getDefault() instanceof String -> createStringValue(key, valueSpec::test, () -> (String) cv.getRaw(), cv::set);
case ValueSpec spec when cv.getClass() == ConfigValue.class && spec.getDefault() instanceof Integer -> createIntegerValue(key, valueSpec, () -> (Integer) cv.getRaw(), cv::set);
case ValueSpec spec when cv.getClass() == ConfigValue.class && spec.getDefault() instanceof Long -> createLongValue(key, valueSpec, () -> (Long) cv.getRaw(), cv::set);
case ValueSpec spec when cv.getClass() == ConfigValue.class && spec.getDefault() instanceof Double -> createDoubleValue(key, valueSpec, () -> (Double) cv.getRaw(), cv::set);
case ValueSpec spec when cv.getClass() == ConfigValue.class && spec.getDefault() instanceof Enum<?> -> createEnumValue(key, valueSpec, (Supplier) cv::getRaw, (Consumer) cv::set);
case null -> null;

default -> switch (cv) {
Expand Down Expand Up @@ -627,7 +646,7 @@ protected Collection<? extends Element> createSyntheticValues() {
}

protected boolean isNonDefault(ModConfigSpec.ConfigValue<?> cv) {
return !Objects.equals(cv.get(), cv.getDefault());
return !Objects.equals(cv.getRaw(), cv.getDefault());
}

protected boolean isAnyNondefault() {
Expand Down Expand Up @@ -692,7 +711,7 @@ protected Element createOtherSection(final String key, final Object value) {
*/
@Nullable
protected Element createOtherValue(final String key, final ConfigValue<?> value) {
final StringWidget label = new StringWidget(Button.DEFAULT_WIDTH, Button.DEFAULT_HEIGHT, Component.literal(Objects.toString(value.get())), font).alignLeft();
final StringWidget label = new StringWidget(Button.DEFAULT_WIDTH, Button.DEFAULT_HEIGHT, Component.literal(Objects.toString(value.getRaw())), font).alignLeft();
label.setTooltip(Tooltip.create(UNSUPPORTED_ELEMENT));
return new Element(getTranslationComponent(key), getTooltipComponent(key, null), label, false);
}
Expand Down Expand Up @@ -921,7 +940,7 @@ protected void createResetButton() {
}, getValueSpec(key).correct(null), v -> {
cv.set(v);
onChanged(key);
}, cv.get()));
}, cv.getRaw()));
}
}
undoManager.add(list);
Expand Down Expand Up @@ -986,7 +1005,7 @@ public ConfigurationListScreen(final Context context, final String key, final Co
this.key = key;
this.spec = spec;
this.valueList = valueList; // === (ListValueSpec)getValueSpec(key)
this.cfgList = new ArrayList<>(valueList.get());
this.cfgList = new ArrayList<>(valueList.getRaw());
}

@Override
Expand Down
23 changes: 18 additions & 5 deletions src/main/java/net/neoforged/neoforge/common/ModConfigSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -1211,16 +1211,25 @@ public List<String> getPath() {
*/
@Override
public T get() {
Preconditions.checkNotNull(spec, "Cannot get config value before spec is built");
var loadedConfig = spec.loadedConfig;
Preconditions.checkState(loadedConfig != null, "Cannot get config value before config is loaded.");

if (cachedValue == null) {
cachedValue = getRaw(loadedConfig.config(), path, defaultSupplier);
cachedValue = getRaw();
}
return cachedValue;
}

/**
* Returns the uncached value for the configuration setting, throwing if the config has not yet been loaded.
* <p>
* <em>Do not call this for any other purpose than editing the value. Use {@link #get()} instead.</em>
*/
@Override
public T getRaw() {
Preconditions.checkNotNull(spec, "Cannot get config value before spec is built");
var loadedConfig = spec.loadedConfig;
Preconditions.checkState(loadedConfig != null, "Cannot get config value before config is loaded.");
return getRaw(loadedConfig.config(), path, defaultSupplier);
}

public T getRaw(Config config, List<String> path, Supplier<T> defaultSupplier) {
return config.getOrElse(path, defaultSupplier);
}
Expand Down Expand Up @@ -1387,5 +1396,9 @@ public enum RestartType {
private boolean isValid(ModConfig.Type type) {
return !invalidTypes.contains(type);
}

public RestartType with(RestartType other) {
return other == NONE ? this : (other == GAME || this == GAME) ? GAME : WORLD;
}
}
}
13 changes: 10 additions & 3 deletions src/main/resources/assets/neoforge/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,17 @@
"neoforge.configuration.uitext.server": "Server Options",
"neoforge.configuration.uitext.startup": "Startup Options",
"neoforge.configuration.uitext.restart.game.title": "Minecraft needs to be restarted",
"neoforge.configuration.uitext.restart.game.text": "One or more of the configuration option that were changed only take effect when the game is started.",
"neoforge.configuration.uitext.restart.game.text": "One or more of the configuration option that were changed will only take effect when the game is started.",
"neoforge.configuration.uitext.restart.server.title": "World needs to be reloaded",
"neoforge.configuration.uitext.restart.server.text": "One or more of the configuration option that were changed only take effect when a world is started or loaded.",
"neoforge.configuration.uitext.restart.return": "Not yet...",
"neoforge.configuration.uitext.restart.server.text": "One or more of the configuration option that were changed will only take effect when the world is reloaded.",
"neoforge.configuration.uitext.restart.return": "Ignore",
"neoforge.configuration.uitext.restart.return.tooltip": [
{
"text": "Your changes will have no effect until you restart!",
"color": "red",
"bold": true
}
],

"neoforge.configuration.title": "NeoForge Configuration",
"neoforge.configuration.section.neoforge.client.toml": "Client settings",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public static class Server {
static {
final var builder = new ModConfigSpec.Builder();

builder.worldRestart().define("worldRestart", false);

SPEC = builder.build();
}
}
Expand All @@ -87,6 +89,9 @@ public static class Common {

builder.pop(2);

builder.worldRestart().define("worldRestart", false);
builder.gameRestart().define("gameRestart", false);

SPEC = builder.build();
}
}
Expand Down

0 comments on commit bc4f5e6

Please sign in to comment.