2021-08-25 14:10:17 +02:00
|
|
|
package com.minelittlepony.unicopia.util;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.lang.reflect.Type;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.GsonBuilder;
|
|
|
|
import com.google.gson.JsonParseException;
|
|
|
|
import com.minelittlepony.common.util.settings.ToStringAdapter;
|
|
|
|
import com.minelittlepony.unicopia.Unicopia;
|
|
|
|
|
|
|
|
import net.minecraft.resource.Resource;
|
|
|
|
import net.minecraft.resource.ResourceManager;
|
|
|
|
import net.minecraft.util.Identifier;
|
|
|
|
|
|
|
|
public interface Resources {
|
|
|
|
Gson GSON = new GsonBuilder()
|
|
|
|
.registerTypeAdapter(Identifier.class, new ToStringAdapter<>(Identifier::new))
|
|
|
|
.create();
|
|
|
|
|
|
|
|
static Stream<Resource> getResources(ResourceManager manager, Identifier id) {
|
2022-06-25 00:19:55 +02:00
|
|
|
return manager.getAllResources(id).stream();
|
2021-08-25 14:10:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static Stream<Identifier> loadFile(Resource res, Type type, String msg) throws JsonParseException {
|
2022-06-25 00:19:55 +02:00
|
|
|
try (InputStreamReader reader = new InputStreamReader(res.getInputStream())) {
|
|
|
|
return (GSON.<List<Identifier>>fromJson(reader, type)).stream();
|
2021-08-25 14:10:17 +02:00
|
|
|
} catch (JsonParseException e) {
|
|
|
|
Unicopia.LOGGER.warn(msg + res.getResourcePackName(), e);
|
|
|
|
} catch (IOException ignored) {}
|
|
|
|
|
|
|
|
return Stream.empty();
|
|
|
|
}
|
|
|
|
}
|