/** * Inserts the resource pack into the game. Enabling the resource pack will * not be required, it will load automatically. * <p> * A cache of the pack zip will be kept in "resourcepack/[pack name].zip" * where "resourcepack" is a folder at the same level as the directory passed * into the constructor. */ public void inject() { if (FMLCommonHandler.instance().getEffectiveSide().isClient()) { try { if (defaultResourcePacks == null) { defaultResourcePacks = ReflectionHelper.getPrivateValue(Minecraft.class, Minecraft.getMinecraft(), "defaultResourcePacks", "field_110449_ao", "ap"); } File dest = new File(dir.getParent() + "/resourcepack/" + zip.getName()); EnderFileUtils.safeDelete(dest); FileUtils.copyFile(zip, dest); EnderFileUtils.safeDelete(zip); writeNewFile(new File(dest.getParent() + "/readme.txt"), EnderCore.lang.localize("resourcepack.readme") + "\n\n" + EnderCore.lang.localize("resourcepack.readme2")); defaultResourcePacks.add(new FileResourcePack(dest)); } catch (Exception e) { EnderCore.logger.error("Failed to inject resource pack for mod {}", modid, e); } } else { EnderCore.logger.info("Skipping resource pack, we are on a dedicated server."); } }
/** * Dynamically generates a sound JSON file based on a resource pack's file structure. * * If it's a folder, then load it as a sound collection. * A sound collection falls under the same resource name. When called to play, it will pick a random sound from the collection to play it. * * If it's just a sound file, then load the sound file * * @param pack The resource pack to generate the sound JSON for. * @return The generated sound JSON. */ public static String generateSoundJSON(AbstractResourcePack pack) { StringWriter sw = new StringWriter(); try (JsonGenerator json = Json.createGenerator(sw);) { json.writeStartObject(); if (pack instanceof FileResourcePack) { //For zip resource packs try { generateSoundJSON((FileResourcePack) pack, json); } catch (Exception e) { Error error = new ExceptionInInitializerError("Error generating fake sound JSON file."); error.addSuppressed(e); throw error; } } else if (pack instanceof FolderResourcePack) { //For folder resource packs generateSoundJSON((FolderResourcePack) pack, json); } json.writeEnd().flush(); return sw.toString(); } }
public void func_110516_a() throws IOException { this.field_110524_c = (ResourcePack)(this.field_110523_b.isDirectory()?new FolderResourcePack(this.field_110523_b):new FileResourcePack(this.field_110523_b)); this.field_110521_d = (PackMetadataSection)this.field_110524_c.func_135058_a(this.field_110525_a.field_110621_c, "pack"); try { this.field_110522_e = this.field_110524_c.func_110586_a(); } catch (IOException var2) { ; } if(this.field_110522_e == null) { this.field_110522_e = this.field_110525_a.field_110620_b.func_110586_a(); } this.func_110517_b(); }
private static IResourcePack createResourcePack(File file) { if(file.isDirectory()) { return new FolderResourcePack(file); } else { return new FileResourcePack(file); } }
@SuppressWarnings("unchecked") private static JsonGenerator generateSoundJSON(FileResourcePack pack, JsonGenerator json) throws IOException { try (ZipFile zipFile = new ZipFile(pack.resourcePackFile)) { for (String domain : (Set<String>) pack.getResourceDomains()) { //Load all sounds in the assets/domain/sounds/* if (getZipEntryForResourcePack(pack, "assets/" + domain + "/sounds/") != null) { String prefix = "assets/" + domain + "/sounds/"; zipFile.stream() .filter(e -> e.getName().toLowerCase().startsWith(prefix.toLowerCase()) && !e.getName().equalsIgnoreCase(prefix)) .forEach(e -> { String soundName = e.getName().replaceFirst(prefix, "").replaceFirst("\\.[^\\.]+$", ""); if (soundName.contains("/")) return; json.writeStartObject(soundName); json.write("category", "ambient"); json.writeStartArray("sounds"); if (e.isDirectory()) { zipFile.stream() .filter(e2 -> e2.getName().startsWith(prefix + soundName + "/") && !e2.isDirectory()) .map(ZipEntry::getName) .map(s -> s.replaceFirst(prefix + soundName + "/", "").replaceFirst("\\.[^\\.]+$", "")) .forEach(s -> json.write(soundName + "/" + s)); } else { json.write(soundName); } json.writeEnd().writeEnd(); }); } } } return json; }
public static ZipEntry getZipEntryForResourcePack(FileResourcePack pack, String path) throws IOException { if (pack instanceof NovaFileResourcePack) { Optional<ZipEntry> entry = ((NovaFileResourcePack) pack).findFileCaseInsensitive(path); if (entry.isPresent()) return entry.get(); } try (ZipFile zf = new ZipFile(pack.resourcePackFile)) { return zf.getEntry(path); } }
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) public void injectZipAsResource(String zipDir) { Object value = ObfuscationReflectionHelper.getPrivateValue(Minecraft.class, FMLClientHandler.instance().getClient(), "field_110449_ao", "defaultResourcePacks"); if (value instanceof List) { FileResourcePack pack = new FileResourcePack(new File(zipDir)); ((List) value).add(pack); } }