use of mekanism.api.recipes.inputs.FluidStackIngredient in project Mekanism by mekanism.
the class FluidInputCache method mapInputs.
@Override
public boolean mapInputs(RECIPE recipe, FluidStackIngredient inputIngredient) {
if (inputIngredient instanceof FluidStackIngredient.Single) {
HashedFluid input = HashedFluid.create(((FluidStackIngredient.Single) inputIngredient).getInputRaw());
addNbtInputCache(input, recipe);
} else if (inputIngredient instanceof FluidStackIngredient.Tagged) {
for (Fluid input : ((FluidStackIngredient.Tagged) inputIngredient).getRawInput()) {
addInputCache(input, recipe);
}
} else if (inputIngredient instanceof FluidStackIngredient.Multi) {
return ((FluidStackIngredient.Multi) inputIngredient).forEachIngredient(ingredient -> mapInputs(recipe, ingredient));
} else {
// but if it does add it as a fallback
return true;
}
return false;
}
use of mekanism.api.recipes.inputs.FluidStackIngredient in project Mekanism by mekanism.
the class MekanismRecipeHandler method convertIngredient.
private String convertIngredient(FluidStackIngredient ingredient) {
if (ingredient instanceof FluidStackIngredient.Single) {
JsonObject serialized = ingredient.serialize().getAsJsonObject();
// Note: Handled via implicit casts
return new MCFluidStack(SerializerHelper.deserializeFluid(serialized)).getCommandString();
} else if (ingredient instanceof FluidStackIngredient.Tagged) {
JsonObject serialized = ingredient.serialize().getAsJsonObject();
// Note: Handled via implicit casts
return getTagWithExplicitAmount(TagManagerFluid.INSTANCE.getTag(serialized.get(JsonConstants.TAG).getAsString()), serialized.getAsJsonPrimitive(JsonConstants.AMOUNT).getAsInt());
} else if (ingredient instanceof FluidStackIngredient.Multi) {
FluidStackIngredient.Multi multiIngredient = (FluidStackIngredient.Multi) ingredient;
StringBuilder builder = new StringBuilder(CrTConstants.CLASS_FLUID_STACK_INGREDIENT + ".createMulti(");
multiIngredient.forEachIngredient(i -> {
builder.append(convertIngredient(i)).append(", ");
return false;
});
// Remove trailing comma and space
builder.setLength(builder.length() - 2);
builder.append(")");
return builder.toString();
}
return "Unimplemented fluidstack ingredient: " + ingredient;
}
use of mekanism.api.recipes.inputs.FluidStackIngredient in project Mekanism by mekanism.
the class ElectrolysisRecipeMapper method handleRecipe.
@Override
public boolean handleRecipe(IMappingCollector<NormalizedSimpleStack, Long> mapper, IRecipe<?> iRecipe, INSSFakeGroupManager groupManager) {
if (!(iRecipe instanceof ElectrolysisRecipe)) {
// Double check that we have a type of recipe we know how to handle
return false;
}
boolean handled = false;
ElectrolysisRecipe recipe = (ElectrolysisRecipe) iRecipe;
FluidStackIngredient input = recipe.getInput();
for (FluidStack representation : input.getRepresentations()) {
Pair<@NonNull GasStack, @NonNull GasStack> output = recipe.getOutput(representation);
GasStack leftOutput = output.getLeft();
GasStack rightOutput = output.getRight();
if (!leftOutput.isEmpty() && !rightOutput.isEmpty()) {
NormalizedSimpleStack nssInput = NSSFluid.createFluid(representation);
NormalizedSimpleStack nssLeftOutput = NSSGas.createGas(leftOutput);
NormalizedSimpleStack nssRightOutput = NSSGas.createGas(rightOutput);
// Add trying to calculate left output (using it as if we needed negative of right output)
IngredientHelper ingredientHelper = new IngredientHelper(mapper);
ingredientHelper.put(nssInput, representation.getAmount());
ingredientHelper.put(nssRightOutput, -rightOutput.getAmount());
if (ingredientHelper.addAsConversion(nssLeftOutput, leftOutput.getAmount())) {
handled = true;
}
// Add trying to calculate right output (using it as if we needed negative of left output)
ingredientHelper.resetHelper();
ingredientHelper.put(nssInput, representation.getAmount());
ingredientHelper.put(nssLeftOutput, -leftOutput.getAmount());
if (ingredientHelper.addAsConversion(nssRightOutput, rightOutput.getAmount())) {
handled = true;
}
}
}
return handled;
}
use of mekanism.api.recipes.inputs.FluidStackIngredient in project Mekanism by mekanism.
the class BaseCrTExampleProvider method getIngredientRepresentation.
private String getIngredientRepresentation(CrTImportsComponent imports, FluidStackIngredient ingredient) {
if (ingredient instanceof FluidStackIngredient.Single) {
JsonObject serialized = ingredient.serialize().getAsJsonObject();
String stackRepresentation = new MCFluidStack(SerializerHelper.deserializeFluid(serialized)).getCommandString();
return imports.addImport(CrTConstants.CLASS_FLUID_STACK_INGREDIENT) + ".from(" + stackRepresentation + ")";
} else if (ingredient instanceof FluidStackIngredient.Tagged) {
JsonObject serialized = ingredient.serialize().getAsJsonObject();
String tagRepresentation = TagManagerFluid.INSTANCE.getTag(serialized.get(JsonConstants.TAG).getAsString()).getCommandString();
return imports.addImport(CrTConstants.CLASS_FLUID_STACK_INGREDIENT) + ".from(" + tagRepresentation + ", " + serialized.getAsJsonPrimitive(JsonConstants.AMOUNT) + ")";
} else if (ingredient instanceof FluidStackIngredient.Multi) {
FluidStackIngredient.Multi multiIngredient = (FluidStackIngredient.Multi) ingredient;
StringBuilder builder = new StringBuilder(imports.addImport(CrTConstants.CLASS_FLUID_STACK_INGREDIENT) + ".createMulti(");
if (!multiIngredient.forEachIngredient(i -> {
String rep = getIngredientRepresentation(imports, i);
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.FluidStackIngredient in project Mekanism by mekanism.
the class PressurizedReactionRecipeSerializer method fromJson.
@Nonnull
@Override
public RECIPE fromJson(@Nonnull ResourceLocation recipeId, @Nonnull JsonObject json) {
JsonElement itemInput = JSONUtils.isArrayNode(json, JsonConstants.ITEM_INPUT) ? JSONUtils.getAsJsonArray(json, JsonConstants.ITEM_INPUT) : JSONUtils.getAsJsonObject(json, JsonConstants.ITEM_INPUT);
ItemStackIngredient solidIngredient = ItemStackIngredient.deserialize(itemInput);
JsonElement fluidInput = JSONUtils.isArrayNode(json, JsonConstants.FLUID_INPUT) ? JSONUtils.getAsJsonArray(json, JsonConstants.FLUID_INPUT) : JSONUtils.getAsJsonObject(json, JsonConstants.FLUID_INPUT);
FluidStackIngredient fluidIngredient = FluidStackIngredient.deserialize(fluidInput);
JsonElement gasInput = JSONUtils.isArrayNode(json, JsonConstants.GAS_INPUT) ? JSONUtils.getAsJsonArray(json, JsonConstants.GAS_INPUT) : JSONUtils.getAsJsonObject(json, JsonConstants.GAS_INPUT);
GasStackIngredient gasIngredient = GasStackIngredient.deserialize(gasInput);
FloatingLong energyRequired = FloatingLong.ZERO;
if (json.has(JsonConstants.ENERGY_REQUIRED)) {
energyRequired = SerializerHelper.getFloatingLong(json, JsonConstants.ENERGY_REQUIRED);
}
int duration;
JsonElement ticks = json.get(JsonConstants.DURATION);
if (!JSONUtils.isNumberValue(ticks)) {
throw new JsonSyntaxException("Expected duration to be a number greater than zero.");
}
duration = ticks.getAsJsonPrimitive().getAsInt();
if (duration <= 0) {
throw new JsonSyntaxException("Expected duration to be a number greater than zero.");
}
ItemStack itemOutput = ItemStack.EMPTY;
GasStack gasOutput = GasStack.EMPTY;
if (json.has(JsonConstants.ITEM_OUTPUT)) {
itemOutput = SerializerHelper.getItemStack(json, JsonConstants.ITEM_OUTPUT);
if (itemOutput.isEmpty()) {
throw new JsonSyntaxException("Reaction chamber item output must not be empty, if it is defined.");
}
if (json.has(JsonConstants.GAS_OUTPUT)) {
// The gas is optional given we have an output item
gasOutput = SerializerHelper.getGasStack(json, JsonConstants.GAS_OUTPUT);
if (gasOutput.isEmpty()) {
throw new JsonSyntaxException("Reaction chamber gas output must not be empty, if it is defined.");
}
}
} else {
// If we don't have an output item, we are required to have an output gas
gasOutput = SerializerHelper.getGasStack(json, JsonConstants.GAS_OUTPUT);
if (gasOutput.isEmpty()) {
throw new JsonSyntaxException("Reaction chamber gas output must not be empty, if there is no item output.");
}
}
return this.factory.create(recipeId, solidIngredient, fluidIngredient, gasIngredient, energyRequired, duration, itemOutput, gasOutput);
}
Aggregations