NaturesAura/src/main/java/de/ellpeck/naturesaura/recipes/EnabledCondition.java

56 lines
1.7 KiB
Java
Raw Normal View History

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