Skip to content

Commit 51d4bf2

Browse files
committed
Added MultiPaper support
1 parent d30f6cf commit 51d4bf2

File tree

6 files changed

+159
-9
lines changed

6 files changed

+159
-9
lines changed

pom.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,31 @@
113113
<skipTests>${skipTests}</skipTests>
114114
</configuration>
115115
</plugin>
116+
<plugin>
117+
<groupId>org.apache.maven.plugins</groupId>
118+
<artifactId>maven-enforcer-plugin</artifactId>
119+
<version>3.6.1</version>
120+
<executions>
121+
<execution>
122+
<phase>validate</phase>
123+
<goals><goal>enforce</goal></goals>
124+
<configuration>
125+
<rules>
126+
<requireNoRepositories>
127+
<allowedRepositories>
128+
<allowedRepository>central</allowedRepository>
129+
<allowedRepository>enginehub-repo</allowedRepository>
130+
<allowedRepository>spigot-repo</allowedRepository>
131+
<allowedRepository>paper-repo</allowedRepository>
132+
<allowedRepository>codemc-repo</allowedRepository>
133+
<allowedRepository>jitpack.io</allowedRepository>
134+
</allowedRepositories>
135+
</requireNoRepositories>
136+
</rules>
137+
</configuration>
138+
</execution>
139+
</executions>
140+
</plugin>
116141
</plugins>
117142
</build>
118143
<profiles>

src/main/java/net/coreprotect/config/ConfigHandler.java

Lines changed: 95 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
import oshi.hardware.CentralProcessor;
4141

4242
public class ConfigHandler extends Queue {
43+
44+
public enum CacheType {
45+
MATERIALS, BLOCKDATA, ART, ENTITIES, WORLDS
46+
}
47+
4348
public static int SERVER_VERSION = 0;
4449
public static final int EDITION_VERSION = 2;
4550
public static final String EDITION_BRANCH = VersionUtils.getBranch();
@@ -48,6 +53,7 @@ public class ConfigHandler extends Queue {
4853
public static final String JAVA_VERSION = "11.0";
4954
public static final String MINECRAFT_VERSION = "1.16";
5055
public static final String LATEST_VERSION = "1.21.8";
56+
public static final String PATCH_VERSION = "23.0";
5157
public static String path = "plugins/CoreProtect/";
5258
public static String sqlite = "database.db";
5359
public static String host = "127.0.0.1";
@@ -268,7 +274,7 @@ public static void loadDatabase() {
268274
Database.createDatabaseTables(ConfigHandler.prefix, false, null, Config.getGlobal().MYSQL, false);
269275
}
270276

271-
public static void loadTypes(Statement statement) {
277+
public static void loadMaterials(Statement statement) {
272278
try {
273279
String query = "SELECT id,material FROM " + ConfigHandler.prefix + "material_map";
274280
ResultSet rs = statement.executeQuery(query);
@@ -286,9 +292,16 @@ public static void loadTypes(Statement statement) {
286292
}
287293
}
288294
rs.close();
295+
}
296+
catch (Exception e) {
297+
e.printStackTrace();
298+
}
299+
}
289300

290-
query = "SELECT id,data FROM " + ConfigHandler.prefix + "blockdata_map";
291-
rs = statement.executeQuery(query);
301+
public static void loadBlockdata(Statement statement) {
302+
try {
303+
String query = "SELECT id,data FROM " + ConfigHandler.prefix + "blockdata_map";
304+
ResultSet rs = statement.executeQuery(query);
292305
ConfigHandler.blockdata.clear();
293306
ConfigHandler.blockdataReversed.clear();
294307
blockdataId = 0;
@@ -303,9 +316,16 @@ public static void loadTypes(Statement statement) {
303316
}
304317
}
305318
rs.close();
319+
}
320+
catch (Exception e) {
321+
e.printStackTrace();
322+
}
323+
}
306324

307-
query = "SELECT id,art FROM " + ConfigHandler.prefix + "art_map";
308-
rs = statement.executeQuery(query);
325+
public static void loadArt(Statement statement) {
326+
try {
327+
String query = "SELECT id,art FROM " + ConfigHandler.prefix + "art_map";
328+
ResultSet rs = statement.executeQuery(query);
309329
ConfigHandler.art.clear();
310330
ConfigHandler.artReversed.clear();
311331
artId = 0;
@@ -320,9 +340,16 @@ public static void loadTypes(Statement statement) {
320340
}
321341
}
322342
rs.close();
343+
}
344+
catch (Exception e) {
345+
e.printStackTrace();
346+
}
347+
}
323348

