Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort api 1.20.1 #3517

Open
wants to merge 4 commits into
base: 1.20.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions Common/src/main/java/mezz/jei/common/config/ClientConfig.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package mezz.jei.common.config;

import com.google.common.base.Preconditions;

import mezz.jei.api.runtime.config.IJeiConfigValue;
import mezz.jei.common.config.file.IConfigCategoryBuilder;
import mezz.jei.common.config.file.IConfigSchema;
import mezz.jei.common.config.file.IConfigSchemaBuilder;
import mezz.jei.common.config.file.serializers.EnumSerializer;
import mezz.jei.common.config.file.serializers.IngredientSortStageSerializer;
import mezz.jei.common.config.file.serializers.ListSerializer;
import mezz.jei.common.platform.Services;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public final class ClientConfig implements IClientConfig {
@Nullable
private static IClientConfig instance;
private Optional<IConfigSchema> configSchema = Optional.empty();

private final Supplier<Boolean> centerSearchBarEnabled;
private final Supplier<Boolean> lowMemorySlowSearchEnabled;
Expand Down Expand Up @@ -90,11 +96,15 @@ public ClientConfig(IConfigSchemaBuilder schema) {
ingredientSorterStages = sorting.addList(
"IngredientSortStages",
IngredientSortStage.defaultStages,
new ListSerializer<>(new EnumSerializer<>(IngredientSortStage.class)),
new ListSerializer<IngredientSortStage>(new IngredientSortStageSerializer()),
"Sorting order for the ingredient list"
);
}

public void setSchema(IConfigSchema schema) {
this.configSchema = Optional.ofNullable(schema);
}

/**
* Only use this for hacky stuff like the debug plugin
*/
Expand Down Expand Up @@ -158,4 +168,37 @@ public int getMaxRecipeGuiHeight() {
public List<IngredientSortStage> getIngredientSorterStages() {
return ingredientSorterStages.get();
}

@Override
public void setIngredientSorterStages(List<IngredientSortStage> ingredientSortStages) {
if (configSchema.isEmpty()) {
return;
}
@SuppressWarnings("unchecked")
IJeiConfigValue<List<IngredientSortStage>> stages = (IJeiConfigValue<List<IngredientSortStage>>)configSchema.get().getConfigValue("sorting", "IngredientSortStages").orElseGet(null);
if (stages != null) {
stages.set(ingredientSortStages);
}

}

@Override
public String getSerializedIngredientSorterStages() {
return ingredientSorterStages.get().stream()
.map(o -> o.name)
.collect(Collectors.joining(", "));
}

@Override
public void setIngredientSorterStages(String ingredientSortStages) {
if (configSchema.isEmpty()) {
return;
}
@SuppressWarnings("unchecked")
IJeiConfigValue<List<IngredientSortStage>> stages = (IJeiConfigValue<List<IngredientSortStage>>)configSchema.get().getConfigValue("sorting", "IngredientSortStages").orElseGet(null);
if (stages != null) {
stages.setUsingSerializedValue(ingredientSortStages);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ public interface IClientConfig {
int getMaxRecipeGuiHeight();

List<IngredientSortStage> getIngredientSorterStages();

void setIngredientSorterStages(List<IngredientSortStage> ingredientSortStages);

String getSerializedIngredientSorterStages();

void setIngredientSorterStages(String ingredientSortStages);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,84 @@
package mezz.jei.common.config;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public enum IngredientSortStage {
MOD_NAME, INGREDIENT_TYPE, ALPHABETICAL, CREATIVE_MENU, TAG, ARMOR, MAX_DURABILITY;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


public class IngredientSortStage {
private static final Logger LOGGER = LogManager.getLogger();
public final String name;
public final Boolean customStage;

private IngredientSortStage(String name, Boolean customStage) {
this.name = name.toUpperCase().trim();
this.customStage = customStage;
var existingStage = getStage(name);
if (existingStage == null) {
if (customStage) {
LOGGER.info("Adding Custom Sort Stage: " + name);
} else {
LOGGER.info("Adding Built-in Sort Stage: " + name);
}
allStages.put(name, this);
} else if (existingStage.customStage) {
LOGGER.info("Replacing Sort Stage: " + name);
//Replace the existing one, maybe the new one is an internal comparator.
allStages.put(name, this);
} else {
LOGGER.debug("Ignoring Duplicate Sort Stage: " + name);
}
//Don't replace our built-in stages.
}

private IngredientSortStage(String name) {
this(name, true);
}

private static Map<String, IngredientSortStage> allStages = new HashMap<String, IngredientSortStage>(10);

public static final IngredientSortStage MOD_NAME = new IngredientSortStage("MOD_NAME", false);
public static final IngredientSortStage INGREDIENT_TYPE = new IngredientSortStage("INGREDIENT_TYPE", false);
public static final IngredientSortStage ALPHABETICAL = new IngredientSortStage("ALPHABETICAL", false);
public static final IngredientSortStage CREATIVE_MENU = new IngredientSortStage("CREATIVE_MENU", false);
public static final IngredientSortStage TAG = new IngredientSortStage("TAG", false);
public static final IngredientSortStage ARMOR = new IngredientSortStage("ARMOR", false);
public static final IngredientSortStage MAX_DURABILITY = new IngredientSortStage("MAX_DURABILITY", false);

public static final List<IngredientSortStage> defaultStages = List.of(
IngredientSortStage.MOD_NAME,
IngredientSortStage.INGREDIENT_TYPE,
IngredientSortStage.CREATIVE_MENU
);

public static Collection<IngredientSortStage> getAllStages() {
return allStages.values();
}

public static IngredientSortStage getOrCreateStage(String name) {
var stage = getStage(name);
if (stage == null) {
stage = new IngredientSortStage(name, true);
}
return stage;
}

public static IngredientSortStage getStage(String needle) {
needle = needle.toUpperCase().trim();
return allStages.get(needle);
}

public static final List<String> defaultStageNames = List.of(
IngredientSortStage.MOD_NAME.name,
IngredientSortStage.INGREDIENT_TYPE.name,
IngredientSortStage.CREATIVE_MENU.name
);

public static Collection<String> getAllStageNames() {
return allStages.keySet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.nio.file.Path;

public class JeiClientConfigs implements IJeiClientConfigs {
private final IClientConfig clientConfig;
private final ClientConfig clientConfig;
private final IIngredientFilterConfig ingredientFilterConfig;
private final IIngredientGridConfig ingredientListConfig;
private final IIngredientGridConfig bookmarkListConfig;
Expand All @@ -25,6 +25,7 @@ public JeiClientConfigs(Path configFile) {
bookmarkListConfig = new IngredientGridConfig("BookmarkList", builder, HorizontalAlignment.LEFT);

schema = builder.build();
clientConfig.setSchema(schema);
}

public void register(FileWatcher fileWatcher, ConfigManager configManager) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public String getName() {
return name;
}

@Override
@Unmodifiable
public Optional<ConfigValue<?>> getConfigValue(String configValueName) {
ConfigValue<?> configValue = valueMap.get(configValueName);
return Optional.ofNullable(configValue);
Expand All @@ -41,6 +43,7 @@ public Collection<ConfigValue<?>> getConfigValues() {
return this.valueMap.values();
}

@Override
public Set<String> getValueNames() {
return this.valueMap.keySet();
}
Expand Down
27 changes: 27 additions & 0 deletions Common/src/main/java/mezz/jei/common/config/file/ConfigSchema.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package mezz.jei.common.config.file;

import mezz.jei.api.runtime.config.IJeiConfigCategory;
import mezz.jei.api.runtime.config.IJeiConfigValue;
import mezz.jei.common.config.ConfigManager;
import mezz.jei.common.util.DeduplicatingRunner;
import org.apache.logging.log4j.LogManager;
Expand All @@ -11,6 +13,7 @@
import java.nio.file.Path;
import java.time.Duration;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;

public class ConfigSchema implements IConfigSchema {
Expand Down Expand Up @@ -79,6 +82,30 @@ public List<ConfigCategory> getCategories() {
return categories;
}

@Override
public Optional<? extends IJeiConfigCategory> getCategory(String categoryName) {
ConfigCategory found = null;
for (ConfigCategory category : categories) {
if (category.getName().equals(categoryName)) {
found = category;
break;
}
}
return Optional.ofNullable(found);
}

@Override
public Optional<? extends IJeiConfigValue<?>> getConfigValue(String categoryName, String valueName) {
var cat = this.getCategory(categoryName);

if (cat.isEmpty()) {
return Optional.ofNullable(null);
}

return cat.get().getConfigValue(valueName);

}

@Override
public Path getPath() {
return path;
Expand Down
46 changes: 46 additions & 0 deletions Common/src/main/java/mezz/jei/common/config/file/ConfigValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ public T getDefaultValue() {
return defaultValue;
}

/*
* Allows retreiving the default value without having to know the real data type of the value.
*/
@Override
public String getSerializedDefaultValue() {
return serializer.serialize(defaultValue);
}

@Override
public T getValue() {
if (schema != null) {
Expand All @@ -54,18 +62,56 @@ public T getValue() {
return currentValue;
}

/*
* Allows retreiving the value without having to know the real data type of the value.
*/
@Override
public String getSerializedValue() {
if (schema != null) {
schema.loadIfNeeded();
}
return serializer.serialize(currentValue);
}

@Override
public IJeiConfigValueSerializer<T> getSerializer() {
return serializer;
}

/*
* This one is for internal loading.
*/
public List<String> setFromSerializedValue(String value) {
IJeiConfigValueSerializer.IDeserializeResult<T> deserializeResult = serializer.deserialize(value);
deserializeResult.getResult()
.ifPresent(t -> currentValue = t);
return deserializeResult.getErrors();
}

/*
* Update the value without knowing the exact underlying type.
*/
@Override
public boolean setUsingSerializedValue(String value) {
IJeiConfigValueSerializer.IDeserializeResult<T> deserializeResult = serializer.deserialize(value);
if (!deserializeResult.getErrors().isEmpty()) {
LOGGER.error("Tried to set invalid value : {}\n{}", value, serializer.getValidValuesDescription());
return false;
}
if (deserializeResult.getResult().isPresent()) {
T realValue = deserializeResult.getResult().get();
if (!currentValue.equals(realValue)) {
currentValue = realValue;
if (schema != null) {
schema.markDirty();
}
return true;
}
}
return false;
}


@Override
public boolean set(T value) {
if (!serializer.isValid(value)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package mezz.jei.common.config.file.serializers;

import mezz.jei.api.runtime.config.IJeiConfigValueSerializer;
import mezz.jei.common.config.IngredientSortStage;

import java.util.Collection;
import java.util.Optional;
import java.util.stream.Collectors;

public class IngredientSortStageSerializer implements IJeiConfigValueSerializer<IngredientSortStage> {

public IngredientSortStageSerializer() {
}

@Override
public String serialize(IngredientSortStage value) {
return value.name;
}

@Override
public DeserializeResult<IngredientSortStage> deserialize(String string) {
string = string.trim();
if (string.startsWith("\"") && string.endsWith("\"")) {
string = string.substring(1, string.length() - 1);
}
//Since valid values could be added after we read the config, we can't validate them yet.
var stage = IngredientSortStage.getOrCreateStage(string);
return new DeserializeResult<>(stage);
}

@Override
public String getValidValuesDescription() {
String names = IngredientSortStage.getAllStageNames().stream()
.collect(Collectors.joining(", "));

return "[%s]".formatted(names);
}

@Override
public boolean isValid(IngredientSortStage value) {
//TODO: Not sure if this only occurs after addins have a chance to register their sorters.
//If so, we just need to return true.
var stage = IngredientSortStage.getStage(value.name);
return stage != null;
}

@Override
public Optional<Collection<IngredientSortStage>> getAllValidValues() {
return Optional.of(IngredientSortStage.getAllStages());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ default Iterable<Integer> getColors(V ingredient) {
* @return an ItemStack for JEI to give the player, or an empty stack if there is nothing that can be given.
*/
default ItemStack getCheatItemStack(V ingredient) {
return ItemStack.EMPTY;
return null;
}

/**
Expand Down
Loading