Skip to content

Commit 7c5df69

Browse files
Add files via upload
1 parent 9dfd97f commit 7c5df69

100 files changed

Lines changed: 17273 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

api/BlockAPI.java

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package net.coreprotect.api;
2+
3+
import java.sql.Connection;
4+
import java.sql.ResultSet;
5+
import java.sql.Statement;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
import org.bukkit.block.Block;
10+
11+
import net.coreprotect.config.Config;
12+
import net.coreprotect.config.ConfigHandler;
13+
import net.coreprotect.database.Database;
14+
import net.coreprotect.database.statement.UserStatement;
15+
import net.coreprotect.utility.BlockUtils;
16+
import net.coreprotect.utility.StringUtils;
17+
import net.coreprotect.utility.WorldUtils;
18+
19+
/**
20+
* Provides API methods for block-related lookups in the CoreProtect database.
21+
*/
22+
public class BlockAPI {
23+
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+
*/
41+
public static List<String[]> performLookup(Block block, int offset) {
42+
List<String[]> result = new ArrayList<>();
43+
44+
if (!Config.getGlobal().API_ENABLED) {
45+
return result;
46+
}
47+
48+
if (block == null) {
49+
return result;
50+
}
51+
52+
try (Connection connection = Database.getConnection(false, 1000)) {
53+
if (connection == null) {
54+
return result;
55+
}
56+
57+
int x = block.getX();
58+
int y = block.getY();
59+
int z = block.getZ();
60+
int time = (int) (System.currentTimeMillis() / 1000L);
61+
int worldId = WorldUtils.getWorldId(block.getWorld().getName());
62+
int checkTime = 0;
63+
64+
if (offset > 0) {
65+
checkTime = time - offset;
66+
}
67+
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";
70+
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+
}
84+
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+
}
93+
}
94+
}
95+
catch (Exception e) {
96+
e.printStackTrace();
97+
}
98+
99+
return result;
100+
}
101+
}

api/QueueLookup.java

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package net.coreprotect.api;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.ListIterator;
7+
import java.util.Map;
8+
9+
import org.bukkit.Location;
10+
import org.bukkit.Material;
11+
import org.bukkit.block.Block;
12+
import org.bukkit.block.BlockState;
13+
14+
import net.coreprotect.config.Config;
15+
import net.coreprotect.consumer.Consumer;
16+
import net.coreprotect.consumer.Queue;
17+
import net.coreprotect.consumer.process.Process;
18+
import net.coreprotect.utility.MaterialUtils;
19+
import net.coreprotect.utility.StringUtils;
20+
import net.coreprotect.utility.WorldUtils;
21+
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+
*/
26+
public class QueueLookup extends Queue {
27+
28+
/**
29+
* Private constructor to prevent instantiation.
30+
* This is a utility class with static methods only.
31+
*/
32+
private QueueLookup() {
33+
throw new IllegalStateException("API class");
34+
}
35+
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+
*/
44+
public static List<String[]> performLookup(Block block) {
45+
List<String[]> result = new ArrayList<>();
46+
47+
if (!Config.getGlobal().API_ENABLED) {
48+
return result;
49+
}
50+
51+
if (block == null) {
52+
return result;
53+
}
54+
55+
try {
56+
// Determine total count of actions in the consumer queues
57+
int consumerCount = calculateConsumerCount();
58+
59+
if (consumerCount == 0) {
60+
return result;
61+
}
62+
63+
// Get data from the current consumer
64+
int currentConsumer = Consumer.currentConsumer;
65+
ArrayList<Object[]> consumerData = Consumer.consumer.get(currentConsumer);
66+
Map<Integer, String[]> users = Consumer.consumerUsers.get(currentConsumer);
67+
Map<Integer, Object> consumerObject = Consumer.consumerObjects.get(currentConsumer);
68+
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
73+
ListIterator<Object[]> iterator = consumerData.listIterator();
74+
while (iterator.hasNext()) {
75+
Object[] data = iterator.next();
76+
int id = (int) data[0];
77+
int action = (int) data[1];
78+
79+
// Only process block break and place actions
80+
if (action != Process.BLOCK_BREAK && action != Process.BLOCK_PLACE) {
81+
continue;
82+
}
83+
84+
String[] userData = users.get(id);
85+
Object objectData = consumerObject.get(id);
86+
87+
// Verify the action pertains to the requested block
88+
if (isActionForBlock(userData, objectData, blockLocation)) {
89+
Material blockType = (Material) data[2];
90+
int legacyData = (int) data[3];
91+
String blockData = (String) data[7];
92+
String user = userData[0];
93+
BlockState blockState = (BlockState) objectData;
94+
Location location = blockState.getLocation();
95+
int worldId = WorldUtils.getWorldId(location.getWorld().getName());
96+
int resultType = MaterialUtils.getBlockId(blockType);
97+
int time = (int) (System.currentTimeMillis() / 1000L);
98+
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));
102+
}
103+
}
104+
105+
// Reverse the result list to match database lookup order (most recent first)
106+
Collections.reverse(result);
107+
}
108+
catch (Exception e) {
109+
e.printStackTrace();
110+
}
111+
112+
return result;
113+
}
114+
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+
}
145+
}

0 commit comments

Comments
 (0)