package net.frozenorb.apiv3.model; import lombok.Getter; import lombok.ToString; import net.frozenorb.apiv3.accessor.Servers; import org.bson.Document; import java.time.Instant; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.UUID; import java.util.stream.Collectors; @ToString public final class Server extends BaseModel { @Getter private String id; @Getter private String bungeeId; @Getter private String displayName; @Getter private String secret; @Getter private String group; @Getter private String internalIp; @Getter private Instant lastUpdate; @Getter private double lastTps; @Getter private List players; public Server(Document json) { super(Servers.COLLECTION_NAME); this.id = json.getString("_id"); this.bungeeId = json.getString("bungeeId"); this.displayName = json.getString("displayName"); this.secret = json.getString("secret"); this.group = json.getString("group"); this.internalIp = json.getString("internalIp"); this.lastUpdate = (Instant) json.get("lastUpdate"); this.lastTps = ((Number) json.get("lastTps")).doubleValue(); this.players = new ArrayList<>(); for (Object uuidString : (Collection) json.get("players")) { players.add(UUID.fromString((String) uuidString)); } setId(id); } public Document toLiteJson() { Document json = new Document(); json.put("id", id); json.put("bungeeId", bungeeId); json.put("displayName", displayName); json.put("group", group); json.put("internalIp", internalIp); return json; } public Document toFullJson() { Document json = toLiteJson(); json.put("lastUpdate", lastUpdate.toString()); json.put("lastTps", lastTps); json.put("players", players.stream().map(UUID::toString).collect(Collectors.toList())); return json; } }