diff --git a/pom.xml b/pom.xml index ffaf6ec..8ad0cbe 100755 --- a/pom.xml +++ b/pom.xml @@ -113,6 +113,31 @@ ${skipTests} + + org.apache.maven.plugins + maven-enforcer-plugin + 3.6.1 + + + validate + enforce + + + + + central + enginehub-repo + spigot-repo + paper-repo + codemc-repo + jitpack.io + + + + + + + diff --git a/src/main/java/net/coreprotect/config/ConfigHandler.java b/src/main/java/net/coreprotect/config/ConfigHandler.java index 6effac7..e395ab7 100644 --- a/src/main/java/net/coreprotect/config/ConfigHandler.java +++ b/src/main/java/net/coreprotect/config/ConfigHandler.java @@ -40,6 +40,11 @@ import net.coreprotect.utility.VersionUtils; import oshi.hardware.CentralProcessor; public class ConfigHandler extends Queue { + + public enum CacheType { + MATERIALS, BLOCKDATA, ART, ENTITIES, WORLDS + } + public static int SERVER_VERSION = 0; public static final int EDITION_VERSION = 2; public static final String EDITION_BRANCH = VersionUtils.getBranch(); @@ -48,6 +53,7 @@ public class ConfigHandler extends Queue { public static final String JAVA_VERSION = "11.0"; public static final String MINECRAFT_VERSION = "1.16"; public static final String LATEST_VERSION = "1.21.8"; + public static final String PATCH_VERSION = "23.0"; public static String path = "plugins/CoreProtect/"; public static String sqlite = "database.db"; public static String host = "127.0.0.1"; @@ -268,7 +274,7 @@ public class ConfigHandler extends Queue { Database.createDatabaseTables(ConfigHandler.prefix, false, null, Config.getGlobal().MYSQL, false); } - public static void loadTypes(Statement statement) { + public static void loadMaterials(Statement statement) { try { String query = "SELECT id,material FROM " + ConfigHandler.prefix + "material_map"; ResultSet rs = statement.executeQuery(query); @@ -286,9 +292,16 @@ public class ConfigHandler extends Queue { } } rs.close(); + } + catch (Exception e) { + e.printStackTrace(); + } + } - query = "SELECT id,data FROM " + ConfigHandler.prefix + "blockdata_map"; - rs = statement.executeQuery(query); + public static void loadBlockdata(Statement statement) { + try { + String query = "SELECT id,data FROM " + ConfigHandler.prefix + "blockdata_map"; + ResultSet rs = statement.executeQuery(query); ConfigHandler.blockdata.clear(); ConfigHandler.blockdataReversed.clear(); blockdataId = 0; @@ -303,9 +316,16 @@ public class ConfigHandler extends Queue { } } rs.close(); + } + catch (Exception e) { + e.printStackTrace(); + } + } - query = "SELECT id,art FROM " + ConfigHandler.prefix + "art_map"; - rs = statement.executeQuery(query); + public static void loadArt(Statement statement) { + try { + String query = "SELECT id,art FROM " + ConfigHandler.prefix + "art_map"; + ResultSet rs = statement.executeQuery(query); ConfigHandler.art.clear(); ConfigHandler.artReversed.clear(); artId = 0; @@ -320,9 +340,16 @@ public class ConfigHandler extends Queue { } } rs.close(); + } + catch (Exception e) { + e.printStackTrace(); + } + } - query = "SELECT id,entity FROM " + ConfigHandler.prefix + "entity_map"; - rs = statement.executeQuery(query); + public static void loadEntities(Statement statement) { + try { + String query = "SELECT id,entity FROM " + ConfigHandler.prefix + "entity_map"; + ResultSet rs = statement.executeQuery(query); ConfigHandler.entities.clear(); ConfigHandler.entitiesReversed.clear(); entityId = 0; @@ -343,6 +370,67 @@ public class ConfigHandler extends Queue { } } + public static void loadTypes(Statement statement) { + loadMaterials(statement); + loadBlockdata(statement); + loadArt(statement); + loadEntities(statement); + } + + /** + * Unified method to reload cache from database when DATABASE_LOCK is false (multi-server setup) + * + * @param type + * The type of cache to reload + * @param name + * The name to look up after reload + * @return The ID if found after reload, or -1 if not found + */ + public static int reloadAndGetId(CacheType type, String name) { + // Only reload if DATABASE_LOCK is false (multi-server setup) + if (Config.getGlobal().DATABASE_LOCK) { + return -1; + } + + try (Connection connection = Database.getConnection(true)) { + if (connection != null) { + Statement statement = connection.createStatement(); + + // Reload appropriate cache based on type + switch (type) { + case MATERIALS: + loadMaterials(statement); + statement.close(); + return materials.getOrDefault(name, -1); + case BLOCKDATA: + loadBlockdata(statement); + statement.close(); + return blockdata.getOrDefault(name, -1); + case ART: + loadArt(statement); + statement.close(); + return art.getOrDefault(name, -1); + case ENTITIES: + loadEntities(statement); + statement.close(); + return entities.getOrDefault(name, -1); + case WORLDS: + loadWorlds(statement); + statement.close(); + return worlds.getOrDefault(name, -1); + default: + statement.close(); + return -1; + } + } + } + catch (Exception e) { + e.printStackTrace(); + } + + return -1; + } + public static void loadWorlds(Statement statement) { try { String query = "SELECT id,world FROM " + ConfigHandler.prefix + "world"; diff --git a/src/main/java/net/coreprotect/services/VersionCheckService.java b/src/main/java/net/coreprotect/services/VersionCheckService.java index dba40c8..85e8834 100644 --- a/src/main/java/net/coreprotect/services/VersionCheckService.java +++ b/src/main/java/net/coreprotect/services/VersionCheckService.java @@ -44,6 +44,13 @@ public class VersionCheckService { return false; } + // Patch version validation + if (VersionUtils.newVersion(ConfigHandler.PATCH_VERSION, VersionUtils.getPluginVersion()) && !ConfigHandler.EDITION_BRANCH.contains("-dev")) { + Chat.console(Phrase.build(Phrase.VERSION_INCOMPATIBLE, "CoreProtect", "v" + VersionUtils.getPluginVersion())); + Chat.sendConsoleMessage(Color.GREY + "[CoreProtect] " + Phrase.build(Phrase.INVALID_BRANCH_2)); + return false; + } + // Branch validation if (ConfigHandler.EDITION_BRANCH.length() == 0) { Chat.sendConsoleMessage(Color.RED + "[CoreProtect] " + Phrase.build(Phrase.INVALID_BRANCH_1)); diff --git a/src/main/java/net/coreprotect/utility/EntityUtils.java b/src/main/java/net/coreprotect/utility/EntityUtils.java index 342e75f..7e1ab37 100644 --- a/src/main/java/net/coreprotect/utility/EntityUtils.java +++ b/src/main/java/net/coreprotect/utility/EntityUtils.java @@ -33,6 +33,12 @@ public class EntityUtils extends Queue { id = ConfigHandler.entities.get(name); } else if (internal) { + // Check if another server has already added this entity (multi-server setup) + id = ConfigHandler.reloadAndGetId(ConfigHandler.CacheType.ENTITIES, name); + if (id != -1) { + return id; + } + int entityID = ConfigHandler.entityId + 1; ConfigHandler.entities.put(name, entityID); ConfigHandler.entitiesReversed.put(entityID, name); diff --git a/src/main/java/net/coreprotect/utility/MaterialUtils.java b/src/main/java/net/coreprotect/utility/MaterialUtils.java index f0f568e..25129cd 100644 --- a/src/main/java/net/coreprotect/utility/MaterialUtils.java +++ b/src/main/java/net/coreprotect/utility/MaterialUtils.java @@ -35,6 +35,12 @@ public class MaterialUtils extends Queue { id = ConfigHandler.materials.get(name); } else if (internal) { + // Check if another server has already added this material (multi-server setup) + id = ConfigHandler.reloadAndGetId(ConfigHandler.CacheType.MATERIALS, name); + if (id != -1) { + return id; + } + int mid = ConfigHandler.materialId + 1; ConfigHandler.materials.put(name, mid); ConfigHandler.materialsReversed.put(mid, name); @@ -54,6 +60,12 @@ public class MaterialUtils extends Queue { id = ConfigHandler.blockdata.get(data); } else if (internal) { + // Check if another server has already added this blockdata (multi-server setup) + id = ConfigHandler.reloadAndGetId(ConfigHandler.CacheType.BLOCKDATA, data); + if (id != -1) { + return id; + } + int bid = ConfigHandler.blockdataId + 1; ConfigHandler.blockdata.put(data, bid); ConfigHandler.blockdataReversed.put(bid, data); @@ -135,6 +147,12 @@ public class MaterialUtils extends Queue { id = ConfigHandler.art.get(name); } else if (internal) { + // Check if another server has already added this art (multi-server setup) + id = ConfigHandler.reloadAndGetId(ConfigHandler.CacheType.ART, name); + if (id != -1) { + return id; + } + int artID = ConfigHandler.artId + 1; ConfigHandler.art.put(name, artID); ConfigHandler.artReversed.put(artID, name); diff --git a/src/main/java/net/coreprotect/utility/WorldUtils.java b/src/main/java/net/coreprotect/utility/WorldUtils.java index 4af911e..a63b5ab 100644 --- a/src/main/java/net/coreprotect/utility/WorldUtils.java +++ b/src/main/java/net/coreprotect/utility/WorldUtils.java @@ -16,6 +16,12 @@ public class WorldUtils extends Queue { int id = -1; try { if (ConfigHandler.worlds.get(name) == null) { + // Check if another server has already added this world (multi-server setup) + id = ConfigHandler.reloadAndGetId(ConfigHandler.CacheType.WORLDS, name); + if (id != -1) { + return id; + } + int wid = ConfigHandler.worldId + 1; ConfigHandler.worlds.put(name, wid); ConfigHandler.worldsReversed.put(wid, name); @@ -84,7 +90,7 @@ public class WorldUtils extends Queue { return id; } - + public static String getWidIndex(String queryTable) { String index = ""; boolean isMySQL = net.coreprotect.config.Config.getGlobal().MYSQL; @@ -121,4 +127,4 @@ public class WorldUtils extends Queue { return index; } -} \ No newline at end of file +}