use of mekanism.api.recipes.inputs.ItemStackIngredient in project Mekanism by mekanism.
the class BaseCrTExampleProvider method getIngredientRepresentation.
private String getIngredientRepresentation(CrTImportsComponent imports, ItemStackIngredient ingredient) {
if (ingredient instanceof ItemStackIngredient.Single) {
JsonObject serialized = ingredient.serialize().getAsJsonObject();
// While it is easier to compare types of some stuff using the inner ingredient, some things
// are easier to get the information of out of the serialized ingredient
JsonObject serializedIngredient = serialized.getAsJsonObject(JsonConstants.INGREDIENT);
Ingredient vanillaIngredient = ((ItemStackIngredient.Single) ingredient).getInputRaw();
int amount = JSONUtils.getAsInt(serialized, JsonConstants.AMOUNT, 1);
String representation = null;
if (amount > 1) {
// Special case handling for when we would want to use a different constructor
if (vanillaIngredient.isVanilla() && !serializedIngredient.isJsonArray() && serializedIngredient.has(JsonConstants.ITEM)) {
Item item = ForgeRegistries.ITEMS.getValue(new ResourceLocation(JSONUtils.getAsString(serializedIngredient, JsonConstants.ITEM)));
representation = ItemStackHelper.getCommandString(new ItemStack(item, amount));
amount = 1;
} else if (vanillaIngredient instanceof NBTIngredient) {
ItemStack stack = CraftingHelper.getItemStack(serializedIngredient, true);
stack.setCount(amount);
representation = ItemStackHelper.getCommandString(stack);
amount = 1;
}
}
if (representation == null) {
representation = IIngredient.fromIngredient(vanillaIngredient).getCommandString();
}
String path = imports.addImport(CrTConstants.CLASS_ITEM_STACK_INGREDIENT);
if (amount == 1) {
return path + ".from(" + representation + ")";
}
return path + ".from(" + representation + ", " + amount + ")";
} else if (ingredient instanceof ItemStackIngredient.Multi) {
ItemStackIngredient.Multi multiIngredient = (ItemStackIngredient.Multi) ingredient;
StringBuilder builder = new StringBuilder(imports.addImport(CrTConstants.CLASS_ITEM_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;
}
Aggregations