Skip to content

Instantly share code, notes, and snippets.

@ferrybig
Created April 5, 2016 17:23
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 ferrybig/311d570cb45f418bcaad38ceb319e9c5 to your computer and use it in GitHub Desktop.
Save ferrybig/311d570cb45f418bcaad38ceb319e9c5 to your computer and use it in GitHub Desktop.
Creates a list of usernames and uuid based on replay mod files
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.PrintStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.zip.ZipFile;
import org.json.simple.JSONObject;
import java.util.UUID;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.stream.Stream;
/**
*
* @author Fernando
*/
public class UserNames {
public static String getText(InputStream url) throws IOException {
StringBuilder response;
try (BufferedReader in = new BufferedReader(
new InputStreamReader(url))) {
response = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
}
return response.toString();
}
public static String getText(String url) throws IOException {
URL website = new URL(url);
URLConnection connection = website.openConnection();
return getText(connection.getInputStream());
}
public static void main(String... args) throws FileNotFoundException, IOException, ClassNotFoundException, InterruptedException {
File[] replaymodFiles = new File[]{
new File("C:\\Users\\Fernando\\AppData\\Roaming\\.minecraft\\replay_downloads"),
new File("C:\\Users\\Fernando\\AppData\\Roaming\\.minecraft\\replay_recordings"),
// new File("C:\\Users\\Fernando\\AppData\\Roaming\\.minecraft\\replay_recordings_old"),
new File("D:\\Fernando\\Documents\\replay_recordings_old"),
};
List<File> toScan = Arrays.stream(replaymodFiles).flatMap(i -> Arrays.stream(i.listFiles())).collect(Collectors.toList());
AtomicLong globalsleepdelay = new AtomicLong();
Map<UUID, String> ok = new HashMap<>(300, 0.5f);
Set<UUID> failed = new HashSet<>(300, 0.5f);
final File usernameFile = new File("usernames.txt");
final File failedFile = new File("failed.txt");
if (usernameFile.exists()) {
try (Scanner scan = new Scanner(usernameFile)) {
String line;
while (scan.hasNextLine()) {
String[] ar = scan.nextLine().split(",");
if (ar.length == 2) {
ok.put(UUID.fromString(ar[1].trim()), ar[0].trim());
}
}
}
}
if (failedFile.exists()) {
try (Scanner scan = new Scanner(failedFile)) {
String line;
while (scan.hasNextLine()) {
failed.add(UUID.fromString(scan.nextLine()));
}
}
}
Set<UUID> failedSync = Collections.synchronizedSet(failed);
try (PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream(usernameFile), 1024*32), false)) {
toScan.parallelStream().flatMap(i -> {
try (ZipFile file = new ZipFile(i)) {
String json = getText(file.getInputStream(file.getEntry("metaData.json")));
JsonElement obj = new JsonParser().parse(json);
JsonArray arr = obj.getAsJsonObject().getAsJsonArray("players");
UUID[] retr = new UUID[arr.size()];
for (int ii = 0; ii < retr.length; ii++) {
retr[ii] = UUID.fromString(arr.get(ii).getAsString());
}
return Arrays.stream(retr);
} catch (IOException ex) {
System.err.println("Failed: " + i);
ex.printStackTrace();
return Stream.empty();
}
}).distinct().flatMap(uuid -> {
//System.err.println("-- "+n.getKey());
if (ok.containsKey(uuid)) {
return Stream.of(String.format("%1$-16s, %2$s", ok.get(uuid), uuid));
}
if (failedSync.contains(uuid)) {
return Stream.empty();
}
String data = null;
long sleepdelay = Math.max(1, globalsleepdelay.get());
long retries = 0;
while (data == null) {
try {
Thread.sleep(sleepdelay);
final String uuidString = uuid.toString().replace("-", "");
if(uuidString.charAt(12) != '4') {
System.err.println("Invalid: " + uuidString);
failedSync.add(uuid);
return Stream.empty();
}
data = getText("http://eu.mc-api.net/v3/name/" + (uuidString));
sleepdelay /= 2;
} catch (FileNotFoundException ex) {
System.err.println("Err: " + ex.toString());
failedSync.add(uuid);
return Stream.empty();
} catch (IOException ex) {
System.err.println("-- error: " + ex.toString() + " -- sleep: " + sleepdelay + " -- retry count: " + retries);
try {
Thread.sleep(sleepdelay);
} catch (InterruptedException ex1) {
throw new RuntimeException(ex);
}
sleepdelay *= 3;
retries++;
if (retries > 10) {
continue;
}
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
}
globalsleepdelay.set(sleepdelay);
if (data.length() == 0) {
return Stream.empty();
}
String[] blocks = data.split("\"");
String name = blocks[15].replaceFirst("([0-9a-fA-F]{8})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]+)", "$1-$2-$3-$4-$5");
return Stream.of(String.format("%1$-16s, %2$s", name, uuid));
}).sorted().collect(Collectors.toList()).forEach(out::println);
}
try (PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream(failedFile), 1024*32), false)) {
failed.forEach(out::println);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment