|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Ferrariic, Seltzer Bro, Cyborger1 |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 9 | + * list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 19 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 21 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 22 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 23 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | +package com.botdetector; |
| 27 | + |
| 28 | +import com.botdetector.model.CaseInsensitiveString; |
| 29 | +import com.google.common.collect.ImmutableMap; |
| 30 | +import com.google.common.collect.ImmutableSet; |
| 31 | +import java.util.HashMap; |
| 32 | +import java.util.Map; |
| 33 | +import java.util.regex.Matcher; |
| 34 | +import java.util.regex.Pattern; |
| 35 | +import javax.inject.Inject; |
| 36 | +import net.runelite.api.Client; |
| 37 | +import net.runelite.api.clan.ClanMember; |
| 38 | +import net.runelite.api.clan.ClanRank; |
| 39 | +import net.runelite.api.clan.ClanSettings; |
| 40 | +import net.runelite.api.clan.ClanTitle; |
| 41 | +import net.runelite.api.widgets.Widget; |
| 42 | +import net.runelite.client.util.Text; |
| 43 | + |
| 44 | +public class BotDetectorClanHighlighter |
| 45 | +{ |
| 46 | + private static final String CLAN_NAME = "Bot Detector"; |
| 47 | + |
| 48 | + private static final int HIGHLIGHT_COLOR = 0x00ff00; |
| 49 | + |
| 50 | + private static final int NAME_OFFSET = 1; |
| 51 | + private static final int SPRITE_OFFSET = 2; |
| 52 | + private static final int WIDGETS_PER_NAME = 3; |
| 53 | + |
| 54 | + private static final Pattern POPUP_TITLE_PLAYER_NAME_PATTERN = Pattern.compile("^Set rank for ([\\w\\-\\s]{1,12}):$"); |
| 55 | + |
| 56 | + private static final ImmutableSet<ClanRank> EXCLUDE_RANKS = ImmutableSet.of( |
| 57 | + ClanRank.GUEST, ClanRank.OWNER, ClanRank.JMOD |
| 58 | + ); |
| 59 | + |
| 60 | + @Inject |
| 61 | + private Client client; |
| 62 | + |
| 63 | + private Map<CaseInsensitiveString, ClanRank> toHighlight; |
| 64 | + |
| 65 | + protected void startUp() |
| 66 | + { |
| 67 | + toHighlight = null; |
| 68 | + } |
| 69 | + |
| 70 | + protected void shutDown() |
| 71 | + { |
| 72 | + toHighlight = null; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Gets the left-hand side name list widgets from the clan members interface. |
| 77 | + * @return An array of widgets, or {@code null} if the clan members interface is not currently loaded. |
| 78 | + */ |
| 79 | + private Widget[] getNameWidgets() |
| 80 | + { |
| 81 | + Widget members = client.getWidget(693, 10); |
| 82 | + if (members == null) |
| 83 | + { |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + Widget[] dyn = members.getDynamicChildren(); |
| 88 | + if (dyn.length % WIDGETS_PER_NAME != 0) |
| 89 | + { |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + return dyn; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Gets the current ranks for the members in the clan. The caller must be in {@link #CLAN_NAME}. |
| 98 | + * @return A map of clan member names and their current rank, or {@code null} if the caller is not currently in the correct clan. |
| 99 | + */ |
| 100 | + public ImmutableMap<CaseInsensitiveString, ClanRank> getClanMemberRanks() |
| 101 | + { |
| 102 | + ClanSettings cs = client.getClanSettings(); |
| 103 | + if (cs == null || !CLAN_NAME.equals(cs.getName())) |
| 104 | + { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + return cs.getMembers().stream().collect(ImmutableMap.toImmutableMap( |
| 109 | + cm -> BotDetectorPlugin.normalizeAndWrapPlayerName(cm.getName()), ClanMember::getRank)); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Sets {@link #toHighlight}, then calls {@link #updateHighlight()}. |
| 114 | + * @param toHighlight The map of clan members to highlight and the rank they should be. |
| 115 | + */ |
| 116 | + public void setHighlight(Map<CaseInsensitiveString, ClanRank> toHighlight) |
| 117 | + { |
| 118 | + this.toHighlight = toHighlight; |
| 119 | + updateHighlight(); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Highlights the players that need their ranks changed according to {@link #toHighlight}, |
| 124 | + * assuming the clan members interface is currently loaded. If the rank changer popup is up, |
| 125 | + * the correct rank to set will also be highlighted. |
| 126 | + * The caller must be in {@link #CLAN_NAME}, also see {@link #setHighlight(Map)}. |
| 127 | + */ |
| 128 | + public void updateHighlight() |
| 129 | + { |
| 130 | + if (toHighlight == null) |
| 131 | + { |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + ClanSettings cs = client.getClanSettings(); |
| 136 | + if (cs == null || !CLAN_NAME.equals(cs.getName())) |
| 137 | + { |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + ImmutableMap<CaseInsensitiveString, ClanRank> currentRanks = getClanMemberRanks(); |
| 142 | + if (currentRanks == null) |
| 143 | + { |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + Widget[] nameWidgets = getNameWidgets(); |
| 148 | + if (nameWidgets == null) |
| 149 | + { |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + Map<CaseInsensitiveString, String> checkPopupNames = new HashMap<>(); |
| 154 | + for (int i = 0; i < nameWidgets.length; i += WIDGETS_PER_NAME) |
| 155 | + { |
| 156 | + Widget nameWidget = nameWidgets[i + NAME_OFFSET]; |
| 157 | + CaseInsensitiveString name = BotDetectorPlugin.normalizeAndWrapPlayerName(nameWidget.getText()); |
| 158 | + ClanRank newRank = toHighlight.get(name); |
| 159 | + if (newRank == null || newRank == currentRanks.get(name) || EXCLUDE_RANKS.contains(newRank)) |
| 160 | + { |
| 161 | + continue; |
| 162 | + } |
| 163 | + |
| 164 | + ClanTitle title = cs.titleForRank(newRank); |
| 165 | + if (title == null) |
| 166 | + { |
| 167 | + continue; |
| 168 | + } |
| 169 | + |
| 170 | + nameWidget.setTextColor(HIGHLIGHT_COLOR); |
| 171 | + checkPopupNames.put(name, title.getName()); |
| 172 | + } |
| 173 | + |
| 174 | + // Highlight correct rank in popup if present |
| 175 | + Widget popupTitle = client.getWidget(289, 4); |
| 176 | + Widget popupRanks = client.getWidget(289, 6); |
| 177 | + if (popupTitle == null || popupRanks == null) |
| 178 | + { |
| 179 | + return; |
| 180 | + } |
| 181 | + |
| 182 | + Widget[] popupRanksDyn = popupRanks.getDynamicChildren(); |
| 183 | + if (popupRanks.getDynamicChildren().length % WIDGETS_PER_NAME != 0) |
| 184 | + { |
| 185 | + return; |
| 186 | + } |
| 187 | + |
| 188 | + Matcher match = POPUP_TITLE_PLAYER_NAME_PATTERN.matcher(popupTitle.getChild(1).getText()); |
| 189 | + if (!match.matches()) |
| 190 | + { |
| 191 | + return; |
| 192 | + } |
| 193 | + |
| 194 | + CaseInsensitiveString popupName = BotDetectorPlugin.normalizeAndWrapPlayerName(match.group(1)); |
| 195 | + |
| 196 | + String highlightRank = checkPopupNames.get(popupName); |
| 197 | + if (highlightRank == null) |
| 198 | + { |
| 199 | + return; |
| 200 | + } |
| 201 | + |
| 202 | + for (int i = 0; i < popupRanksDyn.length; i += WIDGETS_PER_NAME) |
| 203 | + { |
| 204 | + Widget w = popupRanksDyn[i + NAME_OFFSET]; |
| 205 | + if (highlightRank.equals(Text.removeTags(w.getText()))) |
| 206 | + { |
| 207 | + w.setTextColor(HIGHLIGHT_COLOR); |
| 208 | + break; |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | +} |
0 commit comments