/*
 * Copyright (c) 2018 Abex
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package net.runelite.client.callback;

import com.google.inject.Inject;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;
import javax.inject.Singleton;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client;

@Singleton
@Slf4j
public class ClientThread
{
	private ConcurrentLinkedQueue<Runnable> invokes = new ConcurrentLinkedQueue<>();

	@Inject
	private Client client;

	public void invokeLater(Runnable r)
	{
		if (client.isClientThread())
		{
			r.run();
			return;
		}
		invokes.add(r);
	}

	void invoke()
	{
		assert client.isClientThread();
		Iterator<Runnable> ir = invokes.iterator();
		for (; ir.hasNext(); )
		{
			Runnable r = ir.next();
			ir.remove();
			try
			{
				r.run();
			}
			catch (ThreadDeath d)
			{
				throw d;
			}
			catch (Throwable e)
			{
				log.warn("Exception in invokeLater", e);
			}
		}
	}
}

@@ -1502,16 +1502,59 @@
 dQueue;%0A
+import java.util.function.BooleanSupplier;%0A
 import j
@@ -1720,24 +1720,31 @@
 edQueue%3C
-Runnable
+BooleanSupplier
 %3E invoke
@@ -1860,51 +1860,348 @@
 %0A%09%09i
-f (client.isClientThread())%0A%09%09%7B%0A%09%09%09r.run();
+nvokeLater(() -%3E%0A%09%09%7B%0A%09%09%09r.run();%0A%09%09%09return true;%0A%09%09%7D);%0A%09%7D%0A%0A%09/**%0A%09 * Will run r on the game thread, at a unspecified point in the future.%0A%09 * If r returns false, r will be ran again, at a later point%0A%09 */%0A%09public void invokeLater(BooleanSupplier r)%0A%09%7B%0A%09%09if (client.isClientThread())%0A%09%09%7B%0A%09%09%09if (r.getAsBoolean())%0A%09%09%09%7B%0A%09%09%09%09invokes.add(r);%0A%09%09%09%7D
 %0A%09%09%09
@@ -2297,24 +2297,31 @@
 terator%3C
-Runnable
+BooleanSupplier
 %3E ir = i
@@ -2371,24 +2371,31 @@
 %0A%09%09%7B%0A%09%09%09
-Runnable
+BooleanSupplier
  r = ir.
@@ -2409,41 +2409,69 @@
 %0A%09%09%09
-ir.remove();%0A%09%09%09try%0A%09%09%09%7B%0A%09%09%09%09r.ru
+boolean remove = true;%0A%09%09%09try%0A%09%09%09%7B%0A%09%09%09%09remove = r.getAsBoolea
 n();
@@ -2602,16 +2602,58 @@
 );%0A%09%09%09%7D%0A
+%09%09%09if (remove)%0A%09%09%09%7B%0A%09%09%09%09ir.remove();%0A%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%7D

Allow ClientThread to re-run invokeLater-ed methods