Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 70 additions & 3 deletions src/main/java/guideme/internal/siteexport/SiteExportWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import guideme.internal.siteexport.model.FluidInfoJson;
import guideme.internal.siteexport.model.ItemInfoJson;
import guideme.internal.siteexport.model.NavigationNodeJson;
import guideme.internal.siteexport.model.RecipeDisplaysJson;
import guideme.internal.siteexport.model.SiteExportJson;
import guideme.internal.util.Platform;
import guideme.libs.mdast.MdAstVisitor;
Expand All @@ -32,6 +33,7 @@
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HexFormat;
import java.util.List;
Expand All @@ -56,6 +58,10 @@
import net.minecraft.world.item.crafting.SingleItemRecipe;
import net.minecraft.world.item.crafting.SmithingRecipe;
import net.minecraft.world.item.crafting.SmithingTransformRecipe;
import net.minecraft.world.item.crafting.display.RecipeDisplay;
import net.minecraft.world.item.crafting.display.ShapedCraftingRecipeDisplay;
import net.minecraft.world.item.crafting.display.ShapelessCraftingRecipeDisplay;
import net.minecraft.world.item.crafting.display.SlotDisplay;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.storage.TagValueOutput;
import net.neoforged.neoforge.fluids.FluidStack;
Expand Down Expand Up @@ -179,7 +185,12 @@ public void addFluid(String id, FluidStack fluid, String iconPath) {
siteExport.fluids.put(fluidInfo.id, fluidInfo);
}

public void addRecipe(ResourceKey<Recipe<?>> id, CraftingRecipe recipe) {
/**
* Adds a standard shaped or shapeless crafting recipe.
*
* @return false if the recipe is not a standard shaped or shapeless crafting recipe.
*/
public boolean addRecipe(ResourceKey<Recipe<?>> id, CraftingRecipe recipe) {
Map<String, Object> fields = new HashMap<>();
if (recipe instanceof ShapedRecipe shapedRecipe) {
fields.put("shapeless", false);
Expand All @@ -198,11 +209,64 @@ public void addRecipe(ResourceKey<Recipe<?>> id, CraftingRecipe recipe) {
fields.put("resultItem", resultItem);
fields.put("resultCount", resultItem.count());
} else {
LOG.warn("Cannot handle crafting recipe of type {}", recipe);
return;
return false;
}

addRecipe(id, recipe, fields);
return true;
}

/**
* Adds the recipe displays of a recipe that could not be exported otherwise. They are exported separately from the
* recipes, since there is no 1:1 relationship between a recipe and its displays.
*
* @return false if the recipe has no recipe displays.
*/
public boolean addRecipeDisplays(ResourceKey<Recipe<?>> id, Recipe<?> recipe) {
var displays = new ArrayList<JsonObject>();
for (var display : recipe.display()) {
displays.add(GSON.toJsonTree(getRecipeDisplayFields(display)).getAsJsonObject());
}
if (displays.isEmpty()) {
return false;
}

var exportedDisplays = new RecipeDisplaysJson();
exportedDisplays.special = recipe.isSpecial();
exportedDisplays.displays = displays;
if (siteExport.recipeDisplays.put(id.identifier().toString(), exportedDisplays) != null) {
throw new RuntimeException("Duplicate recipe displays for id " + id);
}
return true;
}

private static Map<String, Object> getRecipeDisplayFields(RecipeDisplay display) {
Map<String, Object> fields = new HashMap<>();
fields.put("type", BuiltInRegistries.RECIPE_DISPLAY.getKey(display.type()).toString());
fields.put("result", resolveItemIds(display.result()));
fields.put("resultCount", display.result().resolveForFirstStack(Platform.getSlotDisplayContext()).getCount());
fields.put("craftingStation", resolveItemIds(display.craftingStation()));

if (display instanceof ShapedCraftingRecipeDisplay shapedDisplay) {
fields.put("width", shapedDisplay.width());
fields.put("height", shapedDisplay.height());
fields.put("ingredients",
shapedDisplay.ingredients().stream().map(SiteExportWriter::resolveItemIds).toList());
} else if (display instanceof ShapelessCraftingRecipeDisplay shapelessDisplay) {
fields.put("ingredients",
shapelessDisplay.ingredients().stream().map(SiteExportWriter::resolveItemIds).toList());
}

return fields;
}

private static List<String> resolveItemIds(SlotDisplay display) {
return display.resolveForStacks(Platform.getSlotDisplayContext())
.stream()
.filter(stack -> !stack.isEmpty())
.map(stack -> BuiltInRegistries.ITEM.getKey(stack.getItem()).toString())
.distinct()
.toList();
}

public void addRecipe(ResourceKey<Recipe<?>> id, SingleItemRecipe recipe) {
Expand Down Expand Up @@ -240,6 +304,9 @@ public void addRecipe(ResourceKey<Recipe<?>> id, Recipe<?> recipe, Map<String, O

var type = BuiltInRegistries.RECIPE_TYPE.getKey(recipe.getType()).toString();
jsonObject.addProperty("type", type);
if (recipe.isSpecial()) {
jsonObject.addProperty("special", true);
}

if (siteExport.recipes.put(id.identifier().toString(), jsonObject) != null) {
throw new RuntimeException("Duplicate recipe id " + id);
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/guideme/internal/siteexport/SiteExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,24 +294,28 @@ private void dumpRecipes(SiteExportWriter writer) {
}

if (recipe instanceof CraftingRecipe craftingRecipe) {
if (craftingRecipe.isSpecial()) {
continue;
}
writer.addRecipe(id, craftingRecipe);
handled = writer.addRecipe(id, craftingRecipe);
} else if (recipe instanceof AbstractCookingRecipe cookingRecipe) {
writer.addRecipe(id, cookingRecipe);
handled = true;
} else if (recipe instanceof SmithingRecipe smithingRecipe) {
writer.addRecipe(id, smithingRecipe);
handled = true;
} else if (recipe instanceof SingleItemRecipe singleItemRecipe) {
writer.addRecipe(id, singleItemRecipe);
handled = true;
} else {
var recipeFields = getCustomRecipeFields(id, recipe);
if (recipeFields != null) {
writer.addRecipe(id, recipe, recipeFields);
} else {
LOG.warn("Unable to handle recipe {} of type {}", holder.id(), recipe.getType());
handled = true;
}
}

// Fall back to exporting the recipe displays, which is how the recipe is shown in-game as well
if (!handled && !writer.addRecipeDisplays(id, recipe)) {
LOG.warn("Unable to handle recipe {} of type {}", holder.id(), recipe.getType());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package guideme.internal.siteexport.model;

import com.google.gson.JsonObject;
import java.util.ArrayList;
import java.util.List;

/**
* The recipe displays of a recipe that was not exported as a recipe.
*/
public class RecipeDisplaysJson {
/**
* Special recipes are only used for an item if there is no other recipe for it.
*/
public boolean special;

public List<JsonObject> displays = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public class SiteExportJson {
*/
public Map<String, JsonObject> recipes = new HashMap<>();

/**
* Recipe displays of recipes that were not exported as recipes, indexed by their recipe ID. Missing in exports from
* older versions.
*/
public Map<String, RecipeDisplaysJson> recipeDisplays = new HashMap<>();

public Map<String, ItemInfoJson> items = new HashMap<>();

public List<NavigationNodeJson> navigationRootNodes = new ArrayList<>();
Expand Down
Loading