Skip to content

Commit

Permalink
feat: add legacy datagen providers for easier migration and per-modlo…
Browse files Browse the repository at this point in the history
…ader sub provider registration helpers
  • Loading branch information
klikli-dev committed Jun 17, 2024
1 parent 2b8d602 commit 2ee5907
Show file tree
Hide file tree
Showing 13 changed files with 370 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
public abstract class CategoryProvider extends ModonomiconProviderBase {

protected final ModonomiconProviderBase parent;
protected final Map<String, List<BookPageModel>> cachedPages = new Object2ObjectOpenHashMap<>();
protected CategoryEntryMap entryMap;
protected BookCategoryModel category;
protected int currentSortIndex;
Expand All @@ -49,119 +48,6 @@ protected Map<String, String> macros() {
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
}

/**
* @deprecated use {@link EntryProvider()} instead.
*/
@Deprecated(forRemoval = true)
protected BookEntryModel entry(String location, ResourceLocation texture) {
return this.entry(location).withIcon(texture);
}

/**
* @deprecated use {@link EntryProvider()} instead.
*/
@Deprecated(forRemoval = true)
protected BookEntryModel entry(String location, ResourceLocation texture, int width, int height) {
return this.entry(location).withIcon(texture, width, height);
}

/**
* @deprecated use {@link EntryProvider()} instead.
*/
@Deprecated(forRemoval = true)
protected BookEntryModel entry(String location, ItemLike icon) {
return this.entry(location).withIcon(icon);
}

/**
* @deprecated use {@link EntryProvider()} instead.
*/
@Deprecated(forRemoval = true)
protected BookEntryModel entry(char location, ResourceLocation texture) {
return this.entry(location).withIcon(texture);
}

/**
* @deprecated use {@link EntryProvider()} instead.
*/
@Deprecated(forRemoval = true)
protected BookEntryModel entry(char location, ResourceLocation texture, int width, int height) {
return this.entry(location).withIcon(texture, width, height);
}

/**
* @deprecated use {@link EntryProvider()} instead.
*/
@Deprecated(forRemoval = true)
protected BookEntryModel entry(char location, ItemLike icon) {
return this.entry(location).withIcon(icon);
}

/**
* @deprecated use {@link EntryProvider()} instead.
*/
@Deprecated(forRemoval = true)
protected BookEntryModel entry(char location) {
return this.entry().withLocation(this.entryMap().get(location));
}

/**
* @deprecated use {@link EntryProvider()} instead.
*/
@Deprecated(forRemoval = true)
protected BookEntryModel entry(String location) {
return this.entry().withLocation(this.entryMap().get(location));
}

/**
* @deprecated use {@link EntryProvider()} instead.
*/
@Deprecated(forRemoval = true)
protected BookEntryModel entry() {
var entry = BookEntryModel.create(
this.modLoc(this.context().categoryId() + "/" + this.context().entryId()),
this.context().entryName()
)
.withDescription(this.context().entryDescription());
if (this.cachedPages.containsKey(this.context().entry())) {
entry.withPages(this.cachedPages.get(this.context().entry()));
this.cachedPages.remove(this.context().entry());
}
return entry;
}

/**
* Adds a page to the cached pages of this category provider.
* Make sure to call this.context().page(<pageId>) before calling this method!
* The page will be added to the next entry created with this.entry(...)
* Needs to be called after this.context().entry(<entryId>)
*
* @param model the page model
* @deprecated use {@link EntryProvider()} instead.
*/
@Deprecated(forRemoval = true)
protected <T extends BookPageModel> T page(T model) {
this.cachedPages.computeIfAbsent(this.context().entry(), k -> new ArrayList<>()).add(model);
return model;
}

/**
* Registers the page with the current context and adds it to the cached pages of this category provider.
* No need to call this.context().page(<pageId>). This method will do that for you.
* The page will be added to the next entry created with this.entry(...)
* Needs to be called after this.context().entry(<entryId>)
*
* @param modelSupplier A supplier that provides a page model. It is a supplier, because that way you can use this.context() within the supplier and it will correctly use the given page as part of the context.
* @deprecated use {@link EntryProvider()} instead.
*/
@Deprecated(forRemoval = true)
protected <T extends BookPageModel> T page(String page, Supplier<T> modelSupplier) {
this.context().page(page);
var model = modelSupplier.get();
this.cachedPages.computeIfAbsent(this.context().entry(), k -> new ArrayList<>()).add(model);
return model;
}

protected BookEntryParentModel parent(BookEntryModel parentEntry) {
return BookEntryParentModel.create(parentEntry.getId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected void pageText(String text, Object... args) {
*
* @param model the page model
*/
protected <T extends BookPageModel> T page(T model) {
protected <T extends BookPageModel<?>> T page(T model) {
return this.add(model);
}

Expand All @@ -77,20 +77,19 @@ protected <T extends BookPageModel> T page(T model) {
*
* @param modelSupplier A supplier that provides a page model. It is a supplier, because that way you can use this.context() within the supplier and it will correctly use the given page as part of the context.
*/
protected <T extends BookPageModel> T page(String page, Supplier<T> modelSupplier) {
protected <T extends BookPageModel<?>> T page(String page, Supplier<T> modelSupplier) {
this.context().page(page);
var model = modelSupplier.get();
return this.add(model);
}

protected <T extends BookPageModel> T add(T page) {
protected <T extends BookPageModel<?>> T add(T page) {
this.entry.withPage(page);
return page;
}

protected <T extends BookPageModel> List<T> add(List<T> pages) {
//noinspection unchecked
this.entry.withPages((List<BookPageModel>) pages);
protected List<BookPageModel<?>> add(List<BookPageModel<?>> pages) {
this.entry.withPages(pages);
return pages;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* SPDX-FileCopyrightText: 2022 klikli-dev
*
* SPDX-License-Identifier: MIT
*/


package com.klikli_dev.modonomicon.api.datagen;

import com.klikli_dev.modonomicon.api.datagen.book.BookCategoryModel;
import com.klikli_dev.modonomicon.api.datagen.book.BookModel;
import net.minecraft.data.PackOutput;
import net.minecraft.resources.ResourceLocation;

import java.util.Map;
import java.util.function.BiConsumer;

/**
* A book sup provider that is oriented on the legacy book provider API for easier migration.
* It still needs to be handed over to a book provider!
*/
public abstract class LegacyBookProvider extends ModonomiconProviderBase implements BookSubProvider {
protected BookModel book;
protected String bookId;
protected int currentSortIndex;

/**
* Copy of the old constructor to keep compatibility, despite not needing all parameters.
*/
public LegacyBookProvider(String bookId, PackOutput packOutput, String modId, ModonomiconLanguageProvider defaultLang, ModonomiconLanguageProvider... translations) {
this(bookId, modId, defaultLang, translations);
}

/**
* @param defaultLang The LanguageProvider to fill with this book provider. IMPORTANT: the Language Provider needs to be added to the DataGenerator AFTER the BookProvider.
*/
public LegacyBookProvider(String bookId, String modId, ModonomiconLanguageProvider defaultLang, ModonomiconLanguageProvider... translations) {
this(bookId, modId, defaultLang, makeLangMap(defaultLang, translations));
}

/**
* @param defaultLang The LanguageProvider to fill with this book provider. IMPORTANT: the Language Provider needs to be added to the DataGenerator AFTER the BookProvider.
*/
public LegacyBookProvider(String bookId, String modId, ModonomiconLanguageProvider defaultLang, Map<String, ModonomiconLanguageProvider> translations) {
super(modId, defaultLang, translations, new BookContextHelper(modId), new ConditionHelper());
this.book = null;

this.bookId = bookId;
this.currentSortIndex = 0;
}

public String bookId() {
return this.bookId;
}

/**
* Register a macro (= simple string.replace() of macro -> value) to be used in all category providers of this book.
*/
protected void registerDefaultMacro(String macro, String value) {
this.registerMacro(macro, value);
}

protected BookCategoryModel add(BookCategoryModel category) {
if (category.getSortNumber() == -1) {
category.withSortNumber(this.currentSortIndex++);
}
this.book.withCategory(category);
return category;
}

@Override
public void generate(BiConsumer<ResourceLocation, BookModel> consumer) {
this.registerDefaultMacros();

this.context().book(this.bookId());
var book = this.generateBook();

consumer.accept(this.book.getId(), this.book);
}

/**
* Call registerMacro() here to make macros (= simple string.replace() of macro -> value) available to all category providers of this book.
*/
protected abstract void registerDefaultMacros();

/**
* Override this to generate your book.
* Each BookProvider should generate only one book.
* Context already is set to the book id provided in the constructor.
*/
protected abstract BookModel generateBook();
}
Loading

0 comments on commit 2ee5907

Please sign in to comment.