|
23 | 23 | import net.minecraft.server.packs.repository.Pack; |
24 | 24 | import net.minecraft.server.packs.repository.PackSource; |
25 | 25 | import net.minecraft.server.packs.resources.ReloadableResourceManager; |
26 | | -import net.minecraft.server.packs.resources.Resource; |
27 | 26 |
|
28 | 27 | import java.util.*; |
29 | 28 |
|
30 | 29 | import net.minecraft.resources.*; |
31 | | -import net.minecraftforge.fml.ModList; |
32 | 30 | import org.apache.logging.log4j.LogManager; |
33 | 31 | import org.apache.logging.log4j.Logger; |
34 | 32 |
|
|
40 | 38 | import cam72cam.mod.net.Packet; |
41 | 39 | import cam72cam.mod.net.PacketDirection; |
42 | 40 | import cam72cam.mod.render.Light; |
| 41 | +import cam72cam.mod.resource.BuiltinPack; |
43 | 42 | import cam72cam.mod.resource.Identifier; |
44 | 43 | import cam72cam.mod.text.Command; |
45 | 44 | import cam72cam.mod.util.MinecraftFiles; |
46 | 45 | import cam72cam.mod.util.ModCoreCommand; |
47 | 46 | import cam72cam.mod.world.ChunkManager; |
48 | 47 | import net.minecraft.client.Minecraft; |
49 | | -import net.minecraft.resources.ResourceLocation; |
50 | 48 | import net.minecraft.server.packs.*; |
51 | 49 | import net.minecraft.util.Unit; |
52 | 50 | import net.minecraftforge.common.MinecraftForge; |
|
63 | 61 | import org.apache.commons.lang3.StringUtils; |
64 | 62 | import org.apache.commons.lang3.SystemUtils; |
65 | 63 |
|
66 | | -import javax.annotation.Nullable; |
67 | 64 | import java.io.File; |
68 | 65 | import java.io.IOException; |
69 | 66 | import java.util.stream.Collectors; |
@@ -198,6 +195,10 @@ public static void catching(Throwable ex) { |
198 | 195 | } |
199 | 196 | } |
200 | 197 |
|
| 198 | + public List<Mod> getLoadedMods() { |
| 199 | + return mods; |
| 200 | + } |
| 201 | + |
201 | 202 | private static Proxy proxy = DistExecutor.runForDist(() -> ClientProxy::new, () -> ServerProxy::new); |
202 | 203 | /** Hooked into forge's proxy system and fires off corresponding events */ |
203 | 204 | public static class Proxy { |
@@ -241,184 +242,61 @@ public void setup() { |
241 | 242 | } |
242 | 243 | Config.getMaxTextureSize(); //populate |
243 | 244 |
|
244 | | - // Force first and last (and inject mod time) BUG: sounds can still be overridden by resource packs |
245 | | - Minecraft.getInstance().getResourcePackRepository().addPackFinder(consumer -> { |
246 | | - List<PackResources> packs = new ArrayList<>(); |
247 | | - packs.add(new TranslationResourcePack()); |
248 | | - |
249 | | - for (Mod m : mods) { |
250 | | - PackResources modPack = createPack(ModList.get().getModFileById(m.modID()).getFile().getFilePath().toFile()); |
251 | | - packs.add(modPack); |
252 | | - String configDir = FMLPaths.CONFIGDIR.get().toString(); |
253 | | - new File(configDir).mkdirs(); |
254 | | - |
255 | | - File folder = new File(configDir + File.separator + m.modID()); |
256 | | - if (folder.exists()) { |
257 | | - if (folder.isDirectory()) { |
258 | | - File[] files = folder.listFiles((dir, name) -> name.endsWith(".zip")); |
259 | | - for (File file : files) { |
260 | | - packs.add(createPack(file)); |
261 | | - } |
| 245 | + BuiltinPack.loadClientResources(); |
262 | 246 |
|
263 | | - File[] folders = folder.listFiles((dir, name) -> dir.isDirectory()); |
264 | | - for (File dir : folders) { |
265 | | - packs.add(createPack(dir)); |
266 | | - } |
267 | | - } |
268 | | - } else { |
269 | | - folder.mkdirs(); |
270 | | - } |
271 | | - packs.add(modPack); |
| 247 | + //Wrapper for lang format language files |
| 248 | + BuiltinPack.conditional(ident -> { |
| 249 | + String path = ident.getPath(); |
| 250 | + if (!path.startsWith("lang/") || !path.endsWith(".json")) { |
| 251 | + return null; |
272 | 252 | } |
273 | 253 |
|
274 | | - |
275 | | - for (PackResources pack : packs) { |
276 | | - consumer.accept(Pack.create(pack.packId(), |
277 | | - Component.literal(""), |
278 | | - true, |
279 | | - s -> pack, |
280 | | - new Pack.Info(Component.literal(""), 13, FeatureFlagSet.of()), |
281 | | - PackType.SERVER_DATA, |
282 | | - Pack.Position.TOP, |
283 | | - true, |
284 | | - PackSource.DEFAULT |
285 | | - )); |
| 254 | + Identifier lang = new Identifier(ident.toString().replace(".json", ".lang")); |
| 255 | + if (!lang.canLoad()) { |
| 256 | + return null; |
286 | 257 | } |
287 | | - }); |
288 | | - } |
289 | 258 |
|
290 | | - @Override |
291 | | - public void event(ModEvent event, Mod m) { |
292 | | - super.event(event, m); |
293 | | - m.clientEvent(event); |
294 | | - } |
295 | | - |
296 | | - private static class TranslationResourcePack extends AbstractPackResources { |
297 | | - public TranslationResourcePack() { |
298 | | - super("translation Hackery", false); |
299 | | - } |
300 | | - |
301 | | - @org.jetbrains.annotations.Nullable |
302 | | - @Override |
303 | | - public IoSupplier<InputStream> getRootResource(String... p_252049_) { |
304 | | - return null; |
305 | | - } |
| 259 | + Map<String, String> translationMap = new HashMap<>(); |
| 260 | + try { |
| 261 | + for (InputStream stream : lang.getResourceStreamAll()) { |
| 262 | + try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) { |
| 263 | + String line; |
| 264 | + while ((line = reader.readLine()) != null) { |
| 265 | + //Remove comment |
| 266 | + line = line.trim(); |
| 267 | + if (line.isEmpty() || line.startsWith("#")) { |
| 268 | + continue; |
| 269 | + } |
306 | 270 |
|
307 | | - @org.jetbrains.annotations.Nullable |
308 | | - @Override |
309 | | - public IoSupplier<InputStream> getResource(PackType type, ResourceLocation resourcePath) { |
310 | | - if (resourcePath.getPath().contains("lang/") && resourcePath.getPath().endsWith(".json")) { |
311 | | - // Magical Translations! |
312 | | - ResourceLocation lang = ResourceLocation.fromNamespaceAndPath(resourcePath.getNamespace(), resourcePath.getPath().replace("json", "lang")); |
313 | | - List<Resource> langFiles = Minecraft.getInstance().getResourceManager().getResourceStack(lang); |
314 | | - if (!langFiles.isEmpty()) { |
315 | | - Map<String, String> translationMap = new HashMap<>(); |
316 | | - for (Resource resource : langFiles) { |
317 | | - try (BufferedReader reader = resource.openAsReader()) { |
318 | | - String line; |
319 | | - while ((line = reader.readLine()) != null) { |
320 | | - //Remove comment |
321 | | - line = line.trim(); |
322 | | - int comment = line.indexOf("#"); |
323 | | - if (line.isEmpty() || comment == 0) { |
324 | | - continue; |
325 | | - } |
326 | | - |
327 | | - String[] splits = line.split("=", 2); |
328 | | - if (splits.length == 2) { |
329 | | - translationMap.put(splits[0].trim(), splits[1].trim()); |
330 | | - } |
| 271 | + String[] splits = line.split("=", 2); |
| 272 | + if (splits.length == 2) { |
| 273 | + translationMap.put(splits[0].trim(), splits[1].trim()); |
331 | 274 | } |
332 | | - } catch (IOException e) { |
333 | | - throw new RuntimeException(e); |
334 | 275 | } |
335 | 276 | } |
336 | | - |
337 | | - Set<String> translations = new HashSet<>(); |
338 | | - translationMap.forEach((key, value) -> { |
339 | | - if (!key.isEmpty()) { |
340 | | - translations.add(String.format("\"%s\": \"%s\"", key, value)); |
341 | | - translations.add(String.format("\"%s\": \"%s\"", key.replace(":", "."), value)); |
342 | | - translations.add(String.format("\"%s\": \"%s\"", key.replace(".name", ""), value)); |
343 | | - translations.add(String.format("\"%s\": \"%s\"", key.replace(".name", "").replace(":", "."), value)); |
344 | | - } |
345 | | - }); |
346 | | - String output = "{" + String.join(",", translations) + "}"; |
347 | | - return () -> new ByteArrayInputStream(output.getBytes(StandardCharsets.UTF_8)); |
348 | 277 | } |
| 278 | + } catch (IOException e) { |
| 279 | + throw new RuntimeException(e); |
349 | 280 | } |
350 | | - return null; |
351 | | - } |
352 | | - |
353 | | - @Override |
354 | | - public void listResources(PackType p_10289_, String p_251379_, String p_251932_, ResourceOutput p_249347_) { |
355 | | - |
356 | | - } |
357 | | - |
358 | | - @Override |
359 | | - public Set<String> getNamespaces(PackType p_195759_1_) { |
360 | | - return mods.stream().map(Mod::modID).collect(Collectors.toSet()); |
361 | | - } |
362 | 281 |
|
363 | | - @Override |
364 | | - public void close() { |
365 | | - |
366 | | - } |
367 | | - |
368 | | - @Nullable |
369 | | - @Override |
370 | | - public <T> T getMetadataSection(MetadataSectionSerializer<T> p_195760_1_) throws IOException { |
371 | | - return getMetadataFromStream(p_195760_1_, new ByteArrayInputStream("{}".getBytes())); |
372 | | - } |
373 | | - } |
374 | | - |
375 | | - private static class UMCFolderPack extends PathPackResources { |
376 | | - private final Path root; |
377 | | - |
378 | | - public UMCFolderPack(File folder) { |
379 | | - super(folder.getName(), folder.toPath(), false); |
380 | | - this.root = folder.toPath(); |
381 | | - } |
382 | | - |
383 | | - @Override |
384 | | - public IoSupplier<InputStream> getResource(PackType p_249352_, ResourceLocation p_251715_) { |
385 | | - Path path = this.root.resolve(p_249352_.getDirectory()).resolve(p_251715_.getNamespace()); |
386 | | - return getResource(p_251715_, path); |
387 | | - } |
388 | | - |
389 | | - public static IoSupplier<InputStream> getResource(ResourceLocation p_250145_, Path p_251046_) { |
390 | | - return FileUtil.decomposePath(p_250145_.getPath()).get().map((p_251647_) -> { |
391 | | - Path path = FileUtil.resolvePath(p_251046_, p_251647_); |
392 | | - return Files.exists(path) ? new Identifier.IoInputStreamMod(() -> Files.newInputStream(path), path.toFile().lastModified()) : null; |
393 | | - }, (p_248714_) -> { |
394 | | - LogUtils.getLogger().error("Invalid path {}: {}", p_250145_, p_248714_.message()); |
395 | | - return null; |
| 282 | + Set<String> translations = new HashSet<>(); |
| 283 | + translationMap.forEach((key, value) -> { |
| 284 | + if (!key.isEmpty()) { |
| 285 | + translations.add(String.format("\"%s\": \"%s\"", key, value)); |
| 286 | + translations.add(String.format("\"%s\": \"%s\"", key.replace(":", "."), value)); |
| 287 | + translations.add(String.format("\"%s\": \"%s\"", key.replace(".name", ""), value)); |
| 288 | + translations.add(String.format("\"%s\": \"%s\"", key.replace(".name", "").replace(":", "."), value)); |
| 289 | + } |
396 | 290 | }); |
397 | | - } |
398 | | - } |
399 | | - |
400 | | - private static class UMCFilePack extends FilePackResources { |
401 | | - private final File path; |
402 | | - |
403 | | - public UMCFilePack(File fileIn) { |
404 | | - super(fileIn.getName(), fileIn, false); |
405 | | - this.path = fileIn; |
406 | | - } |
407 | | - |
408 | | - @Override |
409 | | - public IoSupplier<InputStream> getResource(PackType p_249605_, ResourceLocation p_252147_) { |
410 | | - IoSupplier<InputStream> found = super.getResource(p_249605_, p_252147_); |
411 | | - return found != null ? new Identifier.IoInputStreamMod(found, path.lastModified()) : null; |
412 | | - } |
| 291 | + String output = "{" + String.join(",", translations) + "}"; |
| 292 | + return output.getBytes(StandardCharsets.UTF_8); |
| 293 | + }); |
413 | 294 | } |
414 | 295 |
|
415 | | - |
416 | | - private static PackResources createPack(File path) { |
417 | | - if (path.isDirectory()) { |
418 | | - return new UMCFolderPack(path); |
419 | | - } else { |
420 | | - return new UMCFilePack(path); |
421 | | - } |
| 296 | + @Override |
| 297 | + public void event(ModEvent event, Mod m) { |
| 298 | + super.event(event, m); |
| 299 | + m.clientEvent(event); |
422 | 300 | } |
423 | 301 | } |
424 | 302 |
|
@@ -473,8 +351,9 @@ public void clientEvent(ModEvent event) { |
473 | 351 | // Instance can be null during data gen |
474 | 352 | if (Minecraft.getInstance() != null) { |
475 | 353 | ((ReloadableResourceManager) Minecraft.getInstance().getResourceManager()).registerReloadListener((stage, resourceManager, preparationsProfiler, reloadProfiler, backgroundExecutor, gameExecutor) -> |
476 | | - stage.wait(Unit.INSTANCE).thenRun(ClientEvents::fireReload)); |
| 354 | + stage.wait(Unit.INSTANCE).thenRun(ClientEvents::fireReload).thenRun(BuiltinPack::reload)); |
477 | 355 | } |
| 356 | + break; |
478 | 357 | case SETUP: |
479 | 358 | try { |
480 | 359 | Minecraft.getInstance().createSearchTrees(); |
|
0 commit comments