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