Skip to content

Commit

Permalink
Fix mod updating
Browse files Browse the repository at this point in the history
  • Loading branch information
thejudge156 committed Aug 2, 2023
1 parent e9c0157 commit fa753a8
Showing 1 changed file with 65 additions and 65 deletions.
130 changes: 65 additions & 65 deletions lib/src/main/java/pojlib/instance/MinecraftInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,81 +108,81 @@ public List<String> generateLaunchArgs(MinecraftAccount account) {
public void updateOrDownloadsMods() {
API_V1.finishedDownloading = false;
new Thread(()->{
try {
File mods = new File(Constants.USER_HOME + "/mods-new.json");
File modsOld = new File(Constants.USER_HOME + "/mods.json");
File customMods = new File(Constants.MC_DIR + "/custom_mods.json");

if (API_V1.developerMods) {
DownloadUtils.downloadFile(DEV_MODS, mods);
} else { DownloadUtils.downloadFile(MODS, mods); }

CustomMods customModsObj = GsonUtils.jsonFileToObject(customMods.getAbsolutePath(), CustomMods.class);
JsonObject obj = GsonUtils.jsonFileToObject(mods.getAbsolutePath(), JsonObject.class);
JsonObject objOld = GsonUtils.jsonFileToObject(modsOld.getAbsolutePath(), JsonObject.class);

ArrayList<String> versions = new ArrayList<>();
ArrayList<String> downloads = new ArrayList<>();
ArrayList<String> name = new ArrayList<>();

JsonArray verMods = obj.getAsJsonArray(this.versionName);
for (JsonElement verMod : verMods) {
JsonObject object = verMod.getAsJsonObject();
versions.add(object.get("version").getAsString());
downloads.add(object.get("download_link").getAsString());
name.add(object.get("slug").getAsString());
}
try {
File mods = new File(Constants.USER_HOME + "/mods-new.json");
File modsOld = new File(Constants.USER_HOME + "/mods.json");
File customMods = new File(Constants.MC_DIR + "/custom_mods.json");

if (API_V1.developerMods) {
DownloadUtils.downloadFile(DEV_MODS, mods);
} else { DownloadUtils.downloadFile(MODS, mods); }

CustomMods customModsObj = GsonUtils.jsonFileToObject(customMods.getAbsolutePath(), CustomMods.class);
JsonObject obj = GsonUtils.jsonFileToObject(mods.getAbsolutePath(), JsonObject.class);
JsonObject objOld = GsonUtils.jsonFileToObject(modsOld.getAbsolutePath(), JsonObject.class);

ArrayList<String> versions = new ArrayList<>();
ArrayList<String> downloads = new ArrayList<>();
ArrayList<String> name = new ArrayList<>();

JsonArray verMods = obj.getAsJsonArray(this.versionName);
for (JsonElement verMod : verMods) {
JsonObject object = verMod.getAsJsonObject();
versions.add(object.get("version").getAsString());
downloads.add(object.get("download_link").getAsString());
name.add(object.get("slug").getAsString());
}

if(modsOld.exists()) {
InputStream stream = Files.newInputStream(mods.toPath());
int size = stream.available();
byte[] buffer = new byte[size];
stream.read(buffer);
stream.close();
FileUtil.write(modsOld.getAbsolutePath(), buffer);
int i = 0;
boolean downloadAll = !(new File(Constants.MC_DIR + "/mods/" + this.versionName).exists());
for (String download : downloads) {
if(!Objects.equals(versions.get(i), ((JsonObject) objOld.getAsJsonArray(versionName).get(i)).getAsJsonPrimitive("version").getAsString()) || downloadAll) {
if(mods.exists()) {
InputStream stream = Files.newInputStream(mods.toPath());
int size = stream.available();
byte[] buffer = new byte[size];
stream.read(buffer);
stream.close();
FileUtil.write(modsOld.getAbsolutePath(), buffer);
int i = 0;
boolean downloadAll = !(new File(Constants.MC_DIR + "/mods/" + this.versionName).exists());
for (String download : downloads) {
if(!versions.get(i).equals(((JsonObject) objOld.getAsJsonArray(versionName).get(i)).getAsJsonPrimitive("version").getAsString()) || downloadAll) {
API_V1.currentDownload = name.get(i);
DownloadUtils.downloadFile(download, new File(Constants.MC_DIR + "/mods/" + this.versionName + "/" + name.get(i) + ".jar"));
}
i++;
}
mods.delete();
} else {
InputStream stream = Files.newInputStream(mods.toPath());
int size = stream.available();
byte[] buffer = new byte[size];
stream.read(buffer);
stream.close();
FileUtil.write(modsOld.getAbsolutePath(), buffer);
int i = 0;
for (String download : downloads) {
API_V1.currentDownload = name.get(i);
DownloadUtils.downloadFile(download, new File(Constants.MC_DIR + "/mods/" + this.versionName + "/" + name.get(i) + ".jar"));
i++;
}
i++;
mods.delete();
}
mods.delete();
} else {
InputStream stream = Files.newInputStream(mods.toPath());
int size = stream.available();
byte[] buffer = new byte[size];
stream.read(buffer);
stream.close();
FileUtil.write(modsOld.getAbsolutePath(), buffer);
int i = 0;
for (String download : downloads) {
API_V1.currentDownload = name.get(i);
DownloadUtils.downloadFile(download, new File(Constants.MC_DIR + "/mods/" + this.versionName + "/" + name.get(i) + ".jar"));
i++;
}
mods.delete();
}

if(customMods.exists()) {
for(CustomMods.InstanceMods instMods : customModsObj.instances) {
if(!instMods.version.equals(this.versionName)) {
continue;
}
for(CustomMods.ModInfo info : instMods.mods) {
API_V1.currentDownload = info.name;
DownloadUtils.downloadFile(info.url, new File(Constants.MC_DIR + "/mods/" + this.versionName + "/" + info.name + ".jar"));
if(customMods.exists()) {
for(CustomMods.InstanceMods instMods : customModsObj.instances) {
if(!instMods.version.equals(this.versionName)) {
continue;
}
for(CustomMods.ModInfo info : instMods.mods) {
API_V1.currentDownload = info.name;
DownloadUtils.downloadFile(info.url, new File(Constants.MC_DIR + "/mods/" + this.versionName + "/" + info.name + ".jar"));
}
}
}

} catch (IOException e) {
e.printStackTrace();
}

} catch (IOException e) {
e.printStackTrace();
}

API_V1.finishedDownloading = true;
API_V1.finishedDownloading = true;
}).start();
}

Expand Down

0 comments on commit fa753a8

Please sign in to comment.