Skip to content

Commit

Permalink
Backported 1.20 recipe ID concurreny bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Dec 6, 2023
1 parent 7f91e09 commit 72533bc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 21 deletions.
23 changes: 4 additions & 19 deletions common/src/main/java/dev/latvian/mods/kubejs/recipe/RecipeJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -407,27 +407,12 @@ public ResourceLocation getOrCreateId() {
var prefix = js.id.getNamespace() + ":kjs_";

if (ids == null) {
id = new ResourceLocation(prefix + UtilsJS.getUniqueId(json));
return id;
}

if (ids.startsWith("minecraft:")) {
ids = ids.substring("minecraft:".length());
} else if (ids.startsWith("kubejs:")) {
ids = ids.substring("kubejs:".length());
}

ids = ids.replace(':', '_').replace('/', '_');

int i = 2;
id = new ResourceLocation(prefix + ids);

while (type.event.takenIds.containsKey(id)) {
id = new ResourceLocation(prefix + ids + "_" + i);
i++;
ids = UtilsJS.getUniqueId(json);
} else {
ids = RecipeSchema.normalizeId(ids).replace(':', '_').replace('/', '_');
}

type.event.takenIds.put(id, this);
id = type.event.takeId(this, prefix, ids);
}

return id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,19 @@ public void printExamples(String type) {
}
}

public synchronized ResourceLocation takeId(RecipeJS recipe, String prefix, String ids) {
int i = 2;
var id = new ResourceLocation(prefix + ids);

while (takenIds.containsKey(id)) {
id = new ResourceLocation(prefix + ids + '_' + i);
i++;
}

takenIds.put(id, recipe);
return id;
}

public void setItemErrors(boolean b) {
RecipeJS.itemErrors = b;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,52 @@ public RecipeSchema uniqueId(Function<RecipeJS, String> uniqueIdFunction) {
return this;
}

public static String normalizeId(String id) {
if (id.startsWith("minecraft:")) {
return id.substring(10);
} else if (id.startsWith("kubejs:")) {
return id.substring(7);
} else {
return id;
}
}

public RecipeSchema uniqueOutputId(RecipeKey<OutputItem> resultItemKey) {
return uniqueId(r -> {
var item = r.getValue(resultItemKey);
return item == null || item.isEmpty() ? null : item.item.kjs$getId();
return item == null || item.isEmpty() ? null : normalizeId(item.item.kjs$getId());
});
}

public RecipeSchema uniqueOutputArrayId(RecipeKey<OutputItem[]> resultItemKey) {
return uniqueId(r -> {
var array = r.getValue(resultItemKey);

if (array == null || array.length == 0) {
return null;
}

var sb = new StringBuilder();

for (var item : array) {
if (!item.isEmpty()) {
if (!sb.isEmpty()) {
sb.append('_');
}

sb.append(normalizeId(item.item.kjs$getId()));
}
}

return sb.isEmpty() ? null : sb.toString();
});
}

public RecipeSchema uniqueInputId(RecipeKey<InputItem> resultItemKey) {
return uniqueId(r -> {
var ingredient = r.getValue(resultItemKey);
var item = ingredient == null ? null : ingredient.ingredient.kjs$getFirst();
return item == null || item.isEmpty() ? null : item.kjs$getId();
return item == null || item.isEmpty() ? null : normalizeId(item.kjs$getId());
});
}

Expand Down

0 comments on commit 72533bc

Please sign in to comment.