|
| 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