Last active
July 1, 2020 09:02
-
-
Save ethan-gallant/96c20403d6da81d4ab2e06da22d96a02 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Ban { | |
public String message; | |
public Date expiresAt; | |
Ban(String message, Date expiresAt) { | |
this.message = message; | |
this.expiresAt = expiresAt; | |
} | |
} | |
private void createIfNotExists() { | |
Document playerDocument = mongoManager.getPlayerData().find(Filters.eq("uuid", this.bukkitPlayer.getUniqueId().toString())).first(); | |
if (playerDocument == null) { | |
Document player = new Document("uuid", bukkitPlayer.getUniqueId().toString()) | |
.append("coins", "0") | |
.append("ips", Collections.singletonList(bukkitPlayer.getAddress().toString())) | |
.append("last_login", new Date()) | |
.append("first_joined", new Date()) | |
.append("punishments", Collections.emptyList()); | |
mongoManager.getPlayerData().insertOne(player); | |
return; | |
} | |
mongoManager.getPlayerData().updateOne(Filters.eq("uuid", this.bukkitPlayer.getUniqueId().toString()), | |
new Document("$set", | |
new Document("last_login", new Date()) | |
) | |
); | |
} | |
public Ban getBan() { | |
BasicDBObject bdbo = new BasicDBObject(); | |
BasicDBObject gte = new BasicDBObject(); | |
BasicDBObject exists = new BasicDBObject(); | |
gte.put("$gte", new Date()); | |
exists.put("$exists", false); | |
bdbo.put("expires_at", gte); | |
bdbo.put("type", "ban"); | |
bdbo.put("pardoned_at", exists); | |
Document playerObj = mongoManager.getPlayerData().find( | |
Filters.and( | |
Filters.elemMatch("punishments", bdbo), | |
Filters.eq("uuid", this.getUUID().toString()) | |
) | |
).projection( | |
new Document("punishments.$", 1) | |
).first(); | |
if (playerObj == null || playerObj.isEmpty()) { | |
System.out.println("Player was not found in db"); | |
return null; | |
} | |
List<Document> punishments = (List<Document>) playerObj.get("punishments"); | |
if (punishments.isEmpty()) { | |
System.out.println("Getting bans was null"); | |
return null; | |
} | |
return new Ban(punishments.get(0).getString("message"), punishments.get(0).getDate("expires_at")); | |
} | |
public void ban(String reason, Duration duration, UUID enforcer) { | |
LocalDate d = LocalDate.now(); | |
mongoManager.getPlayerData().updateOne(Filters.eq("uuid", this.bukkitPlayer.getUniqueId().toString()), | |
new Document("$push", | |
new Document("punishments", | |
new Document("enforcer", enforcer.toString()) | |
.append("expires_at", Date.from(Instant.from(duration.addTo(d)))) | |
.append("type", "ban") | |
.append("message", reason) | |
.append("created_at", new Date()) | |
) | |
) | |
); | |
}; | |
public void logIP(String ip) { | |
mongoManager.getPlayerData().updateOne(Filters.eq("uuid", this.bukkitPlayer.getUniqueId().toString()), | |
new Document("$addToSet", | |
new Document("ips", ip) | |
) | |
); | |
} | |
public int getStat(String statName) { | |
Document playerObj = mongoManager.getPlayerData().find(Filters.eq("uuid", this.bukkitPlayer.getUniqueId().toString())).first(); | |
if (playerObj == null || playerObj.isEmpty()) { | |
System.out.println("Player was not found in db"); | |
return 0; | |
} | |
Document stats = (Document) playerObj.get("stats"); | |
if(stats == null) { | |
return 0; | |
} | |
try { | |
return stats.getInteger(statName); | |
} catch (ClassCastException e) | |
{ | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment