Skip to content

Instantly share code, notes, and snippets.

@MissingNoShiny
Created May 1, 2018 15:26
Show Gist options
  • Select an option

  • Save MissingNoShiny/55e39f384cd5c975448b4905539bc50d to your computer and use it in GitHub Desktop.

Select an option

Save MissingNoShiny/55e39f384cd5c975448b4905539bc50d to your computer and use it in GitHub Desktop.
Script utilisé pour passer les données joueur du serveur offline au serveur online
import com.google.common.base.Charsets;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<String, String> playerMap = getPlayerMap();
System.out.println("Map length : " + playerMap.size());
File[] directories = new File("./data").listFiles(File::isDirectory);
for (File directory : directories) {
System.out.println("Dossier " + directory.getName());
File[] fileList = directory.listFiles();
for (File file : fileList) {
String baseName = getBaseName(file.getName());
String extension = getFileExtension(file.getName());
if (!playerMap.containsKey(baseName)) {
System.out.println("Fichier " + file.getName() + " supprimé");
file.delete();
} else {
String newName = playerMap.get(baseName);
renameFile(file.getPath(), newName + extension);
System.out.println(file.getName() + " renommé en " + newName + extension);
}
}
}
}
public static ArrayList<String> getPlayerList() {
ArrayList<String> playerList = new ArrayList<>();
try {
FileInputStream fstream = new FileInputStream("playerList.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = br.readLine()) != null) {
playerList.add(strLine);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return playerList;
}
public static Map<String, String> getPlayerMap() {
Map<String, String> playerMap = new HashMap<>();
ArrayList<String> playerList = getPlayerList();
for (String player : playerList) {
String offlineUUID = getOfflineUUID(player);
String onlineUUID = getOnlineUUID(player);
playerMap.put(offlineUUID, onlineUUID);
}
return playerMap;
}
public static String getOfflineUUID(String username) {
UUID offlineUUID = UUID.nameUUIDFromBytes(("OfflinePlayer:" + username).getBytes(Charsets.UTF_8));
return offlineUUID.toString();
}
public static String getOnlineUUID(String username) {
String uuid = null;
URL url = null;
try {
url = new URL("https://api.mojang.com/users/profiles/minecraft/" + username);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
URLConnection connection = url.openConnection();
Scanner jsonScanner = new Scanner(connection.getInputStream(), "UTF-8");
String json = jsonScanner.next();
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
uuid = jsonObject.get("id").getAsString();
jsonScanner.close();
} catch (Exception e) {
e.printStackTrace();
}
return getLongUUID(uuid);
}
public static String getLongUUID(String shortUUID) {
StringBuilder stringBuilder = new StringBuilder(shortUUID);
stringBuilder.insert(20, '-');
stringBuilder.insert(16, '-');
stringBuilder.insert(12, '-');
stringBuilder.insert(8, '-');
return stringBuilder.toString();
}
public static String getFileExtension(String fileName) {
try {
return fileName.substring(fileName.lastIndexOf("."));
} catch (Exception e) {
return "";
}
}
public static String getBaseName(String fileName) {
int index = fileName.lastIndexOf('.');
if (index == -1) {
return fileName;
} else {
return fileName.substring(0, index);
}
}
public static void renameFile(String path, String newName) {
Path source = Paths.get(path);
try {
Files.move(source, source.resolveSibling(newName));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment