Search in sources :

Example 16 with ChemicalType

use of mekanism.api.chemical.ChemicalType in project Mekanism by mekanism.

the class SerializerHelper method getChemicalType.

/**
 * Gets and deserializes a Chemical Type from a given Json Object.
 *
 * @param json Json Object.
 *
 * @return Chemical Type.
 */
public static ChemicalType getChemicalType(@Nonnull JsonObject json) {
    if (!json.has(JsonConstants.CHEMICAL_TYPE)) {
        throw new JsonSyntaxException("Missing '" + JsonConstants.CHEMICAL_TYPE + "', expected to find a string");
    }
    JsonElement element = json.get(JsonConstants.CHEMICAL_TYPE);
    if (!element.isJsonPrimitive()) {
        throw new JsonSyntaxException("Expected '" + JsonConstants.CHEMICAL_TYPE + "' to be a json primitive representing a string");
    }
    String name = element.getAsString();
    ChemicalType chemicalType = ChemicalType.fromString(name);
    if (chemicalType == null) {
        throw new JsonSyntaxException("Invalid chemical type '" + name + "'.");
    }
    return chemicalType;
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) ChemicalType(mekanism.api.chemical.ChemicalType) JsonElement(com.google.gson.JsonElement)

Aggregations

ChemicalType (mekanism.api.chemical.ChemicalType)16 List (java.util.List)5 ChemicalStack (mekanism.api.chemical.ChemicalStack)5 BoxedChemicalStack (mekanism.api.chemical.merged.BoxedChemicalStack)5 Map (java.util.Map)4 ChemicalCrystallizerRecipe (mekanism.api.recipes.ChemicalCrystallizerRecipe)4 JsonSyntaxException (com.google.gson.JsonSyntaxException)3 EnumMap (java.util.EnumMap)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 Predicate (java.util.function.Predicate)3 Nullable (javax.annotation.Nullable)3 Chemical (mekanism.api.chemical.Chemical)3 GasStack (mekanism.api.chemical.gas.GasStack)3 PigmentStack (mekanism.api.chemical.pigment.PigmentStack)3 IChemicalStackIngredient (mekanism.api.recipes.inputs.chemical.IChemicalStackIngredient)3 MekanismRecipeType (mekanism.common.recipe.MekanismRecipeType)3 ChemicalInputCache (mekanism.common.recipe.lookup.cache.type.ChemicalInputCache)3 EnumUtils (mekanism.common.util.EnumUtils)3 World (net.minecraft.world.World)3