From 046db4986b94e9e1f6bb6a69875d64dc48148fe1 Mon Sep 17 00:00:00 2001 From: Derek Chen Date: Sun, 19 Oct 2025 16:36:37 -0500 Subject: [PATCH 1/2] feat: purge command adds entities --- .../net/coreprotect/command/PurgeCommand.java | 64 ++++++++++++------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/src/main/java/net/coreprotect/command/PurgeCommand.java b/src/main/java/net/coreprotect/command/PurgeCommand.java index daf6e56f1..473253a2f 100755 --- a/src/main/java/net/coreprotect/command/PurgeCommand.java +++ b/src/main/java/net/coreprotect/command/PurgeCommand.java @@ -158,10 +158,7 @@ else if (restrictTarget instanceof EntityType) { includeEntity = includeListEntity.toString(); } - if (entity) { - Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.ACTION_NOT_SUPPORTED)); - return; - } + // allow entity restrictions for purge (handled below per-table) boolean optimizeCheck = false; for (String arg : args) { @@ -171,11 +168,13 @@ else if (restrictTarget instanceof EntityType) { } } - final StringBuilder restrictTargets = restrict; - final String includeBlockFinal = includeBlock; - final boolean optimize = optimizeCheck; - final boolean hasBlockRestriction = hasBlock; - final int restrictCountFinal = restrictCount; + final StringBuilder restrictTargets = restrict; + final String includeBlockFinal = includeBlock; + final String includeEntityFinal = includeEntity; + final boolean optimize = optimizeCheck; + final boolean hasBlockRestriction = hasBlock; + final boolean hasEntityRestriction = entity; + final int restrictCountFinal = restrictCount; class BasicThread implements Runnable { @@ -209,7 +208,7 @@ public void run() { Chat.sendGlobalMessage(player, Phrase.build(Phrase.PURGE_STARTED, "#global")); } - if (hasBlockRestriction) { + if (hasBlockRestriction || hasEntityRestriction) { Chat.sendGlobalMessage(player, Phrase.build(Phrase.ROLLBACK_INCLUDE, restrictTargets.toString(), Selector.FIRST, Selector.FIRST, (restrictCountFinal == 1 ? Selector.FIRST : Selector.SECOND))); // include } @@ -262,7 +261,8 @@ public void run() { List purgeTables = Arrays.asList("sign", "container", "item", "skull", "session", "chat", "command", "entity", "block"); List worldTables = Arrays.asList("sign", "container", "item", "session", "chat", "command", "block"); - List restrictTables = Arrays.asList("block"); + // restrictTables: tables which support a "type" column for filtering + List restrictTables = Arrays.asList("block", "entity"); List excludeTables = Arrays.asList("database_lock"); // don't insert data into these tables for (String table : ConfigHandler.databaseTables) { String tableName = table.replaceAll("_", " "); @@ -290,19 +290,30 @@ public void run() { boolean purge = true; String timeLimit = ""; if (purgeTables.contains(table)) { - String blockRestriction = "("; - if (hasBlockRestriction && restrictTables.contains(table)) { - blockRestriction = "type NOT IN(" + includeBlockFinal + ") OR (type IN(" + includeBlockFinal + ") AND "; + String typeRestriction = "("; + if (restrictTables.contains(table)) { + if (table.equals("block") && hasBlockRestriction) { + typeRestriction = "type NOT IN(" + includeBlockFinal + ") OR (type IN(" + includeBlockFinal + ") AND "; + } + else if (table.equals("entity") && hasEntityRestriction) { + typeRestriction = "type NOT IN(" + includeEntityFinal + ") OR (type IN(" + includeEntityFinal + ") AND "; + } + else if ((table.equals("block") && hasBlockRestriction) || (table.equals("entity") && hasEntityRestriction)) { + // handled above + } + else { + purge = false; + } } - else if (hasBlockRestriction) { + else if ((hasBlockRestriction && table.equals("block")) || (hasEntityRestriction && table.equals("entity"))) { purge = false; } if (argWid > 0 && worldTables.contains(table)) { - timeLimit = " WHERE (" + blockRestriction + "wid = '" + argWid + "' AND (time >= '" + timeEnd + "' OR time < '" + timeStart + "'))) OR (wid != '" + argWid + "')"; + timeLimit = " WHERE (" + typeRestriction + "wid = '" + argWid + "' AND (time >= '" + timeEnd + "' OR time < '" + timeStart + "'))) OR (wid != '" + argWid + "')"; } else if (argWid == 0 && purge) { - timeLimit = " WHERE " + blockRestriction + "(time >= '" + timeEnd + "' OR time < '" + timeStart + "'))"; + timeLimit = " WHERE " + typeRestriction + "(time >= '" + timeEnd + "' OR time < '" + timeStart + "'))"; } } query = "INSERT INTO " + purgePrefix + table + " SELECT " + columns + " FROM " + ConfigHandler.prefix + table + timeLimit; @@ -356,12 +367,17 @@ else if (argWid == 0 && purge) { try { boolean purge = purgeTables.contains(table); - String blockRestriction = ""; - if (hasBlockRestriction && restrictTables.contains(table)) { - blockRestriction = "type IN(" + includeBlockFinal + ") AND "; - } - else if (hasBlockRestriction) { - purge = false; + String typeRestriction = ""; + if (restrictTables.contains(table)) { + if (table.equals("block") && hasBlockRestriction) { + typeRestriction = "type IN(" + includeBlockFinal + ") AND "; + } + else if (table.equals("entity") && hasEntityRestriction) { + typeRestriction = "type IN(" + includeEntityFinal + ") AND "; + } + else { + purge = false; + } } String worldRestriction = ""; @@ -373,7 +389,7 @@ else if (argWid > 0) { } if (purge) { - query = "DELETE FROM " + purgePrefix + table + " WHERE " + blockRestriction + "time < '" + timeEnd + "' AND time >= '" + timeStart + "'" + worldRestriction; + query = "DELETE FROM " + purgePrefix + table + " WHERE " + typeRestriction + "time < '" + timeEnd + "' AND time >= '" + timeStart + "'" + worldRestriction; preparedStmt = connection.prepareStatement(query); preparedStmt.execute(); preparedStmt.close(); From f691acb3f1fcbace6626d5a52e9dd1be7f7079f3 Mon Sep 17 00:00:00 2001 From: Derek Chen Date: Tue, 21 Oct 2025 16:22:58 -0500 Subject: [PATCH 2/2] chore: variable refactor --- src/main/java/net/coreprotect/command/PurgeCommand.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/coreprotect/command/PurgeCommand.java b/src/main/java/net/coreprotect/command/PurgeCommand.java index 473253a2f..32073ed20 100755 --- a/src/main/java/net/coreprotect/command/PurgeCommand.java +++ b/src/main/java/net/coreprotect/command/PurgeCommand.java @@ -133,11 +133,12 @@ else if (endTime < 86400) { } else if (restrictTarget instanceof EntityType) { targetName = ((EntityType) restrictTarget).name(); + int entityIdResolved = EntityUtils.getEntityId(targetName, false); if (includeListEntity.length() == 0) { - includeListEntity = includeListEntity.append(EntityUtils.getEntityId(targetName, false)); + includeListEntity = includeListEntity.append(entityIdResolved); } else { - includeListEntity.append(",").append(EntityUtils.getEntityId(targetName, false)); + includeListEntity.append(",").append(entityIdResolved); } targetName = ((EntityType) restrictTarget).name().toLowerCase(Locale.ROOT); @@ -158,8 +159,6 @@ else if (restrictTarget instanceof EntityType) { includeEntity = includeListEntity.toString(); } - // allow entity restrictions for purge (handled below per-table) - boolean optimizeCheck = false; for (String arg : args) { if (arg.trim().equalsIgnoreCase("#optimize")) {