You could simplify it to something like this (Although didn't test it), another step would be changing the traintype to an enum but yeah.
public void setAttackStyle(String trainType)
{
int style = script.getConfigs().get(43);
switch (trainType)
{
case "DEFENCE":
if (style != 3)
{
selectAttackStyle(17, 4);
}
break;
case "ATTACK":
if (style != 0)
{
selectAttackStyle(5, 4);
}
break;
case "STRENGTH":
if (style != 1)
{
selectAttackStyle(9, 4);
}
break;
}
}
private void selectAttackStyle(int childId, int subChildId)
{
if (!script.getTabs().open(Tab.ATTACK))
{
return;
}
RS2Widget widget = script.widgets.get(593, childId, subChildId);
if (widget != null && widget.isVisible())
{
widget.interact();
}
}