diff --git a/src/main/java/net/coreprotect/CoreProtectAPI.java b/src/main/java/net/coreprotect/CoreProtectAPI.java index d51bc99..4d86f65 100755 --- a/src/main/java/net/coreprotect/CoreProtectAPI.java +++ b/src/main/java/net/coreprotect/CoreProtectAPI.java @@ -16,6 +16,7 @@ import org.bukkit.block.data.BlockData; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; +import net.coreprotect.api.QueueLookup; import net.coreprotect.api.SessionLookup; import net.coreprotect.config.Config; import net.coreprotect.consumer.Queue; @@ -175,6 +176,10 @@ public class CoreProtectAPI extends Queue { return null; } + public List queueLookup(Block block) { + return QueueLookup.performLookup(block); + } + public List sessionLookup(String user, int time) { return SessionLookup.performLookup(user, time); } diff --git a/src/main/java/net/coreprotect/api/QueueLookup.java b/src/main/java/net/coreprotect/api/QueueLookup.java new file mode 100644 index 0000000..5befd1f --- /dev/null +++ b/src/main/java/net/coreprotect/api/QueueLookup.java @@ -0,0 +1,85 @@ +package net.coreprotect.api; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockState; + +import net.coreprotect.config.Config; +import net.coreprotect.consumer.Consumer; +import net.coreprotect.consumer.Queue; +import net.coreprotect.consumer.process.Process; +import net.coreprotect.utility.Util; + +public class QueueLookup extends Queue { + + private QueueLookup() { + throw new IllegalStateException("API class"); + } + + public static List performLookup(Block block) { + List result = new ArrayList<>(); + if (!Config.getGlobal().API_ENABLED) { + return result; + } + + try { + int consumerCount = 0; + int currentConsumerSize = Process.getCurrentConsumerSize(); + if (currentConsumerSize == 0) { + consumerCount = Consumer.getConsumerSize(0) + Consumer.getConsumerSize(1); + } + else { + int consumerId = (Consumer.currentConsumer == 1) ? 1 : 0; + consumerCount = Consumer.getConsumerSize(consumerId) + currentConsumerSize; + } + + if (consumerCount == 0) { + return result; + } + + int currentConsumer = Consumer.currentConsumer; + ArrayList consumerData = Consumer.consumer.get(currentConsumer); + Map users = Consumer.consumerUsers.get(currentConsumer); + Map consumerObject = Consumer.consumerObjects.get(currentConsumer); + + Location oldLocation = block.getLocation(); + for (Object[] data : consumerData) { + int id = (int) data[0]; + int action = (int) data[1]; + if (action != Process.BLOCK_BREAK && action != Process.BLOCK_PLACE) { + continue; + } + + String[] userData = users.get(id); + Object objectData = consumerObject.get(id); + if (userData != null && objectData != null && (objectData instanceof BlockState) && ((BlockState) objectData).getLocation().equals(oldLocation)) { + Material blockType = (Material) data[2]; + int legacyData = (int) data[3]; + String blockData = (String) data[7]; + String user = userData[0]; + BlockState blockState = (BlockState) objectData; + Location location = blockState.getLocation(); + int wid = Util.getWorldId(location.getWorld().getName()); + int resultType = Util.getBlockId(blockType); + int time = (int) (System.currentTimeMillis() / 1000L); + + 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 }; + String[] lineData = Util.toStringArray(lookupData); + result.add(lineData); + } + } + + } + catch (Exception e) { + e.printStackTrace(); + } + + return result; + } + +} diff --git a/src/main/java/net/coreprotect/consumer/Queue.java b/src/main/java/net/coreprotect/consumer/Queue.java index ce88f0d..7ccb626 100755 --- a/src/main/java/net/coreprotect/consumer/Queue.java +++ b/src/main/java/net/coreprotect/consumer/Queue.java @@ -57,7 +57,7 @@ public class Queue { Consumer.consumer.get(currentConsumer).add(data); } - private static void queueStandardData(int consumerId, int currentConsumer, String[] user, Object object) { + private static synchronized void queueStandardData(int consumerId, int currentConsumer, String[] user, Object object) { Consumer.consumerUsers.get(currentConsumer).put(consumerId, user); Consumer.consumerObjects.get(currentConsumer).put(consumerId, object); Consumer.consumer_id.put(currentConsumer, new Integer[] { Consumer.consumer_id.get(currentConsumer)[0], 0 });