Skip to content

Commit 83302f5

Browse files
committed
Refactor remaining API classes
1 parent 5c965b8 commit 83302f5

File tree

3 files changed

+237
-69
lines changed

3 files changed

+237
-69
lines changed

src/main/java/net/coreprotect/api/BlockAPI.java

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,49 @@
88

99
import org.bukkit.block.Block;
1010

11+
import net.coreprotect.config.Config;
1112
import net.coreprotect.config.ConfigHandler;
1213
import net.coreprotect.database.Database;
1314
import net.coreprotect.database.statement.UserStatement;
1415
import net.coreprotect.utility.BlockUtils;
1516
import net.coreprotect.utility.StringUtils;
1617
import net.coreprotect.utility.WorldUtils;
1718

19+
/**
20+
* Provides API methods for block-related lookups in the CoreProtect database.
21+
*/
1822
public class BlockAPI {
1923

24+
/**
25+
* Private constructor to prevent instantiation.
26+
* This is a utility class with static methods only.
27+
*/
28+
private BlockAPI() {
29+
throw new IllegalStateException("API class");
30+
}
31+
32+
/**
33+
* Performs a lookup of block-related actions at the specified block.
34+
*
35+
* @param block
36+
* The block to look up
37+
* @param offset
38+
* Time constraint in seconds (0 means no time constraint)
39+
* @return List of results in a String array format
40+
*/
2041
public static List<String[]> performLookup(Block block, int offset) {
2142
List<String[]> result = new ArrayList<>();
2243

44+
if (!Config.getGlobal().API_ENABLED) {
45+
return result;
46+
}
47+
48+
if (block == null) {
49+
return result;
50+
}
51+
2352
try (Connection connection = Database.getConnection(false, 1000)) {
24-
if (block == null) {
53+
if (connection == null) {
2554
return result;
2655
}
2756

@@ -31,44 +60,42 @@ public static List<String[]> performLookup(Block block, int offset) {
3160
int time = (int) (System.currentTimeMillis() / 1000L);
3261
int worldId = WorldUtils.getWorldId(block.getWorld().getName());
3362
int checkTime = 0;
63+
3464
if (offset > 0) {
3565
checkTime = time - offset;
3666
}
3767

38-
if (connection == null) {
39-
return result;
40-
}
68+
try (Statement statement = connection.createStatement()) {
69+
String query = "SELECT time,user,action,type,data,blockdata,rolled_back FROM " + ConfigHandler.prefix + "block " + WorldUtils.getWidIndex("block") + "WHERE wid = '" + worldId + "' AND x = '" + x + "' AND z = '" + z + "' AND y = '" + y + "' AND time > '" + checkTime + "' ORDER BY rowid DESC";
4170

42-
Statement statement = connection.createStatement();
43-
String query = "SELECT time,user,action,type,data,blockdata,rolled_back FROM " + ConfigHandler.prefix + "block " + WorldUtils.getWidIndex("block") + "WHERE wid = '" + worldId + "' AND x = '" + x + "' AND z = '" + z + "' AND y = '" + y + "' AND time > '" + checkTime + "' ORDER BY rowid DESC";
44-
ResultSet results = statement.executeQuery(query);
45-
46-
while (results.next()) {
47-
String resultTime = results.getString("time");
48-
int resultUserId = results.getInt("user");
49-
String resultAction = results.getString("action");
50-
int resultType = results.getInt("type");
51-
String resultData = results.getString("data");
52-
byte[] resultBlockData = results.getBytes("blockdata");
53-
String resultRolledBack = results.getString("rolled_back");
54-
if (ConfigHandler.playerIdCacheReversed.get(resultUserId) == null) {
55-
UserStatement.loadName(connection, resultUserId);
56-
}
57-
String resultUser = ConfigHandler.playerIdCacheReversed.get(resultUserId);
58-
String blockData = BlockUtils.byteDataToString(resultBlockData, resultType);
71+
try (ResultSet results = statement.executeQuery(query)) {
72+
while (results.next()) {
73+
String resultTime = results.getString("time");
74+
int resultUserId = results.getInt("user");
75+
String resultAction = results.getString("action");
76+
int resultType = results.getInt("type");
77+
String resultData = results.getString("data");
78+
byte[] resultBlockData = results.getBytes("blockdata");
79+
String resultRolledBack = results.getString("rolled_back");
80+
81+
if (ConfigHandler.playerIdCacheReversed.get(resultUserId) == null) {
82+
UserStatement.loadName(connection, resultUserId);
83+
}
5984

60-
String[] lookupData = new String[] { resultTime, resultUser, String.valueOf(x), String.valueOf(y), String.valueOf(z), String.valueOf(resultType), resultData, resultAction, resultRolledBack, String.valueOf(worldId), blockData };
61-
String[] lineData = StringUtils.toStringArray(lookupData);
62-
result.add(lineData);
85+
String resultUser = ConfigHandler.playerIdCacheReversed.get(resultUserId);
86+
String blockData = BlockUtils.byteDataToString(resultBlockData, resultType);
87+
88+
String[] lookupData = new String[] { resultTime, resultUser, String.valueOf(x), String.valueOf(y), String.valueOf(z), String.valueOf(resultType), resultData, resultAction, resultRolledBack, String.valueOf(worldId), blockData };
89+
90+
result.add(StringUtils.toStringArray(lookupData));
91+
}
92+
}
6393
}
64-
results.close();
65-
statement.close();
6694
}
6795
catch (Exception e) {
6896
e.printStackTrace();
6997
}
7098

7199
return result;
72100
}
73-
74101
}

src/main/java/net/coreprotect/api/QueueLookup.java

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,67 +19,90 @@
1919
import net.coreprotect.utility.StringUtils;
2020
import net.coreprotect.utility.WorldUtils;
2121

22+
/**
23+
* Provides API methods for looking up block-related actions in the processing queue.
24+
* This class allows for retrieving actions that have not yet been saved to the database.
25+
*/
2226
public class QueueLookup extends Queue {
2327

28+
/**
29+
* Private constructor to prevent instantiation.
30+
* This is a utility class with static methods only.
31+
*/
2432
private QueueLookup() {
2533
throw new IllegalStateException("API class");
2634
}
2735

36+
/**
37+
* Performs a lookup of block-related actions in the processing queue for the specified block.
38+
* This allows retrieving actions that have not yet been committed to the database.
39+
*
40+
* @param block
41+
* The block to look up in the processing queue
42+
* @return List of results in a String array format, empty list if API is disabled or no results found
43+
*/
2844
public static List<String[]> performLookup(Block block) {
2945
List<String[]> result = new ArrayList<>();
46+
3047
if (!Config.getGlobal().API_ENABLED) {
3148
return result;
3249
}
3350

51+
if (block == null) {
52+
return result;
53+
}
54+
3455
try {
35-
int consumerCount = 0;
36-
int currentConsumerSize = Process.getCurrentConsumerSize();
37-
if (currentConsumerSize == 0) {
38-
consumerCount = Consumer.getConsumerSize(0) + Consumer.getConsumerSize(1);
39-
}
40-
else {
41-
int consumerId = (Consumer.currentConsumer == 1) ? 1 : 0;
42-
consumerCount = Consumer.getConsumerSize(consumerId) + currentConsumerSize;
43-
}
56+
// Determine total count of actions in the consumer queues
57+
int consumerCount = calculateConsumerCount();
4458

4559
if (consumerCount == 0) {
4660
return result;
4761
}
4862

63+
// Get data from the current consumer
4964
int currentConsumer = Consumer.currentConsumer;
5065
ArrayList<Object[]> consumerData = Consumer.consumer.get(currentConsumer);
5166
Map<Integer, String[]> users = Consumer.consumerUsers.get(currentConsumer);
5267
Map<Integer, Object> consumerObject = Consumer.consumerObjects.get(currentConsumer);
5368

54-
Location oldLocation = block.getLocation();
69+
// Current block location for comparison with actions in the queue
70+
Location blockLocation = block.getLocation();
71+
72+
// Check for block actions in the processing queue
5573
ListIterator<Object[]> iterator = consumerData.listIterator();
5674
while (iterator.hasNext()) {
5775
Object[] data = iterator.next();
5876
int id = (int) data[0];
5977
int action = (int) data[1];
78+
79+
// Only process block break and place actions
6080
if (action != Process.BLOCK_BREAK && action != Process.BLOCK_PLACE) {
6181
continue;
6282
}
6383

6484
String[] userData = users.get(id);
6585
Object objectData = consumerObject.get(id);
66-
if (userData != null && objectData != null && (objectData instanceof BlockState) && ((BlockState) objectData).getLocation().equals(oldLocation)) {
86+
87+
// Verify the action pertains to the requested block
88+
if (isActionForBlock(userData, objectData, blockLocation)) {
6789
Material blockType = (Material) data[2];
6890
int legacyData = (int) data[3];
6991
String blockData = (String) data[7];
7092
String user = userData[0];
7193
BlockState blockState = (BlockState) objectData;
7294
Location location = blockState.getLocation();
73-
int wid = WorldUtils.getWorldId(location.getWorld().getName());
95+
int worldId = WorldUtils.getWorldId(location.getWorld().getName());
7496
int resultType = MaterialUtils.getBlockId(blockType);
7597
int time = (int) (System.currentTimeMillis() / 1000L);
7698

77-
String[] lookupData = new String[] { String.valueOf(time), user, String.valueOf(location.getBlockX()), String.valueOf(location.getBlockY()), String.valueOf(location.getBlockZ()), String.valueOf(resultType), String.valueOf(legacyData), String.valueOf(action), "0", String.valueOf(wid), blockData };
78-
String[] lineData = StringUtils.toStringArray(lookupData);
79-
result.add(lineData);
99+
String[] lookupData = new String[] { String.valueOf(time), user, String.valueOf(location.getBlockX()), String.valueOf(location.getBlockY()), String.valueOf(location.getBlockZ()), String.valueOf(resultType), String.valueOf(legacyData), String.valueOf(action), "0", String.valueOf(worldId), blockData };
100+
101+
result.add(StringUtils.toStringArray(lookupData));
80102
}
81103
}
82104

105+
// Reverse the result list to match database lookup order (most recent first)
83106
Collections.reverse(result);
84107
}
85108
catch (Exception e) {
@@ -89,4 +112,34 @@ public static List<String[]> performLookup(Block block) {
89112
return result;
90113
}
91114

115+
/**
116+
* Calculates the total count of actions in the consumer queues.
117+
*
118+
* @return The total count of actions in the consumer queues
119+
*/
120+
private static int calculateConsumerCount() {
121+
int currentConsumerSize = Process.getCurrentConsumerSize();
122+
if (currentConsumerSize == 0) {
123+
return Consumer.getConsumerSize(0) + Consumer.getConsumerSize(1);
124+
}
125+
else {
126+
int consumerId = (Consumer.currentConsumer == 1) ? 1 : 0;
127+
return Consumer.getConsumerSize(consumerId) + currentConsumerSize;
128+
}
129+
}
130+
131+
/**
132+
* Determines if an action in the queue pertains to the specified block location.
133+
*
134+
* @param userData
135+
* User data associated with the action
136+
* @param objectData
137+
* Object data associated with the action
138+
* @param blockLocation
139+
* Location of the block being looked up
140+
* @return true if the action pertains to the specified block, false otherwise
141+
*/
142+
private static boolean isActionForBlock(String[] userData, Object objectData, Location blockLocation) {
143+
return userData != null && objectData != null && (objectData instanceof BlockState) && ((BlockState) objectData).getLocation().equals(blockLocation);
144+
}
92145
}

0 commit comments

Comments
 (0)