From 25b03511c28b67dc3b3ff928e48e458ee04112bc Mon Sep 17 00:00:00 2001 From: Intelli Date: Tue, 15 Feb 2022 19:59:25 -0700 Subject: [PATCH] Added sessionLookup method to API --- .../java/net/coreprotect/CoreProtectAPI.java | 33 ++++++++- .../net/coreprotect/api/SessionLookup.java | 72 +++++++++++++++++++ 2 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 src/main/java/net/coreprotect/api/SessionLookup.java diff --git a/src/main/java/net/coreprotect/CoreProtectAPI.java b/src/main/java/net/coreprotect/CoreProtectAPI.java index e7cc38c..d51bc99 100755 --- a/src/main/java/net/coreprotect/CoreProtectAPI.java +++ b/src/main/java/net/coreprotect/CoreProtectAPI.java @@ -16,6 +16,7 @@ import org.bukkit.block.data.BlockData; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; +import net.coreprotect.api.SessionLookup; import net.coreprotect.config.Config; import net.coreprotect.consumer.Queue; import net.coreprotect.database.Database; @@ -42,8 +43,18 @@ public class CoreProtectAPI extends Queue { public String getActionString() { int actionID = Integer.parseInt(parse[7]); - String result = "unknown"; + if (parse.length < 13 && Integer.parseInt(parse[6]) == SessionLookup.ID) { + switch (actionID) { + case 0: + return "logout"; + case 1: + return "login"; + default: + return "unknown"; + } + } + String result = "unknown"; if (actionID == 0) { result = "break"; } @@ -79,6 +90,10 @@ public class CoreProtectAPI extends Queue { } public Material getType() { + if (parse.length < 13) { + return null; + } + int actionID = this.getActionId(); int type = Integer.parseInt(parse[5]); String typeName; @@ -95,8 +110,12 @@ public class CoreProtectAPI extends Queue { } public BlockData getBlockData() { + if (parse.length < 13) { + return null; + } + String blockData = parse[12]; - if (blockData.length() == 0) { + if (blockData == null || blockData.length() == 0) { return getType().createBlockData(); } return Bukkit.getServer().createBlockData(blockData); @@ -115,11 +134,15 @@ public class CoreProtectAPI extends Queue { } public boolean isRolledBack() { + if (parse.length < 13) { + return false; + } + return Integer.parseInt(parse[8]) == 1; } public String worldName() { - return Util.getWorldName(Integer.parseInt(parse[9])); + return Util.getWorldName(Integer.parseInt(parse.length < 13 ? parse[5] : parse[9])); } } @@ -152,6 +175,10 @@ public class CoreProtectAPI extends Queue { return null; } + public List sessionLookup(String user, int time) { + return SessionLookup.performLookup(user, time); + } + public boolean hasPlaced(String user, Block block, int time, int offset) { // Determine if a user has placed a block at this location in the last # of seconds. boolean match = false; diff --git a/src/main/java/net/coreprotect/api/SessionLookup.java b/src/main/java/net/coreprotect/api/SessionLookup.java new file mode 100644 index 0000000..33ae340 --- /dev/null +++ b/src/main/java/net/coreprotect/api/SessionLookup.java @@ -0,0 +1,72 @@ +package net.coreprotect.api; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; + +import net.coreprotect.config.Config; +import net.coreprotect.config.ConfigHandler; +import net.coreprotect.database.Database; +import net.coreprotect.database.statement.UserStatement; + +public class SessionLookup { + + public static final int ID = 0; + + public static List performLookup(String user, int offset) { + if (!Config.getGlobal().API_ENABLED) { + return null; + } + + List result = new ArrayList<>(); + try (Connection connection = Database.getConnection(false, 1000)) { + if (connection == null || user == null) { + return result; + } + + String type = String.valueOf(ID); + int time = (int) (System.currentTimeMillis() / 1000L); + int checkTime = 0; + if (offset > 0) { + checkTime = time - offset; + } + + if (ConfigHandler.playerIdCache.get(user.toLowerCase(Locale.ROOT)) == null) { + UserStatement.loadId(connection, user, null); + } + 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"); + + 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); + } + results.close(); + statement.close(); + } + catch (Exception e) { + e.printStackTrace(); + } + + return result; + } + +}