Skip to content

Commit

Permalink
Fix CoreProtect delay caused by queue
Browse files Browse the repository at this point in the history
Closes #162
  • Loading branch information
Krakenied authored and LMBishop committed Dec 21, 2023
1 parent ee59747 commit 98e55ac
Showing 1 changed file with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.leonardobishop.quests.bukkit.BukkitQuestsPlugin;
import net.coreprotect.CoreProtect;
import net.coreprotect.CoreProtectAPI;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;

import java.util.List;
Expand All @@ -16,25 +15,50 @@ public class CoreProtectHook implements AbstractCoreProtectHook {

public CoreProtectHook(BukkitQuestsPlugin plugin) {
this.plugin = plugin;
api = ((CoreProtect) Bukkit.getPluginManager().getPlugin("CoreProtect")).getAPI();
this.api = CoreProtect.getInstance().getAPI();
}

@Override
public CompletableFuture<Boolean> checkBlock(Block block, int time) {
CompletableFuture<Boolean> future = new CompletableFuture<>();
plugin.getScheduler().doAsync(() -> {
List<String[]> lookup = api.blockLookup(block, time);
if (lookup.isEmpty()) {
plugin.getScheduler().doSync(() -> future.complete(false));
} else {
String[] result = lookup.get(0);
List<String[]> queueLookup = api.queueLookup(block);
if (queueLookup.size() >= 2) {
// first queue element when breaking a block is always
// a break action so we just get the second one
String[] result = queueLookup.get(1);
CoreProtectAPI.ParseResult parseResult = api.parseResult(result);
boolean value = !parseResult.getPlayer().isEmpty() && parseResult.getActionId() == 1;

plugin.getScheduler().doSync(() -> future.complete(value));
// queue lookup returns only break and place actions
// so we dont need to skip all the interations (action id 2)
// https://github.com/PlayPro/CoreProtect/blob/master/src/main/java/net/coreprotect/api/QueueLookup.java#L55
if (!parseResult.getPlayer().isEmpty() && parseResult.getActionId() == 1) {
plugin.getScheduler().doSync(() -> future.complete(true));
return;
}
}

List<String[]> blockLookup = api.blockLookup(block, time);
for (String[] result : blockLookup) {
CoreProtectAPI.ParseResult parseResult = api.parseResult(result);

// we need to skip all the interations like door opening (action id 2)
if (parseResult.getActionId() == 2) {
continue;
}

// due to the order the first element that is not
// an interaction is always the one we need to check
// (0=removed, 1=placed, 2=interaction)
// https://docs.coreprotect.net/api/version/v9/#parseresult-parseresultstring-result
boolean placed = parseResult.getActionId() == 1;

plugin.getScheduler().doSync(() -> future.complete(placed));
return;
}

plugin.getScheduler().doSync(() -> future.complete(false));
});
return future;
}

}

0 comments on commit 98e55ac

Please sign in to comment.