Add clan member manager to cache clanmember ranks, use in clanchat plugin
This commit is contained in:
@@ -43,6 +43,7 @@ import net.runelite.client.account.SessionManager;
|
||||
import net.runelite.client.chat.ChatMessageManager;
|
||||
import net.runelite.client.config.ConfigManager;
|
||||
import net.runelite.client.discord.DiscordService;
|
||||
import net.runelite.client.game.ClanManager;
|
||||
import net.runelite.client.game.ItemManager;
|
||||
import net.runelite.client.menus.MenuManager;
|
||||
import net.runelite.client.plugins.PluginManager;
|
||||
@@ -101,6 +102,9 @@ public class RuneLite
|
||||
@Inject
|
||||
private ItemManager itemManager;
|
||||
|
||||
@Inject
|
||||
private ClanManager clanManager;
|
||||
|
||||
Client client;
|
||||
|
||||
public static void main(String[] args) throws Exception
|
||||
@@ -161,6 +165,7 @@ public class RuneLite
|
||||
eventBus.register(chatMessageManager);
|
||||
eventBus.register(pluginManager);
|
||||
eventBus.register(itemManager);
|
||||
eventBus.register(clanManager);
|
||||
|
||||
// Load user configuration
|
||||
configManager.load();
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright (c) 2018, Adam <Adam@sigterm.info>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package net.runelite.client.game;
|
||||
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import com.google.common.eventbus.Subscribe;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Provider;
|
||||
import javax.inject.Singleton;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.runelite.api.ClanMember;
|
||||
import net.runelite.api.ClanMemberRank;
|
||||
import net.runelite.api.Client;
|
||||
import net.runelite.api.events.ClanChanged;
|
||||
import net.runelite.client.util.Text;
|
||||
|
||||
@Singleton
|
||||
@Slf4j
|
||||
public class ClanManager
|
||||
{
|
||||
@Inject
|
||||
private Provider<Client> clientProvider;
|
||||
|
||||
private final LoadingCache<String, ClanMemberRank> clanRanksCache = CacheBuilder.newBuilder()
|
||||
.maximumSize(100)
|
||||
.expireAfterWrite(1, TimeUnit.MINUTES)
|
||||
.build(new CacheLoader<String, ClanMemberRank>()
|
||||
{
|
||||
@Override
|
||||
public ClanMemberRank load(String key) throws Exception
|
||||
{
|
||||
final Client client = clientProvider.get();
|
||||
final ClanMember[] clanMembersArr = client.getClanMembers();
|
||||
|
||||
if (clanMembersArr == null || clanMembersArr.length == 0)
|
||||
{
|
||||
return ClanMemberRank.UNRANKED;
|
||||
}
|
||||
|
||||
return Arrays.stream(clanMembersArr)
|
||||
.filter(Objects::nonNull)
|
||||
.filter(clanMember -> sanitize(clanMember.getUsername()).equals(sanitize(key)))
|
||||
.map(ClanMember::getRank)
|
||||
.findAny()
|
||||
.orElse(ClanMemberRank.UNRANKED);
|
||||
}
|
||||
});
|
||||
|
||||
public ClanMemberRank getRank(String playerName)
|
||||
{
|
||||
return clanRanksCache.getUnchecked(playerName);
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onClanChange(ClanChanged clanChanged)
|
||||
{
|
||||
clanRanksCache.invalidateAll();
|
||||
}
|
||||
|
||||
private static String sanitize(String lookup)
|
||||
{
|
||||
final String cleaned = Text.removeTags(lookup);
|
||||
return cleaned.replace('\u00A0', ' ');
|
||||
}
|
||||
}
|
||||
@@ -24,9 +24,6 @@
|
||||
*/
|
||||
package net.runelite.client.plugins.clanchat;
|
||||
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import com.google.common.eventbus.Subscribe;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.image.ColorModel;
|
||||
@@ -36,13 +33,10 @@ import java.awt.image.WritableRaster;
|
||||
import java.io.IOException;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.inject.Inject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.runelite.api.ChatMessageType;
|
||||
import net.runelite.api.ClanMember;
|
||||
import net.runelite.api.ClanMemberRank;
|
||||
import net.runelite.api.Client;
|
||||
import net.runelite.api.GameState;
|
||||
@@ -51,6 +45,7 @@ import net.runelite.api.events.GameStateChanged;
|
||||
import net.runelite.api.events.SetMessage;
|
||||
import net.runelite.api.widgets.Widget;
|
||||
import net.runelite.api.widgets.WidgetInfo;
|
||||
import net.runelite.client.game.ClanManager;
|
||||
import net.runelite.client.plugins.Plugin;
|
||||
import net.runelite.client.plugins.PluginDescriptor;
|
||||
import net.runelite.client.task.Schedule;
|
||||
@@ -69,36 +64,14 @@ public class ClanChatPlugin extends Plugin
|
||||
"General_clan_rank.png", "Owner_clan_rank.png"
|
||||
};
|
||||
|
||||
|
||||
private final LoadingCache<String, ClanMemberRank> clanRanksCache = CacheBuilder.newBuilder()
|
||||
.maximumSize(100)
|
||||
.expireAfterWrite(1, TimeUnit.MINUTES)
|
||||
.build(new CacheLoader<String, ClanMemberRank>()
|
||||
{
|
||||
@Override
|
||||
public ClanMemberRank load(String key) throws Exception
|
||||
{
|
||||
final ClanMember[] clanMembersArr = client.getClanMembers();
|
||||
|
||||
if (clanMembersArr == null || clanMembersArr.length == 0)
|
||||
{
|
||||
return ClanMemberRank.UNRANKED;
|
||||
}
|
||||
|
||||
return Arrays.stream(clanMembersArr)
|
||||
.filter(Objects::nonNull)
|
||||
.filter(clanMember -> sanitize(clanMember.getUsername()).equals(sanitize(key)))
|
||||
.map(ClanMember::getRank)
|
||||
.findAny()
|
||||
.orElse(ClanMemberRank.UNRANKED);
|
||||
}
|
||||
});
|
||||
|
||||
private int modIconsLength;
|
||||
|
||||
@Inject
|
||||
private Client client;
|
||||
|
||||
@Inject
|
||||
private ClanManager clanManager;
|
||||
|
||||
@Override
|
||||
protected void startUp() throws Exception
|
||||
{
|
||||
@@ -108,12 +81,6 @@ public class ClanChatPlugin extends Plugin
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void shutDown()
|
||||
{
|
||||
clanRanksCache.invalidateAll();
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onGameStateChanged(GameStateChanged gameStateChanged)
|
||||
{
|
||||
@@ -233,8 +200,7 @@ public class ClanChatPlugin extends Plugin
|
||||
|
||||
private void insertClanRankIcon(final SetMessage message)
|
||||
{
|
||||
final String playerName = sanitize(message.getName());
|
||||
final ClanMemberRank rank = clanRanksCache.getUnchecked(playerName);
|
||||
final ClanMemberRank rank = clanManager.getRank(message.getName());
|
||||
|
||||
if (rank != null && rank != ClanMemberRank.UNRANKED)
|
||||
{
|
||||
@@ -249,10 +215,4 @@ public class ClanChatPlugin extends Plugin
|
||||
{
|
||||
return modIconsLength - CLANCHAT_IMAGES.length + clanMemberRank.getValue();
|
||||
}
|
||||
|
||||
private static String sanitize(String lookup)
|
||||
{
|
||||
final String cleaned = lookup.contains("<img") ? lookup.substring(lookup.lastIndexOf('>') + 1) : lookup;
|
||||
return cleaned.replace('\u00A0', ' ');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user