loot tracker plugin: submit loot on client shutdown (#11243)

This commit is contained in:
Trevor
2020-04-10 17:51:09 -04:00
committed by GitHub
parent 242ea42964
commit fae69d4fd2
2 changed files with 28 additions and 5 deletions

View File

@@ -33,6 +33,7 @@ import java.io.InputStreamReader;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.runelite.http.api.RuneLiteAPI; import net.runelite.http.api.RuneLiteAPI;
@@ -52,8 +53,10 @@ public class LootTrackerClient
private final UUID uuid; private final UUID uuid;
public void submit(Collection<LootRecord> lootRecords) public CompletableFuture<Void> submit(Collection<LootRecord> lootRecords)
{ {
CompletableFuture<Void> future = new CompletableFuture<>();
HttpUrl url = RuneLiteAPI.getApiBase().newBuilder() HttpUrl url = RuneLiteAPI.getApiBase().newBuilder()
.addPathSegment("loottracker") .addPathSegment("loottracker")
.build(); .build();
@@ -70,6 +73,7 @@ public class LootTrackerClient
public void onFailure(Call call, IOException e) public void onFailure(Call call, IOException e)
{ {
log.warn("unable to submit loot", e); log.warn("unable to submit loot", e);
future.completeExceptionally(e);
} }
@Override @Override
@@ -77,8 +81,11 @@ public class LootTrackerClient
{ {
log.debug("Submitted loot"); log.debug("Submitted loot");
response.close(); response.close();
future.complete(null);
} }
}); });
return future;
} }
public Collection<LootAggregate> get() throws IOException public Collection<LootAggregate> get() throws IOException

View File

@@ -46,10 +46,13 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.annotation.Nullable;
import javax.inject.Inject; import javax.inject.Inject;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import lombok.AccessLevel; import lombok.AccessLevel;
@@ -80,6 +83,7 @@ import net.runelite.client.callback.ClientThread;
import net.runelite.client.config.ConfigManager; import net.runelite.client.config.ConfigManager;
import net.runelite.client.eventbus.EventBus; import net.runelite.client.eventbus.EventBus;
import net.runelite.client.eventbus.Subscribe; import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.events.ClientShutdown;
import net.runelite.client.events.ConfigChanged; import net.runelite.client.events.ConfigChanged;
import net.runelite.client.events.NpcLootReceived; import net.runelite.client.events.NpcLootReceived;
import net.runelite.client.events.PlayerLootReceived; import net.runelite.client.events.PlayerLootReceived;
@@ -340,6 +344,16 @@ public class LootTrackerPlugin extends Plugin
chestLooted = false; chestLooted = false;
} }
@Subscribe
public void onClientShutdown(ClientShutdown event)
{
Future<Void> future = submitLoot();
if (future != null)
{
event.waitFor(future);
}
}
@Subscribe @Subscribe
public void onGameStateChanged(final GameStateChanged event) public void onGameStateChanged(final GameStateChanged event)
{ {
@@ -629,14 +643,15 @@ public class LootTrackerPlugin extends Plugin
submitLoot(); submitLoot();
} }
private void submitLoot() @Nullable
private CompletableFuture<Void> submitLoot()
{ {
List<LootRecord> copy; List<LootRecord> copy;
synchronized (queuedLoots) synchronized (queuedLoots)
{ {
if (queuedLoots.isEmpty()) if (queuedLoots.isEmpty())
{ {
return; return null;
} }
copy = new ArrayList<>(queuedLoots); copy = new ArrayList<>(queuedLoots);
@@ -645,12 +660,13 @@ public class LootTrackerPlugin extends Plugin
if (lootTrackerClient == null || !config.saveLoot()) if (lootTrackerClient == null || !config.saveLoot())
{ {
return; return null;
} }
log.debug("Submitting {} loot records", copy.size()); log.debug("Submitting {} loot records", copy.size());
lootTrackerClient.submit(copy); CompletableFuture<Void> future = lootTrackerClient.submit(copy);
return future;
} }
private void takeInventorySnapshot() private void takeInventorySnapshot()