Skip to content

Commit

Permalink
Implement Instance List
Browse files Browse the repository at this point in the history
  • Loading branch information
CADIndie committed Jan 28, 2024
1 parent 684e59d commit 4d84556
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions src/main/java/pojlib/instance/MinecraftInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,19 @@

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import pojlib.account.MinecraftAccount;
import pojlib.api.API_V1;
import pojlib.install.*;
import pojlib.install.FabricMeta;
import pojlib.install.Installer;
import pojlib.install.MinecraftMeta;
import pojlib.install.QuiltMeta;
import pojlib.install.VersionInfo;
import pojlib.util.Constants;
import pojlib.util.CoreMods;
import pojlib.util.CustomMods;
Expand All @@ -21,19 +31,12 @@
import pojlib.util.Logger;
import pojlib.util.VLoader;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MinecraftInstance {
public static final String MODS = "https://raw.githubusercontent.com/QuestCraftPlusPlus/Pojlib/QuestCraft/mods.json";
public static final String DEV_MODS = "https://raw.githubusercontent.com/QuestCraftPlusPlus/Pojlib/QuestCraft/devmods.json";
public static final String CUSTOM_MODS = "custom_mods.json";
public String versionName;
public String instanceName;
public String versionType;
public String classpath;
public String gameDir;
Expand All @@ -59,6 +62,7 @@ public static MinecraftInstance create(Activity activity, String instanceName, S
Logger.getInstance().appendToLog("Creating new instance: " + instanceName);

MinecraftInstance instance = new MinecraftInstance();
instance.instanceName = instanceName;
instance.versionName = minecraftVersion.id;
instance.gameDir = new File(gameDir).getAbsolutePath();

Expand Down Expand Up @@ -109,9 +113,36 @@ public static MinecraftInstance create(Activity activity, String instanceName, S
GsonUtils.objectToJsonFile(gameDir + "/instances/" + instanceName + "/instance.json", instance);
API_V1.finishedDownloading = true;
}).start();

updateInstancesJson(gameDir, instance);
return instance;
}

private static synchronized void updateInstancesJson(String gameDir, MinecraftInstance instance) {
String instancesFilePath = gameDir + "/instances.json";
JsonArray instancesArray;

try {
if (new File(instancesFilePath).exists()) {
String jsonContent = FileUtil.read(instancesFilePath);
instancesArray = GsonUtils.GLOBAL_GSON.fromJson(jsonContent, JsonArray.class);
} else {
instancesArray = new JsonArray();
}

JsonObject instancesJson = new JsonObject();
instancesJson.addProperty("instanceName", instance.instanceName);
instancesJson.addProperty("instanceVersion", instance.versionName);
instancesJson.addProperty("gameDir", instance.gameDir);

instancesArray.add(instancesJson);

GsonUtils.objectToJsonFile(instancesFilePath, instancesArray);
} catch (IOException e) {
e.printStackTrace();
}
}

// Load an instance from json
public static MinecraftInstance load(String instanceName, String gameDir) {
String path = gameDir + "/instances/" + instanceName + "/instance.json";
Expand Down

0 comments on commit 4d84556

Please sign in to comment.