Skip to content

Commit

Permalink
feat: fire loot notif for pk chests if total value exceeds min (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
iProdigy authored Jan 31, 2024
1 parent 033cd90 commit f4690cd
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Unreleased

- Minor: Fire loot notification for PK loot chests with sufficiently high total value. (#417)
- Minor: Include region information in death notification metadata. (#420)
- Minor: Allow customization of region IDs where deaths should be ignored. (#415)
- Bugfix: Treat deaths in the graveyard room of the mage training arena as safe. (#418)
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/dinkplugin/DinkPluginConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,8 @@ default boolean lootIcons() {
@ConfigItem(
keyName = "minLootValue",
name = "Min Loot value",
description = "The minimum value of an item for a notification to be sent",
description = "The minimum value of an item for a notification to be sent.<br/>" +
"For PK chests, the <i>total</i> value of the items is compared with this threshold",
position = 33,
section = lootSection
)
Expand Down
33 changes: 28 additions & 5 deletions src/main/java/dinkplugin/notifiers/LootNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import dinkplugin.message.Embed;
import dinkplugin.message.NotificationBody;
import dinkplugin.message.NotificationType;
import dinkplugin.message.templating.Evaluable;
import dinkplugin.message.templating.Replacements;
import dinkplugin.message.templating.Template;
import dinkplugin.message.templating.impl.JoiningReplacement;
Expand Down Expand Up @@ -198,10 +199,12 @@ private void handleNotify(Collection<ItemStack> items, String dropper, LootRecor
SerializedItemStack stack = ItemUtils.stackFromItem(itemManager, item.getId(), item.getQuantity());
long totalPrice = stack.getTotalPrice();
boolean worthy = totalPrice >= minValue || matches(itemNameAllowlist, stack.getName());
if (worthy && !matches(itemNameDenylist, stack.getName())) {
sendMessage = true;
lootMessage.component(ItemUtils.templateStack(stack, true));
if (icons) embeds.add(Embed.ofImage(ItemUtils.getItemImageUrl(item.getId())));
if (!matches(itemNameDenylist, stack.getName())) {
if (worthy) {
sendMessage = true;
lootMessage.component(ItemUtils.templateStack(stack, true));
if (icons) embeds.add(Embed.ofImage(ItemUtils.getItemImageUrl(item.getId())));
}
if (max == null || totalPrice > max.getTotalPrice()) {
max = stack;
}
Expand All @@ -210,6 +213,26 @@ private void handleNotify(Collection<ItemStack> items, String dropper, LootRecor
totalStackValue += totalPrice;
}

Evaluable lootMsg;
if (!sendMessage && totalStackValue >= minValue && max != null && "Loot Chest".equalsIgnoreCase(dropper)) {
// Special case: PK loot keys should trigger notification if total value exceeds configured minimum even
// if no single item itself would exceed the min value config - github.com/pajlads/DinkPlugin/issues/403
sendMessage = true;

JoiningReplacement msg = lootMessage.build();
if (msg.getComponents().isEmpty()) {
// ensure %LOOT% isn't empty
lootMsg = Replacements.ofMultiple(" ",
Replacements.ofText("Various items including:"),
ItemUtils.templateStack(max, true)
);
} else {
lootMsg = msg;
}
} else {
lootMsg = lootMessage.build();
}

if (sendMessage) {
String overrideUrl = getWebhookUrl();
if (config.lootRedirectPlayerKill() && !config.pkWebhook().isBlank()) {
Expand All @@ -222,7 +245,7 @@ private void handleNotify(Collection<ItemStack> items, String dropper, LootRecor
.template(config.lootNotifyMessage())
.replacementBoundary("%")
.replacement("%USERNAME%", Replacements.ofText(Utils.getPlayerName(client)))
.replacement("%LOOT%", lootMessage.build())
.replacement("%LOOT%", lootMsg)
.replacement("%TOTAL_VALUE%", Replacements.ofText(QuantityFormatter.quantityToStackSize(totalStackValue)))
.replacement("%SOURCE%", Replacements.ofText(dropper))
.build();
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/dinkplugin/notifiers/LootNotifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import dinkplugin.message.templating.Template;
import dinkplugin.notifiers.data.LootNotificationData;
import dinkplugin.notifiers.data.SerializedItemStack;
import dinkplugin.util.ItemUtils;
import net.runelite.api.ItemID;
import net.runelite.api.NPC;
import net.runelite.api.NpcID;
Expand All @@ -26,6 +27,7 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
Expand Down Expand Up @@ -395,6 +397,55 @@ void testNotifyPlayerForwardBlank() {
);
}

@Test
void testNotifyPkChest() {
// update mocks
final int minValue = 750;
when(config.minLootValue()).thenReturn(minValue);
assert OPAL_PRICE < minValue && TUNA_PRICE < minValue;

// fire event
String source = "Loot Chest";
List<ItemStack> items = List.of(new ItemStack(ItemID.OPAL, 1, null), new ItemStack(ItemID.TUNA, 2, null));
int totalValue = OPAL_PRICE + 2 * TUNA_PRICE;
LootReceived event = new LootReceived(source, -1, LootRecordType.EVENT, items, 1);
plugin.onLootReceived(event);

// verify notification message
verify(messageHandler).createMessage(
PRIMARY_WEBHOOK_URL,
false,
NotificationBody.builder()
.text(
Template.builder()
.template(String.format("%s has looted: Various items including: 1 x {{opal}} (%d) from %s for %s gp", PLAYER_NAME, OPAL_PRICE, source, QuantityFormatter.quantityToStackSize(totalValue)))
.replacement("{{opal}}", Replacements.ofWiki("Opal"))
.build()
)
.extra(new LootNotificationData(List.of(new SerializedItemStack(ItemID.OPAL, 1, OPAL_PRICE, "Opal"), new SerializedItemStack(ItemID.TUNA, 2, TUNA_PRICE, "Tuna")), source, LootRecordType.EVENT, null))
.type(NotificationType.LOOT)
.thumbnailUrl(ItemUtils.getItemImageUrl(ItemID.TUNA))
.build()
);
}

@Test
void testIgnorePkChest() {
// update mocks
final int minValue = 750;
when(config.minLootValue()).thenReturn(minValue);
assert OPAL_PRICE < minValue && TUNA_PRICE < minValue;

// fire event
String source = "Loot Chest";
List<ItemStack> items = List.of(new ItemStack(ItemID.OPAL, 1, null), new ItemStack(ItemID.TUNA, 1, null));
LootReceived event = new LootReceived(source, -1, LootRecordType.EVENT, items, 1);
plugin.onLootReceived(event);

// ensure no notification
verify(messageHandler, never()).createMessage(any(), anyBoolean(), any());
}

@Test
void testIgnorePlayer() {
// prepare mocks
Expand Down

0 comments on commit f4690cd

Please sign in to comment.