324-
query = "SELECT id,entity FROM " + ConfigHandler.prefix + "entity_map";
325-
rs = statement.executeQuery(query);
349+
public static void loadEntities(Statement statement) {
350+
try {
351+
String query = "SELECT id,entity FROM " + ConfigHandler.prefix + "entity_map";
352+
ResultSet rs = statement.executeQuery(query);
326353
ConfigHandler.entities.clear();
327354
ConfigHandler.entitiesReversed.clear();
328355
entityId = 0;
@@ -343,6 +370,67 @@ public static void loadTypes(Statement statement) {
343370
}
344371
}
345372

373+
public static void loadTypes(Statement statement) {
374+
loadMaterials(statement);
375+
loadBlockdata(statement);
376+
loadArt(statement);
377+
loadEntities(statement);
378+
}
379+
380+
/**
381+
* Unified method to reload cache from database when DATABASE_LOCK is false (multi-server setup)
382+
*
383+
* @param type
384+
* The type of cache to reload
385+
* @param name
386+
* The name to look up after reload
387+
* @return The ID if found after reload, or -1 if not found
388+
*/
389+
public static int reloadAndGetId(CacheType type, String name) {
390+
// Only reload if DATABASE_LOCK is false (multi-server setup)
391+
if (Config.getGlobal().DATABASE_LOCK) {
392+
return -1;
393+
}
394+
395+
try (Connection connection = Database.getConnection(true)) {
396+
if (connection != null) {
397+
Statement statement = connection.createStatement();
398+
399+
// Reload appropriate cache based on type
400+
switch (type) {
401+
case MATERIALS:
402+
loadMaterials(statement);
403+
statement.close();
404+
return materials.getOrDefault(name, -1);
405+
case BLOCKDATA:
406+
loadBlockdata(statement);
407+
statement.close();
408+
return blockdata.getOrDefault(name, -1);
409+
case ART:
410+
loadArt(statement);
411+
statement.close();
412+
return art.getOrDefault(name, -1);
413+
case ENTITIES:
414+
loadEntities(statement);
415+
statement.close();
416+
return entities.getOrDefault(name, -1);
417+
case WORLDS:
418+
loadWorlds(statement);
419+
statement.close();
420+
return worlds.getOrDefault(name, -1);
421+
default:
422+
statement.close();
423+
return -1;
424+
}
425+
}
426+
}
427+
catch (Exception e) {
428+
e.printStackTrace();
429+
}
430+
431+
return -1;
432+
}
433+
346434
public static void loadWorlds(Statement statement) {
347435
try {
348436
String query = "SELECT id,world FROM " + ConfigHandler.prefix + "world";

src/main/java/net/coreprotect/services/VersionCheckService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ public static boolean performVersionChecks() {
4444
return false;
4545
}
4646

47+
// Patch version validation
48+
if (VersionUtils.newVersion(ConfigHandler.PATCH_VERSION, VersionUtils.getPluginVersion()) && !ConfigHandler.EDITION_BRANCH.contains("-dev")) {
49+
Chat.console(Phrase.build(Phrase.VERSION_INCOMPATIBLE, "CoreProtect", "v" + VersionUtils.getPluginVersion()));
50+
Chat.sendConsoleMessage(Color.GREY + "[CoreProtect] " + Phrase.build(Phrase.INVALID_BRANCH_2));
51+
return false;
52+
}
53+
4754
// Branch validation
4855
if (ConfigHandler.EDITION_BRANCH.length() == 0) {
4956
Chat.sendConsoleMessage(Color.RED + "[CoreProtect] " + Phrase.build(Phrase.INVALID_BRANCH_1));

src/main/java/net/coreprotect/utility/EntityUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ public static int getEntityId(String name, boolean internal) {
3333
id = ConfigHandler.entities.get(name);
3434
}
3535
else if (internal) {
36+
// Check if another server has already added this entity (multi-server setup)
37+
id = ConfigHandler.reloadAndGetId(ConfigHandler.CacheType.ENTITIES, name);
38+
if (id != -1) {
39+
return id;
40+
}
41+
3642
int entityID = ConfigHandler.entityId + 1;
3743
ConfigHandler.entities.put(name, entityID);
3844
ConfigHandler.entitiesReversed.put(entityID, name);

src/main/java/net/coreprotect/utility/MaterialUtils.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ public static int getBlockId(String name, boolean internal) {
3535
id = ConfigHandler.materials.get(name);
3636
}
3737
else if (internal) {
38+
// Check if another server has already added this material (multi-server setup)
39+
id = ConfigHandler.reloadAndGetId(ConfigHandler.CacheType.MATERIALS, name);
40+
if (id != -1) {
41+
return id;
42+
}
43+
3844
int mid = ConfigHandler.materialId + 1;
3945
ConfigHandler.materials.put(name, mid);
4046
ConfigHandler.materialsReversed.put(mid, name);
@@ -54,6 +60,12 @@ public static int getBlockdataId(String data, boolean internal) {
5460
id = ConfigHandler.blockdata.get(data);
5561
}
5662
else if (internal) {
63+
// Check if another server has already added this blockdata (multi-server setup)
64+
id = ConfigHandler.reloadAndGetId(ConfigHandler.CacheType.BLOCKDATA, data);
65+
if (id != -1) {
66+
return id;
67+
}
68+
5769
int bid = ConfigHandler.blockdataId + 1;
5870
ConfigHandler.blockdata.put(data, bid);
5971
ConfigHandler.blockdataReversed.put(bid, data);
@@ -135,6 +147,12 @@ public static int getArtId(String name, boolean internal) {
135147
id = ConfigHandler.art.get(name);
136148
}
137149
else if (internal) {
150+
// Check if another server has already added this art (multi-server setup)
151+
id = ConfigHandler.reloadAndGetId(ConfigHandler.CacheType.ART, name);
152+
if (id != -1) {
153+
return id;
154+
}
155+
138156
int artID = ConfigHandler.artId + 1;
139157
ConfigHandler.art.put(name, artID);
140158
ConfigHandler.artReversed.put(artID, name);

src/main/java/net/coreprotect/utility/WorldUtils.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ public static int getWorldId(String name) {
1616
int id = -1;
1717
try {
1818
if (ConfigHandler.worlds.get(name) == null) {
19+
// Check if another server has already added this world (multi-server setup)
20+
id = ConfigHandler.reloadAndGetId(ConfigHandler.CacheType.WORLDS, name);
21+
if (id != -1) {
22+
return id;
23+
}
24+
1925
int wid = ConfigHandler.worldId + 1;
2026
ConfigHandler.worlds.put(name, wid);
2127
ConfigHandler.worldsReversed.put(wid, name);
@@ -84,7 +90,7 @@ else if (worldName.toLowerCase(java.util.Locale.ROOT).replaceAll("[^a-zA-Z0-9]",
8490

8591
return id;
8692
}
87-
93+
8894
public static String getWidIndex(String queryTable) {
8995
String index = "";
9096
boolean isMySQL = net.coreprotect.config.Config.getGlobal().MYSQL;
@@ -121,4 +127,4 @@ public static String getWidIndex(String queryTable) {
121127

122128
return index;
123129
}
124-
}
130+
}

0 commit comments

Comments
 (0)