package org.cyclops.cyclopscore.infobook;

import com.google.common.collect.Maps;
import net.minecraftforge.client.event.RecipesUpdatedEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.ForgeRegistries;

import java.util.Map;

/**
 * Registry for info books for a mod.
 * @author rubensworks
 */
public class InfoBookRegistry implements IInfoBookRegistry {

    private final Map<IInfoBook, String> bookPaths = Maps.newIdentityHashMap();
    private final Map<IInfoBook, InfoSection> bookRoots = Maps.newIdentityHashMap();

    public InfoBookRegistry() {
        MinecraftForge.EVENT_BUS.register(this);
    }

    @Override
    public void registerInfoBook(IInfoBook infoBook, String path) {
        bookPaths.put(infoBook, path);
    }

    @Override
    public void registerSection(IInfoBook infoBook, String parentSection, String sectionPath) {
        InfoSection section = infoBook.getSection(parentSection);
        if (section == null) {
            throw new IllegalArgumentException(String.format("Could not find section '%s' in infobook '%s'.", parentSection, infoBook));
        }
        section.registerSection(InfoBookParser.initializeInfoBook(infoBook, sectionPath, section));
    }

    @Override
    public InfoSection getRoot(IInfoBook infoBook) {
        return bookRoots.get(infoBook);
    }

    @SubscribeEvent
    public void onRecipesLoaded(RecipesUpdatedEvent event) {
        // Load after recipes are loaded client-side
        for (Map.Entry<IInfoBook, String> entry : bookPaths.entrySet()) {
            bookRoots.put(entry.getKey(), InfoBookParser.initializeInfoBook(entry.getKey(), entry.getValue(), null));
            // Reset the infobook history
            entry.getKey().setCurrentSection(null);
        }
    }
}

@@ -36,16 +36,56 @@
 obook;%0A%0A
+import com.google.common.collect.Lists;%0A
 import c
@@ -228,109 +228,8 @@
 ge;%0A
-import net.minecraftforge.event.RegistryEvent;%0Aimport net.minecraftforge.eventbus.api.EventPriority;%0A
 impo
@@ -283,129 +283,31 @@
 nt;%0A
+%0A
 import 
-net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;%0Aimport net.minecraftforge.registries.ForgeRegistries;%0A
+java.util.List;
 %0Aimp
@@ -621,16 +621,99 @@
 shMap();
+%0A    private final List%3CSectionInjection%3E sectionInjections = Lists.newArrayList();
 %0A%0A    pu
@@ -1044,205 +1044,59 @@
     
-InfoSection section = infoBook.getSection(parentSection);%0A        if (section == null) %7B%0A            throw new IllegalArgumentException(String.format(%22Could not find section '%25s' in 
+sectionInjections.add(new SectionInjection(
 info
-b
+B
 ook
- '%25s'.%22
 , pa
@@ -1111,106 +1111,8 @@
 ion,
- infoBook));%0A        %7D%0A        section.registerSection(InfoBookParser.initializeInfoBook(infoBook,
  sec
@@ -1119,25 +1119,16 @@
 tionPath
-, section
 ));%0A    
@@ -1674,16 +1674,1305 @@
       %7D%0A
-    %7D
+%0A        // Load section injections%0A        for (SectionInjection sectionInjection : sectionInjections) %7B%0A            InfoSection section = sectionInjection.getInfoBook().getSection(sectionInjection.getParentSection());%0A            if (section == null) %7B%0A                throw new IllegalArgumentException(String.format(%22Could not find section '%25s' in infobook '%25s'.%22, sectionInjection.getParentSection(), sectionInjection.getInfoBook()));%0A            %7D%0A            section.registerSection(InfoBookParser.initializeInfoBook(sectionInjection.getInfoBook(), sectionInjection.getSectionPath(), section));%0A        %7D%0A    %7D%0A%0A    private static final class SectionInjection %7B%0A        private final IInfoBook infoBook;%0A        private final String parentSection;%0A        private final String sectionPath;%0A%0A        private SectionInjection(IInfoBook infoBook, String parentSection, String sectionPath) %7B%0A            this.infoBook = infoBook;%0A            this.parentSection = parentSection;%0A            this.sectionPath = sectionPath;%0A        %7D%0A%0A        public IInfoBook getInfoBook() %7B%0A            return infoBook;%0A        %7D%0A%0A        public String getParentSection() %7B%0A            return parentSection;%0A        %7D%0A%0A        public String getSectionPath() %7B%0A            return sectionPath;%0A        %7D%0A    %7D%0A
 %0A%7D%0A

Fix infobook section injection broken in 1.15