Skip to content

Commit

Permalink
attack styles: add defensive casting to weapon types for staffs
Browse files Browse the repository at this point in the history
Fix race with accessing client vars on startUp
  • Loading branch information
Adam- committed Jun 19, 2018
1 parent 760927e commit e6a2848
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client;
import net.runelite.api.GameState;
import net.runelite.api.VarPlayer;
import net.runelite.api.Skill;
import net.runelite.api.VarPlayer;
import net.runelite.api.Varbits;
import net.runelite.api.events.ConfigChanged;
import net.runelite.api.events.GameStateChanged;
Expand All @@ -46,6 +46,7 @@
import static net.runelite.api.widgets.WidgetID.COMBAT_GROUP_ID;
import net.runelite.api.widgets.WidgetInfo;
import static net.runelite.api.widgets.WidgetInfo.TO_GROUP;
import net.runelite.client.callback.ClientThread;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor;
Expand All @@ -71,6 +72,9 @@ public class AttackStylesPlugin extends Plugin
@Inject
private Client client;

@Inject
private ClientThread clientThread;

@Inject
private AttackStylesConfig config;

Expand All @@ -93,20 +97,28 @@ protected void startUp() throws Exception

if (client.getGameState() == GameState.LOGGED_IN)
{
updateWarnedSkills(config.warnForAttack(), Skill.ATTACK);
updateWarnedSkills(config.warnForStrength(), Skill.STRENGTH);
updateWarnedSkills(config.warnForDefence(), Skill.DEFENCE);
updateWarnedSkills(config.warnForRanged(), Skill.RANGED);
updateWarnedSkills(config.warnForMagic(), Skill.MAGIC);
updateAttackStyle(
client.getVar(Varbits.EQUIPPED_WEAPON_TYPE),
client.getVar(VarPlayer.ATTACK_STYLE),
client.getVar(Varbits.DEFENSIVE_CASTING_MODE));
updateWarning(false);
processWidgets();
clientThread.invokeLater(this::start);
}
}

private void start()
{
updateWarnedSkills(config.warnForAttack(), Skill.ATTACK);
updateWarnedSkills(config.warnForStrength(), Skill.STRENGTH);
updateWarnedSkills(config.warnForDefence(), Skill.DEFENCE);
updateWarnedSkills(config.warnForRanged(), Skill.RANGED);
updateWarnedSkills(config.warnForMagic(), Skill.MAGIC);
attackStyleVarbit = client.getVar(VarPlayer.ATTACK_STYLE);
equippedWeaponTypeVarbit = client.getVar(Varbits.EQUIPPED_WEAPON_TYPE);
castingModeVarbit = client.getVar(Varbits.DEFENSIVE_CASTING_MODE);
updateAttackStyle(
equippedWeaponTypeVarbit,
attackStyleVarbit,
castingModeVarbit);
updateWarning(false);
processWidgets();
}

@Override
protected void shutDown()
{
Expand All @@ -133,11 +145,6 @@ public void hideWidgets(WidgetHiddenChanged event)
return;
}

if (widgetsToHide == null)
{
return;
}

processWidgets();
}

Expand Down Expand Up @@ -301,13 +308,14 @@ private void hideWarnedStyles(boolean enabled)
// Iterate over attack styles
for (int i = 0; i < attackStyles.length; i++)
{
if (attackStyles[i] == null)
AttackStyle attackStyle = attackStyles[i];
if (attackStyle == null)
{
continue;
}

boolean warnedSkill = false;
for (Skill skill : attackStyles[i].getSkills())
for (Skill skill : attackStyle.getSkills())
{
if (warnedSkills.contains(skill))
{
Expand All @@ -317,12 +325,12 @@ private void hideWarnedStyles(boolean enabled)
}

// Magic staves defensive casting mode
if (equippedWeaponType == WeaponType.TYPE_18)
if (attackStyle == AttackStyle.DEFENSIVE_CASTING || !enabled)
{
widgetsToHide.put(equippedWeaponType, WidgetInfo.COMBAT_DEFENSIVE_SPELL_BOX, enabled && (warnedSkills.contains(Skill.DEFENCE) || warnedSkill));
widgetsToHide.put(equippedWeaponType, WidgetInfo.COMBAT_DEFENSIVE_SPELL_ICON, enabled && (warnedSkills.contains(Skill.DEFENCE) || warnedSkill));
widgetsToHide.put(equippedWeaponType, WidgetInfo.COMBAT_DEFENSIVE_SPELL_SHIELD, enabled && (warnedSkills.contains(Skill.DEFENCE) || warnedSkill));
widgetsToHide.put(equippedWeaponType, WidgetInfo.COMBAT_DEFENSIVE_SPELL_TEXT, enabled && (warnedSkills.contains(Skill.DEFENCE) || warnedSkill));
widgetsToHide.put(equippedWeaponType, WidgetInfo.COMBAT_DEFENSIVE_SPELL_BOX, enabled && warnedSkill);
widgetsToHide.put(equippedWeaponType, WidgetInfo.COMBAT_DEFENSIVE_SPELL_ICON, enabled && warnedSkill);
widgetsToHide.put(equippedWeaponType, WidgetInfo.COMBAT_DEFENSIVE_SPELL_SHIELD, enabled && warnedSkill);
widgetsToHide.put(equippedWeaponType, WidgetInfo.COMBAT_DEFENSIVE_SPELL_TEXT, enabled && warnedSkill);
}

// Remove appropriate combat option
Expand All @@ -344,7 +352,7 @@ private void hideWarnedStyles(boolean enabled)
widgetsToHide.put(equippedWeaponType, WidgetInfo.COMBAT_SPELLS, enabled && warnedSkill);
break;
default:
log.warn("Unreachable default case for equipped weapon type attack styles");
// 5 can be defensive casting
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public enum WeaponType
TYPE_15(CONTROLLED, CONTROLLED, CONTROLLED, DEFENSIVE),
TYPE_16(ACCURATE, AGGRESSIVE, CONTROLLED, DEFENSIVE),
TYPE_17(ACCURATE, AGGRESSIVE, AGGRESSIVE, DEFENSIVE),
TYPE_18(ACCURATE, AGGRESSIVE, null, DEFENSIVE, CASTING),
TYPE_18(ACCURATE, AGGRESSIVE, null, DEFENSIVE, CASTING, DEFENSIVE_CASTING),
TYPE_19(RANGING, RANGING, null, LONGRANGE),
TYPE_20(ACCURATE, CONTROLLED, null, DEFENSIVE),
TYPE_21(ACCURATE, AGGRESSIVE, null, DEFENSIVE),
TYPE_21(ACCURATE, AGGRESSIVE, null, DEFENSIVE, CASTING, DEFENSIVE_CASTING),
TYPE_22(ACCURATE, AGGRESSIVE, AGGRESSIVE, DEFENSIVE),
TYPE_23(CASTING, CASTING, null, DEFENSIVE_CASTING),
TYPE_24(ACCURATE, AGGRESSIVE, CONTROLLED, DEFENSIVE),
Expand Down

0 comments on commit e6a2848

Please sign in to comment.