Serialize Sessions into json without Gson during shutdown

Affects issues:
- Fixed #1970
This commit is contained in:
Risto Lahtela 2021-07-02 17:59:25 +03:00
parent b3200d1734
commit 8d445aaccc
6 changed files with 64 additions and 11 deletions

View File

@ -57,4 +57,8 @@ public class Counter {
"count=" + count +
'}';
}
public String toJson() {
return "{\"count\": " + count + "}";
}
}

View File

@ -191,16 +191,14 @@ public class FinishedSession implements DateHolder {
* @return Serialized format
*/
public String serializeCSV() {
Gson gson = new Gson();
return String.valueOf(playerUUID) + ';' +
serverUUID + ';' +
start + ';' +
end + ';' +
afkTime + ';' +
gson.toJson(getExtraData(WorldTimes.class).orElseGet(WorldTimes::new)) + ';' +
gson.toJson(getExtraData(PlayerKills.class).orElseGet(PlayerKills::new)) + ';' +
gson.toJson(getExtraData(MobKillCounter.class).orElseGet(MobKillCounter::new)) + ';' +
gson.toJson(getExtraData(DeathCounter.class).orElseGet(DeathCounter::new));
getExtraData(WorldTimes.class).orElseGet(WorldTimes::new).toJson() + ';' +
getExtraData(PlayerKills.class).orElseGet(PlayerKills::new).toJson() + ';' +
getExtraData(MobKillCounter.class).orElseGet(MobKillCounter::new).toJson() + ';' +
getExtraData(DeathCounter.class).orElseGet(DeathCounter::new).toJson();
}
}

View File

@ -16,6 +16,8 @@
*/
package com.djrapitops.plan.gathering.domain;
import org.apache.commons.text.TextStringBuilder;
import java.util.Map;
import java.util.Optional;
@ -57,11 +59,16 @@ public class GMTimes extends TimeKeeper {
public static String magicNumberToGMName(int magicNumber) {
switch (magicNumber) {
case 0: return SURVIVAL;
case 1: return CREATIVE;
case 2: return ADVENTURE;
case 3: return SPECTATOR;
default: return "UNKOWN";
case 0:
return SURVIVAL;
case 1:
return CREATIVE;
case 2:
return ADVENTURE;
case 3:
return SPECTATOR;
default:
return "UNKOWN";
}
}
@ -123,4 +130,15 @@ public class GMTimes extends TimeKeeper {
", lastStateChange=" + lastStateChange +
'}';
}
public String toJson() {
return "{\"times\": {" +
new TextStringBuilder().appendWithSeparators(times.entrySet().stream()
.map(entry -> "\"" + entry.getKey() + "\": " + entry.getValue())
.iterator(), ",").build() +
" }," +
" \"state\": \"" + state + "\"," +
" \"lastStateChange\": " + lastStateChange +
"}";
}
}

View File

@ -124,4 +124,14 @@ public class PlayerKill implements DateHolder {
public boolean isNotSelfKill() {
return !isSelfKill();
}
public String toJson() {
return "{\"killer\": \"" + killer + "\"," +
" \"victim\": \"" + victim + "\"," +
" \"weapon\": \"" + weapon + "\"," +
" \"date\": " + date + "," +
" \"victimName\": \"" + victimName + "\"," +
" \"killerName\": \"" + killerName + "\"" +
"}";
}
}

View File

@ -17,6 +17,7 @@
package com.djrapitops.plan.gathering.domain;
import com.djrapitops.plan.utilities.comparators.DateHolderRecentComparator;
import org.apache.commons.text.TextStringBuilder;
import java.util.ArrayList;
import java.util.Collection;
@ -73,4 +74,13 @@ public class PlayerKills {
"kills=" + kills +
'}';
}
public String toJson() {
return "{" +
" \"kills\": [" +
new TextStringBuilder().appendWithSeparators(kills
.stream().map(PlayerKill::toJson).iterator(), ",").build() +
" ]" +
"}";
}
}

View File

@ -16,6 +16,8 @@
*/
package com.djrapitops.plan.gathering.domain;
import org.apache.commons.text.TextStringBuilder;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@ -197,4 +199,15 @@ public class WorldTimes {
setGMTimesForWorld(entry.getKey(), entry.getValue());
}
}
public String toJson() {
return "{\"times\": {" +
new TextStringBuilder().appendWithSeparators(times.entrySet().stream()
.map(entry -> "\"" + entry.getKey() + "\": " + entry.getValue().toJson())
.iterator(), ",").build() +
" }," +
" \"currentWorld\": \"" + currentWorld + "\"," +
" \"currentGamemode\": \"" + currentGamemode + "\"" +
"}";
}
}