From c5d5308753a23c7dcc06247ec6e7bddfbdf6bcad Mon Sep 17 00:00:00 2001 From: Intelli Date: Tue, 15 Feb 2022 20:20:46 -0700 Subject: [PATCH] Minor cleanup in SessionLookup class --- .../net/coreprotect/api/SessionLookup.java | 46 ++++++++++--------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/src/main/java/net/coreprotect/api/SessionLookup.java b/src/main/java/net/coreprotect/api/SessionLookup.java index 33ae340..6050069 100644 --- a/src/main/java/net/coreprotect/api/SessionLookup.java +++ b/src/main/java/net/coreprotect/api/SessionLookup.java @@ -16,12 +16,16 @@ public class SessionLookup { public static final int ID = 0; + private SessionLookup() { + throw new IllegalStateException("API class"); + } + public static List performLookup(String user, int offset) { + List result = new ArrayList<>(); if (!Config.getGlobal().API_ENABLED) { - return null; + return result; } - List result = new ArrayList<>(); try (Connection connection = Database.getConnection(false, 1000)) { if (connection == null || user == null) { return result; @@ -39,28 +43,28 @@ public class SessionLookup { } int userId = ConfigHandler.playerIdCache.get(user.toLowerCase(Locale.ROOT)); - Statement statement = connection.createStatement(); - String query = "SELECT time,user,wid,x,y,z,action FROM " + ConfigHandler.prefix + "session WHERE user = '" + userId + "' AND time > '" + checkTime + "' ORDER BY rowid DESC"; - ResultSet results = statement.executeQuery(query); - while (results.next()) { - String resultTime = results.getString("time"); - int resultUserId = results.getInt("user"); - String resultWorldId = results.getString("wid"); - String resultX = results.getString("x"); - String resultY = results.getString("y"); - String resultZ = results.getString("z"); - String resultAction = results.getString("action"); + try (Statement statement = connection.createStatement()) { + String query = "SELECT time,user,wid,x,y,z,action FROM " + ConfigHandler.prefix + "session WHERE user = '" + userId + "' AND time > '" + checkTime + "' ORDER BY rowid DESC"; + ResultSet results = statement.executeQuery(query); + while (results.next()) { + String resultTime = results.getString("time"); + int resultUserId = results.getInt("user"); + String resultWorldId = results.getString("wid"); + String resultX = results.getString("x"); + String resultY = results.getString("y"); + String resultZ = results.getString("z"); + String resultAction = results.getString("action"); - if (ConfigHandler.playerIdCacheReversed.get(resultUserId) == null) { - UserStatement.loadName(connection, resultUserId); + if (ConfigHandler.playerIdCacheReversed.get(resultUserId) == null) { + UserStatement.loadName(connection, resultUserId); + } + String resultUser = ConfigHandler.playerIdCacheReversed.get(resultUserId); + + String[] lookupData = new String[] { resultTime, resultUser, resultX, resultY, resultZ, resultWorldId, type, resultAction }; + result.add(lookupData); } - String resultUser = ConfigHandler.playerIdCacheReversed.get(resultUserId); - - String[] lookupData = new String[] { resultTime, resultUser, resultX, resultY, resultZ, resultWorldId, type, resultAction }; - result.add(lookupData); + results.close(); } - results.close(); - statement.close(); } catch (Exception e) { e.printStackTrace();