Created
March 18, 2019 04:16
-
-
Save Daomephsta/40e9c2da5c1dec1e6e0b701c74c04cf1 to your computer and use it in GitHub Desktop.
GuideLoaderClient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.daomephsta.inscribe.client.guide; | |
import java.io.IOException; | |
import java.util.*; | |
import java.util.concurrent.CompletableFuture; | |
import java.util.concurrent.Executor; | |
import org.jdom2.Element; | |
import org.jdom2.JDOMException; | |
import org.jdom2.input.SAXBuilder; | |
import org.jdom2.input.sax.XMLReaderSchemaFactory; | |
import io.github.daomephsta.inscribe.client.guide.xmlformat.GuideDefinitionClient; | |
import io.github.daomephsta.inscribe.client.guide.xmlformat.Schemas; | |
import io.github.daomephsta.inscribe.common.Inscribe; | |
import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener; | |
import net.minecraft.resource.ResourceManager; | |
import net.minecraft.util.Identifier; | |
import net.minecraft.util.profiler.Profiler; | |
public class GuideLoaderClient implements IdentifiableResourceReloadListener | |
{ | |
public static final GuideLoaderClient INSTANCE = new GuideLoaderClient(); | |
private static final Identifier ID = new Identifier(Inscribe.MOD_ID, "guide_loader_client"); | |
private static final String FOLDER_NAME = Inscribe.MOD_ID + "_guides"; | |
private static final String GUIDE_DEFINITION_FILENAME = "guide_def_client.xml"; | |
private GuideLoaderClient() {} | |
@Override | |
public CompletableFuture<Void> apply(Helper helper, ResourceManager resourceManager, Profiler loadProfiler, Profiler applyProfiler, Executor loadExecutor, Executor applyExecutor) | |
{ | |
CompletableFuture<Void> schemaLoaderFuture = Schemas.INSTANCE.apply(helper, resourceManager, loadProfiler, applyProfiler, loadExecutor, applyExecutor); | |
return CompletableFuture.allOf(schemaLoaderFuture) | |
.thenCompose(v -> load(resourceManager, loadProfiler, loadExecutor)) | |
.thenCompose(data -> apply(data, resourceManager, applyProfiler, applyExecutor)); | |
} | |
public CompletableFuture<Collection<GuideDefinitionClient>> load(ResourceManager resourceManager, Profiler profiler, Executor executor) | |
{ | |
CompletableFuture<Collection<GuideDefinitionClient>> future = new CompletableFuture<>(); | |
future.thenRunAsync(() -> | |
{ | |
SAXBuilder guideDefinitionBuilder = new SAXBuilder(); | |
guideDefinitionBuilder.setIgnoringBoundaryWhitespace(true); | |
guideDefinitionBuilder.setXMLReaderFactory(new XMLReaderSchemaFactory(Schemas.INSTANCE.guideDefinitionClient())); | |
Collection<GuideDefinitionClient> guideDefinitions = new ArrayList<>(); | |
for (Identifier guideDefPath : resourceManager.findResources(FOLDER_NAME, path -> path.endsWith(GUIDE_DEFINITION_FILENAME))) | |
{ | |
try | |
{ | |
System.out.println("Loading guide definition from " + guideDefPath); | |
guideDefinitions.add(loadGuideDefinition(guideDefinitionBuilder, resourceManager, guideDefPath)); | |
} | |
catch (JDOMException | IOException e) | |
{ | |
future.completeExceptionally(e); | |
} | |
} | |
future.complete(guideDefinitions); | |
}, executor); | |
return future; | |
} | |
private GuideDefinitionClient loadGuideDefinition(SAXBuilder builder, ResourceManager resourceManager, Identifier path) throws JDOMException, IOException | |
{ | |
Element root = builder.build(resourceManager.getResource(path).getInputStream()).getRootElement(); | |
return GuideDefinitionClient.fromXml(root); | |
} | |
public CompletableFuture<Void> apply(Collection<GuideDefinitionClient> data, ResourceManager resourceManager, Profiler profiler, Executor executor) | |
{ | |
System.out.println("Applying guide definitions " + data); | |
return CompletableFuture.completedFuture(null); | |
} | |
@Override | |
public Identifier getFabricId() | |
{ | |
return ID; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment