Skip to content

Commit

Permalink
Fix idle notifications for animation changes
Browse files Browse the repository at this point in the history
- Send idle notification only after animation ended and the timeout has
passed, and not start counting the timeout since animation started.
- Change the 1 second schedule to GameTick hook because animations are
changed based on ticks

Fixes runelite#282

Signed-off-by: Tomas Slusny <[email protected]>
  • Loading branch information
deathbeam committed Jan 22, 2018
1 parent 2ad3c28 commit 1115436
Showing 1 changed file with 118 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,19 @@
import com.google.inject.Provides;
import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import javax.inject.Inject;
import net.runelite.api.Actor;
import net.runelite.api.Client;
import net.runelite.api.GameState;
import net.runelite.api.Player;
import net.runelite.api.Skill;
import net.runelite.client.Notifier;
import net.runelite.client.config.ConfigManager;
import net.runelite.api.events.AnimationChanged;
import net.runelite.api.events.GameStateChanged;
import net.runelite.api.events.GameTick;
import net.runelite.client.Notifier;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.task.Schedule;
import net.runelite.client.ui.ClientUI;

@PluginDescriptor(
Expand Down Expand Up @@ -242,7 +241,7 @@ public void onAnimationChanged(AnimationChanged event)
/* Magic */
case MAGIC_CHARGING_ORBS:
notifyIdle = true;
lastAnimating = Instant.now();
lastAnimating = null;
break;
}
}
Expand Down Expand Up @@ -271,54 +270,94 @@ public void onGameStateChanged(GameStateChanged gameStateChanged)
}
}

@Schedule(
period = 1,
unit = ChronoUnit.SECONDS
)
public void checkIdle()
@Subscribe
public void onGameTick(GameTick event)
{
Player local = client.getLocalPlayer();
final Player local = client.getLocalPlayer();
final Duration waitDuration = Duration.ofMillis(config.getTimeout());

if (!config.isEnabled() || client.getGameState() != GameState.LOGGED_IN || local == null)
{
return;
}

if (client.getMouseIdleTicks() > LOGOUT_WARNING_AFTER_TICKS
&& client.getKeyboardIdleTicks() > LOGOUT_WARNING_AFTER_TICKS)
if (checkIdleLogout())
{
if (notifyIdleLogout)
{
sendNotification("[" + local.getName() + "] is about to log out from idling too long!");
notifyIdleLogout = false;
}
sendNotification("[" + local.getName() + "] is about to log out from idling too long!");
}
else

if (check6hrLogout())
{
notifyIdleLogout = true;
sendNotification("[" + local.getName() + "] is about to log out from being online for 6 hours!");
}

if (Instant.now().compareTo(sixHourWarningTime) >= 0)
if (checkAnimationIdle(waitDuration, local))
{
if (notify6HourLogout)
{
sendNotification("[" + local.getName() + "] is about to log out from being online for 6 hours!");
notify6HourLogout = false;
}
sendNotification("[" + local.getName() + "] is now idle!");
}
else

if (checkOutOfCombat(waitDuration, local))
{
notify6HourLogout = true;
sendNotification("[" + local.getName() + "] is now out of combat!");
}

Duration waitDuration = Duration.ofMillis(config.getTimeout());
if (notifyIdle && local.getAnimation() == IDLE
&& Instant.now().compareTo(lastAnimating.plus(waitDuration)) >= 0)
if (checkLowHitpoints(waitDuration))
{
sendNotification("[" + local.getName() + "] is now idle!");
notifyIdle = false;
sendNotification("[" + local.getName() + "] has low hitpoints!");
}

if (checkLowPrayer(waitDuration))
{
sendNotification("[" + local.getName() + "] has low prayer!");
}
}

private boolean checkLowHitpoints(Duration waitDuration)
{
if (client.getRealSkillLevel(Skill.HITPOINTS) > config.getHitpointsThreshold())
{
if (client.getBoostedSkillLevel(Skill.HITPOINTS) <= config.getHitpointsThreshold())
{
if (!notifyHitpoints && Instant.now().compareTo(lastHitpoints.plus(waitDuration)) >= 0)
{
notifyHitpoints = true;
return true;
}
}
else
{
lastHitpoints = Instant.now();
notifyHitpoints = false;
}
}

return false;
}

private boolean checkLowPrayer(Duration waitDuration)
{
if (client.getRealSkillLevel(Skill.PRAYER) > config.getPrayerThreshold())
{
if (client.getBoostedSkillLevel(Skill.PRAYER) <= config.getPrayerThreshold())
{
if (!notifyPrayer && Instant.now().compareTo(lastPrayer.plus(waitDuration)) >= 0)
{
notifyPrayer = true;
return true;
}
}
else
{
lastPrayer = Instant.now();
notifyPrayer = false;
}
}

return false;
}

private boolean checkOutOfCombat(Duration waitDuration, Player local)
{
Actor opponent = local.getInteracting();
boolean isPlayer = opponent instanceof Player;

Expand All @@ -341,43 +380,70 @@ else if (opponent == null)

if (lastInteracting != null && Instant.now().compareTo(lastInteracting.plus(waitDuration)) >= 0)
{
sendNotification("[" + local.getName() + "] is now out of combat!");
lastInteracting = null;
return true;
}

if (client.getRealSkillLevel(Skill.HITPOINTS) > config.getHitpointsThreshold())
return false;
}

private boolean checkIdleLogout()
{
if (client.getMouseIdleTicks() > LOGOUT_WARNING_AFTER_TICKS
&& client.getKeyboardIdleTicks() > LOGOUT_WARNING_AFTER_TICKS)
{
if (client.getBoostedSkillLevel(Skill.HITPOINTS) <= config.getHitpointsThreshold())
if (notifyIdleLogout)
{
if (!notifyHitpoints && Instant.now().compareTo(lastHitpoints.plus(waitDuration)) >= 0)
{
sendNotification("[" + local.getName() + "] has low hitpoints!");
notifyHitpoints = true;
}
notifyIdleLogout = false;
return true;
}
else
}
else
{
notifyIdleLogout = true;
}

return false;
}

private boolean check6hrLogout()
{
if (Instant.now().compareTo(sixHourWarningTime) >= 0)
{
if (notify6HourLogout)
{
lastHitpoints = Instant.now();
notifyHitpoints = false;
notify6HourLogout = false;
return true;
}
}
else
{
notify6HourLogout = true;
}

if (client.getRealSkillLevel(Skill.PRAYER) > config.getPrayerThreshold())
return false;
}

private boolean checkAnimationIdle(Duration waitDuration, Player local)
{
if (notifyIdle)
{
if (client.getBoostedSkillLevel(Skill.PRAYER) <= config.getPrayerThreshold())
if (lastAnimating != null)
{
if (!notifyPrayer && Instant.now().compareTo(lastPrayer.plus(waitDuration)) >= 0)
if (Instant.now().compareTo(lastAnimating.plus(waitDuration)) >= 0)
{
sendNotification("[" + local.getName() + "] has low prayer!");
notifyPrayer = true;
notifyIdle = false;
lastAnimating = null;
return true;
}
}
else
else if (local.getAnimation() == IDLE)
{
lastPrayer = Instant.now();
notifyPrayer = false;
lastAnimating = Instant.now();
}
}

return false;
}

private void sendNotification(String message)
Expand Down

0 comments on commit 1115436

Please sign in to comment.