This commit is contained in:
Intelli committed 2026-10-18 12:29:19 -06:00
1 parent 38b6d87220
commit 2d484dffff
1 file changed
+83 -69
@@ -65,11 +65,21 @@ import net.coreprotect.utility.VersionUtils;
public class ConfigHandler extends Queue {
public enum CacheType {
MATERIALS(ReferenceKind.MATERIAL),
BLOCKDATA(ReferenceKind.BLOCK_DATA),
ART(ReferenceKind.ART),
ENTITIES(ReferenceKind.ENTITY),
WORLDS(ReferenceKind.WORLD);
MATERIALS(
ReferenceKind.MATERIAL
),
BLOCKDATA(
ReferenceKind.BLOCK_DATA
),
ART(
ReferenceKind.ART
),
ENTITIES(
ReferenceKind.ENTITY
),
WORLDS(
ReferenceKind.WORLD
);
private final ReferenceKind kind;
@@ -145,13 +155,11 @@ public class ConfigHandler extends Queue {
return Collections.synchronizedMap(new HashMap<>(values));
}
private static IdentifierCache loadIdentifierCache(Statement statement, String table, String valueColumn)
throws SQLException {
private static IdentifierCache loadIdentifierCache(Statement statement, String table, String valueColumn) throws SQLException {
Map<String, Integer> values = new HashMap<>();
Map<Integer, String> reversed = new HashMap<>();
int maximumId = 0;
try (ResultSet resultSet = statement
.executeQuery("SELECT id," + valueColumn + " FROM " + ConfigHandler.prefix + table)) {
try (ResultSet resultSet = statement.executeQuery("SELECT id," + valueColumn + " FROM " + ConfigHandler.prefix + table)) {
while (resultSet.next()) {
int id = resultSet.getInt("id");
String value = resultSet.getString(valueColumn);
@@ -183,8 +191,7 @@ public class ConfigHandler extends Queue {
private final IntSupplier maximum;
private final IntConsumer maximumSetter;
private IdentifierStore(ReferenceKind kind, Supplier<Map<String, Integer>> values, Supplier<Map<Integer, String>> reversed, IntSupplier maximum,
IntConsumer maximumSetter) {
private IdentifierStore(ReferenceKind kind, Supplier<Map<String, Integer>> values, Supplier<Map<Integer, String>> reversed, IntSupplier maximum, IntConsumer maximumSetter) {
this.kind = kind;
this.values = values;
this.reversed = reversed;
@@ -213,8 +220,7 @@ public class ConfigHandler extends Queue {
Integer storedId = names.get(value);
String storedValue = identifiers.get(id);
if ((storedId != null && storedId != id) || (storedValue != null && !storedValue.equals(value))) {
throw new IllegalStateException("CoreProtect " + kind.name().toLowerCase(Locale.ROOT) + " identifier " + id + "='" + value
+ "' conflicts with cached " + storedId + "='" + storedValue + "'");
throw new IllegalStateException("CoreProtect " + kind.name().toLowerCase(Locale.ROOT) + " identifier " + id + "='" + value + "' conflicts with cached " + storedId + "='" + storedValue + "'");
}
return true;
}
@@ -301,7 +307,8 @@ public class ConfigHandler extends Queue {
if (!batch.commit()) {
throw new IllegalStateException("Unable to initialize ClickHouse player records");
}
} catch (Exception exception) {
}
catch (Exception exception) {
throw new IllegalStateException("Unable to initialize ClickHouse player records", exception);
}
return;
@@ -335,8 +342,7 @@ public class ConfigHandler extends Queue {
}
public static boolean isBlacklisted(String user, String object) {
if (ConfigHandler.blacklist.containsKey(object)
|| ConfigHandler.blacklist.containsKey(user.toLowerCase(Locale.ROOT))) {
if (ConfigHandler.blacklist.containsKey(object) || ConfigHandler.blacklist.containsKey(user.toLowerCase(Locale.ROOT))) {
return true;
}
return isFilterBlacklisted(user, object);
@@ -372,13 +378,14 @@ public class ConfigHandler extends Queue {
String[] blSplit = blLine.split(BLACKLIST_FILTER_SEPARATOR);
if (blSplit.length == 1) {
ConfigHandler.blacklist.put(blLine, true);
} else {
ConfigHandler.FilteredBlacklist.computeIfAbsent(blSplit[0], k -> new HashSet<>())
.add(blSplit[1]);
}
else {
ConfigHandler.FilteredBlacklist.computeIfAbsent(blSplit[0], k -> new HashSet<>()).add(blSplit[1]);
}
}
}
} catch (Exception e) {
}
catch (Exception e) {
ErrorReporter.report(e);
}
}
@@ -387,8 +394,7 @@ public class ConfigHandler extends Queue {
File configFolder = new File(ConfigHandler.path);
File configFile = new File(configFolder, ConfigFile.CONFIG);
File[] existingFiles = configFolder.listFiles();
duckDBFallbackAllowed = !configFile.exists()
&& (!configFolder.exists() || (existingFiles != null && existingFiles.length == 0));
duckDBFallbackAllowed = !configFile.exists() && (!configFolder.exists() || (existingFiles != null && existingFiles.length == 0));
Config.init();
ConfigFile.init(ConfigFile.LANGUAGE); // load user phrases
@@ -400,7 +406,8 @@ public class ConfigHandler extends Queue {
if (global.hasOption("use-mysql") && global.MYSQL != ConfigHandler.databaseType.isMySQL()) {
Chat.sendConsoleMessage(Color.YELLOW + "[CoreProtect] " + Phrase.build(Phrase.DATABASE_TYPE_OVERRIDE));
}
} else {
}
else {
ConfigHandler.databaseType = global.MYSQL ? DatabaseType.MYSQL : DatabaseType.SQLITE;
}
global.MYSQL = ConfigHandler.databaseType.isMySQL();
@@ -449,15 +456,16 @@ public class ConfigHandler extends Queue {
try {
if (preparedClickHouse != null) {
Database.installClickHouseDatabase(preparedClickHouse);
} else {
}
else {
initializeDatabase();
}
if (!migrationActivation && Database.hasIncompleteMigrationMarker()) {
throw new IllegalStateException(
"Database contains an incomplete CoreProtect migration and cannot be activated");
throw new IllegalStateException("Database contains an incomplete CoreProtect migration and cannot be activated");
}
ConfigHandler.databaseReachable = true;
} catch (Exception exception) {
}
catch (Exception exception) {
Database.closeConnection();
ConfigHandler.databaseReachable = false;
if (exception instanceof RuntimeException) {
@@ -471,11 +479,10 @@ public class ConfigHandler extends Queue {
if (ConfigHandler.databaseType.isClickHouse()) {
try {
Config global = Config.getGlobal();
ClickHouseJdbcConfig config = new ClickHouseJdbcConfig(global.CLICKHOUSE_HOST, global.CLICKHOUSE_PORT,
global.CLICKHOUSE_DATABASE, global.CLICKHOUSE_USERNAME, global.CLICKHOUSE_PASSWORD,
global.CLICKHOUSE_TLS);
ClickHouseJdbcConfig config = new ClickHouseJdbcConfig(global.CLICKHOUSE_HOST, global.CLICKHOUSE_PORT, global.CLICKHOUSE_DATABASE, global.CLICKHOUSE_USERNAME, global.CLICKHOUSE_PASSWORD, global.CLICKHOUSE_TLS);
Database.initializeClickHouse(config, ConfigHandler.prefix);
} catch (Exception exception) {
}
catch (Exception exception) {
throw new IllegalStateException("Failed to initialize ClickHouse", exception);
}
return;
@@ -489,7 +496,8 @@ public class ConfigHandler extends Queue {
boolean canExecute = false;
try {
canExecute = tempFile.canExecute();
} catch (Exception exception) {
}
catch (Exception exception) {
// execute access denied by security manager
}
@@ -507,39 +515,40 @@ public class ConfigHandler extends Queue {
if (ConfigHandler.databaseType.isDuckDB() && duckDBFallbackAllowed) {
try {
DuckDBNativeSupport.verifyAvailable();
} catch (Throwable failure) {
}
catch (Throwable failure) {
if (!DuckDBNativeSupport.isNativeUnavailable(failure)) {
throw new IllegalStateException("Unable to verify DuckDB on this system. For native-library extraction or loading errors, check free space and permissions in the JVM temporary directory. "
+ "To select another directory, add -Djava.io.tmpdir=/absolute/path/to/cache before -jar and fully restart the server. "
+ "The directory must already exist, have sufficient space, and permit writing and native-library loading.", failure);
throw new IllegalStateException("Unable to verify DuckDB on this system. For native-library extraction or loading errors, check free space and permissions in the JVM temporary directory. " + "To select another directory, add -Djava.io.tmpdir=/absolute/path/to/cache before -jar and fully restart the server. " + "The directory must already exist, have sufficient space, and permit writing and native-library loading.", failure);
}
DatabaseConfigWriter.persistDatabaseType(DatabaseType.SQLITE);
ConfigHandler.databaseType = DatabaseType.SQLITE;
Config global = Config.getGlobal();
global.DATABASE_TYPE = DatabaseType.SQLITE.name().toLowerCase(Locale.ROOT);
global.MYSQL = false;
Chat.sendConsoleMessage(Color.YELLOW + "[CoreProtect] " + Phrase.build(Phrase.DATABASE_FALLBACK,
DatabaseType.DUCKDB.getDisplayName(), DatabaseType.SQLITE.getDisplayName()));
} finally {
Chat.sendConsoleMessage(Color.YELLOW + "[CoreProtect] " + Phrase.build(Phrase.DATABASE_FALLBACK, DatabaseType.DUCKDB.getDisplayName(), DatabaseType.SQLITE.getDisplayName()));
}
finally {
duckDBFallbackAllowed = false;
}
}
Class.forName(ConfigHandler.databaseType.isDuckDB() ? "org.duckdb.DuckDBDriver" : "org.sqlite.JDBC");
} catch (Exception e) {
}
catch (Exception e) {
throw new IllegalStateException("Failed to initialize " + ConfigHandler.databaseType.getDisplayName(), e);
}
} else {
}
else {
HikariConfig config = new HikariConfig();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
config.setDriverClassName("com.mysql.cj.jdbc.Driver");
} catch (Exception e) {
}
catch (Exception e) {
config.setDriverClassName("com.mysql.jdbc.Driver");
}
config.setJdbcUrl(
"jdbc:mysql://" + ConfigHandler.host + ":" + ConfigHandler.port + "/" + ConfigHandler.database);
config.setJdbcUrl("jdbc:mysql://" + ConfigHandler.host + ":" + ConfigHandler.port + "/" + ConfigHandler.database);
config.setUsername(ConfigHandler.username);
config.setPassword(ConfigHandler.password);
config.setMaximumPoolSize(ConfigHandler.maximumPoolSize);
@@ -579,7 +588,8 @@ public class ConfigHandler extends Queue {
ConfigHandler.materialsReversed = syncMap(cache.reversed);
materialId = cache.maximumId;
return true;
} catch (Exception e) {
}
catch (Exception e) {
ErrorReporter.report(e);
return false;
}
@@ -592,7 +602,8 @@ public class ConfigHandler extends Queue {
ConfigHandler.blockdataReversed = syncMap(cache.reversed);
blockdataId = cache.maximumId;
return true;
} catch (Exception e) {
}
catch (Exception e) {
ErrorReporter.report(e);
return false;
}
@@ -605,7 +616,8 @@ public class ConfigHandler extends Queue {
ConfigHandler.artReversed = syncMap(cache.reversed);
artId = cache.maximumId;
return true;
} catch (Exception e) {
}
catch (Exception e) {
ErrorReporter.report(e);
return false;
}
@@ -618,7 +630,8 @@ public class ConfigHandler extends Queue {
ConfigHandler.entitiesReversed = syncMap(cache.reversed);
entityId = cache.maximumId;
return true;
} catch (Exception e) {
}
catch (Exception e) {
ErrorReporter.report(e);
return false;
}
@@ -643,9 +656,9 @@ public class ConfigHandler extends Queue {
* Reloads an identifier cache when multi-server cache refresh is permitted.
*
* @param type
* The type of cache to reload
* The type of cache to reload
* @param name
* The name to look up after reload
* The name to look up after reload
* @return The ID if found after reload, or -1 if not found
*/
public static synchronized int reloadAndGetId(CacheType type, String name) {
@@ -812,7 +825,8 @@ public class ConfigHandler extends Queue {
return false;
}
}
} catch (Exception e) {
}
catch (Exception e) {
ErrorReporter.report(e);
return false;
}
@@ -879,11 +893,7 @@ public class ConfigHandler extends Queue {
locked = false;
unixtimestamp = (int) (System.currentTimeMillis() / 1000L);
int checkTime = unixtimestamp - 15;
String query = "SELECT * FROM " + ConfigHandler.prefix
+ "database_lock WHERE rowid='1' AND (status='"
+ Database.DATABASE_LOCK_MIGRATION_INCOMPLETE
+ "' OR (status='" + Database.DATABASE_LOCK_ACTIVE + "' AND time >= '" + checkTime
+ "')) LIMIT 1";
String query = "SELECT * FROM " + ConfigHandler.prefix + "database_lock WHERE rowid='1' AND (status='" + Database.DATABASE_LOCK_MIGRATION_INCOMPLETE + "' OR (status='" + Database.DATABASE_LOCK_ACTIVE + "' AND time >= '" + checkTime + "')) LIMIT 1";
ResultSet rs = statement.executeQuery(query);
while (rs.next()) {
if (unixtimestamp < waitTime) {
@@ -892,13 +902,11 @@ public class ConfigHandler extends Queue {
lockMessage = true;
}
Thread.sleep(1000);
} else {
Chat.sendConsoleMessage(
Color.RED + "[CoreProtect] " + Phrase.build(Phrase.DATABASE_LOCKED_2));
Chat.sendConsoleMessage(
Color.GREY + "[CoreProtect] " + Phrase.build(Phrase.DATABASE_LOCKED_3));
Chat.sendConsoleMessage(
Color.GREY + "[CoreProtect] " + Phrase.build(Phrase.DATABASE_LOCKED_4));
}
else {
Chat.sendConsoleMessage(Color.RED + "[CoreProtect] " + Phrase.build(Phrase.DATABASE_LOCKED_2));
Chat.sendConsoleMessage(Color.GREY + "[CoreProtect] " + Phrase.build(Phrase.DATABASE_LOCKED_3));
Chat.sendConsoleMessage(Color.GREY + "[CoreProtect] " + Phrase.build(Phrase.DATABASE_LOCKED_4));
return false;
}
@@ -907,7 +915,8 @@ public class ConfigHandler extends Queue {
rs.close();
}
}
} catch (Exception e) {
}
catch (Exception e) {
ErrorReporter.report(e);
}
@@ -927,9 +936,11 @@ public class ConfigHandler extends Queue {
if (startup) {
ListenerHandler.registerNetworking(); // Register channels for networking API
}
} catch (CancellationException e) {
}
catch (CancellationException e) {
throw e;
} catch (Exception e) {
}
catch (Exception e) {
ErrorReporter.report(e);
return false;
}
@@ -951,7 +962,8 @@ public class ConfigHandler extends Queue {
if (worldEditPlugin != null && worldEditPlugin.isEnabled()) {
VersionUtils.loadWorldEdit();
}
} else if (ConfigHandler.worldeditEnabled) {
}
else if (ConfigHandler.worldeditEnabled) {
VersionUtils.unloadWorldEdit();
}
@@ -968,7 +980,8 @@ public class ConfigHandler extends Queue {
statement.close();
return validVersion && databaseLock;
} catch (Exception e) {
}
catch (Exception e) {
ErrorReporter.report(e);
}
@@ -979,7 +992,8 @@ public class ConfigHandler extends Queue {
try {
Database.closeConnection();
ListenerHandler.unregisterNetworking(); // Unregister channels for networking API
} catch (Exception e) {
}
catch (Exception e) {
ErrorReporter.report(e);
}
}