hydra: optimize eventbus subscriptions

This commit is contained in:
Lucwousin
2019-07-24 09:00:03 +02:00
parent 4264aeecaf
commit ea061fd473

View File

@@ -28,6 +28,7 @@ import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
@@ -50,6 +51,7 @@ import net.runelite.client.eventbus.EventBus;
import net.runelite.client.plugins.Plugin; import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor; import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.plugins.PluginType; import net.runelite.client.plugins.PluginType;
import net.runelite.client.plugins.alchemicalhydra.Hydra.AttackStyle;
import net.runelite.client.ui.overlay.OverlayManager; import net.runelite.client.ui.overlay.OverlayManager;
@PluginDescriptor( @PluginDescriptor(
@@ -95,8 +97,7 @@ public class HydraPlugin extends Plugin
@Override @Override
protected void startUp() protected void startUp()
{ {
addSubscriptions(); eventBus.subscribe(GameStateChanged.class, this, this::onGameStateChanged);
inHydraInstance = checkArea(); inHydraInstance = checkArea();
lastAttackTick = -1; lastAttackTick = -1;
poisonProjectiles.clear(); poisonProjectiles.clear();
@@ -106,6 +107,8 @@ public class HydraPlugin extends Plugin
protected void shutDown() protected void shutDown()
{ {
eventBus.unregister(this); eventBus.unregister(this);
eventBus.unregister("fight");
eventBus.unregister("npcSpawned");
inHydraInstance = false; inHydraInstance = false;
hydra = null; hydra = null;
@@ -114,13 +117,11 @@ public class HydraPlugin extends Plugin
lastAttackTick = -1; lastAttackTick = -1;
} }
private void addSubscriptions() private void addFightSubscriptions()
{ {
eventBus.subscribe(GameStateChanged.class, this, this::onGameStateChanged); eventBus.subscribe(AnimationChanged.class, "fight", this::onAnimationChanged);
eventBus.subscribe(NpcSpawned.class, this, this::onNpcSpawned); eventBus.subscribe(ProjectileMoved.class, "fight", this::onProjectileMoved);
eventBus.subscribe(AnimationChanged.class, this, this::onAnimationChanged); eventBus.subscribe(ChatMessage.class, "fight", this::onChatMessage);
eventBus.subscribe(ProjectileMoved.class, this, this::onProjectileMoved);
eventBus.subscribe(ChatMessage.class, this, this::onChatMessage);
} }
private void onGameStateChanged(GameStateChanged state) private void onGameStateChanged(GameStateChanged state)
@@ -140,14 +141,20 @@ public class HydraPlugin extends Plugin
removeOverlays(); removeOverlays();
hydra = null; hydra = null;
} }
eventBus.unregister("npcSpawned");
eventBus.unregister("fight");
return; return;
} }
eventBus.subscribe(NpcSpawned.class, "npcSpawned", this::onNpcSpawned);
for (NPC npc : client.getNpcs()) for (NPC npc : client.getNpcs())
{ {
if (npc.getId() == NpcID.ALCHEMICAL_HYDRA) if (npc.getId() == NpcID.ALCHEMICAL_HYDRA)
{ {
hydra = new Hydra(npc); hydra = new Hydra(npc);
addFightSubscriptions();
break; break;
} }
} }
@@ -157,12 +164,14 @@ public class HydraPlugin extends Plugin
private void onNpcSpawned(NpcSpawned event) private void onNpcSpawned(NpcSpawned event)
{ {
if (!inHydraInstance || event.getNpc().getId() != NpcID.ALCHEMICAL_HYDRA) if (event.getNpc().getId() != NpcID.ALCHEMICAL_HYDRA)
{ {
return; return;
} }
eventBus.unregister("npcSpawned");
hydra = new Hydra(event.getNpc()); hydra = new Hydra(event.getNpc());
addFightSubscriptions();
addOverlays(); addOverlays();
} }
@@ -196,6 +205,8 @@ public class HydraPlugin extends Plugin
case FOUR: case FOUR:
hydra = null; hydra = null;
poisonProjectiles.clear(); poisonProjectiles.clear();
eventBus.unregister("fight");
eventBus.subscribe(NpcSpawned.class, "npcSpawned", this::onNpcSpawned);
removeOverlays(); removeOverlays();
return; return;
default: default:
@@ -215,7 +226,7 @@ public class HydraPlugin extends Plugin
} }
Set<LocalPoint> exPoisonProjectiles = new HashSet<>(); Set<LocalPoint> exPoisonProjectiles = new HashSet<>();
for (Map.Entry<LocalPoint, Projectile> entry : poisonProjectiles.entrySet()) for (Entry<LocalPoint, Projectile> entry : poisonProjectiles.entrySet())
{ {
if (entry.getValue().getEndCycle() < client.getGameCycle()) if (entry.getValue().getEndCycle() < client.getGameCycle())
{ {
@@ -250,7 +261,7 @@ public class HydraPlugin extends Plugin
poisonProjectiles.put(event.getPosition(), projectile); poisonProjectiles.put(event.getPosition(), projectile);
} }
else if (client.getTickCount() != lastAttackTick else if (client.getTickCount() != lastAttackTick
&& (id == Hydra.AttackStyle.MAGIC.getProjectileID() || id == Hydra.AttackStyle.RANGED.getProjectileID())) && (id == AttackStyle.MAGIC.getProjectileID() || id == AttackStyle.RANGED.getProjectileID()))
{ {
hydra.handleAttack(id); hydra.handleAttack(id);
lastAttackTick = client.getTickCount(); lastAttackTick = client.getTickCount();