From f31e366da66e3333904bdf1a407bdcf9791a3ac1 Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 17 Oct 2019 18:50:25 -0400 Subject: [PATCH] random events: fix menu being hidden for own events This changes the plugin to work by just using interact change events, which will happen regardless of the spawn events and whether or not it is interacting at the time of spawn. This fixes two bugs in the existing implementation: 1) not updating lastEventTick on interact change, causing currentRandomEvent to get timed out near immediately 2) not sending notifications for npcs which spawn while interacting --- .../randomevents/RandomEventPlugin.java | 74 +++++++------------ 1 file changed, 25 insertions(+), 49 deletions(-) diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/randomevents/RandomEventPlugin.java b/runelite-client/src/main/java/net/runelite/client/plugins/randomevents/RandomEventPlugin.java index 9c9c75b778..bc9cb52977 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/randomevents/RandomEventPlugin.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/randomevents/RandomEventPlugin.java @@ -28,20 +28,18 @@ package net.runelite.client.plugins.randomevents; import com.google.common.collect.ImmutableSet; import com.google.inject.Provides; import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; import java.util.Set; import javax.inject.Inject; +import lombok.extern.slf4j.Slf4j; import net.runelite.api.Actor; import net.runelite.api.Client; import net.runelite.api.MenuAction; import net.runelite.api.NPC; import net.runelite.api.NpcID; import net.runelite.api.Player; -import net.runelite.api.events.GameTick; import net.runelite.api.events.InteractingChanged; import net.runelite.api.events.MenuEntryAdded; -import net.runelite.api.events.NpcSpawned; +import net.runelite.api.events.NpcDespawned; import net.runelite.client.Notifier; import net.runelite.client.config.ConfigManager; import net.runelite.client.eventbus.Subscribe; @@ -53,6 +51,7 @@ import net.runelite.client.plugins.PluginDescriptor; description = "Notify when random events appear and remove talk/dismiss options on events that aren't yours.", enabledByDefault = false ) +@Slf4j public class RandomEventPlugin extends Plugin { private static final Set EVENT_NPCS = ImmutableSet.of( @@ -85,10 +84,8 @@ public class RandomEventPlugin extends Plugin ); private static final int RANDOM_EVENT_TIMEOUT = 150; - private Map spawnedNpcs = new HashMap<>(); private NPC currentRandomEvent; - // event npcs teleport to you to stay in range, we need to throttle spawns - private int lastEventTick = -RANDOM_EVENT_TIMEOUT; + private int lastNotificationTick = -RANDOM_EVENT_TIMEOUT; // to avoid double notifications @Inject private Client client; @@ -108,34 +105,8 @@ public class RandomEventPlugin extends Plugin @Override protected void shutDown() throws Exception { - lastEventTick = 0; + lastNotificationTick = 0; currentRandomEvent = null; - spawnedNpcs.clear(); - } - - @Subscribe - public void onNpcSpawned(NpcSpawned event) - { - NPC npc = event.getNpc(); - - if (!EVENT_NPCS.contains(npc.getId())) - { - return; - } - - // only occasionally do event npcs spawn with non-null interacting - if (npc.getInteracting() == client.getLocalPlayer()) - { - if (client.getTickCount() - lastEventTick > RANDOM_EVENT_TIMEOUT) - { - currentRandomEvent = npc; - lastEventTick = client.getTickCount(); - } - } - else - { - spawnedNpcs.put(npc, client.getTickCount()); - } } @Subscribe @@ -143,31 +114,36 @@ public class RandomEventPlugin extends Plugin { Actor source = event.getSource(); Actor target = event.getTarget(); + Player player = client.getLocalPlayer(); - if (spawnedNpcs.containsKey(source)) + // Check that the npc is interacting with the player and the player isn't interacting with the npc, so + // that the notification doesn't fire from talking to other user's randoms + if (target != player || player.getInteracting() == source || !(source instanceof NPC) || !EVENT_NPCS.contains(((NPC) source).getId())) { - Player player = client.getLocalPlayer(); - if (player == target && client.getTickCount() - lastEventTick > RANDOM_EVENT_TIMEOUT) + return; + } + + log.debug("Random event spawn: {}", source.getName()); + + currentRandomEvent = (NPC) source; + + if (client.getTickCount() - lastNotificationTick > RANDOM_EVENT_TIMEOUT) + { + lastNotificationTick = client.getTickCount(); + + if (shouldNotify(currentRandomEvent.getId())) { - currentRandomEvent = (NPC) source; - if (shouldNotify(currentRandomEvent.getId())) - { - notifier.notify("Random event spawned: " + currentRandomEvent.getName()); - } + notifier.notify("Random event spawned: " + currentRandomEvent.getName()); } } } @Subscribe - public void onGameTick(GameTick event) + public void onNpcDespawned(NpcDespawned npcDespawned) { - if (!spawnedNpcs.isEmpty()) - { - // allow 2 ticks for interacting to get set - spawnedNpcs.entrySet().removeIf(entry -> client.getTickCount() - entry.getValue() >= 2); - } + NPC npc = npcDespawned.getNpc(); - if (client.getTickCount() - lastEventTick > RANDOM_EVENT_TIMEOUT) + if (npc == currentRandomEvent) { currentRandomEvent = null; }