com.parzivail.util.data.KeyedReloadableLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pswg Show documentation
Show all versions of pswg Show documentation
Explore the galaxy with Galaxies: Parzi's Star Wars Mod!
package com.parzivail.util.data;
import I;
import com.google.common.collect.Maps;
import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener;
import net.minecraft.class_2960;
import net.minecraft.class_3298;
import net.minecraft.class_3300;
import net.minecraft.class_3302;
import net.minecraft.class_3695;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
public abstract class KeyedReloadableLoader implements IdentifiableResourceReloadListener
{
private static final Logger LOGGER = LogManager.getLogger();
private final String fileSuffix;
private final int fileSuffixLength;
private final String startingPath;
public KeyedReloadableLoader(String startingPath, String fileExtension)
{
this.startingPath = startingPath;
fileSuffix = "." + fileExtension;
fileSuffixLength = fileSuffix.length();
}
public abstract T readResource(class_3300 resourceManager, class_3695 profiler, InputStream stream) throws IOException;
@Override
public final CompletableFuture method_25931(class_3302.class_4045 synchronizer, class_3300 manager, class_3695 prepareProfiler, class_3695 applyProfiler, Executor prepareExecutor, Executor applyExecutor)
{
return CompletableFuture.supplyAsync(() -> this.prepare(manager, prepareProfiler), prepareExecutor)
.thenCompose(synchronizer::method_18352)
.thenAcceptAsync(object -> this.apply(object, manager, applyProfiler), applyExecutor);
}
protected Map prepare(class_3300 resourceManager, class_3695 profiler)
{
Map map = Maps.newHashMap();
var i = this.startingPath.length() + 1;
var resources = resourceManager.method_14488(this.startingPath, (s) -> s.method_12832().endsWith(fileSuffix));
for (var resourceEntry : resources.entrySet())
{
var resourceId = resourceEntry.getKey();
var resourcePath = resourceId.method_12832();
var resourceIdWithoutExt = new class_2960(resourceId.method_12836(), resourcePath.substring(i, resourcePath.length() - fileSuffixLength));
var resource = resourceEntry.getValue();
try (var inputStream = resource.method_14482())
{
var element = readResource(resourceManager, profiler, inputStream);
if (element != null)
{
var popped = map.put(resourceIdWithoutExt, element);
if (popped != null)
throw new IllegalStateException("Duplicate data file ignored with ID " + resourceIdWithoutExt);
}
else
LOGGER.error("Couldn't load data file {} from {} as it's null or empty", resourceIdWithoutExt, resourceId);
}
catch (Exception ex)
{
LOGGER.error("Couldn't parse data file {} from {}", resourceIdWithoutExt, resourceId, ex);
}
}
return map;
}
protected abstract void apply(Map prepared, class_3300 manager, class_3695 profiler);
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy