Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multiple matching Stacks #27

Merged
merged 1 commit into from
Oct 27, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,57 @@ public String getDataName() {
return "recipes";
}

private void generateShapedRecipe(DynamicRegistryManager registryManager, JsonObject finalObj, ShapedRecipe sr, int n) {
boolean hasIncremented = false;
var ingredients = sr.getIngredients();
List<Integer> ingr = new ArrayList<>();
for (int i = 0; i < 9; i++) {
if (i >= ingredients.size()) {
ingr.add(-1);
continue;
}
var stacks = ingredients.get(i);
var matching = stacks.getMatchingStacks(); // FIXME: fix when there are more than one matching stack
if (matching.length == 0) {
ingr.add(-1);
} else if (matching.length > n){
ingr.add(getRawIdFor(matching[n].getItem()));
} else {
ingr.add(getRawIdFor(matching[0].getItem()));
}
if (matching.length-1 > n && !hasIncremented) {
generateShapedRecipe(registryManager, finalObj, sr, n+1);
hasIncremented = true;
}
}

JsonArray inShape = new JsonArray();

var iter = ingr.iterator();
for (int y = 0; y < sr.getHeight(); y++) {
var jsonRow = new JsonArray();
for (int z = 0; z < sr.getWidth(); z++) {
jsonRow.add(iter.next());
}
inShape.add(jsonRow);
}

JsonObject finalRecipe = new JsonObject();
finalRecipe.add("inShape", inShape);

var resultObject = new JsonObject();
resultObject.addProperty("id", getRawIdFor(sr.getResult(registryManager).getItem()));
resultObject.addProperty("count", sr.getResult(registryManager).getCount());
finalRecipe.add("result", resultObject);

String id = ((Integer) getRawIdFor(sr.getResult(registryManager).getItem())).toString();

if (!finalObj.has(id)) {
finalObj.add(id, new JsonArray());
}
finalObj.get(id).getAsJsonArray().add(finalRecipe);
}

@Override
public JsonElement generateDataJson() {
DynamicRegistryManager registryManager = DGU.getWorld().getRegistryManager();
Expand All @@ -34,49 +85,7 @@ public JsonElement generateDataJson() {
for (RecipeEntry<?> recipeE : Objects.requireNonNull(DGU.getWorld()).getRecipeManager().values()) {
Recipe<?> recipe = recipeE.value();
if (recipe instanceof ShapedRecipe sr) {
var ingredients = sr.getIngredients();
List<Integer> ingr = new ArrayList<>();
for (int i = 0; i < 9; i++) {
if (i >= ingredients.size()) {
ingr.add(-1);
continue;
}
var stacks = ingredients.get(i);
var matching = stacks.getMatchingStacks();
if (matching.length == 0) {
ingr.add(-1);
} else {
ingr.add(getRawIdFor(matching[0].getItem()));
}
}
//Lists.reverse(ingr);

JsonArray inShape = new JsonArray();


var iter = ingr.iterator();
for (int y = 0; y < sr.getHeight(); y++) {
var jsonRow = new JsonArray();
for (int z = 0; z < sr.getWidth(); z++) {
jsonRow.add(iter.next());
}
inShape.add(jsonRow);
}

JsonObject finalRecipe = new JsonObject();
finalRecipe.add("inShape", inShape);

var resultObject = new JsonObject();
resultObject.addProperty("id", getRawIdFor(sr.getResult(registryManager).getItem()));
resultObject.addProperty("count", sr.getResult(registryManager).getCount());
finalRecipe.add("result", resultObject);

String id = ((Integer) getRawIdFor(sr.getResult(registryManager).getItem())).toString();

if (!finalObj.has(id)) {
finalObj.add(id, new JsonArray());
}
finalObj.get(id).getAsJsonArray().add(finalRecipe);
generateShapedRecipe(registryManager, finalObj, sr, 0);
// var input = new JsonArray();
// var ingredients = sr.getIngredients().stream().toList();
// for (int y = 0; y < sr.getHeight(); y++) {
Expand Down
Loading