diff options
author | Florian Rinke <develop@torui.de> | 2021-10-11 17:52:17 +0200 |
---|---|---|
committer | Florian Rinke <develop@torui.de> | 2021-10-11 17:52:17 +0200 |
commit | 16b9821f55eec8f4a77ee1743cbc3f9b4ba3e303 (patch) | |
tree | c7ede24fb2a7c0871c92c66fe4b4f7806344d326 /src/main/java/de/torui/coflsky/minecraft_integration | |
parent | 9e923dbdf82b2a1fbebcb9c4559400f921104229 (diff) | |
download | COFL-16b9821f55eec8f4a77ee1743cbc3f9b4ba3e303.tar.gz COFL-16b9821f55eec8f4a77ee1743cbc3f9b4ba3e303.tar.bz2 COFL-16b9821f55eec8f4a77ee1743cbc3f9b4ba3e303.zip |
implements #11, #8
Diffstat (limited to 'src/main/java/de/torui/coflsky/minecraft_integration')
-rw-r--r-- | src/main/java/de/torui/coflsky/minecraft_integration/CoflSessionManager.java | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/src/main/java/de/torui/coflsky/minecraft_integration/CoflSessionManager.java b/src/main/java/de/torui/coflsky/minecraft_integration/CoflSessionManager.java new file mode 100644 index 0000000..ebff38a --- /dev/null +++ b/src/main/java/de/torui/coflsky/minecraft_integration/CoflSessionManager.java @@ -0,0 +1,141 @@ +package de.torui.coflsky.minecraft_integration; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.time.Duration; +import java.time.LocalDateTime; +import java.time.ZonedDateTime; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.UUID; +import java.util.stream.Collectors; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; + +import net.minecraftforge.fml.common.Loader; + +public class CoflSessionManager { + private static Gson gson = new GsonBuilder() .registerTypeAdapter(ZonedDateTime.class, new TypeAdapter<ZonedDateTime>() { + @Override + public void write(JsonWriter out, ZonedDateTime value) throws IOException { + out.value(value.toString()); + } + + @Override + public ZonedDateTime read(JsonReader in) throws IOException { + return ZonedDateTime.parse(in.nextString()); + } + }) + .enableComplexMapKeySerialization().create(); + public static class CoflSession { + + public String SessionUUID; + public ZonedDateTime timestampCreated; + public CoflSession() {} + public CoflSession(String sessionUUID, ZonedDateTime timestampCreated) { + super(); + SessionUUID = sessionUUID; + this.timestampCreated = timestampCreated; + } + + } + + public static void UpdateCoflSessions() throws IOException { + Map<String, CoflSession> sessions = GetCoflSessions(); + + for (String username : sessions.keySet()) { + if(!isValidSession(sessions.get(username))) { + DeleteCoflSession(username); + } + } + } + + public static Path GetTempFileFolder() { + + Path dataPath = Paths.get(Loader.instance().getConfigDir().getPath(), "CoflSky", "sessions"); + dataPath.toFile().mkdirs(); + + return dataPath; + } + + public static Map<String, CoflSession> GetCoflSessions() throws IOException{ + + File[] sessions = GetTempFileFolder().toFile().listFiles(); + + Map<String, CoflSession> map = new HashMap<>(); + + for (int i= 0; i<sessions.length;i++) { + map.put(sessions[i].getName(), GetCoflSession(sessions[i].getName())); + } + + return map; + } + + public static boolean isValidSession(CoflSession session) { + if(session.timestampCreated.plus(Duration.ofDays(7)).isAfter(ZonedDateTime.now())) { + return true; + } + return false; + } + + private static Path GetUserPath(String username) { + return Paths.get(GetTempFileFolder().toString() + "/" + username); + } + public static void DeleteCoflSession(String username) { + Path path =GetUserPath(username); + path.toFile().delete(); + } + public static void DeleteAllCoflSessions() { + Path path =GetTempFileFolder(); + path.toFile().delete(); + } + + public static CoflSession GetCoflSession(String username) throws IOException { + Path path = GetUserPath(username); + File file = path.toFile(); + + if(!file.exists()) { + CoflSession session = new CoflSession(UUID.randomUUID().toString(), ZonedDateTime.now()); + OverwriteCoflSession(username, session); + return session; + } + + BufferedReader reader = new BufferedReader( new InputStreamReader(new FileInputStream(file))); + String raw = reader.lines().collect(Collectors.joining("\n")); + + reader.close(); + CoflSession session = gson.fromJson(raw, CoflSession.class); + return session; + } + + public static boolean OverwriteCoflSession(String username, CoflSession session) throws IOException { + + + Path path = GetUserPath(username); + File file = path.toFile(); + file.createNewFile(); + + String data = gson.toJson(session); + + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))); + bw.append(data); + bw.flush(); + bw.close(); + + return true; + } +} |