mirror of
https://github.com/plexusorg/Plex.git
synced 2025-07-04 00:46:40 +00:00
so far it is working but the only thing preventing me from going to fully asynchronous calls for punishments is httpd...
This commit is contained in:
2
src/main/java/dev/plex/cache/DataUtils.java
vendored
2
src/main/java/dev/plex/cache/DataUtils.java
vendored
@ -1,6 +1,7 @@
|
||||
package dev.plex.cache;
|
||||
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.cache.player.PlayerCache;
|
||||
import dev.plex.player.PlexPlayer;
|
||||
import dev.plex.storage.StorageType;
|
||||
import java.util.UUID;
|
||||
@ -43,6 +44,7 @@ public class DataUtils
|
||||
return PlayerCache.getPlexPlayerMap().get(uuid);
|
||||
}
|
||||
|
||||
|
||||
if (Plex.get().getStorageType() == StorageType.MONGODB)
|
||||
{
|
||||
return Plex.get().getMongoPlayerData().getByUUID(uuid);
|
||||
|
@ -1,4 +1,4 @@
|
||||
package dev.plex.cache;
|
||||
package dev.plex.cache.player;
|
||||
|
||||
import dev.morphia.Datastore;
|
||||
import dev.morphia.query.Query;
|
||||
@ -8,6 +8,7 @@ import dev.morphia.query.experimental.updates.UpdateOperators;
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.player.PlexPlayer;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
@ -104,6 +105,11 @@ public class MongoPlayerData
|
||||
updateOps.execute();
|
||||
}
|
||||
|
||||
public List<PlexPlayer> getPlayers()
|
||||
{
|
||||
return datastore.find(PlexPlayer.class).stream().toList();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Saves the player's information in the database
|
@ -1,8 +1,8 @@
|
||||
package dev.plex.cache;
|
||||
package dev.plex.cache.player;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import dev.plex.player.PlexPlayer;
|
||||
import dev.plex.player.PunishedPlayer;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@ -20,19 +20,19 @@ public class PlayerCache
|
||||
/**
|
||||
* A key/value pair where the key is the unique ID of the Punished Player
|
||||
*/
|
||||
private static final Map<UUID, PunishedPlayer> punishedPlayerMap = Maps.newHashMap();
|
||||
// private static final Map<UUID, PunishedPlayer> punishedPlayerMap = Maps.newHashMap();
|
||||
|
||||
public static Map<UUID, PunishedPlayer> getPunishedPlayerMap()
|
||||
{
|
||||
return punishedPlayerMap;
|
||||
}
|
||||
// public static Map<UUID, PunishedPlayer> getPunishedPlayerMap()
|
||||
// {
|
||||
// return punishedPlayerMap;
|
||||
// }
|
||||
|
||||
public static Map<UUID, PlexPlayer> getPlexPlayerMap()
|
||||
{
|
||||
return plexPlayerMap;
|
||||
}
|
||||
|
||||
public static PunishedPlayer getPunishedPlayer(UUID uuid)
|
||||
/*public static PunishedPlayer getPunishedPlayer(UUID uuid)
|
||||
{
|
||||
if (!getPunishedPlayerMap().containsKey(uuid))
|
||||
{
|
||||
@ -40,7 +40,7 @@ public class PlayerCache
|
||||
}
|
||||
return getPunishedPlayerMap().get(uuid);
|
||||
}
|
||||
|
||||
*/
|
||||
public static PlexPlayer getPlexPlayer(UUID uuid)
|
||||
{
|
||||
return getPlexPlayerMap().get(uuid);
|
@ -1,4 +1,4 @@
|
||||
package dev.plex.cache;
|
||||
package dev.plex.cache.player;
|
||||
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import com.google.gson.Gson;
|
130
src/main/java/dev/plex/cache/sql/SQLPunishment.java
vendored
Normal file
130
src/main/java/dev/plex/cache/sql/SQLPunishment.java
vendored
Normal file
@ -0,0 +1,130 @@
|
||||
package dev.plex.cache.sql;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.punishment.Punishment;
|
||||
import dev.plex.punishment.PunishmentType;
|
||||
import dev.plex.util.PlexLog;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class SQLPunishment
|
||||
{
|
||||
private static final String SELECT = "SELECT * FROM `punishments` WHERE punished=?";
|
||||
private static final String SELECT_BY = "SELECT * FROM `punishments` WHERE punisher=?";
|
||||
|
||||
private static final String INSERT = "INSERT INTO `punishments` (`punished`, `punisher`, `punishedUsername`, `ip`, `type`, `reason`, `customTime`, `active`, `endDate`) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
private static final String UPDATE_BAN = "UPDATE `punishments` SET active=? WHERE active=? AND punished=? AND type=?";
|
||||
|
||||
public CompletableFuture<List<Punishment>> getPunishments()
|
||||
{
|
||||
return CompletableFuture.supplyAsync(() ->
|
||||
{
|
||||
List<Punishment> punishments = Lists.newArrayList();
|
||||
try (Connection con = Plex.get().getSqlConnection().getCon())
|
||||
{
|
||||
PreparedStatement statement = con.prepareStatement("SELECT * FROM `punishments`");
|
||||
ResultSet set = statement.executeQuery();
|
||||
while (set.next())
|
||||
{
|
||||
Punishment punishment = new Punishment(UUID.fromString(set.getString("punished")), UUID.fromString(set.getString("punisher")));
|
||||
punishment.setActive(set.getBoolean("active"));
|
||||
punishment.setType(PunishmentType.valueOf(set.getString("type")));
|
||||
punishment.setCustomTime(set.getBoolean("customTime"));
|
||||
punishment.setPunishedUsername(set.getString("punishedUsername"));
|
||||
punishment.setEndDate(LocalDateTime.ofInstant(Instant.ofEpochMilli(set.getLong("endDate")), ZoneId.systemDefault()));
|
||||
punishment.setReason(set.getString("reason"));
|
||||
punishment.setIp(set.getString("ip"));
|
||||
punishments.add(punishment);
|
||||
}
|
||||
} catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return punishments;
|
||||
});
|
||||
}
|
||||
|
||||
public List<Punishment> getPunishments(UUID uuid)
|
||||
{
|
||||
List<Punishment> punishments = Lists.newArrayList();
|
||||
try (Connection con = Plex.get().getSqlConnection().getCon())
|
||||
{
|
||||
PreparedStatement statement = con.prepareStatement(SELECT);
|
||||
statement.setString(1, uuid.toString());
|
||||
ResultSet set = statement.executeQuery();
|
||||
while (set.next())
|
||||
{
|
||||
Punishment punishment = new Punishment(UUID.fromString(set.getString("punished")), set.getString("punisher") == null ? null : UUID.fromString(set.getString("punisher")));
|
||||
punishment.setActive(set.getBoolean("active"));
|
||||
punishment.setType(PunishmentType.valueOf(set.getString("type")));
|
||||
punishment.setCustomTime(set.getBoolean("customTime"));
|
||||
punishment.setPunishedUsername(set.getString("punishedUsername"));
|
||||
punishment.setEndDate(LocalDateTime.ofInstant(Instant.ofEpochMilli(set.getLong("endDate")), ZoneId.systemDefault()));
|
||||
punishment.setReason(set.getString("reason"));
|
||||
punishment.setIp(set.getString("ip"));
|
||||
punishments.add(punishment);
|
||||
}
|
||||
} catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return punishments;
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> insertPunishment(Punishment punishment)
|
||||
{
|
||||
|
||||
return CompletableFuture.runAsync(() ->
|
||||
{
|
||||
try (Connection con = Plex.get().getSqlConnection().getCon())
|
||||
{
|
||||
PlexLog.debug("Running execute punishment on " + punishment.getPunished().toString());
|
||||
PreparedStatement statement = con.prepareStatement(INSERT);
|
||||
statement.setString(1, punishment.getPunished().toString());
|
||||
statement.setString(2, punishment.getPunisher() == null ? null : punishment.getPunisher().toString());
|
||||
statement.setString(3, punishment.getPunishedUsername());
|
||||
statement.setString(4, punishment.getIp());
|
||||
statement.setString(5, punishment.getType().name());
|
||||
statement.setString(6, punishment.getReason());
|
||||
statement.setBoolean(7, punishment.isCustomTime());
|
||||
statement.setBoolean(8, punishment.isActive());
|
||||
statement.setLong(9, punishment.getEndDate().toInstant(ZoneOffset.UTC).toEpochMilli());
|
||||
PlexLog.debug("Executing punishment");
|
||||
statement.execute();
|
||||
} catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> removeBan(UUID uuid)
|
||||
{
|
||||
return CompletableFuture.runAsync(() ->
|
||||
{
|
||||
try (Connection con = Plex.get().getSqlConnection().getCon())
|
||||
{
|
||||
PreparedStatement statement = con.prepareStatement(UPDATE_BAN);
|
||||
statement.setBoolean(1, false);
|
||||
statement.setBoolean(2, true);
|
||||
statement.setString(3, uuid.toString());
|
||||
statement.setString(4, PunishmentType.BAN.name());
|
||||
statement.executeUpdate();
|
||||
} catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user