From 02dcc873d620e7cb82d95b95d5c3f5653c7f5023 Mon Sep 17 00:00:00 2001 From: Intelli Date: Mon, 7 Mar 2022 16:39:21 -0700 Subject: [PATCH] Added hidden "exclude-tnt" rollback option to exclude TNT blocks (default: false) --- .../java/net/coreprotect/CoreProtectAPI.java | 15 ++++++----- .../coreprotect/command/CommandHandler.java | 26 ++++++++++++------- .../coreprotect/command/LookupCommand.java | 13 +++++----- .../command/RollbackRestoreCommand.java | 16 +++++++----- .../java/net/coreprotect/config/Config.java | 2 ++ .../net/coreprotect/config/ConfigHandler.java | 2 +- .../database/ContainerRollback.java | 3 ++- .../java/net/coreprotect/database/Lookup.java | 13 +++++----- .../net/coreprotect/database/Rollback.java | 19 ++++++++++---- 9 files changed, 68 insertions(+), 41 deletions(-) diff --git a/src/main/java/net/coreprotect/CoreProtectAPI.java b/src/main/java/net/coreprotect/CoreProtectAPI.java index e41dbba..52440d0 100755 --- a/src/main/java/net/coreprotect/CoreProtectAPI.java +++ b/src/main/java/net/coreprotect/CoreProtectAPI.java @@ -3,8 +3,10 @@ package net.coreprotect; import java.sql.Connection; import java.sql.Statement; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Locale; +import java.util.Map; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -147,17 +149,17 @@ public class CoreProtectAPI extends Queue { } } - private static List parseList(List list) { - List result = new ArrayList<>(); + private static Map parseList(List list) { + Map result = new HashMap<>(); if (list != null) { for (Object value : list) { if (value instanceof Material || value instanceof EntityType) { - result.add(value); + result.put(value, false); } else if (value instanceof Integer) { Material material = Util.getType((Integer) value); - result.add(material); + result.put(material, false); } } } @@ -417,7 +419,7 @@ public class CoreProtectAPI extends Queue { return null; } - private List processData(int time, int radius, Location location, List restrictBlocks, List excludeBlocks, List restrictUsers, List excludeUsers, List actionList, int action, int lookup, int offset, int rowCount, boolean useLimit) { + private List processData(int time, int radius, Location location, Map restrictBlocksMap, Map excludeBlocks, List restrictUsers, List excludeUsers, List actionList, int action, int lookup, int offset, int rowCount, boolean useLimit) { // You need to either specify time/radius or time/user List result = new ArrayList<>(); List uuids = new ArrayList<>(); @@ -434,6 +436,7 @@ public class CoreProtectAPI extends Queue { actionList = new ArrayList<>(); } + List restrictBlocks = new ArrayList<>(restrictBlocksMap.keySet()); if (actionList.size() == 0 && restrictBlocks.size() > 0) { boolean addedMaterial = false; boolean addedEntity = false; @@ -530,7 +533,7 @@ public class CoreProtectAPI extends Queue { } @Deprecated - private List processData(String user, int time, int radius, Location location, List restrictBlocks, List excludeBlocks, int action, int lookup, int offset, int rowCount, boolean useLimit) { + private List processData(String user, int time, int radius, Location location, Map restrictBlocks, Map excludeBlocks, int action, int lookup, int offset, int rowCount, boolean useLimit) { ArrayList restrictUsers = new ArrayList<>(); if (user != null) { restrictUsers.add(user); diff --git a/src/main/java/net/coreprotect/command/CommandHandler.java b/src/main/java/net/coreprotect/command/CommandHandler.java index efdf045..dfde176 100755 --- a/src/main/java/net/coreprotect/command/CommandHandler.java +++ b/src/main/java/net/coreprotect/command/CommandHandler.java @@ -3,8 +3,10 @@ package net.coreprotect.command; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -255,9 +257,9 @@ public class CommandHandler implements CommandExecutor { return result; } - protected static List parseExcluded(CommandSender player, String[] inputArguments, List argAction) { + protected static Map parseExcluded(CommandSender player, String[] inputArguments, List argAction) { String[] argumentArray = inputArguments.clone(); - List excluded = new ArrayList<>(); + Map excluded = new HashMap<>(); int count = 0; int next = 0; for (String argument : argumentArray) { @@ -276,20 +278,22 @@ public class CommandHandler implements CommandExecutor { String[] i2 = argument.split(","); for (String i3 : i2) { if (i3.equals("#natural")) { - excluded.addAll(naturalBlocks); + for (Material block : naturalBlocks) { + excluded.put(block, false); + } } else { Material i3_material = Util.getType(i3); if (i3_material != null && (i3_material.isBlock() || argAction.contains(4))) { - excluded.add(i3_material); + excluded.put(i3_material, false); } else { EntityType i3_entity = Util.getEntityType(i3); if (i3_entity != null) { - excluded.add(i3_entity); + excluded.put(i3_entity, false); } else if (i3_material != null) { - excluded.add(i3_material); + excluded.put(i3_material, false); } } } @@ -303,20 +307,22 @@ public class CommandHandler implements CommandExecutor { } else { if (argument.equals("#natural")) { - excluded.addAll(naturalBlocks); + for (Material block : naturalBlocks) { + excluded.put(block, false); + } } else { Material iMaterial = Util.getType(argument); if (iMaterial != null && (iMaterial.isBlock() || argAction.contains(4))) { - excluded.add(iMaterial); + excluded.put(iMaterial, false); } else { EntityType iEntity = Util.getEntityType(argument); if (iEntity != null) { - excluded.add(iEntity); + excluded.put(iEntity, false); } else if (iMaterial != null) { - excluded.add(iMaterial); + excluded.put(iMaterial, false); } } } diff --git a/src/main/java/net/coreprotect/command/LookupCommand.java b/src/main/java/net/coreprotect/command/LookupCommand.java index b5e2f5c..21995ea 100755 --- a/src/main/java/net/coreprotect/command/LookupCommand.java +++ b/src/main/java/net/coreprotect/command/LookupCommand.java @@ -6,6 +6,7 @@ import java.text.NumberFormat; import java.util.ArrayList; import java.util.List; import java.util.Locale; +import java.util.Map; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -49,7 +50,7 @@ public class LookupCommand { int argNoisy = CommandHandler.parseNoisy(args); List argAction = CommandHandler.parseAction(args); List argBlocks = CommandHandler.parseRestricted(player, args, argAction); - List argExclude = CommandHandler.parseExcluded(player, args, argAction); + Map argExclude = CommandHandler.parseExcluded(player, args, argAction); List argExcludeUsers = CommandHandler.parseExcludedUsers(player, args); String ts = CommandHandler.parseTimeString(args); long[] argTime = CommandHandler.parseTime(args); @@ -94,7 +95,7 @@ public class LookupCommand { } /* check for invalid block/entity combinations (exclude) */ - for (Object arg : argExclude) { + for (Object arg : argExclude.keySet()) { if (arg instanceof Material) { hasBlock = true; } @@ -266,9 +267,9 @@ public class LookupCommand { } if (argAction.contains(4) && argAction.contains(11)) { // a:inventory - argExclude.add(Material.FIRE); - argExclude.add(Material.WATER); - argExclude.add(Material.FARMLAND); + argExclude.put(Material.FIRE, false); + argExclude.put(Material.WATER, false); + argExclude.put(Material.FARMLAND, false); argExcludeUsers.add("#hopper"); } @@ -695,7 +696,7 @@ public class LookupCommand { final int restricted = argRestricted; // final List uuid_list = arg_uuids; final List blist = argBlocks; - final List elist = argExclude; + final Map elist = argExclude; final List euserlist = argExcludeUsers; final int page = pa; final int displayResults = re; diff --git a/src/main/java/net/coreprotect/command/RollbackRestoreCommand.java b/src/main/java/net/coreprotect/command/RollbackRestoreCommand.java index d1f118d..59ddc5d 100755 --- a/src/main/java/net/coreprotect/command/RollbackRestoreCommand.java +++ b/src/main/java/net/coreprotect/command/RollbackRestoreCommand.java @@ -5,6 +5,7 @@ import java.sql.Statement; import java.util.ArrayList; import java.util.List; import java.util.Locale; +import java.util.Map; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -39,7 +40,7 @@ public class RollbackRestoreCommand { int argNoisy = CommandHandler.parseNoisy(args); List argAction = CommandHandler.parseAction(args); List argBlocks = CommandHandler.parseRestricted(player, args, argAction); - List argExclude = CommandHandler.parseExcluded(player, args, argAction); + Map argExclude = CommandHandler.parseExcluded(player, args, argAction); List argExcludeUsers = CommandHandler.parseExcludedUsers(player, args); String ts = CommandHandler.parseTimeString(args); long[] argTime = CommandHandler.parseTime(args); @@ -76,7 +77,7 @@ public class RollbackRestoreCommand { } /* check for invalid block/entity combinations (exclude) */ - for (Object arg : argExclude) { + for (Object arg : argExclude.keySet()) { if (arg instanceof Material) { hasBlock = true; } @@ -186,11 +187,14 @@ public class RollbackRestoreCommand { return; } - argExclude.add(Material.FIRE); - argExclude.add(Material.WATER); - argExclude.add(Material.FARMLAND); + argExclude.put(Material.FIRE, false); + argExclude.put(Material.WATER, false); + argExclude.put(Material.FARMLAND, false); argExcludeUsers.add("#hopper"); } + else if (!argAction.contains(4) && Config.getGlobal().EXCLUDE_TNT && !argExclude.containsKey(Material.TNT) && !argBlocks.contains(Material.TNT)) { + argExclude.put(Material.TNT, true); + } if (g == 1 && (argUsers.size() > 0 || (argUsers.size() == 0 && argRadius != null))) { Integer MAX_RADIUS = Config.getGlobal().MAX_RADIUS; @@ -329,7 +333,7 @@ public class RollbackRestoreCommand { final String rtime = ts; final List uuidList = argUuids; final List blist = argBlocks; - final List elist = argExclude; + final Map elist = argExclude; final List euserlist = argExcludeUsers; final Location locationFinal = lo; final int finalArgWid = argWid; diff --git a/src/main/java/net/coreprotect/config/Config.java b/src/main/java/net/coreprotect/config/Config.java index 4d7ee68..f6f9251 100644 --- a/src/main/java/net/coreprotect/config/Config.java +++ b/src/main/java/net/coreprotect/config/Config.java @@ -46,6 +46,7 @@ public class Config extends Language { public boolean DATABASE_LOCK; public boolean LOG_CANCELLED_CHAT; public boolean HOPPER_FILTER_META; + public boolean EXCLUDE_TNT; public boolean MYSQL; public boolean CHECK_UPDATES; public boolean API_ENABLED; @@ -185,6 +186,7 @@ public class Config extends Language { this.DATABASE_LOCK = this.getBoolean("database-lock", true); this.LOG_CANCELLED_CHAT = this.getBoolean("log-cancelled-chat", true); this.HOPPER_FILTER_META = this.getBoolean("hopper-filter-meta", false); + this.EXCLUDE_TNT = this.getBoolean("exclude-tnt", false); this.DONATION_KEY = this.getString("donation-key"); this.MYSQL = this.getBoolean("use-mysql"); this.PREFIX = this.getString("table-prefix"); diff --git a/src/main/java/net/coreprotect/config/ConfigHandler.java b/src/main/java/net/coreprotect/config/ConfigHandler.java index 35403c2..1bd0a75 100644 --- a/src/main/java/net/coreprotect/config/ConfigHandler.java +++ b/src/main/java/net/coreprotect/config/ConfigHandler.java @@ -105,7 +105,7 @@ public class ConfigHandler extends Queue { public static Map lookupPage = syncMap(); public static Map lookupCommand = syncMap(); public static Map> lookupBlist = syncMap(); - public static Map> lookupElist = syncMap(); + public static Map> lookupElist = syncMap(); public static Map> lookupEUserlist = syncMap(); public static Map> lookupUlist = syncMap(); public static Map> lookupAlist = syncMap(); diff --git a/src/main/java/net/coreprotect/database/ContainerRollback.java b/src/main/java/net/coreprotect/database/ContainerRollback.java index 7b7756d..93b4ae1 100644 --- a/src/main/java/net/coreprotect/database/ContainerRollback.java +++ b/src/main/java/net/coreprotect/database/ContainerRollback.java @@ -4,6 +4,7 @@ import java.sql.Statement; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Map; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -28,7 +29,7 @@ import net.coreprotect.utility.Util; public class ContainerRollback extends Queue { - public static void performContainerRollbackRestore(Statement statement, CommandSender user, List checkUuids, List checkUsers, String timeString, List restrictList, List excludeList, List excludeUserList, List actionList, final Location location, Integer[] radius, long startTime, long endTime, boolean restrictWorld, boolean lookup, boolean verbose, final int rollbackType) { + public static void performContainerRollbackRestore(Statement statement, CommandSender user, List checkUuids, List checkUsers, String timeString, List restrictList, Map excludeList, List excludeUserList, List actionList, final Location location, Integer[] radius, long startTime, long endTime, boolean restrictWorld, boolean lookup, boolean verbose, final int rollbackType) { try { long timeStart = System.currentTimeMillis(); diff --git a/src/main/java/net/coreprotect/database/Lookup.java b/src/main/java/net/coreprotect/database/Lookup.java index 34e24c5..de483f0 100755 --- a/src/main/java/net/coreprotect/database/Lookup.java +++ b/src/main/java/net/coreprotect/database/Lookup.java @@ -6,6 +6,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Locale; +import java.util.Map; import org.bukkit.Location; import org.bukkit.Material; @@ -75,7 +76,7 @@ public class Lookup extends Queue { return newList; } - public static long countLookupRows(Statement statement, CommandSender user, List checkUuids, List checkUsers, List restrictList, List excludeList, List excludeUserList, List actionList, Location location, Integer[] radius, Long[] rowData, long startTime, long endTime, boolean restrictWorld, boolean lookup) { + public static long countLookupRows(Statement statement, CommandSender user, List checkUuids, List checkUsers, List restrictList, Map excludeList, List excludeUserList, List actionList, Location location, Integer[] radius, Long[] rowData, long startTime, long endTime, boolean restrictWorld, boolean lookup) { Long rows = 0L; try { @@ -102,7 +103,7 @@ public class Lookup extends Queue { return rows; } - public static List performLookup(Statement statement, CommandSender user, List checkUuids, List checkUsers, List restrictList, List excludeList, List excludeUserList, List actionList, Location location, Integer[] radius, long startTime, long endTime, boolean restrictWorld, boolean lookup) { + public static List performLookup(Statement statement, CommandSender user, List checkUuids, List checkUsers, List restrictList, Map excludeList, List excludeUserList, List actionList, Location location, Integer[] radius, long startTime, long endTime, boolean restrictWorld, boolean lookup) { List newList = new ArrayList<>(); try { @@ -116,7 +117,7 @@ public class Lookup extends Queue { return newList; } - static List performLookupRaw(Statement statement, CommandSender user, List checkUuids, List checkUsers, List restrictList, List excludeList, List excludeUserList, List actionList, Location location, Integer[] radius, Long[] rowData, long startTime, long endTime, int limitOffset, int limitCount, boolean restrictWorld, boolean lookup) { + static List performLookupRaw(Statement statement, CommandSender user, List checkUuids, List checkUsers, List restrictList, Map excludeList, List excludeUserList, List actionList, Location location, Integer[] radius, Long[] rowData, long startTime, long endTime, int limitOffset, int limitCount, boolean restrictWorld, boolean lookup) { List list = new ArrayList<>(); List invalidRollbackActions = new ArrayList<>(); invalidRollbackActions.add(2); @@ -272,7 +273,7 @@ public class Lookup extends Queue { return list; } - public static List performPartialLookup(Statement statement, CommandSender user, List checkUuids, List checkUsers, List restrictList, List excludeList, List excludeUserList, List actionList, Location location, Integer[] radius, Long[] rowData, long startTime, long endTime, int limitOffset, int limitCount, boolean restrictWorld, boolean lookup) { + public static List performPartialLookup(Statement statement, CommandSender user, List checkUuids, List checkUsers, List restrictList, Map excludeList, List excludeUserList, List actionList, Location location, Integer[] radius, Long[] rowData, long startTime, long endTime, int limitOffset, int limitCount, boolean restrictWorld, boolean lookup) { List newList = new ArrayList<>(); try { @@ -286,7 +287,7 @@ public class Lookup extends Queue { return newList; } - private static ResultSet rawLookupResultSet(Statement statement, CommandSender user, List checkUuids, List checkUsers, List restrictList, List excludeList, List excludeUserList, List actionList, Location location, Integer[] radius, Long[] rowData, long startTime, long endTime, int limitOffset, int limitCount, boolean restrictWorld, boolean lookup, boolean count) { + private static ResultSet rawLookupResultSet(Statement statement, CommandSender user, List checkUuids, List checkUsers, List restrictList, Map excludeList, List excludeUserList, List actionList, Location location, Integer[] radius, Long[] rowData, long startTime, long endTime, int limitOffset, int limitCount, boolean restrictWorld, boolean lookup, boolean count) { ResultSet results = null; try { @@ -391,7 +392,7 @@ public class Lookup extends Queue { StringBuilder excludeListMaterial = new StringBuilder(); StringBuilder excludeListEntity = new StringBuilder(); - for (Object restrictTarget : excludeList) { + for (Object restrictTarget : excludeList.keySet()) { String targetName = ""; if (restrictTarget instanceof Material) { diff --git a/src/main/java/net/coreprotect/database/Rollback.java b/src/main/java/net/coreprotect/database/Rollback.java index 9a36e3f..3403fa5 100644 --- a/src/main/java/net/coreprotect/database/Rollback.java +++ b/src/main/java/net/coreprotect/database/Rollback.java @@ -95,7 +95,7 @@ import net.coreprotect.utility.entity.HangingUtil; public class Rollback extends Queue { - public static List performRollbackRestore(Statement statement, CommandSender user, List checkUuids, List checkUsers, String timeString, List restrictList, List excludeList, List excludeUserList, List actionList, Location location, Integer[] radius, long startTime, long endTime, boolean restrictWorld, boolean lookup, boolean verbose, final int rollbackType, final int preview) { + public static List performRollbackRestore(Statement statement, CommandSender user, List checkUuids, List checkUsers, String timeString, List restrictList, Map excludeList, List excludeUserList, List actionList, Location location, Integer[] radius, long startTime, long endTime, boolean restrictWorld, boolean lookup, boolean verbose, final int rollbackType, final int preview) { List list = new ArrayList<>(); try { @@ -112,12 +112,12 @@ public class Rollback extends Queue { boolean ROLLBACK_ITEMS = false; List itemRestrictList = new ArrayList<>(restrictList); - List itemExcludeList = new ArrayList<>(excludeList); + Map itemExcludeList = new HashMap<>(excludeList); if (actionList.contains(1)) { for (Object target : restrictList) { if (target instanceof Material) { - if (!excludeList.contains(target)) { + if (!excludeList.containsKey(target)) { if (BlockGroup.CONTAINERS.contains(target)) { ROLLBACK_ITEMS = true; itemRestrictList.clear(); @@ -137,6 +137,7 @@ public class Rollback extends Queue { itemActionList.add(4); } + itemExcludeList.entrySet().removeIf(entry -> Boolean.TRUE.equals(entry.getValue())); itemList = Lookup.performLookupRaw(statement, user, checkUuids, checkUsers, itemRestrictList, itemExcludeList, excludeUserList, itemActionList, location, radius, null, startTime, endTime, -1, -1, restrictWorld, lookup); } @@ -1277,7 +1278,7 @@ public class Rollback extends Queue { return null; } - static void finishRollbackRestore(CommandSender user, Location location, List checkUsers, List restrictList, List excludeList, List excludeUserList, List actionList, String timeString, Integer chunkCount, Double seconds, Integer itemCount, Integer blockCount, Integer entityCount, int rollbackType, Integer[] radius, boolean verbose, boolean restrictWorld, int preview) { + static void finishRollbackRestore(CommandSender user, Location location, List checkUsers, List restrictList, Map excludeList, List excludeUserList, List actionList, String timeString, Integer chunkCount, Double seconds, Integer itemCount, Integer blockCount, Integer entityCount, int rollbackType, Integer[] radius, boolean verbose, boolean restrictWorld, int preview) { try { if (preview == 2) { Chat.sendMessage(user, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.PREVIEW_CANCELLED)); @@ -1418,7 +1419,15 @@ public class Rollback extends Queue { boolean entity = false; int excludeCount = 0; - for (Object excludeTarget : excludeList) { + for (Map.Entry entry : excludeList.entrySet()) { + Object excludeTarget = entry.getKey(); + Boolean excludeTargetInternal = entry.getValue(); + + // don't display default block excludes + if (Boolean.TRUE.equals(excludeTargetInternal)) { + continue; + } + // don't display that excluded water/fire/farmland in inventory rollbacks if (actionList.contains(4) && actionList.contains(11)) { if (excludeTarget.equals(Material.FIRE) || excludeTarget.equals(Material.WATER) || excludeTarget.equals(Material.FARMLAND)) {