From 26f24ae54de4da64e83371a8bf5341a1f37b23fb Mon Sep 17 00:00:00 2001 From: Scott Burns Date: Thu, 16 May 2019 01:06:29 +0200 Subject: [PATCH] Refactor PrayAgainstPrayer --- .../prayagainstplayer/PlayerContainer.java | 20 +- .../PrayAgainstPlayerConfig.java | 255 +++++++++++------- .../PrayAgainstPlayerOverlay.java | 163 +++++++---- .../PrayAgainstPlayerOverlayPrayerTab.java | 67 +++-- .../PrayAgainstPlayerPlugin.java | 255 +++++++++++------- 5 files changed, 483 insertions(+), 277 deletions(-) diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/prayagainstplayer/PlayerContainer.java b/runelite-client/src/main/java/net/runelite/client/plugins/prayagainstplayer/PlayerContainer.java index 6c1370ef76..68f78ab065 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/prayagainstplayer/PlayerContainer.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/prayagainstplayer/PlayerContainer.java @@ -31,26 +31,34 @@ import net.runelite.api.Player; * When they attacked me * And (in milliseconds) when to expire the overlay around them */ -public class PlayerContainer { +public class PlayerContainer +{ private Player player; private long whenTheyAttackedMe; private int millisToExpireHighlight; - public PlayerContainer(Player player, long whenTheyAttackedMe, int millisToExpireHighlight) { + PlayerContainer(Player player, long whenTheyAttackedMe, int millisToExpireHighlight) + { this.player = player; this.whenTheyAttackedMe = whenTheyAttackedMe; this.millisToExpireHighlight = millisToExpireHighlight; } - //getters - public Player getPlayer() { + public Player getPlayer() + { return player; } - public long getWhenTheyAttackedMe() { + + long getWhenTheyAttackedMe() + { return whenTheyAttackedMe; } - public int getMillisToExpireHighlight() { return millisToExpireHighlight; }; + + int getMillisToExpireHighlight() + { + return millisToExpireHighlight; + } } diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/prayagainstplayer/PrayAgainstPlayerConfig.java b/runelite-client/src/main/java/net/runelite/client/plugins/prayagainstplayer/PrayAgainstPlayerConfig.java index ce453fd3d9..6317c62a07 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/prayagainstplayer/PrayAgainstPlayerConfig.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/prayagainstplayer/PrayAgainstPlayerConfig.java @@ -24,160 +24,209 @@ package net.runelite.client.plugins.prayagainstplayer; +import java.awt.Color; import net.runelite.client.config.Config; import net.runelite.client.config.ConfigGroup; import net.runelite.client.config.ConfigItem; -import java.awt.*; - @ConfigGroup("prayagainstplayer") -public interface PrayAgainstPlayerConfig extends Config { +public interface PrayAgainstPlayerConfig extends Config +{ @ConfigItem( - position = 0, - keyName = "attackerPlayerColor", - name = "Attacker color", - description = "This is the color that will be used to highlight attackers." + position = 0, + keyName = "attackerPlayerColor", + name = "Attacker color", + description = "This is the color that will be used to highlight attackers." ) - default Color attackerPlayerColor() { return new Color(0xFF0006); } + default Color attackerPlayerColor() + { + return new Color(0xFF0006); + } @ConfigItem( - position = 1, - keyName = "potentialPlayerColor", - name = "Potential Attacker color", - description = "This is the color that will be used to highlight potential attackers." + position = 1, + keyName = "potentialPlayerColor", + name = "Potential Attacker color", + description = "This is the color that will be used to highlight potential attackers." ) - default Color potentialPlayerColor() { return new Color(0xFFFF00); } - - //// - @ConfigItem( - position = 2, - keyName = "attackerTargetTimeout", - name = "Attacker Timeout", - description = "Seconds until attacker is no longer highlighted." - ) - default int attackerTargetTimeout() { return 10; } + default Color potentialPlayerColor() + { + return new Color(0xFFFF00); + } @ConfigItem( - position = 3, - keyName = "potentialTargetTimeout", - name = "Potential Attacker Timeout", - description = "Seconds until potential attacker is no longer highlighted." + position = 2, + keyName = "attackerTargetTimeout", + name = "Attacker Timeout", + description = "Seconds until attacker is no longer highlighted." ) - default int potentialTargetTimeout() { return 10; } + default int attackerTargetTimeout() + { + return 10; + } @ConfigItem( - position = 4, - keyName = "newSpawnTimeout", - name = "New Player Timeout", - description = "Seconds until logged in/spawned player is no longer highlighted." + position = 3, + keyName = "potentialTargetTimeout", + name = "Potential Attacker Timeout", + description = "Seconds until potential attacker is no longer highlighted." ) - default int newSpawnTimeout() { return 5; } - //// - - //// - @ConfigItem( - position = 5, - keyName = "ignoreFriends", - name = "Ignore Friends", - description = "This lets you decide whether you want friends to be highlighted by this plugin." - ) - default boolean ignoreFriends() { return true; } + default int potentialTargetTimeout() + { + return 10; + } @ConfigItem( - position = 6, - keyName = "ignoreClanMates", - name = "Ignore Clan Mates", - description = "This lets you decide whether you want clan mates to be highlighted by this plugin." + position = 4, + keyName = "newSpawnTimeout", + name = "New Player Timeout", + description = "Seconds until logged in/spawned player is no longer highlighted." ) - default boolean ignoreClanMates() { return true; } - //// + default int newSpawnTimeout() + { + return 5; + } @ConfigItem( - position = 7, - keyName = "markNewPlayer", - name = "Mark new player as potential attacker", - description = "Marks someone that logged in or teleported as a potential attacker for your safety\nDO NOT RUN THIS IN WORLD 1-2 GRAND EXCHANGE!" + position = 5, + keyName = "ignoreFriends", + name = "Ignore Friends", + description = "This lets you decide whether you want friends to be highlighted by this plugin." ) - default boolean markNewPlayer() { return false; } + default boolean ignoreFriends() + { + return true; + } @ConfigItem( - position = 8, - keyName = "drawTargetPrayAgainst", - name = "Draw what to pray on attacker", - description = "Tells you what to pray from what weapon the attacker is holding" + position = 6, + keyName = "ignoreClanMates", + name = "Ignore Clan Mates", + description = "This lets you decide whether you want clan mates to be highlighted by this plugin." ) - default boolean drawTargetPrayAgainst() { return true; } + default boolean ignoreClanMates() + { + return true; + } @ConfigItem( - position = 9, - keyName = "drawPotentialTargetPrayAgainst", - name = "Draw what to pray on potential attacker", - description = "Tells you what to pray from what weapon the potential attacker is holding" + position = 7, + keyName = "markNewPlayer", + name = "Mark new player as potential attacker", + description = "Marks someone that logged in or teleported as a potential attacker for your safety\nDO NOT RUN THIS IN WORLD 1-2 GRAND EXCHANGE!" ) - default boolean drawPotentialTargetPrayAgainst() { return true; } + default boolean markNewPlayer() + { + return false; + } @ConfigItem( - position = 10, - keyName = "drawTargetPrayAgainstPrayerTab", - name = "Draw what to pray from prayer tab", - description = "Tells you what to pray from what weapon the attacker is holding from the prayer tab" + position = 8, + keyName = "drawTargetPrayAgainst", + name = "Draw what to pray on attacker", + description = "Tells you what to pray from what weapon the attacker is holding" ) - default boolean drawTargetPrayAgainstPrayerTab() { return false; } + default boolean drawTargetPrayAgainst() + { + return true; + } @ConfigItem( - position = 11, - keyName = "drawTargetsName", - name = "Draw name on attacker", - description = "Configures whether or not the attacker\'s name should be shown" + position = 9, + keyName = "drawPotentialTargetPrayAgainst", + name = "Draw what to pray on potential attacker", + description = "Tells you what to pray from what weapon the potential attacker is holding" ) - default boolean drawTargetsName() { return true; } + default boolean drawPotentialTargetPrayAgainst() + { + return true; + } @ConfigItem( - position = 12, - keyName = "drawPotentialTargetsName", - name = "Draw name on potential attacker", - description = "Configures whether or not the potential attacker\'s name should be shown" + position = 10, + keyName = "drawTargetPrayAgainstPrayerTab", + name = "Draw what to pray from prayer tab", + description = "Tells you what to pray from what weapon the attacker is holding from the prayer tab" ) - default boolean drawPotentialTargetsName() { return true; } + default boolean drawTargetPrayAgainstPrayerTab() + { + return false; + } @ConfigItem( - position = 13, - keyName = "drawTargetHighlight", - name = "Draw highlight around attacker", - description = "Configures whether or not the attacker should be highlighted" + position = 11, + keyName = "drawTargetsName", + name = "Draw name on attacker", + description = "Configures whether or not the attacker\'s name should be shown" ) - default boolean drawTargetHighlight() { return true; } + default boolean drawTargetsName() + { + return true; + } @ConfigItem( - position = 14, - keyName = "drawPotentialTargetHighlight", - name = "Draw highlight around potential attacker", - description = "Configures whether or not the potential attacker should be highlighted" + position = 12, + keyName = "drawPotentialTargetsName", + name = "Draw name on potential attacker", + description = "Configures whether or not the potential attacker\'s name should be shown" ) - default boolean drawPotentialTargetHighlight() { return true; } + default boolean drawPotentialTargetsName() + { + return true; + } @ConfigItem( - position = 15, - keyName = "drawTargetTile", - name = "Draw tile under attacker", - description = "Configures whether or not the attacker\'s tile be highlighted" + position = 13, + keyName = "drawTargetHighlight", + name = "Draw highlight around attacker", + description = "Configures whether or not the attacker should be highlighted" ) - default boolean drawTargetTile() { return false; } + default boolean drawTargetHighlight() + { + return true; + } @ConfigItem( - position = 16, - keyName = "drawPotentialTargetTile", - name = "Draw tile under potential attacker", - description = "Configures whether or not the potential attacker\'s tile be highlighted" + position = 14, + keyName = "drawPotentialTargetHighlight", + name = "Draw highlight around potential attacker", + description = "Configures whether or not the potential attacker should be highlighted" ) - default boolean drawPotentialTargetTile() { return false; } + default boolean drawPotentialTargetHighlight() + { + return true; + } @ConfigItem( - position = 17, - keyName = "drawUnknownWeapons", - name = "Draw unknown weapons", - description = "Configures whether or not the unknown weapons should be shown when a player equips one" + position = 15, + keyName = "drawTargetTile", + name = "Draw tile under attacker", + description = "Configures whether or not the attacker\'s tile be highlighted" ) - default boolean drawUnknownWeapons() { return false; } + default boolean drawTargetTile() + { + return false; + } + @ConfigItem( + position = 16, + keyName = "drawPotentialTargetTile", + name = "Draw tile under potential attacker", + description = "Configures whether or not the potential attacker\'s tile be highlighted" + ) + default boolean drawPotentialTargetTile() + { + return false; + } + + @ConfigItem( + position = 17, + keyName = "drawUnknownWeapons", + name = "Draw unknown weapons", + description = "Configures whether or not the unknown weapons should be shown when a player equips one" + ) + default boolean drawUnknownWeapons() + { + return false; + } } diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/prayagainstplayer/PrayAgainstPlayerOverlay.java b/runelite-client/src/main/java/net/runelite/client/plugins/prayagainstplayer/PrayAgainstPlayerOverlay.java index e54efd8127..3a3f25269e 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/prayagainstplayer/PrayAgainstPlayerOverlay.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/prayagainstplayer/PrayAgainstPlayerOverlay.java @@ -24,27 +24,35 @@ package net.runelite.client.plugins.prayagainstplayer; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics2D; +import java.awt.Polygon; +import java.awt.image.BufferedImage; +import java.util.ConcurrentModificationException; +import javax.inject.Inject; import net.runelite.api.Client; import net.runelite.api.ItemComposition; import net.runelite.api.Player; -import net.runelite.api.kit.KitType; -import net.runelite.client.ui.overlay.*; -import net.runelite.client.util.Text; import net.runelite.api.Point; +import net.runelite.api.kit.KitType; +import net.runelite.client.ui.overlay.Overlay; +import net.runelite.client.ui.overlay.OverlayLayer; +import net.runelite.client.ui.overlay.OverlayPosition; +import net.runelite.client.ui.overlay.OverlayPriority; +import net.runelite.client.ui.overlay.OverlayUtil; +import net.runelite.client.util.Text; -import javax.inject.Inject; -import java.awt.*; -import java.awt.image.BufferedImage; -import java.util.ConcurrentModificationException; - -class PrayAgainstPlayerOverlay extends Overlay { +class PrayAgainstPlayerOverlay extends Overlay +{ private final PrayAgainstPlayerPlugin plugin; private final PrayAgainstPlayerConfig config; private final Client client; @Inject - private PrayAgainstPlayerOverlay(PrayAgainstPlayerPlugin plugin, PrayAgainstPlayerConfig config, Client client) { + private PrayAgainstPlayerOverlay(PrayAgainstPlayerPlugin plugin, PrayAgainstPlayerConfig config, Client client) + { super(plugin); this.plugin = plugin; this.config = config; @@ -57,73 +65,127 @@ class PrayAgainstPlayerOverlay extends Overlay { @Override - public Dimension render(Graphics2D graphics) { + public Dimension render(Graphics2D graphics) + { renderPotentialPlayers(graphics); renderAttackingPlayers(graphics); return null; } - private void renderPotentialPlayers(Graphics2D graphics) { - if (plugin.getPotentialPlayersAttackingMe() == null || !plugin.getPotentialPlayersAttackingMe().isEmpty()) { - try { - for (PlayerContainer container : plugin.getPotentialPlayersAttackingMe()) { - if ((System.currentTimeMillis() > (container.getWhenTheyAttackedMe() + container.getMillisToExpireHighlight())) && (container.getPlayer().getInteracting() != client.getLocalPlayer())) { - plugin.removePlayerFromPotentialContainer(container); + private void renderPotentialPlayers(Graphics2D graphics) + { + if (plugin.getPotentialPlayersAttackingMe() == null || !plugin.getPotentialPlayersAttackingMe().isEmpty()) + { + try + { + if (plugin.getPotentialPlayersAttackingMe() != null) + { + for (PlayerContainer container : plugin.getPotentialPlayersAttackingMe()) + { + if ((System.currentTimeMillis() > (container.getWhenTheyAttackedMe() + container.getMillisToExpireHighlight())) && (container.getPlayer().getInteracting() != client.getLocalPlayer())) + { + plugin.removePlayerFromPotentialContainer(container); + } + if (config.drawPotentialTargetsName()) + { + renderNameAboveHead(graphics, container.getPlayer(), config.potentialPlayerColor()); + } + if (config.drawPotentialTargetHighlight()) + { + renderHighlightedPlayer(graphics, container.getPlayer(), config.potentialPlayerColor()); + } + if (config.drawPotentialTargetTile()) + { + renderTileUnderPlayer(graphics, container.getPlayer(), config.potentialPlayerColor()); + } + if (config.drawPotentialTargetPrayAgainst()) + { + renderPrayAgainstOnPlayer(graphics, container.getPlayer(), config.potentialPlayerColor()); + } } - if (config.drawPotentialTargetsName()) renderNameAboveHead(graphics, container.getPlayer(), config.potentialPlayerColor()); - if (config.drawPotentialTargetHighlight()) renderHighlightedPlayer(graphics, container.getPlayer(), config.potentialPlayerColor()); - if (config.drawPotentialTargetTile()) renderTileUnderPlayer(graphics, container.getPlayer(), config.potentialPlayerColor()); - if (config.drawPotentialTargetPrayAgainst()) renderPrayAgainstOnPlayer(graphics, container.getPlayer(), config.potentialPlayerColor()); } - } catch (ConcurrentModificationException e) { + } + catch (ConcurrentModificationException ignored) + { } } } - private void renderAttackingPlayers(Graphics2D graphics) { - if (plugin.getPlayersAttackingMe() == null || !plugin.getPlayersAttackingMe().isEmpty()) { - try { - for (PlayerContainer container : plugin.getPlayersAttackingMe()) { - if ((System.currentTimeMillis() > (container.getWhenTheyAttackedMe() + container.getMillisToExpireHighlight())) && (container.getPlayer().getInteracting() != client.getLocalPlayer())) { - plugin.removePlayerFromAttackerContainer(container); - } + private void renderAttackingPlayers(Graphics2D graphics) + { + if (plugin.getPlayersAttackingMe() == null || !plugin.getPlayersAttackingMe().isEmpty()) + { + try + { + if (plugin.getPlayersAttackingMe() != null) + { + for (PlayerContainer container : plugin.getPlayersAttackingMe()) + { + if ((System.currentTimeMillis() > (container.getWhenTheyAttackedMe() + container.getMillisToExpireHighlight())) && (container.getPlayer().getInteracting() != client.getLocalPlayer())) + { + plugin.removePlayerFromAttackerContainer(container); + } - if (config.drawTargetsName()) renderNameAboveHead(graphics, container.getPlayer(), config.attackerPlayerColor()); - if (config.drawTargetHighlight()) renderHighlightedPlayer(graphics, container.getPlayer(), config.attackerPlayerColor()); - if (config.drawTargetTile()) renderTileUnderPlayer(graphics, container.getPlayer(), config.attackerPlayerColor()); - if (config.drawTargetPrayAgainst()) renderPrayAgainstOnPlayer(graphics, container.getPlayer(), config.attackerPlayerColor()); + if (config.drawTargetsName()) + { + renderNameAboveHead(graphics, container.getPlayer(), config.attackerPlayerColor()); + } + if (config.drawTargetHighlight()) + { + renderHighlightedPlayer(graphics, container.getPlayer(), config.attackerPlayerColor()); + } + if (config.drawTargetTile()) + { + renderTileUnderPlayer(graphics, container.getPlayer(), config.attackerPlayerColor()); + } + if (config.drawTargetPrayAgainst()) + { + renderPrayAgainstOnPlayer(graphics, container.getPlayer(), config.attackerPlayerColor()); + } + } } - } catch (ConcurrentModificationException e) { + } + catch (ConcurrentModificationException ignored) + { } } } - private void renderNameAboveHead(Graphics2D graphics, Player player, Color color) { + private void renderNameAboveHead(Graphics2D graphics, Player player, Color color) + { final String name = Text.sanitize(player.getName()); final int offset = player.getLogicalHeight() + 40; Point textLocation = player.getCanvasTextLocation(graphics, name, offset); - if (textLocation != null) { + if (textLocation != null) + { OverlayUtil.renderTextLocation(graphics, textLocation, name, color); } } - private void renderHighlightedPlayer(Graphics2D graphics, Player player, Color color) { - try { + private void renderHighlightedPlayer(Graphics2D graphics, Player player, Color color) + { + try + { OverlayUtil.renderPolygon(graphics, player.getConvexHull(), color); - } catch (NullPointerException e) { + } + catch (NullPointerException ignored) + { } } - private void renderTileUnderPlayer(Graphics2D graphics, Player player, Color color) { + private void renderTileUnderPlayer(Graphics2D graphics, Player player, Color color) + { Polygon poly = player.getCanvasTilePoly(); OverlayUtil.renderPolygon(graphics, poly, color); } - private void renderPrayAgainstOnPlayer(Graphics2D graphics, Player player, Color color) { + private void renderPrayAgainstOnPlayer(Graphics2D graphics, Player player, Color color) + { final int offset = (player.getLogicalHeight() / 2) + 75; BufferedImage icon; - switch (WeaponType.checkWeaponOnPlayer(client, player)) { + switch (WeaponType.checkWeaponOnPlayer(client, player)) + { case WEAPON_MELEE: icon = plugin.getProtectionIcon(WeaponType.WEAPON_MELEE); break; @@ -137,12 +199,17 @@ class PrayAgainstPlayerOverlay extends Overlay { icon = null; break; } - try { - if (icon != null) { + try + { + if (icon != null) + { Point point = player.getCanvasImageLocation(icon, offset); OverlayUtil.renderImageLocation(graphics, point, icon); - } else { - if (config.drawUnknownWeapons()) { + } + else + { + if (config.drawUnknownWeapons()) + { int itemId = player.getPlayerComposition().getEquipmentId(KitType.WEAPON); ItemComposition itemComposition = client.getItemDefinition(itemId); @@ -151,7 +218,9 @@ class PrayAgainstPlayerOverlay extends Overlay { OverlayUtil.renderTextLocation(graphics, point, str, color); } } - } catch (Exception e) { + } + catch (Exception ignored) + { } } diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/prayagainstplayer/PrayAgainstPlayerOverlayPrayerTab.java b/runelite-client/src/main/java/net/runelite/client/plugins/prayagainstplayer/PrayAgainstPlayerOverlayPrayerTab.java index 4e505675f6..885bdd2e7d 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/prayagainstplayer/PrayAgainstPlayerOverlayPrayerTab.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/prayagainstplayer/PrayAgainstPlayerOverlayPrayerTab.java @@ -24,24 +24,33 @@ package net.runelite.client.plugins.prayagainstplayer; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics2D; +import java.awt.Polygon; +import java.awt.Rectangle; +import java.util.ConcurrentModificationException; +import javax.inject.Inject; import net.runelite.api.Client; import net.runelite.api.Player; import net.runelite.api.widgets.Widget; import net.runelite.api.widgets.WidgetInfo; -import net.runelite.client.ui.overlay.*; +import net.runelite.client.ui.overlay.Overlay; +import net.runelite.client.ui.overlay.OverlayLayer; +import net.runelite.client.ui.overlay.OverlayPosition; +import net.runelite.client.ui.overlay.OverlayPriority; +import net.runelite.client.ui.overlay.OverlayUtil; -import javax.inject.Inject; -import java.awt.*; -import java.util.ConcurrentModificationException; - -class PrayAgainstPlayerOverlayPrayerTab extends Overlay { +class PrayAgainstPlayerOverlayPrayerTab extends Overlay +{ private final PrayAgainstPlayerPlugin plugin; private final PrayAgainstPlayerConfig config; private final Client client; @Inject - private PrayAgainstPlayerOverlayPrayerTab (PrayAgainstPlayerPlugin plugin, PrayAgainstPlayerConfig config, Client client) { + private PrayAgainstPlayerOverlayPrayerTab(PrayAgainstPlayerPlugin plugin, PrayAgainstPlayerConfig config, Client client) + { super(plugin); this.plugin = plugin; this.config = config; @@ -54,30 +63,46 @@ class PrayAgainstPlayerOverlayPrayerTab extends Overlay { @Override - public Dimension render(Graphics2D graphics) { - if (plugin.getPlayersAttackingMe() == null || !plugin.getPlayersAttackingMe().isEmpty()) { - try { - for (PlayerContainer container : plugin.getPlayersAttackingMe()) { - if (plugin.getPlayersAttackingMe() != null && plugin.getPlayersAttackingMe().size() > 0) { - //no reason to show you what prayers to pray in your prayer tab if multiple people are attacking you - if ((plugin.getPlayersAttackingMe().size() == 1) && (config.drawTargetPrayAgainstPrayerTab())) { - renderPrayerToClick(graphics, container.getPlayer()); + public Dimension render(Graphics2D graphics) + { + if (plugin.getPlayersAttackingMe() == null || !plugin.getPlayersAttackingMe().isEmpty()) + { + try + { + if (plugin.getPlayersAttackingMe() != null) + { + for (PlayerContainer container : plugin.getPlayersAttackingMe()) + { + if (plugin.getPlayersAttackingMe() != null && plugin.getPlayersAttackingMe().size() > 0) + { + // no reason to show you what prayers to pray in your prayer tab if multiple people are attacking you + if ((plugin.getPlayersAttackingMe().size() == 1) && (config.drawTargetPrayAgainstPrayerTab())) + { + renderPrayerToClick(graphics, container.getPlayer()); + } } } } - } catch (ConcurrentModificationException e) { + } + catch (ConcurrentModificationException ignored) + { } } return null; } - private void renderPrayerToClick(Graphics2D graphics, Player player) { + private void renderPrayerToClick(Graphics2D graphics, Player player) + { Widget PROTECT_FROM_MAGIC = client.getWidget(WidgetInfo.PRAYER_PROTECT_FROM_MAGIC); Widget PROTECT_FROM_RANGED = client.getWidget(WidgetInfo.PRAYER_PROTECT_FROM_MISSILES); Widget PROTECT_FROM_MELEE = client.getWidget(WidgetInfo.PRAYER_PROTECT_FROM_MELEE); Color color = Color.RED; - if (PROTECT_FROM_MELEE.isHidden()) return; - switch (WeaponType.checkWeaponOnPlayer(client, player)) { + if (PROTECT_FROM_MELEE.isHidden()) + { + return; + } + switch (WeaponType.checkWeaponOnPlayer(client, player)) + { case WEAPON_MAGIC: OverlayUtil.renderPolygon(graphics, rectangleToPolygon(PROTECT_FROM_MAGIC.getBounds()), color); break; @@ -92,9 +117,11 @@ class PrayAgainstPlayerOverlayPrayerTab extends Overlay { } } - private static Polygon rectangleToPolygon(Rectangle rect) { + private static Polygon rectangleToPolygon(Rectangle rect) + { int[] xpoints = {rect.x, rect.x + rect.width, rect.x + rect.width, rect.x}; int[] ypoints = {rect.y, rect.y, rect.y + rect.height, rect.y + rect.height}; + return new Polygon(xpoints, ypoints, 4); } diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/prayagainstplayer/PrayAgainstPlayerPlugin.java b/runelite-client/src/main/java/net/runelite/client/plugins/prayagainstplayer/PrayAgainstPlayerPlugin.java index 0f9d144bcb..d785615546 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/prayagainstplayer/PrayAgainstPlayerPlugin.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/prayagainstplayer/PrayAgainstPlayerPlugin.java @@ -25,8 +25,27 @@ package net.runelite.client.plugins.prayagainstplayer; import com.google.inject.Provides; -import net.runelite.api.*; -import net.runelite.api.events.*; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.image.BufferedImage; +import java.awt.image.ColorModel; +import java.awt.image.DataBufferByte; +import java.awt.image.IndexColorModel; +import java.awt.image.WritableRaster; +import java.util.ArrayList; +import java.util.Arrays; +import javax.inject.Inject; +import net.runelite.api.AnimationID; +import net.runelite.api.Client; +import net.runelite.api.GameState; +import net.runelite.api.IndexedSprite; +import net.runelite.api.Player; +import net.runelite.api.SpriteID; +import net.runelite.api.events.AnimationChanged; +import net.runelite.api.events.GameStateChanged; +import net.runelite.api.events.InteractingChanged; +import net.runelite.api.events.PlayerDespawned; +import net.runelite.api.events.PlayerSpawned; import net.runelite.client.config.ConfigManager; import net.runelite.client.eventbus.Subscribe; import net.runelite.client.game.SpriteManager; @@ -36,33 +55,28 @@ import net.runelite.client.plugins.PluginType; import net.runelite.client.ui.overlay.OverlayManager; import net.runelite.client.util.ImageUtil; -import javax.inject.Inject; -import java.awt.*; -import java.awt.image.*; -import java.util.ArrayList; -import java.util.Arrays; - @PluginDescriptor( - name = "Pray Against Player", - description = "Use plugin in PvP situations for best results!!", - tags = {"highlight", "pvp", "overlay", "players"}, - type = PluginType.PVP + name = "Pray Against Player", + description = "Use plugin in PvP situations for best results!!", + tags = {"highlight", "pvp", "overlay", "players"}, + type = PluginType.PVP ) /** * I am fully aware that there is plenty of overhead and is a MESS! * If you'd like to contribute please do! */ -public class PrayAgainstPlayerPlugin extends Plugin { +public class PrayAgainstPlayerPlugin extends Plugin +{ private static final int[] PROTECTION_ICONS = { - SpriteID.PRAYER_PROTECT_FROM_MISSILES, - SpriteID.PRAYER_PROTECT_FROM_MELEE, - SpriteID.PRAYER_PROTECT_FROM_MAGIC + SpriteID.PRAYER_PROTECT_FROM_MISSILES, + SpriteID.PRAYER_PROTECT_FROM_MELEE, + SpriteID.PRAYER_PROTECT_FROM_MAGIC }; private static final Dimension PROTECTION_ICON_DIMENSION = new Dimension(33, 33); private static final Color PROTECTION_ICON_OUTLINE_COLOR = new Color(33, 33, 33); - public final BufferedImage[] ProtectionIcons = new BufferedImage[PROTECTION_ICONS.length]; + private final BufferedImage[] ProtectionIcons = new BufferedImage[PROTECTION_ICONS.length]; private ArrayList potentialPlayersAttackingMe; private ArrayList playersAttackingMe; @@ -86,19 +100,23 @@ public class PrayAgainstPlayerPlugin extends Plugin { private PrayAgainstPlayerConfig config; @Provides - PrayAgainstPlayerConfig provideConfig(ConfigManager configManager) { + PrayAgainstPlayerConfig provideConfig(ConfigManager configManager) + { return configManager.getConfig(PrayAgainstPlayerConfig.class); } @Subscribe - public void onGameStateChanged(GameStateChanged gameStateChanged) { - if (gameStateChanged.getGameState() == GameState.LOGGED_IN) { + public void onGameStateChanged(GameStateChanged gameStateChanged) + { + if (gameStateChanged.getGameState() == GameState.LOGGED_IN) + { loadProtectionIcons(); } } @Override - protected void startUp() { + protected void startUp() + { potentialPlayersAttackingMe = new ArrayList<>(); playersAttackingMe = new ArrayList<>(); overlayManager.add(overlay); @@ -106,31 +124,44 @@ public class PrayAgainstPlayerPlugin extends Plugin { } @Override - protected void shutDown() throws Exception { + protected void shutDown() throws Exception + { overlayManager.remove(overlay); overlayManager.remove(overlayPrayerTab); } @Subscribe - protected void onAnimationChanged(AnimationChanged animationChanged) { - if ((animationChanged.getActor() instanceof Player) && (animationChanged.getActor().getInteracting() instanceof Player) && (animationChanged.getActor().getInteracting() == client.getLocalPlayer())) { + protected void onAnimationChanged(AnimationChanged animationChanged) + { + if ((animationChanged.getActor() instanceof Player) && (animationChanged.getActor().getInteracting() instanceof Player) && (animationChanged.getActor().getInteracting() == client.getLocalPlayer())) + { Player sourcePlayer = (Player) animationChanged.getActor(); - //is the client is a friend/clan and the config is set to ignore friends/clan dont add them to list - if (client.isFriended(sourcePlayer.getName(), true) && config.ignoreFriends()) return; - if (client.isClanMember(sourcePlayer.getName()) && config.ignoreClanMates()) return; + // is the client is a friend/clan and the config is set to ignore friends/clan dont add them to list + if (client.isFriended(sourcePlayer.getName(), true) && config.ignoreFriends()) + { + return; + } + if (client.isClanMember(sourcePlayer.getName()) && config.ignoreClanMates()) + { + return; + } - if ((sourcePlayer.getAnimation() != -1) && (!isBlockAnimation(sourcePlayer.getAnimation()))) { - //if attacker attacks again, reset his timer so overlay doesn't go away - if (findPlayerInAttackerList(sourcePlayer) != null) { + if ((sourcePlayer.getAnimation() != -1) && (!isBlockAnimation(sourcePlayer.getAnimation()))) + { + // if attacker attacks again, reset his timer so overlay doesn't go away + if (findPlayerInAttackerList(sourcePlayer) != null) + { resetPlayerFromAttackerContainerTimer(findPlayerInAttackerList(sourcePlayer)); } - //if he attacks and he was in the potential attackers list, remove him - if (!potentialPlayersAttackingMe.isEmpty() && potentialPlayersAttackingMe.contains(findPlayerInPotentialList(sourcePlayer))) { + // if he attacks and he was in the potential attackers list, remove him + if (!potentialPlayersAttackingMe.isEmpty() && potentialPlayersAttackingMe.contains(findPlayerInPotentialList(sourcePlayer))) + { removePlayerFromPotentialContainer(findPlayerInPotentialList(sourcePlayer)); } - //if he's not in the attackers list, add him - if (findPlayerInAttackerList(sourcePlayer) == null) { + // if he's not in the attackers list, add him + if (findPlayerInAttackerList(sourcePlayer) == null) + { PlayerContainer container = new PlayerContainer(sourcePlayer, System.currentTimeMillis(), (config.attackerTargetTimeout() * 1000)); playersAttackingMe.add(container); } @@ -139,16 +170,25 @@ public class PrayAgainstPlayerPlugin extends Plugin { } @Subscribe - protected void onInteractingChanged(InteractingChanged interactingChanged) { - //if someone interacts with you, add them to the potential attackers list - if ((interactingChanged.getSource() instanceof Player) && (interactingChanged.getTarget() instanceof Player)) { + protected void onInteractingChanged(InteractingChanged interactingChanged) + { + // if someone interacts with you, add them to the potential attackers list + if ((interactingChanged.getSource() instanceof Player) && (interactingChanged.getTarget() instanceof Player)) + { Player sourcePlayer = (Player) interactingChanged.getSource(); Player targetPlayer = (Player) interactingChanged.getTarget(); - if ((targetPlayer == client.getLocalPlayer()) && (findPlayerInPotentialList(sourcePlayer) == null)) { //we're being interacted with + if ((targetPlayer == client.getLocalPlayer()) && (findPlayerInPotentialList(sourcePlayer) == null)) + { //we're being interacted with - //is the client is a friend/clan and the config is set to ignore friends/clan dont add them to list - if (client.isFriended(sourcePlayer.getName(), true) && config.ignoreFriends()) return; - if (client.isClanMember(sourcePlayer.getName()) && config.ignoreClanMates()) return; + // is the client is a friend/clan and the config is set to ignore friends/clan dont add them to list + if (client.isFriended(sourcePlayer.getName(), true) && config.ignoreFriends()) + { + return; + } + if (client.isClanMember(sourcePlayer.getName()) && config.ignoreClanMates()) + { + return; + } PlayerContainer container = new PlayerContainer(sourcePlayer, System.currentTimeMillis(), (config.potentialTargetTimeout() * 1000)); potentialPlayersAttackingMe.add(container); @@ -157,53 +197,71 @@ public class PrayAgainstPlayerPlugin extends Plugin { } @Subscribe - protected void onPlayerDespawned(PlayerDespawned playerDespawned) { + protected void onPlayerDespawned(PlayerDespawned playerDespawned) + { PlayerContainer container = findPlayerInAttackerList(playerDespawned.getPlayer()); PlayerContainer container2 = findPlayerInPotentialList(playerDespawned.getPlayer()); - if (container != null) { + if (container != null) + { playersAttackingMe.remove(container); } - if (container2 != null) { + if (container2 != null) + { potentialPlayersAttackingMe.remove(container2); } } @Subscribe - protected void onPlayerSpawned(PlayerSpawned playerSpawned) { - if (config.markNewPlayer()) { + protected void onPlayerSpawned(PlayerSpawned playerSpawned) + { + if (config.markNewPlayer()) + { Player p = playerSpawned.getPlayer(); - if (client.isFriended(p.getName(), true) && config.ignoreFriends()) return; - if (client.isClanMember(p.getName()) && config.ignoreClanMates()) return; + if (client.isFriended(p.getName(), true) && config.ignoreFriends()) + { + return; + } + if (client.isClanMember(p.getName()) && config.ignoreClanMates()) + { + return; + } PlayerContainer container = findPlayerInPotentialList(p); - if (container == null) { + if (container == null) + { container = new PlayerContainer(p, System.currentTimeMillis(), (config.newSpawnTimeout() * 1000)); potentialPlayersAttackingMe.add(container); } } } - PlayerContainer findPlayerInAttackerList(Player player) { - if (playersAttackingMe.isEmpty()) { + private PlayerContainer findPlayerInAttackerList(Player player) + { + if (playersAttackingMe.isEmpty()) + { return null; } - for (int i = 0 ; i < playersAttackingMe.size() ; i++) { - PlayerContainer container = playersAttackingMe.get(i); - if (container.getPlayer() == player) { + for (PlayerContainer container : playersAttackingMe) + { + if (container.getPlayer() == player) + { return container; } } return null; } - PlayerContainer findPlayerInPotentialList(Player player) { - if (potentialPlayersAttackingMe.isEmpty()) { + private PlayerContainer findPlayerInPotentialList(Player player) + { + if (potentialPlayersAttackingMe.isEmpty()) + { return null; } - for (int i = 0 ; i < potentialPlayersAttackingMe.size() ; i++) { - PlayerContainer container = potentialPlayersAttackingMe.get(i); - if (container.getPlayer() == player) { + for (PlayerContainer container : potentialPlayersAttackingMe) + { + if (container.getPlayer() == player) + { return container; } } @@ -212,29 +270,36 @@ public class PrayAgainstPlayerPlugin extends Plugin { /** * Resets player timer in case he attacks again, so his highlight doesn't go away so easily + * * @param container */ - public void resetPlayerFromAttackerContainerTimer(PlayerContainer container) { + private void resetPlayerFromAttackerContainerTimer(PlayerContainer container) + { removePlayerFromAttackerContainer(container); PlayerContainer newContainer = new PlayerContainer(container.getPlayer(), System.currentTimeMillis(), (config.attackerTargetTimeout() * 1000)); playersAttackingMe.add(newContainer); } - - public void removePlayerFromPotentialContainer(PlayerContainer container) { - if ((potentialPlayersAttackingMe != null) && (!potentialPlayersAttackingMe.isEmpty()) && (potentialPlayersAttackingMe.contains(container))) { + void removePlayerFromPotentialContainer(PlayerContainer container) + { + if ((potentialPlayersAttackingMe != null) && (!potentialPlayersAttackingMe.isEmpty())) + { potentialPlayersAttackingMe.remove(container); } } - public void removePlayerFromAttackerContainer(PlayerContainer container) { - if ((playersAttackingMe != null) && (!playersAttackingMe.isEmpty()) && (playersAttackingMe.contains(container))) { + void removePlayerFromAttackerContainer(PlayerContainer container) + { + if ((playersAttackingMe != null) && (!playersAttackingMe.isEmpty())) + { playersAttackingMe.remove(container); } } - private boolean isBlockAnimation(int anim) { - switch (anim) { + private boolean isBlockAnimation(int anim) + { + switch (anim) + { case AnimationID.BLOCK_DEFENDER: case AnimationID.BLOCK_NO_SHIELD: case AnimationID.BLOCK_SHIELD: @@ -246,49 +311,34 @@ public class PrayAgainstPlayerPlugin extends Plugin { } } - public ArrayList getPotentialPlayersAttackingMe() { return potentialPlayersAttackingMe; } - public ArrayList getPlayersAttackingMe() { return playersAttackingMe; } + ArrayList getPotentialPlayersAttackingMe() + { + return potentialPlayersAttackingMe; + } + + ArrayList getPlayersAttackingMe() + { + return playersAttackingMe; + } //All of the methods below are from the Zulrah plugin!!! Credits to it's respective owner - private void loadProtectionIcons() { - final IndexedSprite[] protectionIcons = {}; - final IndexedSprite[] newProtectionIcons = Arrays.copyOf(protectionIcons, PROTECTION_ICONS.length); + private void loadProtectionIcons() + { int curPosition = 0; for (int i = 0; i < PROTECTION_ICONS.length; i++, curPosition++) { final int resource = PROTECTION_ICONS[i]; ProtectionIcons[i] = rgbaToIndexedBufferedImage(ProtectionIconFromSprite(spriteManager.getSprite(resource, 0))); - newProtectionIcons[curPosition] = createIndexedSprite(client, ProtectionIcons[i]); } } - private static IndexedSprite createIndexedSprite(final Client client, final BufferedImage bufferedImage) { - final IndexColorModel indexedCM = (IndexColorModel) bufferedImage.getColorModel(); - - final int width = bufferedImage.getWidth(); - final int height = bufferedImage.getHeight(); - final byte[] pixels = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData(); - final int[] palette = new int[indexedCM.getMapSize()]; - indexedCM.getRGBs(palette); - - final IndexedSprite newIndexedSprite = client.createIndexedSprite(); - newIndexedSprite.setPixels(pixels); - newIndexedSprite.setPalette(palette); - newIndexedSprite.setWidth(width); - newIndexedSprite.setHeight(height); - newIndexedSprite.setOriginalWidth(width); - newIndexedSprite.setOriginalHeight(height); - newIndexedSprite.setOffsetX(0); - newIndexedSprite.setOffsetY(0); - return newIndexedSprite; - } - - private static BufferedImage rgbaToIndexedBufferedImage(final BufferedImage sourceBufferedImage) { + private static BufferedImage rgbaToIndexedBufferedImage(final BufferedImage sourceBufferedImage) + { final BufferedImage indexedImage = new BufferedImage( - sourceBufferedImage.getWidth(), - sourceBufferedImage.getHeight(), - BufferedImage.TYPE_BYTE_INDEXED); + sourceBufferedImage.getWidth(), + sourceBufferedImage.getHeight(), + BufferedImage.TYPE_BYTE_INDEXED); final ColorModel cm = indexedImage.getColorModel(); final IndexColorModel icm = (IndexColorModel) cm; @@ -309,13 +359,16 @@ public class PrayAgainstPlayerPlugin extends Plugin { return resultIndexedImage; } - private static BufferedImage ProtectionIconFromSprite(final BufferedImage freezeSprite) { + private static BufferedImage ProtectionIconFromSprite(final BufferedImage freezeSprite) + { final BufferedImage freezeCanvas = ImageUtil.resizeCanvas(freezeSprite, PROTECTION_ICON_DIMENSION.width, PROTECTION_ICON_DIMENSION.height); return ImageUtil.outlineImage(freezeCanvas, PROTECTION_ICON_OUTLINE_COLOR); } - BufferedImage getProtectionIcon(WeaponType weaponType) { - switch (weaponType) { + BufferedImage getProtectionIcon(WeaponType weaponType) + { + switch (weaponType) + { case WEAPON_RANGED: return ProtectionIcons[0]; case WEAPON_MELEE: