Add support for a page to be module dependent if using a moduleFileRepository

Will be used in Tinkers Complement as its added modifiers are dependent on certain mods/modules being loaded
This commit is contained in:
KnightMiner committed 2019-05-18 00:18:44 -05:00
1 parent 95918e9005
commit d9291f5171
4 files changed
+46 -1

No files matched your search

@@ -0,0 +1,6 @@
package slimeknights.mantle.client.book.data;
public class PageDataModule extends PageData {
/** Module required to load this section, see {@link slimeknights.mantle.pulsar.control.PulseManager#isPulseLoaded(String)} for more details */
public String module = "";
}
@@ -58,7 +58,7 @@ public class SectionData implements IDataItem {
String data = source.resourceToString(pagesInfo);
if(!data.isEmpty()) {
try {
pages = new ArrayList<>(Arrays.asList(BookLoader.GSON.fromJson(data, PageData[].class)));
pages = getPages(data);
} catch(Exception e) {
pages = new ArrayList<>();
PageData pdError = new PageData(true);
@@ -81,6 +81,15 @@ public class SectionData implements IDataItem {
icon.location = source.getResourceLocation(icon.file, true);
}
/**
* Gets a list of pages from the given data
* @param data JSON data
* @return ArrayList of pages for the book
*/
protected ArrayList<PageData> getPages(String data) {
return new ArrayList<>(Arrays.asList(BookLoader.GSON.fromJson(data, PageData[].class)));
}
public void update(@Nullable GuiBook.AdvancementCache advancementCache) {
}
@@ -1,5 +1,27 @@
package slimeknights.mantle.client.book.data;
import slimeknights.mantle.client.book.BookLoader;
import slimeknights.mantle.client.book.repository.ModuleFileRepository;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class SectionDataModule extends SectionData {
/** Module required to load this section, see {@link slimeknights.mantle.pulsar.control.PulseManager#isPulseLoaded(String)} for more details */
public String module = "";
@Override
protected ArrayList<PageData> getPages(String data) {
// this should always be the case, but just for safety
if(source instanceof ModuleFileRepository) {
// just filters out pages where the module is not loaded
Predicate<String> manager = ((ModuleFileRepository)source).getManager();
return new ArrayList<>(Arrays.stream(BookLoader.GSON.fromJson(data, PageDataModule[].class))
.filter((page)->page.module.isEmpty() || manager.test(page.module))
.collect(Collectors.toList()));
}
return super.getPages(data);
}
}
@@ -31,4 +31,12 @@ public class ModuleFileRepository extends FileRepository {
.filter((section)->section.module.isEmpty() || manager.test(section.module))
.collect(Collectors.toList());
}
/**
* Gets the manager used to determine if a module is available
* @return Predicate to test a module availability
*/
public Predicate<String> getManager() {
return manager;
}
}