Skip to content

Commit

Permalink
sync remote/local
Browse files Browse the repository at this point in the history
`allowDeletions=true`: If a remote location has updated its default server list, add the new entries to the local list too.
  • Loading branch information
glowredman committed Jun 28, 2022
1 parent bef1be6 commit 0b9ad30
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 33 deletions.
67 changes: 44 additions & 23 deletions src/main/java/glowredman/defaultserverlist/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
import cpw.mods.fml.common.FMLLog;
import java.io.File;
import java.io.IOException;
Expand All @@ -13,17 +14,16 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.minecraft.client.multiplayer.ServerData;
import org.apache.commons.io.IOUtils;

public class Config {

public static boolean allowDeletions;
public static String url;
public static ConfigObj config = new ConfigObj();
public static final List<ServerData> SERVERS = new ArrayList<>();
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private static Path configPath;
Expand All @@ -32,7 +32,6 @@ public static void preInit(File configDir) {
Reader fileReader = null;
try {
configPath = configDir.toPath().resolve("defaultserverlist.json");
ConfigObj config = new ConfigObj();

if (!Files.exists(configPath)) {
saveConfig(config);
Expand All @@ -41,27 +40,46 @@ public static void preInit(File configDir) {
config = GSON.fromJson(fileReader, ConfigObj.class);
}

allowDeletions = config.allowDeletions;
url = config.url;

Map<String, String> servers;
if (config.useURL) {
servers = GSON.fromJson(
IOUtils.toString(new URL(config.url), StandardCharsets.UTF_8),
new TypeToken<Map<String, String>>() {
private static final long serialVersionUID = -1786059589535074931L;
}.getType());
ConfigObj newConfig = new ConfigObj(servers);
newConfig.useURL = !allowDeletions;
saveConfig(newConfig);
} else {
servers = config.servers;
}
try {
// servers that are currently at the remote location
Map<String, String> remoteDefaultServers = GSON.fromJson(
IOUtils.toString(new URL(config.url), StandardCharsets.UTF_8),
new TypeToken<LinkedHashMap<String, String>>() {
private static final long serialVersionUID = -1786059589535074931L;
}.getType());

if (config.allowDeletions) {
// servers that were added to the remote location since the last time the list was fetched
Map<String, String> diff = new LinkedHashMap<>();

for (Entry<String, String> e : servers.entrySet()) {
SERVERS.add(new ServerData(e.getKey(), e.getValue()));
// calculate diff
remoteDefaultServers.forEach((name, ip) -> {
if (!config.prevDefaultServers.contains(ip)) {
diff.put(name, ip);
}
});

// save if the remote location was updated
if (!diff.isEmpty()) {
config.servers.putAll(diff);
config.prevDefaultServers = remoteDefaultServers.values();
saveConfig(config);
}

} else {
config.servers = remoteDefaultServers;
saveConfig(config);
}
} catch (Exception e) {
FMLLog.warning(
"Could not get default server list from default location! Are you connected to the internet?");
e.printStackTrace();
}
}

config.servers.forEach((name, ip) -> SERVERS.add(new ServerData(name, ip)));

} catch (Exception e) {
FMLLog.severe("Could not parse default server list!");
e.printStackTrace();
Expand All @@ -83,7 +101,10 @@ public static final class ConfigObj {
public boolean useURL = false;
public boolean allowDeletions = true;
public String url = "";
public Map<String, String> servers = new HashMap<>();
public Map<String, String> servers = new LinkedHashMap<>();

@SerializedName("DO_NOT_EDIT_prevDefaultServers")
public Collection<String> prevDefaultServers = new ArrayList<>();

public ConfigObj() {}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package glowredman.defaultserverlist.mixins;

import glowredman.defaultserverlist.Config;
import glowredman.defaultserverlist.Config.ConfigObj;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import net.minecraft.client.multiplayer.ServerData;
Expand Down Expand Up @@ -54,14 +53,12 @@ private void removeDuplicateServers(CallbackInfo ci) {
*/
@Inject(at = @At("TAIL"), method = "saveServerList()V")
private void saveDefaultServerList(CallbackInfo ci) {
if (Config.allowDeletions) {
if (Config.config.allowDeletions) {
try {
Map<String, String> map = new HashMap<>();
Config.SERVERS.forEach(sd -> map.put(sd.serverName, sd.serverIP));
ConfigObj newConfig = new ConfigObj(map);
newConfig.allowDeletions = Config.allowDeletions;
newConfig.url = Config.url;
Config.saveConfig(newConfig);
Map<String, String> newServers = new LinkedHashMap<>();
Config.SERVERS.forEach(serverData -> newServers.put(serverData.serverName, serverData.serverIP));
Config.config.servers = newServers;
Config.saveConfig(Config.config);
} catch (Exception e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -90,7 +87,7 @@ public ServerData getServerData(int index) {
public void removeServerData(int index) {
if (index < servers.size()) {
servers.remove(index);
} else if (Config.allowDeletions) {
} else if (Config.config.allowDeletions) {
Config.SERVERS.remove(index - servers.size());
}
}
Expand Down

0 comments on commit 0b9ad30

Please sign in to comment.