package net.buycraft.plugin.execution;

import com.google.common.collect.Sets;
import lombok.RequiredArgsConstructor;
import net.buycraft.plugin.IBuycraftPlatform;
import net.buycraft.plugin.client.ApiException;
import net.buycraft.plugin.data.QueuedCommand;
import net.buycraft.plugin.data.responses.QueueInformation;
import net.buycraft.plugin.execution.strategy.ToRunQueuedCommand;

import java.io.IOException;
import java.util.*;
import java.util.logging.Level;

@RequiredArgsConstructor
public class ImmediateExecutionRunner implements Runnable {
    private final IBuycraftPlatform platform;
    private final Set<Integer> executingLater = Sets.newConcurrentHashSet();
    private final Random random = new Random();

    @Override
    public void run() {
        if (platform.getApiClient() == null) {
            return; // no API client
        }

        QueueInformation information;

        do {
            try {
                information = platform.getApiClient().retrieveOfflineQueue();
            } catch (IOException | ApiException e) {
                platform.log(Level.SEVERE, "Could not fetch command queue", e);
                return;
            }

            // Filter out commands we're going to execute at a later time.
            for (Iterator<QueuedCommand> it = information.getCommands().iterator(); it.hasNext(); ) {
                QueuedCommand command = it.next();
                if (executingLater.contains(command.getId()))
                    it.remove();
            }

            // Nothing to do? Then let's exit.
            if (information.getCommands().isEmpty()) {
                return;
            }

            // Queue commands for later.
            for (QueuedCommand command : information.getCommands()) {
                platform.getExecutor().queue(new ToRunQueuedCommand(command.getPlayer(), command, false));
            }

            // Sleep for between 0.5-1.5 seconds to help spread load.
            try {
                Thread.sleep(500 + random.nextInt(1000));
            } catch (InterruptedException e) {
                // Shouldn't happen, but in that case just bail out.
                return;
            }
        } while (information.getMeta().isLimited());
    }
}

@@ -427,16 +427,63 @@
 util.*;%0A
+import java.util.concurrent.ConcurrentHashMap;%0A
 import j
@@ -506,16 +506,16 @@
 .Level;%0A
-
 %0A@Requir
@@ -638,24 +638,58 @@
 atform;%0A    
+@SuppressWarnings(%22deprecation%22)%0A%09
 private fina
@@ -732,26 +732,60 @@
 .new
-ConcurrentHashSet(
+SetFromMap(new ConcurrentHashMap%3CInteger, Boolean%3E()
 );%0A 

Fix for 1.7.10 (guava).