Skip to content

Commit

Permalink
whatever
Browse files Browse the repository at this point in the history
  • Loading branch information
eerussianguy committed Aug 27, 2022
1 parent 3943296 commit 4739a51
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 29 deletions.
1 change: 0 additions & 1 deletion resources/recipes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from enum import Enum
from itertools import repeat
from typing import Union

from mcresources import ResourceManager, RecipeContext, utils
Expand Down
53 changes: 33 additions & 20 deletions src/main/java/com/eerussianguy/firmalife/common/FLEvents.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.eerussianguy.firmalife.common;

import java.io.IOException;
import java.nio.file.Path;

import javax.annotation.Nonnull;

import net.minecraft.network.chat.TextComponent;
import net.minecraft.server.packs.PackType;
Expand All @@ -11,9 +14,9 @@
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.resource.PathResourcePack;

import com.eerussianguy.firmalife.FirmaLife;
import com.eerussianguy.firmalife.common.util.SafePathResourcePack;

public class FLEvents
{
Expand All @@ -26,24 +29,34 @@ public static void init()

public static void onPackFinder(AddPackFindersEvent event)
{
try
{
if (event.getPackType() == PackType.SERVER_DATA)
{
var resourcePath = ModList.get().getModFileById(FirmaLife.MOD_ID).getFile().getFilePath();
var pack = new PathResourcePack(ModList.get().getModFileById(FirmaLife.MOD_ID).getFile().getFileName() + ":" + resourcePath, resourcePath);
var metadata = pack.getMetadataSection(PackMetadataSection.SERIALIZER);
if (metadata != null)
{
event.addRepositorySource((consumer, constructor) ->
consumer.accept(constructor.create("builtin/firmalife_data", new TextComponent("Firmalife Data"), true, () -> pack, metadata, Pack.Position.TOP, PackSource.BUILT_IN, false))
);
}
}
}
catch (IOException e)
{
throw new RuntimeException(e);
}
ModList.get().getMods().forEach(mod -> FirmaLife.LOGGER.info(mod.getModId()));
// try
// {
// if (event.getPackType() == PackType.SERVER_DATA)
// {
// var modFile = ModList.get().getModFileById(FirmaLife.MOD_ID).getFile();
// var resourcePath = modFile.getFilePath();
// var pack = new SafePathResourcePack(modFile.getFileName() + ":overload", resourcePath) {
// @Nonnull
// @Override
// protected Path resolve(@Nonnull String... paths)
// {
// return modFile.findResource(paths);
// }
// };
//
// var metadata = pack.getMetadataSection(PackMetadataSection.SERIALIZER);
// if (metadata != null)
// {
// event.addRepositorySource((consumer, constructor) ->
// consumer.accept(constructor.create("builtin/firmalife_data", new TextComponent("Firmalife Data"), true, () -> pack, metadata, Pack.Position.TOP, PackSource.BUILT_IN, false))
// );
// }
// }
// }
// catch (IOException e)
// {
// throw new RuntimeException(e);
// }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.eerussianguy.firmalife.common.util;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import javax.annotation.Nonnull;

import com.google.common.base.Joiner;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackType;
import net.minecraftforge.common.ForgeConfig;
import net.minecraftforge.resource.PathResourcePack;
import net.minecraftforge.resource.ResourceCacheManager;

public class SafePathResourcePack extends PathResourcePack
{
private final ResourceCacheManager cacheManager = new ResourceCacheManager(true, ForgeConfig.COMMON.indexModPackCachesOnThread, (packType, namespace) -> resolve(packType.getDirectory(), namespace).toAbsolutePath());

public SafePathResourcePack(String packName, Path source)
{
super(packName, source);
}

@Override
public Collection<ResourceLocation> getResources(PackType type, String resourceNamespace, String pathIn, int maxDepth, Predicate<String> filter)
{
try
{
Path root = resolve(type.getDirectory(), resourceNamespace).toAbsolutePath();
Path inputPath = root.getFileSystem().getPath(pathIn);

if (ResourceCacheManager.shouldUseCache() && this.cacheManager.hasCached(type, resourceNamespace))
{
return this.cacheManager.getResources(type, resourceNamespace, inputPath, filter);
}

return Files.walk(root)
.map(root::relativize)
.filter(path -> path.getNameCount() <= maxDepth && !path.toString().endsWith(".mcmeta") && path.startsWith(inputPath))
.filter(path -> filter.test(path.getFileName().toString()))
// backporting fix for resource location path validity
.filter(path -> ResourceLocation.isValidPath(Joiner.on('/').join(path)))
.map(path -> new ResourceLocation(resourceNamespace, Joiner.on('/').join(path)))
.collect(Collectors.toList());
}
catch (IOException e)
{
return Collections.emptyList();
}
}

@Override
public void initForNamespace(final String namespace)
{
if (ResourceCacheManager.shouldUseCache())
{
this.cacheManager.index(namespace);
}
}

@Override
public Set<String> getNamespaces(PackType type)
{
if (ResourceCacheManager.shouldUseCache())
{
return this.cacheManager.getNamespaces(type);
}

return getNamespacesFromDisk(type);
}
}
16 changes: 8 additions & 8 deletions src/main/resources/META-INF/mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ authors="EERussianguy" #optional
description='''
An addon for TFC.
'''
[[dependencies.firmalife]]
modId="tfc"
mandatory=true
versionRange="[0,)"
ordering="AFTER"
side="BOTH"
[[dependencies.firmalife]]
modId="forge"
mandatory=true
versionRange="[40,)"
ordering="NONE"
ordering="AFTER"
side="BOTH"
[[dependencies.firmalife]]
modId="minecraft"
mandatory=true
versionRange="[1.18.2,1.19)"
ordering="NONE"
side="BOTH"
[[dependencies.firmalife]]
modId="tfc"
mandatory=true
versionRange="[0,)"
ordering="BEFORE"
ordering="AFTER"
side="BOTH"

0 comments on commit 4739a51

Please sign in to comment.