1919import net .coreprotect .utility .StringUtils ;
2020import 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+ */
2226public 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