Skip to content

Commit

Permalink
Make the patchouli provider actually work correctly when extending fo…
Browse files Browse the repository at this point in the history
…reign books.
  • Loading branch information
noeppi-noeppi committed Dec 10, 2023
1 parent 09f0ef5 commit b0278b3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ItemLike;
import org.moddingx.libx.datagen.provider.patchouli.page.PageJson;
import org.moddingx.libx.mod.ModX;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -16,6 +17,7 @@
*/
public class CategoryBuilder {

public final ModX mod;
public final ResourceLocation id;
private String name;
private String description;
Expand All @@ -24,7 +26,8 @@ public class CategoryBuilder {
private int sort;
private final List<Consumer<JsonObject>> postProcessors;

public CategoryBuilder(ResourceLocation id) {
public CategoryBuilder(ModX mod, ResourceLocation id) {
this.mod = mod;
this.id = id;
this.sort = -1;
this.postProcessors = new ArrayList<>();
Expand Down Expand Up @@ -82,8 +85,8 @@ JsonObject build(BiFunction<String, List<String>, String> translations, int num)
if (this.description == null) throw new IllegalStateException("Category description not set: " + this.id);
if (this.icon == null) throw new IllegalStateException("Category icon not set: " + this.id);
JsonObject json = new JsonObject();
json.addProperty("name", translations.apply(this.name, List.of("category", this.id.getNamespace(), this.id.getPath(), "name")));
json.addProperty("description", translations.apply(this.description, List.of("category", this.id.getNamespace(), this.id.getPath(), "description")));
json.addProperty("name", translations.apply(this.name, List.of("category", this.mod.modid, this.id.getNamespace(), this.id.getPath(), "name")));
json.addProperty("description", translations.apply(this.description, List.of("category", this.mod.modid, this.id.getNamespace(), this.id.getPath(), "description")));
json.add("icon", PageJson.stack(this.icon));
json.addProperty("sortnum", this.sort < 0 ? num : this.sort);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.moddingx.libx.datagen.provider.patchouli.page.PageBuilder;
import org.moddingx.libx.datagen.provider.patchouli.page.PageJson;
import org.moddingx.libx.impl.datagen.patchouli.content.*;
import org.moddingx.libx.mod.ModX;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -26,6 +27,7 @@
*/
public class EntryBuilder {

public final ModX mod;
public final String id;
public final ResourceLocation category;
private String name;
Expand All @@ -34,7 +36,8 @@ public class EntryBuilder {
private Content content;
private final List<Consumer<JsonObject>> postProcessors;

public EntryBuilder(String id, ResourceLocation category) {
public EntryBuilder(ModX mod, String id, ResourceLocation category) {
this.mod = mod;
this.id = id;
this.category = category;
this.name = null;
Expand Down Expand Up @@ -72,7 +75,7 @@ public EntryBuilder icon(ItemStack icon) {
* entry belongs to.
*/
public EntryBuilder advancement(String path) {
return this.advancement(this.category.getNamespace(), path);
return this.advancement(this.mod.modid, path);
}

/**
Expand Down Expand Up @@ -124,7 +127,7 @@ public EntryBuilder flip(String anchor) {
* Adds some images to the entry. The image namespaces are set to the namespace of the category, this entry belongs to.
*/
public EntryBuilder image(String title, String... images) {
return this.image(title, Arrays.stream(images).map(s -> new ResourceLocation(this.category.getNamespace(), s)).toArray(ResourceLocation[]::new));
return this.image(title, Arrays.stream(images).map(s -> new ResourceLocation(this.mod.modid, s)).toArray(ResourceLocation[]::new));
}

/**
Expand All @@ -139,7 +142,7 @@ public EntryBuilder image(String title, ResourceLocation... images) {
* and form a double recipe page.
*/
public EntryBuilder crafting(String path) {
return this.crafting(this.category.getNamespace(), path);
return this.crafting(this.mod.modid, path);
}

/**
Expand All @@ -163,7 +166,7 @@ public EntryBuilder crafting(ResourceLocation id) {
* and form a double recipe page.
*/
public EntryBuilder smelting(String path) {
return this.smelting(this.category.getNamespace(), path);
return this.smelting(this.mod.modid, path);
}

/**
Expand Down Expand Up @@ -246,7 +249,7 @@ JsonObject build(BiFunction<String, List<String>, String> translations, Existing
if (this.name == null) throw new IllegalStateException("Entry name not set: " + this.category + "/" + this.id);
if (this.icon == null) throw new IllegalStateException("Entry icon not set: " + this.category + "/" + this.id);
JsonObject json = new JsonObject();
json.addProperty("name", translations.apply(this.name, List.of("entry", this.category.getNamespace(), this.category.getPath(), this.id)));
json.addProperty("name", translations.apply(this.name, List.of("entry", this.mod.modid, this.category.getNamespace(), this.category.getPath(), this.id)));
json.addProperty("category", this.category.toString());
json.add("icon", PageJson.stack(this.icon));
if (this.advancement != null) {
Expand Down Expand Up @@ -291,7 +294,7 @@ public void addAnchor(String name) {

@Override
public String translate(String localized) {
return translations.apply(localized, List.of("entry", EntryBuilder.this.category.getNamespace(), EntryBuilder.this.category.getPath(), EntryBuilder.this.id, "page" + this.page, "text" + (this.key++)));
return translations.apply(localized, List.of("entry", EntryBuilder.this.mod.modid, EntryBuilder.this.category.getNamespace(), EntryBuilder.this.category.getPath(), EntryBuilder.this.id, "page" + this.page, "text" + (this.key++)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public PatchouliProviderBase(DatagenContext ctx, BookProperties properties) {
* @see CategoryBuilder
*/
public CategoryBuilder category(String id) {
CategoryBuilder builder = new CategoryBuilder(new ResourceLocation(this.bookNamespace, id));
CategoryBuilder builder = new CategoryBuilder(this.mod, new ResourceLocation(this.bookNamespace, id));
this.categories.add(builder);
this.categoryIds.add(id);
return builder;
Expand Down Expand Up @@ -105,7 +105,7 @@ private EntryBuilder entry(String id, String category, boolean foreignEntry) {
} else {
if (!this.categoryIds.contains(category)) throw new IllegalArgumentException("Unknown category: " + category);
}
EntryBuilder builder = new EntryBuilder(id, new ResourceLocation(this.bookNamespace, category));
EntryBuilder builder = new EntryBuilder(this.mod, id, new ResourceLocation(this.bookNamespace, category));
this.entries.add(builder);
return builder;
}
Expand Down

0 comments on commit b0278b3

Please sign in to comment.