diff --git a/src/main/java/dev/heliosares/auxprotect/core/commands/XrayCommand.java b/src/main/java/dev/heliosares/auxprotect/core/commands/XrayCommand.java index 5952c08..10befd2 100644 --- a/src/main/java/dev/heliosares/auxprotect/core/commands/XrayCommand.java +++ b/src/main/java/dev/heliosares/auxprotect/core/commands/XrayCommand.java @@ -82,8 +82,7 @@ public class XrayCommand extends Command { if (time > 0) { ArrayList entries; try { - entries = plugin.getSqlManager().getLookupManager().lookup(plugin.getSqlManager(), - Table.AUXPROTECT_XRAY, + entries = plugin.getSqlManager().getLookupManager().lookup(Table.AUXPROTECT_XRAY, "SELECT * FROM " + Table.AUXPROTECT_XRAY + " WHERE time = " + time, null); } catch (LookupException e) { plugin.print(e); @@ -94,7 +93,7 @@ public class XrayCommand extends Command { sender.sendLang(Language.L.XRAY_TOOMANY); return; } - if (entries.size() == 0 && !override) { + if (entries.isEmpty() && !override) { sender.sendLang(Language.L.XRAY_NOTFOUND); return; } diff --git a/src/main/java/dev/heliosares/auxprotect/database/DbEntry.java b/src/main/java/dev/heliosares/auxprotect/database/DbEntry.java index 3b5d925..6051b66 100644 --- a/src/main/java/dev/heliosares/auxprotect/database/DbEntry.java +++ b/src/main/java/dev/heliosares/auxprotect/database/DbEntry.java @@ -8,6 +8,20 @@ import java.sql.SQLException; public class DbEntry { + protected SQLManager getSql() { + return sql; + } + + void setSql(SQLManager sql) { + this.sql = sql; + } + + public void deResolveUIDs() { + uid = -1; + target_id = -1; + } + + protected SQLManager sql; protected final String world; protected final int x; protected final int y; @@ -30,7 +44,7 @@ public class DbEntry { private byte[] blob; private DbEntry(String userLabel, EntryAction action, boolean state, String world, int x, int y, int z, int pitch, - int yaw, String targetLabel, String data) { + int yaw, String targetLabel, String data, SQLManager sql) { this.time = DatabaseRunnable.getTime(action.getTable()); this.userLabel = userLabel; this.action = action; @@ -43,13 +57,14 @@ public class DbEntry { this.yaw = yaw; this.targetLabel = targetLabel; this.data = data; + this.sql = sql; } /** * */ public DbEntry(String userLabel, EntryAction action, boolean state, String targetLabel, String data) { - this(userLabel, action, state, null, 0, 0, 0, 0, 0, targetLabel, data); + this(userLabel, action, state, null, 0, 0, 0, 0, 0, targetLabel, data, SQLManager.getInstance()); } /** @@ -70,11 +85,11 @@ public class DbEntry { location == null ? 0 : location.getBlockX(), location == null ? 0 : location.getBlockY(), location == null ? 0 : location.getBlockZ(), location == null ? 0 : Math.round(location.getPitch()), - location == null ? 0 : Math.round(location.getYaw()), targetLabel, data); + location == null ? 0 : Math.round(location.getYaw()), targetLabel, data, SQLManager.getInstance()); } protected DbEntry(long time, int uid, EntryAction action, boolean state, String world, int x, int y, int z, - int pitch, int yaw, String target, int target_id, String data) { + int pitch, int yaw, String target, int target_id, String data, SQLManager sql) { this.time = time; this.uid = uid; this.action = action; @@ -88,6 +103,7 @@ public class DbEntry { this.targetLabel = target; this.target_id = target_id; this.data = data; + this.sql = sql; } public long getTime() { @@ -114,7 +130,7 @@ public class DbEntry { if (uid > 0) { return uid; } - return uid = SQLManager.getInstance().getUserManager().getUIDFromUUID(getUserUUID(), true, true); + return uid = sql.getUserManager().getUIDFromUUID(getUserUUID(), true, true); } public int getTargetId() throws SQLException, BusyException { @@ -124,7 +140,7 @@ public class DbEntry { if (target_id > 0) { return target_id; } - return target_id = SQLManager.getInstance().getUserManager().getUIDFromUUID(getTargetUUID(), true, true); + return target_id = sql.getUserManager().getUIDFromUUID(getTargetUUID(), true, true); } public String getUser() throws SQLException, BusyException { @@ -137,7 +153,7 @@ public class DbEntry { if (!getUserUUID().startsWith("$") || getUserUUID().length() != 37) { return user = getUserUUID(); } - user = SQLManager.getInstance().getUserManager().getUsernameFromUID(getUid(), false); + user = sql.getUserManager().getUsernameFromUID(getUid(), false); if (user == null) { user = getUserUUID(); } @@ -154,7 +170,7 @@ public class DbEntry { if (action.getTable().hasStringTarget() || !getTargetUUID().startsWith("$") || getTargetUUID().length() != 37) { return target = getTargetUUID(); } - target = SQLManager.getInstance().getUserManager().getUsernameFromUID(getTargetId(), false); + target = sql.getUserManager().getUsernameFromUID(getTargetId(), false); if (target == null) { target = getTargetUUID(); } @@ -166,7 +182,7 @@ public class DbEntry { return targetLabel; } if (target_id > 0) { - targetLabel = SQLManager.getInstance().getUserManager().getUUIDFromUID(target_id, false); + targetLabel = sql.getUserManager().getUUIDFromUID(target_id, false); } else if (target_id == 0) { return targetLabel = ""; } @@ -181,7 +197,7 @@ public class DbEntry { return userLabel; } if (uid > 0) { - userLabel = SQLManager.getInstance().getUserManager().getUUIDFromUID(uid, false); + userLabel = sql.getUserManager().getUUIDFromUID(uid, false); } else if (uid == 0) { return userLabel = ""; } @@ -207,7 +223,7 @@ public class DbEntry { } public byte[] getBlob() throws SQLException, BusyException { - if (blob == null) blob = SQLManager.getInstance().getBlob(this); + if (blob == null) blob = sql.getBlob(this); return blob; } diff --git a/src/main/java/dev/heliosares/auxprotect/database/DbEntryBukkit.java b/src/main/java/dev/heliosares/auxprotect/database/DbEntryBukkit.java index 2162966..faa1ca6 100644 --- a/src/main/java/dev/heliosares/auxprotect/database/DbEntryBukkit.java +++ b/src/main/java/dev/heliosares/auxprotect/database/DbEntryBukkit.java @@ -16,7 +16,7 @@ public class DbEntryBukkit extends DbEntry { } protected DbEntryBukkit(long time, int uid, EntryAction action, boolean state, String world, int x, int y, int z, int pitch, int yaw, String target, int target_id, String data) { - super(time, uid, action, state, world, x, y, z, pitch, yaw, target, target_id, data); + super(time, uid, action, state, world, x, y, z, pitch, yaw, target, target_id, data, SQLManager.getInstance()); } //TODO implement as adapter diff --git a/src/main/java/dev/heliosares/auxprotect/database/DbEntryGroup.java b/src/main/java/dev/heliosares/auxprotect/database/DbEntryGroup.java index 8120d67..760eb1b 100644 --- a/src/main/java/dev/heliosares/auxprotect/database/DbEntryGroup.java +++ b/src/main/java/dev/heliosares/auxprotect/database/DbEntryGroup.java @@ -15,7 +15,7 @@ public class DbEntryGroup extends DbEntry { private long lastTime; protected DbEntryGroup(DbEntry root, Parameters params) { - super(0, 0, EntryAction.GROUPING, false, root.getWorld(), root.getX(), root.getY(), root.getZ(), root.getPitch(), root.getYaw(), null, 0, ""); + super(0, 0, EntryAction.GROUPING, false, root.getWorld(), root.getX(), root.getY(), root.getZ(), root.getPitch(), root.getYaw(), null, 0, "", root.sql); if (root instanceof DbEntryGroup) throw new IllegalArgumentException("Tried to add a group to a group!"); this.range = Math.pow(params.getGroupRange(), 2); try { diff --git a/src/main/java/dev/heliosares/auxprotect/database/LookupManager.java b/src/main/java/dev/heliosares/auxprotect/database/LookupManager.java index 60767e9..a2e302e 100644 --- a/src/main/java/dev/heliosares/auxprotect/database/LookupManager.java +++ b/src/main/java/dev/heliosares/auxprotect/database/LookupManager.java @@ -38,7 +38,7 @@ public class LookupManager { stmt += "\nWHERE " + sqlstmts[0]; } stmt += "\nORDER BY time DESC\nLIMIT " + (SQLManager.MAX_LOOKUP_SIZE + 1) + ";"; - List out = lookup(sql, param.getTable(), stmt, writeparams); + List out = lookup(param.getTable(), stmt, writeparams); if (param.hasFlag(Parameters.Flag.PLAYBACK) || param.hasFlag(Parameters.Flag.INCREMENTAL_POS)) { try { @@ -106,7 +106,6 @@ public class LookupManager { /** * Performs a SQL Lookup in the table provided with the statement provided. * - * @param sqlManager TODO * @param table The table being utilized. This is not user in the * statement and is merely provided for entry parsing * @param stmt The statement to be executed @@ -116,7 +115,7 @@ public class LookupManager { * @see LookupManager#lookup(dev.heliosares.auxprotect.core.Parameters) */ @SuppressWarnings("deprecation") - public ArrayList lookup(SQLManager sqlManager, Table table, String stmt, ArrayList writeParams) + public ArrayList lookup(Table table, String stmt, ArrayList writeParams) throws LookupException { final boolean hasLocation = plugin.getPlatform() == PlatformType.SPIGOT && table.hasLocation(); final boolean hasData = table.hasData(); @@ -161,7 +160,7 @@ public class LookupManager { String world = null; int x = 0, y = 0, z = 0; if (hasLocation) { - world = sqlManager.getWorld(rs.getInt("world_id")); + world = sql.getWorld(rs.getInt("world_id")); x = rs.getInt("x"); y = rs.getInt("y"); z = rs.getInt("z"); @@ -210,7 +209,7 @@ public class LookupManager { } else if (table.hasBlobID() && table.hasItemMeta() && qty >= 0 && damage >= 0) { entry = new SingleItemEntry(time, uid, entryAction, state, world, x, y, z, pitch, yaw, target, target_id, data, qty, damage); } else { - entry = new DbEntry(time, uid, entryAction, state, world, x, y, z, pitch, yaw, target, target_id, data); + entry = new DbEntry(time, uid, entryAction, state, world, x, y, z, pitch, yaw, target, target_id, data, sql); } if (table.hasBlobID()) { @@ -235,7 +234,6 @@ public class LookupManager { plugin.debug("Completed lookup. Total: " + (System.currentTimeMillis() - lookupStart) + "ms Lookup: " + (parseStart - lookupStart) + "ms Parse: " + (System.currentTimeMillis() - parseStart) + "ms", 1); }, 3000L); } catch (BusyException e) { - e.printStackTrace(); throw new LookupException(Language.L.DATABASE_BUSY); } catch (SQLException e) { plugin.warning("Error while executing command"); diff --git a/src/main/java/dev/heliosares/auxprotect/database/MigrationManager.java b/src/main/java/dev/heliosares/auxprotect/database/MigrationManager.java index f8f5fa2..334c931 100644 --- a/src/main/java/dev/heliosares/auxprotect/database/MigrationManager.java +++ b/src/main/java/dev/heliosares/auxprotect/database/MigrationManager.java @@ -442,7 +442,7 @@ public class MigrationManager { migrationActions.put(14, new MigrationAction(sql.isMySQL(), () -> { }, () -> { for (Table table : Table.values()) { - if (!table.hasStringTarget() && !table.hasData()) continue; + if (!table.hasStringTarget() && !table.hasData() || !table.exists(plugin)) continue; sql.execute("ALTER TABLE " + table + " CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;", connection); } })); diff --git a/src/main/java/dev/heliosares/auxprotect/database/PosEntry.java b/src/main/java/dev/heliosares/auxprotect/database/PosEntry.java index 37207df..1dbae1a 100644 --- a/src/main/java/dev/heliosares/auxprotect/database/PosEntry.java +++ b/src/main/java/dev/heliosares/auxprotect/database/PosEntry.java @@ -1,6 +1,5 @@ package dev.heliosares.auxprotect.database; -import dev.heliosares.auxprotect.utils.PosEncoder; import org.bukkit.Location; import java.util.Objects; @@ -20,7 +19,7 @@ public class PosEntry extends DbEntry { } protected PosEntry(long time, int uid, EntryAction action, boolean state, String world, int x, int y, int z, byte increment, int pitch, int yaw, String target, int target_id, String data) { - super(time, uid, action, state, world, x, y, z, pitch, yaw, target, target_id, data); + super(time, uid, action, state, world, x, y, z, pitch, yaw, target, target_id, data, SQLManager.getInstance()); double[] dInc = byteToFractions(increment); this.x = x + dInc[0]; this.y = y + dInc[1]; @@ -30,7 +29,7 @@ public class PosEntry extends DbEntry { public PosEntry(long time, int uid, Location location) { super(time, uid, EntryAction.POS, false, Objects.requireNonNull(location.getWorld()).getName(), (int) Math.round(location.getX()), (int) Math.round(location.getY()), (int) Math.round(location.getZ()), - Math.round(location.getPitch()), Math.round(location.getYaw()), "", -1, ""); + Math.round(location.getPitch()), Math.round(location.getYaw()), "", -1, "", SQLManager.getInstance()); this.x = location.getX(); this.y = location.getY(); this.z = location.getZ(); diff --git a/src/main/java/dev/heliosares/auxprotect/database/SQLManager.java b/src/main/java/dev/heliosares/auxprotect/database/SQLManager.java index 9c1b214..aebfd60 100644 --- a/src/main/java/dev/heliosares/auxprotect/database/SQLManager.java +++ b/src/main/java/dev/heliosares/auxprotect/database/SQLManager.java @@ -567,7 +567,7 @@ public class SQLManager extends ConnectionPool { stmt += " AND time>" + since; } try { - return lookupmanager.lookup(this, Table.AUXPROTECT_XRAY, stmt, null); + return lookupmanager.lookup(Table.AUXPROTECT_XRAY, stmt, null); } catch (LookupException e) { plugin.print(e); } diff --git a/src/main/java/dev/heliosares/auxprotect/database/SingleItemEntry.java b/src/main/java/dev/heliosares/auxprotect/database/SingleItemEntry.java index d0e638e..d30ea48 100644 --- a/src/main/java/dev/heliosares/auxprotect/database/SingleItemEntry.java +++ b/src/main/java/dev/heliosares/auxprotect/database/SingleItemEntry.java @@ -18,8 +18,7 @@ public class SingleItemEntry extends DbEntry { @Nullable private ItemStack item; - public SingleItemEntry(String userLabel, EntryAction action, boolean state, Location location, String targetLabel, - String data, @Nullable ItemStack item) { + public SingleItemEntry(String userLabel, EntryAction action, boolean state, Location location, String targetLabel, String data, @Nullable ItemStack item) { super(userLabel, action, state, location, targetLabel, data); if (item != null) { item = item.clone(); @@ -43,9 +42,8 @@ public class SingleItemEntry extends DbEntry { } } - protected SingleItemEntry(long time, int uid, EntryAction action, boolean state, String world, int x, int y, int z, - int pitch, int yaw, String target, int target_id, String data, int qty, int damage) { - super(time, uid, action, state, world, x, y, z, pitch, yaw, target, target_id, data); + protected SingleItemEntry(long time, int uid, EntryAction action, boolean state, String world, int x, int y, int z, int pitch, int yaw, String target, int target_id, String data, int qty, int damage) { + super(time, uid, action, state, world, x, y, z, pitch, yaw, target, target_id, data, SQLManager.getInstance()); this.qty = qty; this.damage = damage; } diff --git a/src/main/java/dev/heliosares/auxprotect/database/XrayEntry.java b/src/main/java/dev/heliosares/auxprotect/database/XrayEntry.java index a498d4d..7c5ecd5 100644 --- a/src/main/java/dev/heliosares/auxprotect/database/XrayEntry.java +++ b/src/main/java/dev/heliosares/auxprotect/database/XrayEntry.java @@ -21,7 +21,7 @@ public class XrayEntry extends DbEntry { } public XrayEntry(long time, int uid, String world, int x, int y, int z, int target_id, short rating, String data) { - super(time, uid, EntryAction.VEIN, false, world, x, y, z, 0, 0, null, target_id, data); + super(time, uid, EntryAction.VEIN, false, world, x, y, z, 0, 0, null, target_id, data, SQLManager.getInstance()); this.rating = rating; } diff --git a/src/main/java/dev/heliosares/auxprotect/towny/TownyEntry.java b/src/main/java/dev/heliosares/auxprotect/towny/TownyEntry.java index 39904c3..531fbc1 100644 --- a/src/main/java/dev/heliosares/auxprotect/towny/TownyEntry.java +++ b/src/main/java/dev/heliosares/auxprotect/towny/TownyEntry.java @@ -21,7 +21,7 @@ public class TownyEntry extends DbEntry { public TownyEntry(long time, int uid, EntryAction action, boolean state, String world, int x, int y, int z, int pitch, int yaw, String target, int target_id, String data) { - super(time, uid, action, state, world, x, y, z, pitch, yaw, target, target_id, data); + super(time, uid, action, state, world, x, y, z, pitch, yaw, target, target_id, data, SQLManager.getInstance()); } @Override