Skip to content

Commit 4f676cf

Browse files
Merge pull request #2 from remcowesterhoud/chat_command
feat: add !taskman chat command
2 parents 3aaca1a + 6d680e6 commit 4f676cf

6 files changed

Lines changed: 171 additions & 20 deletions

File tree

src/main/java/com/westerhoud/osrs/taskman/TaskmanConfig.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,13 @@ default String websitePassword() {
8181
default boolean showOverlay() {
8282
return false;
8383
}
84+
85+
@ConfigItem(
86+
position = 8,
87+
keyName = "taskmanCommand",
88+
name = "Enable !taskman chat command",
89+
description = "Send your current progress and task into the chat")
90+
default boolean taskmanCommand() {
91+
return true;
92+
}
8493
}

src/main/java/com/westerhoud/osrs/taskman/TaskmanPlugin.java

Lines changed: 90 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,27 @@
44
import com.westerhoud.osrs.taskman.domain.AccountCredentials;
55
import com.westerhoud.osrs.taskman.domain.AccountProgress;
66
import com.westerhoud.osrs.taskman.domain.Task;
7+
import com.westerhoud.osrs.taskman.domain.TaskmanCommandData;
78
import com.westerhoud.osrs.taskman.service.TaskService;
89
import com.westerhoud.osrs.taskman.ui.CurrentTaskOverlay;
910
import com.westerhoud.osrs.taskman.ui.TaskmanPluginPanel;
1011
import java.awt.image.BufferedImage;
12+
import java.io.IOException;
1113
import javax.inject.Inject;
1214
import javax.swing.SwingUtilities;
1315
import lombok.extern.slf4j.Slf4j;
16+
import net.runelite.api.ChatMessageType;
1417
import net.runelite.api.Client;
18+
import net.runelite.api.GameState;
19+
import net.runelite.api.MessageNode;
20+
import net.runelite.api.Player;
21+
import net.runelite.api.events.ChatMessage;
22+
import net.runelite.api.events.GameStateChanged;
23+
import net.runelite.api.events.GameTick;
24+
import net.runelite.client.callback.ClientThread;
25+
import net.runelite.client.chat.ChatColorType;
26+
import net.runelite.client.chat.ChatCommandManager;
27+
import net.runelite.client.chat.ChatMessageBuilder;
1528
import net.runelite.client.config.ConfigManager;
1629
import net.runelite.client.eventbus.Subscribe;
1730
import net.runelite.client.events.ConfigChanged;
@@ -21,30 +34,36 @@
2134
import net.runelite.client.ui.NavigationButton;
2235
import net.runelite.client.ui.overlay.OverlayManager;
2336
import net.runelite.client.util.ImageUtil;
37+
import net.runelite.client.util.Text;
2438
import okhttp3.OkHttpClient;
2539

