fix(coat-bridge): Improve some validation and docs
This commit is contained in:
@@ -4,16 +4,23 @@ import de.siphalor.tweed5.core.api.entry.ConfigEntry;
|
|||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import net.minecraft.network.chat.Component;
|
import net.minecraft.network.chat.Component;
|
||||||
|
import org.jspecify.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
@Builder
|
@Builder
|
||||||
@Getter
|
@Getter
|
||||||
public class ConfigScreenCreateParams<T> {
|
public class ConfigScreenCreateParams<T extends @Nullable Object> {
|
||||||
private final ConfigEntry<T> rootEntry;
|
private final ConfigEntry<T> rootEntry;
|
||||||
private final T currentValue;
|
private final T currentValue;
|
||||||
private final T defaultValue;
|
private final T defaultValue;
|
||||||
private final Component title;
|
/**
|
||||||
|
* The title of the screen, defaults to {@code translationKeyPrefix + ".title"}
|
||||||
|
*/
|
||||||
|
private final @Nullable Component title;
|
||||||
|
/**
|
||||||
|
* The translation key prefix for all entries without a trailing dot.
|
||||||
|
*/
|
||||||
private final String translationKeyPrefix;
|
private final String translationKeyPrefix;
|
||||||
private final Consumer<T> saveHandler;
|
private final @Nullable Consumer<T> saveHandler;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,11 +12,14 @@ import de.siphalor.tweed5.core.api.entry.ConfigEntry;
|
|||||||
import de.siphalor.tweed5.core.api.extension.TweedExtensionSetupContext;
|
import de.siphalor.tweed5.core.api.extension.TweedExtensionSetupContext;
|
||||||
import de.siphalor.tweed5.patchwork.api.PatchworkPartAccess;
|
import de.siphalor.tweed5.patchwork.api.PatchworkPartAccess;
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
|
import net.minecraft.network.chat.Component;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static de.siphalor.tweed5.coat.bridge.api.TweedCoatMappingUtils.translatableComponent;
|
||||||
|
|
||||||
public class TweedCoatBridgeExtensionImpl implements TweedCoatBridgeExtension {
|
public class TweedCoatBridgeExtensionImpl implements TweedCoatBridgeExtension {
|
||||||
private final PatchworkPartAccess<CustomData> customDataAccess;
|
private final PatchworkPartAccess<CustomData> customDataAccess;
|
||||||
private final List<TweedCoatMapper<?>> mappers = new ArrayList<>();
|
private final List<TweedCoatMapper<?>> mappers = new ArrayList<>();
|
||||||
@@ -32,6 +35,8 @@ public class TweedCoatBridgeExtensionImpl implements TweedCoatBridgeExtension {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> ConfigScreen createConfigScreen(ConfigScreenCreateParams<T> params) {
|
public <T> ConfigScreen createConfigScreen(ConfigScreenCreateParams<T> params) {
|
||||||
|
validateCreateParams(params);
|
||||||
|
|
||||||
Minecraft minecraft = Minecraft.getInstance();
|
Minecraft minecraft = Minecraft.getInstance();
|
||||||
|
|
||||||
TweedCoatEntryMappingContext mappingContext = TweedCoatEntryMappingContext.rootBuilder(
|
TweedCoatEntryMappingContext mappingContext = TweedCoatEntryMappingContext.rootBuilder(
|
||||||
@@ -56,13 +61,29 @@ public class TweedCoatBridgeExtensionImpl implements TweedCoatBridgeExtension {
|
|||||||
throw new IllegalStateException("Failed to create root content widget");
|
throw new IllegalStateException("Failed to create root content widget");
|
||||||
}
|
}
|
||||||
|
|
||||||
ConfigScreen configScreen = new ConfigScreen(
|
Component title;
|
||||||
minecraft.screen, params.title(), Collections.singletonList(contentWidget)
|
if (params.title() != null) {
|
||||||
);
|
title = params.title();
|
||||||
configScreen.setOnSave(() -> params.saveHandler().accept(value));
|
} else {
|
||||||
|
title = translatableComponent(params.translationKeyPrefix() + ".title");
|
||||||
|
}
|
||||||
|
ConfigScreen configScreen = new ConfigScreen(minecraft.screen, title, Collections.singletonList(contentWidget));
|
||||||
|
if (params.saveHandler() != null) {
|
||||||
|
configScreen.setOnSave(() -> params.saveHandler().accept(value));
|
||||||
|
}
|
||||||
return configScreen;
|
return configScreen;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("ConstantValue")
|
||||||
|
private <T> void validateCreateParams(ConfigScreenCreateParams<T> params) {
|
||||||
|
if (params.rootEntry() == null) {
|
||||||
|
throw new IllegalArgumentException("Root entry must not be null");
|
||||||
|
}
|
||||||
|
if (params.translationKeyPrefix() == null) {
|
||||||
|
throw new IllegalArgumentException("Translation key prefix must not be null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private <T> TweedCoatEntryMappingResult<T, ?> mapEntry(
|
private <T> TweedCoatEntryMappingResult<T, ?> mapEntry(
|
||||||
ConfigEntry<T> entry,
|
ConfigEntry<T> entry,
|
||||||
TweedCoatEntryMappingContext context
|
TweedCoatEntryMappingContext context
|
||||||
|
|||||||
Reference in New Issue
Block a user