Skip to content

Commit

Permalink
Move a lot of the guide init code from gui open to resource reload. S…
Browse files Browse the repository at this point in the history
…hould fix BuildCraft#4249
  • Loading branch information
AlexIIL committed Aug 19, 2018
1 parent d056bc3 commit bb1f7f7
Show file tree
Hide file tree
Showing 24 changed files with 840 additions and 563 deletions.
1 change: 1 addition & 0 deletions buildcraft_resources/changelog/7.99.18
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Bug fixes:

* [#4133] The fluid triggers relating to percentage of a tank filled don't recognise gaseous fluids properly.
* [#4245] Random stack trace caused by a null world in a WorldTickEvent.
* [#4249] The guidebook gui lags when it is opened.
* Fixed gates sometimes not correctly synchronising the light on the gate when it is first turned on.
* Fixed a few recipes requiring the quarry to be present, rather than itself.
* Fixed GuiScreenBuildCraft not passing on tick()'s to child listeners. Only affects addon developers, as BC itself doesn't use this class for anything yet.
Expand Down
10 changes: 7 additions & 3 deletions common/buildcraft/lib/client/guide/GuiGuide.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ public class GuiGuide extends GuiScreen {
public static final GuiIcon PAGE_LEFT_BACK = new GuiIcon(LEFT_PAGE_BACK, 0, 0, 193, 248);
public static final GuiIcon PAGE_RIGHT_BACK = new GuiIcon(RIGHT_PAGE_BACK, 0, 0, 193, 248);

public static final GuiRectangle PAGE_LEFT_TEXT = new GuiRectangle(23, 25, 168, 190);
public static final GuiRectangle PAGE_RIGHT_TEXT = new GuiRectangle(4, 25, 168, 190);
public static final int PAGE_WIDTH = 168;
public static final int PAGE_HEIGHT = 190;

public static final GuiRectangle PAGE_LEFT_TEXT = new GuiRectangle(23, 25, PAGE_WIDTH, PAGE_HEIGHT);
public static final GuiRectangle PAGE_RIGHT_TEXT = new GuiRectangle(4, 25, PAGE_WIDTH, PAGE_HEIGHT);

public static final GuiIcon PEN_UP = new GuiIcon(ICONS_2, 0, 0, 14, 135);
public static final GuiIcon PEN_ANGLED = new GuiIcon(ICONS_2, 17, 0, 100, 100);
Expand Down Expand Up @@ -140,7 +143,7 @@ public class GuiGuide extends GuiScreen {

public final MousePosition mouse = new MousePosition();

public int sortingOrderIndex = 0;
public TypeOrder sortingOrder = SORTING_TYPES[0];
private boolean isOpen = false, isEditing = false;
private boolean isOpening = false;

Expand Down Expand Up @@ -534,6 +537,7 @@ protected void keyTyped(char typedChar, int keyCode) throws IOException {
pages.add(page);
}
}
refreshChapters();
ISimpleDrawable icon = null;
if (BCLibItems.guide != null) {
GuiStack stackIcon = new GuiStack(new ItemStack(BCLibItems.guide));
Expand Down
93 changes: 93 additions & 0 deletions common/buildcraft/lib/client/guide/GuideManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.TimeUnit;

Expand All @@ -31,6 +33,8 @@
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.client.resources.IResourceManagerReloadListener;
import net.minecraft.client.resources.Language;
import net.minecraft.client.util.SuffixArray;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;

Expand All @@ -40,13 +44,24 @@
import buildcraft.api.BCModules;
import buildcraft.api.core.BCDebugging;
import buildcraft.api.core.BCLog;
import buildcraft.api.statements.IStatement;

import buildcraft.lib.client.guide.data.JsonContents;
import buildcraft.lib.client.guide.data.JsonEntry;
import buildcraft.lib.client.guide.data.JsonTypeTags;
import buildcraft.lib.client.guide.loader.IPageLoader;
import buildcraft.lib.client.guide.loader.MarkdownPageLoader;
import buildcraft.lib.client.guide.loader.entry.IEntryLinkConsumer;
import buildcraft.lib.client.guide.loader.entry.PageEntryType;
import buildcraft.lib.client.guide.parts.GuidePageFactory;
import buildcraft.lib.client.guide.parts.GuidePageStandInRecipes;
import buildcraft.lib.client.guide.parts.contents.ContentsNode;
import buildcraft.lib.client.guide.parts.contents.ContentsNodeGui;
import buildcraft.lib.client.guide.parts.contents.GuidePageContents;
import buildcraft.lib.client.guide.parts.contents.IContentsNode;
import buildcraft.lib.client.guide.parts.contents.PageLink;
import buildcraft.lib.client.guide.parts.contents.PageLinkNormal;
import buildcraft.lib.gui.ISimpleDrawable;
import buildcraft.lib.misc.LocaleUtil;

public enum GuideManager implements IResourceManagerReloadListener {
Expand All @@ -64,6 +79,13 @@ public enum GuideManager implements IResourceManagerReloadListener {
private final Map<ItemStack, GuidePageFactory> generatedPages = new HashMap<>();
public static final boolean DEBUG = BCDebugging.shouldDebugLog("lib.guide.loader");

/** Internal use only! Use {@link #addChild(JsonTypeTags, PageLink)} instead! */
public SuffixArray<PageLink> quickSearcher;
private final Map<TypeOrder, ContentsNode> contents = new HashMap<>();

/** Every object added to the guide. Generally this means {@link Item}'s and {@link IStatement}'s. */
public final Set<Object> objectsAdded = new HashSet<>();

static {
PAGE_LOADERS.put("md", MarkdownPageLoader.INSTANCE);
}
Expand Down Expand Up @@ -146,6 +168,9 @@ private void reload(IResourceManager resourceManager) {
if (!DEFAULT_LANG.equals(langCode)) {
loadLangInternal(resourceManager, langCode);
}

generateContentsPage();

watch.stop();
long time = watch.elapsed(TimeUnit.MILLISECONDS);
int p = entries.size();
Expand Down Expand Up @@ -227,6 +252,63 @@ private void loadLangInternal(IResourceManager resourceManager, String lang) {
}
}

private void generateContentsPage() {
objectsAdded.clear();
contents.clear();
for (TypeOrder order : GuiGuide.SORTING_TYPES) {
contents.put(order, new ContentsNode("root", -1));
}
quickSearcher = new SuffixArray<>();

for (PageEntry<?> entry : GuideManager.INSTANCE.getAllEntries()) {
GuidePageFactory entryFactory = GuideManager.INSTANCE.getFactoryFor(entry);

String translatedTitle = LocaleUtil.localize(entry.title);
ISimpleDrawable icon = entry.createDrawable();
PageLine line = new PageLine(icon, icon, 2, translatedTitle, true);

if (entryFactory != null) {
objectsAdded.add(entry.getBasicValue());
PageLinkNormal pageLink = new PageLinkNormal(line, true, entry.getTooltip(), entryFactory);
addChild(entry.typeTags, pageLink);
}
}

final IEntryLinkConsumer adder = this::addChild;
for (PageEntryType<?> type : new HashSet<>(PageEntryType.REGISTRY.values())) {
type.iterateAllDefault(adder);
}

quickSearcher.generate();
for (ContentsNode node : contents.values()) {
node.sort();
}
}

private void addChild(JsonTypeTags tags, PageLink page) {
for (Entry<TypeOrder, ContentsNode> entry : contents.entrySet()) {
TypeOrder order = entry.getKey();
ContentsNode node = entry.getValue();

String[] ordered = tags.getOrdered(order);
for (int i = 0; i < ordered.length; i++) {
String title = LocaleUtil.localize(ordered[i]);
IContentsNode subNode = node.getChild(title);
if (subNode instanceof ContentsNode) {
node = (ContentsNode) subNode;
} else if (subNode == null) {
ContentsNode subContents = new ContentsNode(title, i);
node.addChild(subContents);
node = subContents;
} else {
throw new IllegalStateException("Unknown node type " + subNode.getClass());
}
}
node.addChild(page);
quickSearcher.add(page, node.getSearchName());
}
}

public ImmutableList<PageEntry<?>> getAllEntries() {
return ImmutableList.copyOf(entries);
}
Expand Down Expand Up @@ -256,4 +338,15 @@ public GuidePageFactory getPageFor(@Nonnull ItemStack stack) {
// Create a dummy page for the stack
return generatedPages.computeIfAbsent(stack, GuidePageStandInRecipes::createFactory);
}

public ContentsNodeGui getGuiContents(GuiGuide gui, GuidePageContents guidePageContents, TypeOrder sortingOrder) {

ContentsNode node = contents.get(sortingOrder);

if (node == null) {
throw new IllegalStateException("Unknown sorting order " + sortingOrder);
}

return new ContentsNodeGui(gui, guidePageContents, node);
}
}
22 changes: 8 additions & 14 deletions common/buildcraft/lib/client/guide/PageEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

package buildcraft.lib.client.guide;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

Expand All @@ -29,8 +28,6 @@ public class PageEntry<T> {
public final PageEntryType<T> entryType;
public final T value;

private final List<String> searchTags = new ArrayList<>();

@Nullable
public static PageEntry<?> createPageEntry(JsonEntry entry) {
String typeName = entry.type;
Expand Down Expand Up @@ -87,17 +84,6 @@ private PageEntry(String title, String page, JsonTypeTags typeTags, PageEntryTyp
this.typeTags = typeTags;
this.entryType = entryType;
this.value = value;

searchTags.add(title);
searchTags.add(typeTags.mod);
searchTags.add(typeTags.subMod);
searchTags.add(typeTags.type);
searchTags.add(typeTags.subType);
searchTags.addAll(entryType.getTooltip(value));
}

public List<String> getSearchTags() {
return searchTags;
}

public boolean matches(Object obj) {
Expand All @@ -109,8 +95,16 @@ public ISimpleDrawable createDrawable() {
return entryType.createDrawable(value);
}

public Object getBasicValue() {
return entryType.getBasicValue(value);
}

@Override
public String toString() {
return "PageEntry [title=" + title + ", page=" + page + "]";
}

public List<String> getTooltip() {
return entryType.getTooltip(value);
}
}
18 changes: 17 additions & 1 deletion common/buildcraft/lib/client/guide/TypeOrder.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,20 @@ public class TypeOrder {
public TypeOrder(ETypeTag... tags) {
this.tags = ImmutableList.copyOf(tags);
}
}

@Override
public int hashCode() {
return tags.hashCode();
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) {
return false;
}
TypeOrder other = (TypeOrder) obj;
return tags.equals(other.tags);
}
}
10 changes: 10 additions & 0 deletions common/buildcraft/lib/client/guide/data/JsonTypeTags.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,17 @@ public JsonTypeTags(String mod, String subMod, String type, String subType) {
this.subType = subType;
}

public JsonTypeTags(String type) {
this(null, null, type, null);
}

public String[] getOrdered(TypeOrder typeOrder) {

if (mod == null && subMod == null && subType == null) {
// Built-in type for "others"
return new String[] { "buildcraft.guide.contents.all_group", type };
}

String[] strings = new String[typeOrder.tags.size()];
for (int i = 0; i < strings.length; i++) {
ETypeTag tag = typeOrder.tags.get(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ public List<String> getTooltip(String value) {
public ISimpleDrawable createDrawable(String value) {
return null;
}

@Override
public void iterateAllDefault(IEntryLinkConsumer consumer) {
// NO-OP
}
}
33 changes: 33 additions & 0 deletions common/buildcraft/lib/client/guide/loader/entry/EntryTypeItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@

import javax.annotation.Nullable;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;

import net.minecraftforge.fml.common.registry.ForgeRegistries;

import buildcraft.lib.client.guide.GuideManager;
import buildcraft.lib.client.guide.data.JsonTypeTags;
import buildcraft.lib.client.guide.loader.MarkdownPageLoader;
import buildcraft.lib.client.guide.parts.contents.PageLinkItemStack;
import buildcraft.lib.gui.GuiStack;
import buildcraft.lib.gui.ISimpleDrawable;
import buildcraft.lib.misc.GuiUtil;
import buildcraft.lib.misc.ItemStackKey;

public class EntryTypeItem extends PageEntryType<ItemStackValueFilter> {

private static final JsonTypeTags TAGS = new JsonTypeTags("buildcraft.guide.contents.item_stacks");

public static final String ID = "minecraft:item_stack";
public static final EntryTypeItem INSTANCE = new EntryTypeItem();

Expand Down Expand Up @@ -89,4 +96,30 @@ public boolean matches(ItemStackValueFilter target, Object value) {
public ISimpleDrawable createDrawable(ItemStackValueFilter value) {
return new GuiStack(value.stack.baseStack);
}

@Override
public Object getBasicValue(ItemStackValueFilter value) {
return value.stack.baseStack.getItem();
}

@Override
public void iterateAllDefault(IEntryLinkConsumer consumer) {
for (Item item : ForgeRegistries.ITEMS) {
if (!GuideManager.INSTANCE.objectsAdded.add(item)) {
continue;
}
NonNullList<ItemStack> stacks = NonNullList.create();
item.getSubItems(CreativeTabs.SEARCH, stacks);
for (int i = 0; i < stacks.size(); i++) {
ItemStack stack = stacks.get(i);

consumer.addChild(TAGS, new PageLinkItemStack(false, stack));
if (i > 50) {
// Woah there, lets not fill up entire pages with what is
// most likely the same item
break;
}
}
}
}
}
Loading

0 comments on commit bb1f7f7

Please sign in to comment.