Skip to content

Instantly share code, notes, and snippets.

@ejm
Created June 28, 2021 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ejm/00b2a377df6eef66e668164619d23851 to your computer and use it in GitHub Desktop.
Save ejm/00b2a377df6eef66e668164619d23851 to your computer and use it in GitHub Desktop.
private Map<Action, List<TabListEntry>> merge(List<TabListEntry> oldList, List<TabListEntry> newList) throws Exception {
Map<UUID, TabListEntry> oldMap = new HashMap<>();
Map<UUID, TabListEntry> newMap = new HashMap<>();
Map<Action, List<TabListEntry>> actions = new HashMap<>();
actions.put(Action.ADD_PLAYER, new ArrayList<>());
actions.put(Action.UPDATE_GAMEMODE, new ArrayList<>());
actions.put(Action.UPDATE_LATENCY, new ArrayList<>());
actions.put(Action.UPDATE_DISPLAY_NAME, new ArrayList<>());
actions.put(Action.REMOVE_PLAYER, new ArrayList<>());
for (TabListEntry entry : oldList) {
oldMap.put(entry.profile().uniqueId(), entry);
}
// Validate the new list
for (TabListEntry entry : newList) {
UUID uuid = entry.profile().uniqueId();
if (newMap.containsKey(uuid)) {
throw new Exception("This will be a DataTransctionResult.FAILURE or something");
}
newMap.put(uuid, entry);
}
for (Map.Entry<UUID, TabListEntry> entry : newMap.entrySet()) {
UUID uuid = entry.getKey();
TabListEntry newEntry = entry.getValue();
if (!oldMap.containsKey(uuid)) {
actions.get(Action.ADD_PLAYER).add(newEntry);
continue;
}
TabListEntry oldEntry = oldMap.get(uuid);
if (!newEntry.gameMode().equals(oldEntry.gameMode())) {
actions.get(Action.UPDATE_GAMEMODE).add(newEntry);
}
if (!(newEntry.latency() == oldEntry.latency())) {
actions.get(Action.UPDATE_LATENCY).add(newEntry);
}
if (!newEntry.displayName().equals(oldEntry.displayName())) {
actions.get(Action.UPDATE_DISPLAY_NAME).add(newEntry);
}
oldMap.remove(uuid);
}
for (Map.Entry<UUID, TabListEntry> entry : oldMap.entrySet()) {
actions.get(Action.REMOVE_PLAYER).add(entry.getValue());
}
return actions;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment