pest control plugin: cleanup & lombokize

This commit is contained in:
Adam
2018-08-18 09:31:59 -04:00
parent d7739fafc5
commit 534fda881e
6 changed files with 50 additions and 128 deletions

View File

@@ -27,6 +27,7 @@ package net.runelite.client.plugins.pestcontrol;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import static net.runelite.client.plugins.pestcontrol.Portal.BLUE;
import static net.runelite.client.plugins.pestcontrol.Portal.PURPLE;
@@ -34,39 +35,42 @@ import static net.runelite.client.plugins.pestcontrol.Portal.RED;
import static net.runelite.client.plugins.pestcontrol.Portal.YELLOW;
@Slf4j
public class Game
class Game
{
// Game starts with all possible rotations
private Rotation[] possibleRotations = Rotation.values();
// Number of shields dropped
private int shieldsDropped;
@Getter
private final PortalContext purple = new PortalContext(PURPLE);
@Getter
private final PortalContext blue = new PortalContext(BLUE);
@Getter
private final PortalContext yellow = new PortalContext(YELLOW);
@Getter
private final PortalContext red = new PortalContext(RED);
public void fall(String color)
void fall(String color)
{
if (color.equalsIgnoreCase("purple"))
switch (color.toLowerCase())
{
fall(purple);
}
else if (color.equalsIgnoreCase("red"))
{
fall(red);
}
else if (color.equalsIgnoreCase("yellow"))
{
fall(yellow);
}
else if (color.equalsIgnoreCase("blue"))
{
fall(blue);
case "purple":
fall(purple);
break;
case "red":
fall(red);
break;
case "yellow":
fall(yellow);
break;
case "blue":
fall(blue);
break;
}
}
public void fall(PortalContext portal)
private void fall(PortalContext portal)
{
if (!portal.isShielded())
{
@@ -75,7 +79,7 @@ public class Game
log.debug("Shield dropped for {}", portal.getPortal());
portal.setIsShielded(false);
portal.setShielded(false);
int shieldDrop = shieldsDropped++;
// Remove impossible rotations
@@ -92,19 +96,19 @@ public class Game
possibleRotations = rotations.toArray(new Rotation[rotations.size()]);
}
public void die(PortalContext portal)
void die(PortalContext portal)
{
if (portal.isIsDead())
if (portal.isDead())
{
return;
}
log.debug("Portal {} died", portal.getPortal());
portal.setIsDead(true);
portal.setDead(true);
}
public Collection<Portal> getNextPortals()
Collection<Portal> getNextPortals()
{
List<Portal> portals = new ArrayList<>();
@@ -120,24 +124,4 @@ public class Game
return portals;
}
public PortalContext getPurple()
{
return purple;
}
public PortalContext getBlue()
{
return blue;
}
public PortalContext getYellow()
{
return yellow;
}
public PortalContext getRed()
{
return red;
}
}

View File

@@ -109,21 +109,11 @@ public class PestControlOverlay extends Overlay
PortalContext yellow = game.getYellow();
PortalContext red = game.getRed();
Widget purpleShield = client.getWidget(PURPLE.getShield());
Widget blueShield = client.getWidget(BLUE.getShield());
Widget yellowShield = client.getWidget(YELLOW.getShield());
Widget redShield = client.getWidget(RED.getShield());
Widget purpleHealth = client.getWidget(PURPLE.getHitpoints());
Widget blueHealth = client.getWidget(BLUE.getHitpoints());
Widget yellowHealth = client.getWidget(YELLOW.getHitpoints());
Widget redHealth = client.getWidget(RED.getHitpoints());
assert purpleShield != null;
assert blueShield != null;
assert yellowShield != null;
assert redShield != null;
// Check for dead portals
if (isZero(purpleHealth))
{
@@ -227,7 +217,7 @@ public class PestControlOverlay extends Overlay
private void renderAttack(Graphics2D graphics, PortalContext portal)
{
if (portal.isShielded() || portal.isIsDead())
if (portal.isShielded() || portal.isDead())
{
return;
}

View File

@@ -30,8 +30,7 @@ import java.util.regex.Pattern;
import javax.inject.Inject;
import net.runelite.api.ChatMessageType;
import net.runelite.api.Client;
import net.runelite.api.GameState;
import net.runelite.api.events.SetMessage;
import net.runelite.api.events.ChatMessage;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.ui.overlay.OverlayManager;
@@ -43,6 +42,8 @@ import net.runelite.client.ui.overlay.OverlayManager;
)
public class PestControlPlugin extends Plugin
{
private final Pattern SHIELD_DROP = Pattern.compile("The ([a-z]+), [^ ]+ portal shield has dropped!", Pattern.CASE_INSENSITIVE);
@Inject
private OverlayManager overlayManager;
@@ -52,8 +53,6 @@ public class PestControlPlugin extends Plugin
@Inject
private PestControlOverlay overlay;
private Pattern pattern = Pattern.compile("The ([a-z]+), [^ ]+ portal shield has dropped!", Pattern.CASE_INSENSITIVE);
@Override
protected void startUp() throws Exception
{
@@ -66,18 +65,12 @@ public class PestControlPlugin extends Plugin
overlayManager.remove(overlay);
}
@Subscribe
private void onSetMessage(SetMessage setMessage)
public void onChatMessage(ChatMessage chatMessage)
{
if (client.getGameState() != GameState.LOADING && client.getGameState() != GameState.LOGGED_IN)
if (overlay.getGame() != null && chatMessage.getType() == ChatMessageType.SERVER)
{
return;
}
if (overlay.getGame() != null && setMessage.getType() == ChatMessageType.SERVER)
{
Matcher matcher = pattern.matcher(setMessage.getValue());
Matcher matcher = SHIELD_DROP.matcher(chatMessage.getMessage());
if (matcher.lookingAt())
{
overlay.getGame().fall(matcher.group(1));

View File

@@ -23,9 +23,15 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.client.plugins.pestcontrol;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import net.runelite.api.widgets.WidgetInfo;
public enum Portal
@AllArgsConstructor
@Getter
@ToString
enum Portal
{
PURPLE(WidgetInfo.PEST_CONTROL_PURPLE_SHIELD, WidgetInfo.PEST_CONTROL_PURPLE_HEALTH, WidgetInfo.PEST_CONTROL_PURPLE_ICON),
BLUE(WidgetInfo.PEST_CONTROL_BLUE_SHIELD, WidgetInfo.PEST_CONTROL_BLUE_HEALTH, WidgetInfo.PEST_CONTROL_BLUE_ICON),
@@ -35,32 +41,4 @@ public enum Portal
private final WidgetInfo shield;
private final WidgetInfo hitpoints;
private final WidgetInfo icon;
private Portal(WidgetInfo shield, WidgetInfo hitpoints, WidgetInfo icon)
{
this.shield = shield;
this.hitpoints = hitpoints;
this.icon = icon;
}
@Override
public String toString()
{
return "Portal(" + name() + ")";
}
public WidgetInfo getShield()
{
return shield;
}
public WidgetInfo getHitpoints()
{
return hitpoints;
}
public WidgetInfo getIcon()
{
return icon;
}
}

View File

@@ -24,39 +24,16 @@
*/
package net.runelite.client.plugins.pestcontrol;
public class PortalContext
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
@RequiredArgsConstructor
@Getter
@Setter
class PortalContext
{
private final Portal portal;
private boolean isShielded = true;
private boolean isDead;
public PortalContext(Portal portal)
{
this.portal = portal;
}
public Portal getPortal()
{
return portal;
}
public boolean isShielded()
{
return isShielded;
}
public void setIsShielded(boolean isShielded)
{
this.isShielded = isShielded;
}
public boolean isIsDead()
{
return isDead;
}
public void setIsDead(boolean isDead)
{
this.isDead = isDead;
}
}

View File

@@ -29,7 +29,7 @@ import static net.runelite.client.plugins.pestcontrol.Portal.PURPLE;
import static net.runelite.client.plugins.pestcontrol.Portal.RED;
import static net.runelite.client.plugins.pestcontrol.Portal.YELLOW;
public enum Rotation
enum Rotation
{
PBYR(PURPLE, BLUE, YELLOW, RED),
PYBR(PURPLE, YELLOW, BLUE, RED),
@@ -40,7 +40,7 @@ public enum Rotation
private final Portal[] portals;
private Rotation(Portal first, Portal second, Portal third, Portal fourth)
Rotation(Portal first, Portal second, Portal third, Portal fourth)
{
portals = new Portal[]
{