2640
@Slf4j
2741
@PluginDescriptor(name = "Taskman")
2842
public class TaskmanPlugin extends Plugin {
2943

30-
public static final String TASKMAN_CONFIG_GROUP = "taskman";
44+
private static final String TASKMAN_CONFIG_GROUP = "taskman";
45+
private static final String TASKMAN_CHAT_COMMAND = "!taskman";
3146
@Inject private Client client;
47+
@Inject private ClientThread clientThread;
3248
@Inject private ClientToolbar clientToolbar;
3349
@Inject private TaskmanConfig config;
3450
@Inject private OkHttpClient okHttpClient;
3551
@Inject private OverlayManager overlayManager;
3652
@Inject private CurrentTaskOverlay currentTaskOverlay;
53+
@Inject private ChatCommandManager chatCommandManager;
3754

3855
private TaskmanPluginPanel sidePanel;
3956
private TaskService taskService;
4057
private NavigationButton navigationButton;
58+
private boolean loggedIn = false;
59+
private boolean sidePanelInitialized = false;
4160

4261
@Override
4362
protected void startUp() throws Exception {
4463
// Sidebar
4564
final BufferedImage icon = ImageUtil.loadImageResource(getClass(), "icon.png");
46-
4765
taskService = new TaskService(okHttpClient);
66+
chatCommandManager.registerCommandAsync(TASKMAN_CHAT_COMMAND, this::getTaskmanCommandData);
4867

4968
sidePanel = new TaskmanPluginPanel(this);
5069
navigationButton =
@@ -63,28 +82,67 @@ protected void shutDown() throws Exception {
6382
// Sidebar
6483
clientToolbar.removeNavigation(navigationButton);
6584
overlayManager.remove(currentTaskOverlay);
85+
chatCommandManager.unregisterCommand(TASKMAN_CHAT_COMMAND);
6686
}
6787

6888
public Task getCurrentTask() throws Exception {
69-
final Task task = taskService.getCurrentTask(getCredentials());
89+
final Task task = taskService.getCurrentTask(getCredentials(), getRsn());
7090
currentTaskOverlay.setTask(task);
7191
return task;
7292
}
7393

7494
public Task generateTask() throws Exception {
75-
final Task task = taskService.generateTask(getCredentials());
95+
final Task task = taskService.generateTask(getCredentials(), getRsn());
7696
currentTaskOverlay.setTask(task);
7797
return task;
7898
}
7999

80100
public Task completeTask() throws Exception {
81-
final Task task = taskService.completeTask(getCredentials());
101+
final Task task = taskService.completeTask(getCredentials(), getRsn());
82102
currentTaskOverlay.setTask(task);
83103
return task;
84104
}
85105

86106
public AccountProgress progress() throws Exception {
87-
return taskService.getAccountProgress(getCredentials());
107+
return taskService.getAccountProgress(getCredentials(), getRsn());
108+
}
109+
110+
private void getTaskmanCommandData(final ChatMessage chatMessage, final String message) {
111+
if (!config.taskmanCommand()) {
112+
return;
113+
}
114+
115+
final ChatMessageType type = chatMessage.getType();
116+
final String rsn;
117+
if (type == ChatMessageType.PRIVATECHATOUT) {
118+
rsn = getRsn();
119+
} else {
120+
rsn = Text.removeTags(chatMessage.getName()).replace('\u00A0', ' ');
121+
}
122+
123+
final TaskmanCommandData data;
124+
try {
125+
data = taskService.getChatCommandData(rsn);
126+
} catch (final IOException ex) {
127+
log.debug("Unable to get chat command data", ex);
128+
return;
129+
}
130+
131+
final String response =
132+
new ChatMessageBuilder()
133+
.append(ChatColorType.NORMAL)
134+
.append("Progress: ")
135+
.append(ChatColorType.HIGHLIGHT)
136+
.append(data.getProgressPercentage() + "% " + data.getTier())
137+
.append(ChatColorType.NORMAL)
138+
.append(" Current task: ")
139+
.append(ChatColorType.HIGHLIGHT)
140+
.append(data.getTask().getName())
141+
.build();
142+
143+
final MessageNode messageNode = chatMessage.getMessageNode();
144+
messageNode.setRuneLiteFormatMessage(response);
145+
client.refreshChat();
88146
}
89147

90148
private AccountCredentials getCredentials() {
@@ -100,6 +158,32 @@ private AccountCredentials getCredentials() {
100158
}
101159
}
102160

161+
private String getRsn() {
162+
final Player player = client.getLocalPlayer();
163+
if (player == null) {
164+
throw new IllegalArgumentException("Please login first!");
165+
}
166+
return player.getName();
167+
}
168+
169+
@Subscribe
170+
public void onGameStateChanged(final GameStateChanged gameStateChanged) {
171+
if (gameStateChanged.getGameState() == GameState.LOGGED_IN) {
172+
loggedIn = true;
173+
} else if (gameStateChanged.getGameState() == GameState.LOGIN_SCREEN) {
174+
loggedIn = false;
175+
sidePanel.onLogout();
176+
}
177+
}
178+
179+
@Subscribe
180+
public void onGameTick(final GameTick gameTick) {
181+
if (!sidePanelInitialized && loggedIn) {
182+
sidePanel.init();
183+
sidePanelInitialized = true;
184+
}
185+
}
186+
103187
@Subscribe
104188
public void onConfigChanged(final ConfigChanged configChanged) {
105189
if (configChanged.getGroup().equals(TASKMAN_CONFIG_GROUP)) {

src/main/java/com/westerhoud/osrs/taskman/api/TaskService.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
import com.westerhoud.osrs.taskman.domain.AccountCredentials;
44
import com.westerhoud.osrs.taskman.domain.AccountProgress;
55
import com.westerhoud.osrs.taskman.domain.Task;
6+
import com.westerhoud.osrs.taskman.domain.TaskmanCommandData;
67
import java.io.IOException;
78

89
public interface TaskService {
910

10-
Task getCurrentTask(final AccountCredentials credentials) throws IOException;
11+
Task getCurrentTask(final AccountCredentials credentials, final String name) throws IOException;
1112

12-
Task generateTask(final AccountCredentials credentials) throws IOException;
13+
Task generateTask(final AccountCredentials credentials, final String name) throws IOException;
1314

14-
Task completeTask(final AccountCredentials credentials) throws IOException;
15+
Task completeTask(final AccountCredentials credentials, final String name) throws IOException;
1516

16-
AccountProgress getAccountProgress(final AccountCredentials credentials) throws IOException;
17+
AccountProgress getAccountProgress(final AccountCredentials credentials, final String name)
18+
throws IOException;
19+
20+
TaskmanCommandData getChatCommandData(String rsn) throws IOException;
1721
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.westerhoud.osrs.taskman.domain;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
@Data
8+
@NoArgsConstructor
9+
@AllArgsConstructor
10+
public class TaskmanCommandData {
11+
private Task task;
12+
private String tier;
13+
private int progressPercentage;
14+
}

src/main/java/com/westerhoud/osrs/taskman/service/TaskService.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
import com.westerhoud.osrs.taskman.domain.AccountProgress;
66
import com.westerhoud.osrs.taskman.domain.ErrorResponse;
77
import com.westerhoud.osrs.taskman.domain.Task;
8+
import com.westerhoud.osrs.taskman.domain.TaskmanCommandData;
89
import java.io.IOException;
910
import lombok.extern.slf4j.Slf4j;
1011
import okhttp3.MediaType;
1112
import okhttp3.OkHttpClient;
1213
import okhttp3.Request;
14+
import okhttp3.Request.Builder;
1315
import okhttp3.RequestBody;
1416
import okhttp3.Response;
1517

@@ -19,13 +21,15 @@ public class TaskService implements com.westerhoud.osrs.taskman.api.TaskService
1921
public static final String TASKMAN_IDENTIFIER_HEADER = "x-taskman-identifier";
2022
public static final String TASKMAN_PASSWORD_HEADER = "x-taskman-password";
2123
public static final String TASKMAN_SOURCE_HEADER = "x-taskman-source";
24+
public static final String TASKMAN_RSN_HEADER = "x-taskman-rsn";
2225
private static final String BASE_URL = "https://osrs-taskman.herokuapp.com/task";
2326
private final OkHttpClient client;
2427
private final Gson gson;
2528
private final String currentUrl;
2629
private final String generateUrl;
2730
private final String completeUrl;
2831
private final String progressUrl;
32+
private final String commandUrl;
2933

3034
public TaskService(final OkHttpClient okHttpClient) {
3135
client = okHttpClient;
@@ -34,10 +38,12 @@ public TaskService(final OkHttpClient okHttpClient) {
3438
generateUrl = BASE_URL + "/generate";
3539
completeUrl = BASE_URL + "/complete";
3640
progressUrl = BASE_URL + "/progress";
41+
commandUrl = BASE_URL + "/command/%s";
3742
}
3843

3944
@Override
40-
public Task getCurrentTask(final AccountCredentials credentials) throws IOException {
45+
public Task getCurrentTask(final AccountCredentials credentials, final String rsn)
46+
throws IOException {
4147
if (!credentials.isValid()) {
4248
throw new IllegalArgumentException(
4349
"Please configure your credentials in the plugin configurations");
@@ -49,38 +55,43 @@ public Task getCurrentTask(final AccountCredentials credentials) throws IOExcept
4955
.addHeader(TASKMAN_IDENTIFIER_HEADER, credentials.getIdentifier())
5056
.addHeader(TASKMAN_PASSWORD_HEADER, credentials.getPassword())
5157
.addHeader(TASKMAN_SOURCE_HEADER, credentials.getSource().name())
58+
.addHeader(TASKMAN_RSN_HEADER, rsn)
5259
.get()
5360
.build();
5461

5562
return executeRequest(request);
5663
}
5764

5865
@Override
59-
public Task generateTask(final AccountCredentials credentials) throws IOException {
66+
public Task generateTask(final AccountCredentials credentials, final String rsn)
67+
throws IOException {
6068
final Request request =
6169
new Request.Builder()
6270
.url(generateUrl)
6371
.header("Content-Type", "application/json")
6472
.addHeader(TASKMAN_SOURCE_HEADER, credentials.getSource().name())
73+
.addHeader(TASKMAN_RSN_HEADER, rsn)
6574
.post(getRequestBody(credentials))
6675
.build();
6776
return executeRequest(request);
6877
}
6978

7079
@Override
71-
public Task completeTask(final AccountCredentials credentials) throws IOException {
80+
public Task completeTask(final AccountCredentials credentials, final String rsn)
81+
throws IOException {
7282
final Request request =
7383
new Request.Builder()
7484
.url(completeUrl)
7585
.header("Content-Type", "application/json")
7686
.addHeader(TASKMAN_SOURCE_HEADER, credentials.getSource().name())
87+
.addHeader(TASKMAN_RSN_HEADER, rsn)
7788
.post(getRequestBody(credentials))
7889
.build();
7990
return executeRequest(request);
8091
}
8192

8293
@Override
83-
public AccountProgress getAccountProgress(final AccountCredentials credentials)
94+
public AccountProgress getAccountProgress(final AccountCredentials credentials, final String rsn)
8495
throws IOException {
8596
if (!credentials.isValid()) {
8697
throw new IllegalArgumentException(
@@ -93,6 +104,7 @@ public AccountProgress getAccountProgress(final AccountCredentials credentials)
93104
.addHeader(TASKMAN_IDENTIFIER_HEADER, credentials.getIdentifier())
94105
.addHeader(TASKMAN_PASSWORD_HEADER, credentials.getPassword())
95106
.addHeader(TASKMAN_SOURCE_HEADER, credentials.getSource().name())
107+
.addHeader(TASKMAN_RSN_HEADER, rsn)
96108
.get()
97109
.build();
98110

@@ -106,6 +118,19 @@ public AccountProgress getAccountProgress(final AccountCredentials credentials)
106118
throw new IllegalArgumentException(error.getMessage());
107119
}
108120

121+
@Override
122+
public TaskmanCommandData getChatCommandData(final String rsn) throws IOException {
123+
final Request request = new Builder().url(String.format(commandUrl, rsn)).get().build();
124+
125+
final Response response = client.newCall(request).execute();
126+
127+
if (response.code() == 200) {
128+
return gson.fromJson(response.body().string(), TaskmanCommandData.class);
129+
} else {
130+
throw new IllegalArgumentException("Could not get task command data for rsn: " + rsn);
131+
}
132+
}
133+
109134
private RequestBody getRequestBody(final AccountCredentials credentials) {
110135
return RequestBody.create(MediaType.parse("application/json"), gson.toJson(credentials));
111136
}

0 commit comments

Comments
 (0)