11package cam72cam .mod .resource ;
22
33import cam72cam .mod .ModCore ;
4- import net .minecraft .client .Minecraft ;
5- import net .minecraft .client .resources .*;
6- import net .minecraft .client .resources .data .IMetadataSection ;
7- import net .minecraft .client .resources .data .MetadataSerializer ;
8- import net .minecraftforge .fml .common .Loader ;
9- import net .minecraftforge .fml .relauncher .Side ;
10- import net .minecraftforge .fml .relauncher .SideOnly ;
11-
4+ import net .minecraft .resources .*;
5+ import net .minecraft .resources .data .IMetadataSectionSerializer ;
6+ import net .minecraft .util .ResourceLocation ;
7+ import net .minecraftforge .api .distmarker .Dist ;
8+ import net .minecraftforge .api .distmarker .OnlyIn ;
9+ import net .minecraftforge .fml .ModList ;
10+ import net .minecraftforge .fml .loading .FMLPaths ;
11+
12+ import javax .annotation .Nullable ;
1213import java .io .*;
1314import java .util .*;
1415import java .util .function .Function ;
16+ import java .util .function .Predicate ;
1517import java .util .stream .Collectors ;
1618
1719/**
@@ -77,36 +79,27 @@ public static void redirect(Identifier requestedPrefix, Identifier actualPrefix)
7779 /**
7880 * Registers a file or folder as a resource pack to the game.
7981 */
80- @ SideOnly ( Side .CLIENT )
82+ @ OnlyIn ( Dist .CLIENT )
8183 public static IResourcePack attach (File path ) {
8284 if (path .isDirectory ()) {
83- return new FolderResourcePack (path ) {
84- @ Override
85- protected InputStream getInputStreamByName (String name ) throws IOException {
86- InputStream stream = super .getInputStreamByName (name );
87- File file = this .getFile (name );
88- return new Identifier .InputStreamMod (stream , file .lastModified ());
89- }
90- };
85+ return new UMCFolderPack (path );
9186 } else {
92- return new FileResourcePack (path ) {
93- @ Override
94- protected InputStream getInputStreamByName (String name ) throws IOException {
95- return new Identifier .InputStreamMod (super .getInputStreamByName (name ), resourcePackFile .lastModified ());
96- }
97- };
87+ return new UMCFilePack (path );
9888 }
9989 }
10090
10191 /**
10292 * Internal
10393 */
104- public static void loadModResource (ModCore .Mod mod ) {
105- List <IResourcePack > packs = Minecraft .getMinecraft ().defaultResourcePacks ;
106-
107- String configDir = Loader .instance ().getConfigDir ().toString ();
94+ public static void loadModResource (ModCore .Mod mod , List <IResourcePack > packs ) {
95+ String configDir = FMLPaths .CONFIGDIR .toString ();
10896 new File (configDir ).mkdirs ();
10997
98+ IResourcePack modPack = BuiltinPack .attach (ModList .get ().getModFileById (mod .modID ()).getFile ().getFilePath ().toFile ());
99+ // Ensure people will get our result first via getResourceStream() and getLastResourceStream()
100+ // (Also injects last modified time access)
101+ // BUG: sounds can still be overridden by resource packs
102+ packs .add (modPack );
110103 File folder = new File (configDir + File .separator + mod .modID ());
111104 if (folder .exists ()) {
112105 if (folder .isDirectory ()) {
@@ -123,6 +116,7 @@ public static void loadModResource(ModCore.Mod mod) {
123116 } else {
124117 folder .mkdirs ();
125118 }
119+ packs .add (modPack );
126120 }
127121
128122 /**
@@ -143,17 +137,15 @@ public static void reload() {
143137 }
144138
145139 /**
146- * Internal, Client side
140+ * Internal, Client side assets loading
147141 */
148- @ SideOnly (Side .CLIENT )
149- private static class InternalPack extends AbstractResourcePack {
142+ private static class InternalPack extends ResourcePack {
150143 public InternalPack () {
151- //We're initializing UMC
152- super (Loader .instance ().activeModContainer ().getSource ());
144+ super (ModList .get ().getModFileById ("universalmodcore" ).getFile ().getFilePath ().toFile ());
153145 }
154146
155147 @ Override
156- protected InputStream getInputStreamByName (String resourcePath ) throws IOException {
148+ public InputStream getInputStream (String resourcePath ) throws IOException {
157149 if ("pack.mcmeta" .equals (resourcePath )) {
158150 return new ByteArrayInputStream ("{}" .getBytes ());
159151 }
@@ -181,7 +173,7 @@ protected InputStream getInputStreamByName(String resourcePath) throws IOExcepti
181173 }
182174
183175 @ Override
184- protected boolean hasResourceName (String resourcePath ) {
176+ public boolean resourceExists (String resourcePath ) {
185177 if (resourcePath .endsWith ("mcmeta" ) && !"pack.mcmeta" .equals (resourcePath )) {
186178 //We don't handle resource metadata
187179 return false ;
@@ -219,27 +211,38 @@ && handleRedirect(ident, entry.getKey(), entry.getValue()).canLoad()) {
219211 }
220212
221213 @ Override
222- public Set <String > getResourceDomains () {
214+ public Collection <ResourceLocation > getAllResourceLocations (ResourcePackType type , String pathIn , int maxDepth , Predicate <String > filter ) {
215+ return Collections .emptyList ();
216+ }
217+
218+ @ Override
219+ public Set <String > getResourceNamespaces (ResourcePackType type ) {
223220 Set <String > collect = ModCore .instance .getLoadedMods ().stream ().map (ModCore .Mod ::modID ).collect (Collectors .toSet ());
224221 collect .add ("universalmodcore" );
225222 return collect ;
226223 }
227224
228225 @ Override
229- public String getPackName () {
226+ public String getName () {
230227 return "UMC Generated Resources" ;
231228 }
232229
230+ @ Nullable
233231 @ Override
234- public <T extends IMetadataSection > T getPackMetadata (MetadataSerializer metadataSerializer , String metadataSectionName ) throws IOException {
235- return super .getPackMetadata (metadataSerializer , metadataSectionName );
232+ public <T > T getMetadata (IMetadataSectionSerializer <T > p_195760_1_ ) throws IOException {
233+ return getResourceMetadata (p_195760_1_ , new ByteArrayInputStream ("{}" .getBytes ()));
234+ }
235+
236+ @ Override
237+ public void close () throws IOException {
238+ //Have nothing to do here
236239 }
237240 }
238241
239242 /**
240- * Internal, Server side
243+ * Internal, Server side assets loading
241244 */
242- @ SideOnly ( Side . SERVER )
245+ @ OnlyIn ( Dist . DEDICATED_SERVER )
243246 public static InputStream loadServerResource (Identifier ident ) throws IOException {
244247 if (ident .getPath ().endsWith ("mcmeta" )) {
245248 //We don't handle resource metadata
@@ -275,6 +278,38 @@ public static InputStream loadServerResource(Identifier ident) throws IOExceptio
275278 return null ;
276279 }
277280
281+ private static class UMCFolderPack extends FolderPack {
282+ public UMCFolderPack (File folder ) {
283+ super (folder );
284+ }
285+
286+ @ Override
287+ public InputStream getInputStream (String name ) throws IOException {
288+ InputStream stream = super .getInputStream (name );
289+ File file = this .getFile (name );
290+ return new Identifier .InputStreamMod (stream , file .lastModified ());
291+ }
292+
293+ @ Override
294+ public boolean resourceExists (String resourcePath ) {
295+ return super .resourceExists (resourcePath );
296+ }
297+ }
298+
299+ private static class UMCFilePack extends FilePack {
300+ private final File path ;
301+
302+ public UMCFilePack (File fileIn ) {
303+ super (fileIn );
304+ this .path = fileIn ;
305+ }
306+
307+ @ Override
308+ public InputStream getInputStream (String name ) throws IOException {
309+ return new Identifier .InputStreamMod (super .getInputStream (name ), path .lastModified ());
310+ }
311+ }
312+
278313 private static Identifier handleRedirect (Identifier src , String requestedPrefix , String actualPrefix ) {
279314 //Replace [requestedPrefix] with [actualPrefix] to redirect the request back
280315 String suffix = src .toString ().substring (requestedPrefix .length ());
@@ -283,12 +318,18 @@ private static Identifier handleRedirect(Identifier src, String requestedPrefix,
283318
284319 private static Identifier nameToLocation (String path ) {
285320 if (path .startsWith ("assets/" )) {
286- //assets/[domain]/[path] -> domain:path, for 1.12- path
321+ //assets/[domain]/[path] -> domain:path
287322 path = path .substring (7 );
288323 int x = path .indexOf ('/' );
289324 return new Identifier (path .substring (0 , x ), path .substring (x + 1 ));
325+ } else if (path .startsWith ("data/" )) {
326+ //data/[domain]/[path] -> domain:path
327+ //However we added this as client-only resources...
328+ //TODO (internal) datapack API
329+ path = path .substring (5 );
330+ int x = path .indexOf ('/' );
331+ return new Identifier (path .substring (0 , x ), path .substring (x + 1 ));
290332 }
291- //Not possible to hit below 1.12, except for pack.mcmeta
292333 return new Identifier ("universalmodcore" , "invalid" );
293334 }
294335}
0 commit comments