runelite-client: add lvl chat command

This commit is contained in:
Adam
2017-07-21 18:17:34 -04:00
parent 8b5f22106e
commit 2db13e4a58
3 changed files with 147 additions and 9 deletions

View File

@@ -31,12 +31,16 @@ import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import net.runelite.api.Client;
import net.runelite.api.GameState;
import net.runelite.http.api.item.Item;
import net.runelite.api.MessageNode;
import net.runelite.client.game.ItemManager;
import net.runelite.client.RuneLite;
import net.runelite.client.events.SetMessage;
import net.runelite.client.game.ItemManager;
import net.runelite.client.plugins.Plugin;
import net.runelite.http.api.hiscore.HiscoreClient;
import net.runelite.http.api.hiscore.HiscoreSkill;
import net.runelite.http.api.hiscore.SingleHiscoreSkillResult;
import net.runelite.http.api.hiscore.Skill;
import net.runelite.http.api.item.Item;
import net.runelite.http.api.item.ItemClient;
import net.runelite.http.api.item.ItemPrice;
import net.runelite.http.api.item.SearchResult;
@@ -53,6 +57,7 @@ public class ChatCommands extends Plugin
private final ItemClient itemClient = new ItemClient();
private final RuneLite runelite = RuneLite.getRunelite();
private final Client client = RuneLite.getClient();
private final HiscoreClient hiscoreClient = new HiscoreClient();
private static final float HIGH_ALCHEMY_CONSTANT = 0.6f;
@@ -74,7 +79,7 @@ public class ChatCommands extends Plugin
@Subscribe
public void onSetMessage(SetMessage setMessage)
{
if (client.getGameState() != GameState.LOGGED_IN || !config.enabled())
if (client.getGameState() != GameState.LOGGED_IN)
{
return;
}
@@ -92,15 +97,23 @@ public class ChatCommands extends Plugin
String message = setMessage.getValue();
if (message.toLowerCase().startsWith("!price") && message.length() > 7)
if (config.price() && message.toLowerCase().startsWith("!price") && message.length() > 7)
{
String search = message.substring(7);
logger.debug("Running lookup for {}", search);
logger.debug("Running price lookup for {}", search);
ScheduledExecutorService executor = runelite.getExecutor();
executor.submit(() -> lookup(setMessage.getMessageNode(), search));
}
else if (config.lvl() && message.toLowerCase().startsWith("!lvl") && message.length() > 5)
{
String search = message.substring(5);
logger.debug("Running level lookup for {}", search);
ScheduledExecutorService executor = runelite.getExecutor();
executor.submit(() -> playerSkillLookup(setMessage, search));
}
}
/**
@@ -164,6 +177,58 @@ public class ChatCommands extends Plugin
}
}
/**
* Looks up the player skill and changes the original message to the
* reponse.
*
* @param setMessage The chat message containing the command.
* @param search The item given with the command.
*/
private void playerSkillLookup(SetMessage setMessage, String search)
{
String player = sanitize(setMessage.getName());
try
{
search = SkillAbbreviations.valueOf(search.toUpperCase()).getName();
}
catch (IllegalArgumentException i)
{
}
HiscoreSkill skill;
try
{
skill = HiscoreSkill.valueOf(search.toUpperCase());
}
catch (IllegalArgumentException i)
{
return;
}
try
{
SingleHiscoreSkillResult result = hiscoreClient.lookup(player, skill);
Skill hiscoreSkill = result.getSkill();
String response = new StringBuilder().append("Level ").append(skill.getName()).append(": ")
.append(hiscoreSkill.getLevel())
.append(" Experience: ").append(String.format("%,d", hiscoreSkill.getExperience()))
.append(" Rank: ").append(String.format("%,d", hiscoreSkill.getRank()))
.toString();
logger.debug("Setting response {}", response);
// XXX hopefully messageNode hasn't been reused yet?
setMessage.getMessageNode().setValue(response);
client.refreshChat();
}
catch (IOException ex)
{
logger.warn("unable to look up skill {} for {}", skill, search, ex);
}
}
/**
* Compares the names of the items in the list with the original input.
* returns the item if its name is equal to the original input or null
@@ -184,4 +249,17 @@ public class ChatCommands extends Plugin
}
return null;
}
/**
* Cleans the playername string from ironman status icon if present and
* corrects spaces
*
* @param lookup Playername to lookup
* @return Cleaned playername
*/
private static String sanitize(String lookup)
{
String cleaned = lookup.contains("<img") ? lookup.substring(lookup.lastIndexOf('>') + 1) : lookup;
return cleaned.replace('\u00A0', ' ');
}
}

View File

@@ -35,13 +35,22 @@ import net.runelite.client.config.ConfigItem;
public interface ChatCommandsConfig
{
@ConfigItem(
keyName = "enabled",
name = "Enable",
description = "Configures whether chat commands are enabled"
keyName = "price",
name = "Price Command",
description = "Configures whether the Price command is enabled"
)
default boolean enabled()
default boolean price()
{
return true;
}
@ConfigItem(
keyName = "lvl",
name = "Level Command",
description = "Configures whether the Level command is enabled"
)
default boolean lvl()
{
return true;
}
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright (c) 2017, 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.plugins.chatcommands;
enum SkillAbbreviations
{
ATT("Attack"),
DEF("Defence"),
STR("Strength"),
HP("Hitpoints"),
RANGE("Ranged"),
WC("Woodcutting"),
FM("Firemaking"),
RC("Runecraft"),
CON("Construction"),
TOTAL("Overall");
private final String name;
SkillAbbreviations(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}