NaturesAura/src/main/java/de/ellpeck/naturesaura/recipes/EnabledCondition.java
2024-02-03 14:56:07 +01:00

55 lines
1.8 KiB
Java

package de.ellpeck.naturesaura.recipes;
import com.google.gson.JsonObject;
import de.ellpeck.naturesaura.ModConfig;
import de.ellpeck.naturesaura.NaturesAura;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import net.neoforged.neoforge.common.ModConfigSpec;
import net.neoforged.neoforge.common.crafting.conditions.ICondition;
import net.neoforged.neoforge.common.crafting.conditions.IConditionSerializer;
public class EnabledCondition implements ICondition {
private static final ResourceLocation NAME = new ResourceLocation(NaturesAura.MOD_ID, "enabled");
private ModConfigSpec.ConfigValue<Boolean> config;
private final String name;
@SuppressWarnings("unchecked")
public EnabledCondition(String name) {
this.name = name;
try {
this.config = (ModConfigSpec.ConfigValue<Boolean>) ModConfig.class.getField(name).get(ModConfig.instance);
} catch (IllegalAccessException | NoSuchFieldException e) {
NaturesAura.LOGGER.error(e);
}
}
@Override
public ResourceLocation getID() {
return EnabledCondition.NAME;
}
@Override
public boolean test(IContext context) {
return this.config != null && this.config.get();
}
public static class Serializer implements IConditionSerializer<EnabledCondition> {
@Override
public void write(JsonObject json, EnabledCondition value) {
json.addProperty("config", value.name);
}
@Override
public EnabledCondition read(JsonObject json) {
return new EnabledCondition(GsonHelper.getAsString(json, "config"));
}
@Override
public ResourceLocation getID() {
return EnabledCondition.NAME;
}
}
}