Skip to content

Commit

Permalink
Add support for a page to be module dependent if using a moduleFileRe…
Browse files Browse the repository at this point in the history
…pository

Will be used in Tinkers Complement as its added modifiers are dependent on certain mods/modules being loaded
  • Loading branch information
KnightMiner committed May 18, 2019
1 parent 95918e9 commit d9291f5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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 = "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void load() {
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);
Expand All @@ -81,6 +81,15 @@ public void load() {
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) {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ public List<SectionData> getSections() {
.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;
}
}

0 comments on commit d9291f5

Please sign in to comment.