ForgePatch/src/main/java/net/minecraftforge/fml/packs/ResourcePackLoader.java

87 lines
3.4 KiB
Java
Raw Normal View History

/*
* Minecraft Forge
* Copyright (c) 2016-2018.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.fml.packs;
import net.minecraft.resources.*;
import net.minecraft.util.ResourceLocation;
2018-06-23 02:45:01 +00:00
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.loading.FMLLoader;
import net.minecraftforge.fml.loading.moddiscovery.ModFile;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
2018-06-23 02:45:01 +00:00
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
2018-06-23 02:45:01 +00:00
import java.nio.file.Files;
import java.util.Map;
2018-10-11 21:50:42 +00:00
import java.util.Map.Entry;
import java.util.Objects;
2018-06-23 02:45:01 +00:00
import java.util.function.Function;
import java.util.stream.Collectors;
import static net.minecraftforge.fml.Logging.CORE;
public class ResourcePackLoader
{
private static final Logger LOGGER = LogManager.getLogger();
2018-06-23 02:45:01 +00:00
private static Map<ModFile, ModFileResourcePack> modResourcePacks;
2018-09-05 00:23:45 +00:00
private static ResourcePackList<?> resourcePackList;
2018-06-23 02:45:01 +00:00
public static ModFileResourcePack getResourcePackFor(String modId)
{
return modResourcePacks.get(ModList.get().getModFileById(modId).getFile());
2018-06-23 02:45:01 +00:00
}
2018-09-05 00:23:45 +00:00
@SuppressWarnings("unchecked")
public static <T extends ResourcePackInfo> T getResourcePackInfoForModId(String modId) {
if (Objects.equals(modId, "minecraft")) {
// Additional resources for MC are associated with forge
return getResourcePackFor("forge").getPackInfo();
}
return getResourcePackFor(modId).getPackInfo();
2018-09-05 00:23:45 +00:00
}
public static <T extends ResourcePackInfo> void loadResourcePacks(ResourcePackList<T> resourcePacks) {
resourcePackList = resourcePacks;
2018-06-23 02:45:01 +00:00
modResourcePacks = ModList.get().getModFiles().stream().
map(mf -> new ModFileResourcePack(mf.getFile())).
collect(Collectors.toMap(ModFileResourcePack::getModFile, Function.identity()));
resourcePacks.addPackFinder(new ModPackFinder());
2018-09-05 00:23:45 +00:00
}
private static class ModPackFinder implements IPackFinder
2018-09-05 00:23:45 +00:00
{
@Override
public <T extends ResourcePackInfo> void addPackInfosToMap(Map<String, T> packList, ResourcePackInfo.IFactory<T> factory)
2018-09-05 00:23:45 +00:00
{
for (Entry<ModFile, ModFileResourcePack> e : modResourcePacks.entrySet())
2018-10-11 21:50:42 +00:00
{
final String name = "mod:" + e.getKey().getModInfos().get(0).getModId();
final T packInfo = ResourcePackInfo.func_195793_a(name, true, e::getValue, factory, ResourcePackInfo.Priority.BOTTOM);
e.getValue().setPackInfo(packInfo);
LOGGER.debug(CORE, "Generating PackInfo named {} for mod file {}", name, e.getKey().getFilePath());
packList.put(name, packInfo);
2018-10-11 21:50:42 +00:00
}
2018-09-05 00:23:45 +00:00
}
2018-10-11 21:50:42 +00:00
}
}