use of mekanism.api.recipes.inputs.chemical.IChemicalStackIngredient in project Mekanism by mekanism.
the class BaseCrTExampleProvider method addSupportedChemical.
private <CHEMICAL extends Chemical<CHEMICAL>, STACK extends ChemicalStack<CHEMICAL>> void addSupportedChemical(Class<STACK> stackClass, Class<? extends ICrTChemicalStack<CHEMICAL, STACK, ?>> stackCrTClass, Class<? extends IChemicalStackIngredient<CHEMICAL, STACK>> ingredientClass, String ingredientType, Function<STACK, CommandStringDisplayable> singleDescription, CrTChemicalTagManager<CHEMICAL> tagManager, ChemicalIngredientDeserializer<CHEMICAL, STACK, ?> deserializer) {
addSupportedConversion(ICrTChemicalStack.class, stackCrTClass, stackClass, (imports, stack) -> singleDescription.apply(stack).getCommandString());
addSupportedConversion(IChemicalStackIngredient.class, ingredientClass, ingredientClass, (imports, ingredient) -> getIngredientRepresentation(ingredient, imports.addImport(ingredientType), deserializer, singleDescription, tagManager), (imports, ingredient) -> {
if (ingredient instanceof ChemicalStackIngredient.SingleIngredient) {
JsonObject serialized = ingredient.serialize().getAsJsonObject();
return singleDescription.apply(deserializer.deserializeStack(serialized)).getCommandString();
} else if (ingredient instanceof ChemicalStackIngredient.TaggedIngredient) {
JsonObject serialized = (JsonObject) ingredient.serialize();
long amount = serialized.getAsJsonPrimitive(JsonConstants.AMOUNT).getAsLong();
if (amount > 0 && amount <= Integer.MAX_VALUE) {
return getTagWithExplicitAmount(tagManager.getTag(serialized.get(JsonConstants.TAG).getAsString()), (int) amount);
}
}
return null;
});
}
use of mekanism.api.recipes.inputs.chemical.IChemicalStackIngredient in project Mekanism by mekanism.
the class MekanismRecipeHandler method convertIngredient.
private <CHEMICAL extends Chemical<CHEMICAL>, STACK extends ChemicalStack<CHEMICAL>> String convertIngredient(String crtClass, CrTChemicalTagManager<CHEMICAL> tagManager, ChemicalIngredientDeserializer<CHEMICAL, STACK, ?> deserializer, IChemicalStackIngredient<CHEMICAL, STACK> ingredient) {
if (ingredient instanceof ChemicalStackIngredient.SingleIngredient) {
// Serialize and deserialize to get easy access to the amount
JsonObject serialized = ingredient.serialize().getAsJsonObject();
// Note: Handled via implicit casts
return convertParam(deserializer.deserializeStack(serialized));
} else if (ingredient instanceof ChemicalStackIngredient.TaggedIngredient) {
JsonObject serialized = ingredient.serialize().getAsJsonObject();
MCTag<CHEMICAL> tag = tagManager.getTag(serialized.get(JsonConstants.TAG).getAsString());
long amount = serialized.getAsJsonPrimitive(JsonConstants.AMOUNT).getAsLong();
if (amount > 0 && amount <= Integer.MAX_VALUE) {
// Note: Handled via implicit casts
return getTagWithExplicitAmount(tag, (int) amount);
}
// Tag with amount can only handle up to max int, so we have to do it explicitly if we have more
return crtClass + ".from(" + tag.getCommandString() + ", " + amount + ")";
} else if (ingredient instanceof ChemicalStackIngredient.MultiIngredient) {
ChemicalStackIngredient.MultiIngredient<CHEMICAL, STACK, ?> multiIngredient = (ChemicalStackIngredient.MultiIngredient<CHEMICAL, STACK, ?>) ingredient;
StringBuilder builder = new StringBuilder(crtClass + ".createMulti(");
multiIngredient.forEachIngredient(i -> {
builder.append(convertIngredient(crtClass, tagManager, deserializer, i)).append(", ");
return false;
});
// Remove trailing comma and space
builder.setLength(builder.length() - 2);
builder.append(")");
return builder.toString();
}
// Shouldn't happen
return "Unimplemented chemical stack ingredient: " + ingredient;
}
use of mekanism.api.recipes.inputs.chemical.IChemicalStackIngredient in project Mekanism by mekanism.
the class ChemicalCrystallizerInputRecipeCache method findFirstRecipe.
@Nullable
private <CHEMICAL extends Chemical<CHEMICAL>, STACK extends ChemicalStack<CHEMICAL>> ChemicalCrystallizerRecipe findFirstRecipe(ChemicalType type, STACK stack) {
Predicate<ChemicalCrystallizerRecipe> matchPredicate = recipe -> ((IChemicalStackIngredient<CHEMICAL, STACK>) recipe.getInput()).test(stack);
ChemicalInputCache<CHEMICAL, STACK, ChemicalCrystallizerRecipe> cache = (ChemicalInputCache<CHEMICAL, STACK, ChemicalCrystallizerRecipe>) typeBasedCache.get(type);
ChemicalCrystallizerRecipe recipe = cache.findFirstRecipe(stack, matchPredicate);
return recipe == null ? findFirstRecipe(typeBasedComplexRecipes.get(type), matchPredicate) : recipe;
}
use of mekanism.api.recipes.inputs.chemical.IChemicalStackIngredient in project Mekanism by mekanism.
the class BaseCrTExampleProvider method getIngredientRepresentation.
private <CHEMICAL extends Chemical<CHEMICAL>, STACK extends ChemicalStack<CHEMICAL>> String getIngredientRepresentation(IChemicalStackIngredient<CHEMICAL, STACK> ingredient, String ingredientType, ChemicalIngredientDeserializer<CHEMICAL, STACK, ?> deserializer, Function<STACK, CommandStringDisplayable> singleDescription, CrTChemicalTagManager<CHEMICAL> tagManager) {
if (ingredient instanceof ChemicalStackIngredient.SingleIngredient) {
JsonObject serialized = ingredient.serialize().getAsJsonObject();
String stackRepresentation = singleDescription.apply(deserializer.deserializeStack(serialized)).getCommandString();
return ingredientType + ".from(" + stackRepresentation + ")";
} else if (ingredient instanceof ChemicalStackIngredient.TaggedIngredient) {
JsonObject serialized = ingredient.serialize().getAsJsonObject();
String tagRepresentation = tagManager.getTag(serialized.get(JsonConstants.TAG).getAsString()).getCommandString();
return ingredientType + ".from(" + tagRepresentation + ", " + serialized.getAsJsonPrimitive(JsonConstants.AMOUNT) + ")";
} else if (ingredient instanceof ChemicalStackIngredient.MultiIngredient) {
ChemicalStackIngredient.MultiIngredient<CHEMICAL, STACK, ?> multiIngredient = (ChemicalStackIngredient.MultiIngredient<CHEMICAL, STACK, ?>) ingredient;
StringBuilder builder = new StringBuilder(ingredientType + ".createMulti(");
if (!multiIngredient.forEachIngredient(i -> {
String rep = getIngredientRepresentation(i, ingredientType, deserializer, singleDescription, tagManager);
if (rep == null) {
return true;
}
builder.append(rep).append(", ");
return false;
})) {
// Remove trailing comma and space
builder.setLength(builder.length() - 2);
builder.append(")");
return builder.toString();
}
}
return null;
}
use of mekanism.api.recipes.inputs.chemical.IChemicalStackIngredient in project Mekanism by mekanism.
the class ChemicalCrystallizerRecipeCategory method setRecipe.
@Override
public void setRecipe(IRecipeLayout recipeLayout, ChemicalCrystallizerRecipe recipe, IIngredients ingredients) {
IGuiItemStackGroup itemStacks = recipeLayout.getItemStacks();
initItem(itemStacks, 0, false, output, recipe.getOutputDefinition());
IChemicalStackIngredient<?, ?> input = recipe.getInput();
if (input instanceof GasStackIngredient) {
initChemical(recipeLayout, recipe, MekanismJEI.TYPE_GAS, (GasStackIngredient) input, null);
} else if (input instanceof InfusionStackIngredient) {
initChemical(recipeLayout, recipe, MekanismJEI.TYPE_INFUSION, (InfusionStackIngredient) input, null);
} else if (input instanceof PigmentStackIngredient) {
initChemical(recipeLayout, recipe, MekanismJEI.TYPE_PIGMENT, (PigmentStackIngredient) input, null);
} else if (input instanceof SlurryStackIngredient) {
SlurryStackIngredient slurryInput = (SlurryStackIngredient) input;
Set<ITag<Item>> tags = new HashSet<>();
for (SlurryStack slurryStack : slurryInput.getRepresentations()) {
Slurry slurry = slurryStack.getType();
if (!slurry.isIn(MekanismTags.Slurries.DIRTY)) {
ITag<Item> oreTag = slurry.getOreTag();
if (oreTag != null) {
tags.add(oreTag);
}
}
}
if (tags.size() == 1) {
initChemical(recipeLayout, recipe, MekanismJEI.TYPE_SLURRY, slurryInput, itemStacks);
// TODO: Eventually come up with a better way to do this to allow for if there outputs based on the input and multiple input types
tags.stream().findFirst().ifPresent(tag -> initItem(itemStacks, 1, false, slurryOreSlot, tag.getValues().stream().map(ItemStack::new).collect(Collectors.toList())));
} else {
initChemical(recipeLayout, recipe, MekanismJEI.TYPE_SLURRY, slurryInput, null);
}
}
}
Aggregations