Repository: pajlads/DinkPlugin (java)

Issue:
Death notifier: Include extra information about zone/area the player died in
If it's possible to get zone name, it could be used for differing the message if a user dies in NMZ vs in a raid

Pull request description:
feat: include region in death metadata
Closes #56


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ae9b0a1..9beb686 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,6 @@
 ## Unreleased
 
+- 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)
 - Dev: Clarify the plugin description. (#419)
diff --git a/docs/json-examples.md b/docs/json-examples.md
index 6e52d38..5c7821c 100644
--- a/docs/json-examples.md
+++ b/docs/json-examples.md
@@ -53,7 +53,12 @@ JSON for non-combat death:
         "priceEach": 3,
         "name": "Feather"
       }
-    ]
+    ],
+    "location": {
+      "regionId": 10546,
+      "plane": 0,
+      "instanced": false
+    }
   },
   "type": "DEATH"
 }
@@ -76,7 +81,12 @@ JSON for PvP scenarios:
         "priceEach": 3,
         "name": "Feather"
       }
-    ]
+    ],
+    "location": {
+      "regionId": 10546,
+      "plane": 0,
+      "instanced": false
+    }
   },
   "type": "DEATH"
 }
@@ -100,7 +110,12 @@ JSON for NPC scenarios:
         "priceEach": 3,
         "name": "Feather"
       }
-    ]
+    ],
+    "location": {
+      "regionId": 10546,
+      "plane": 0,
+      "instanced": false
+    }
   },
   "type": "DEATH"
 }
diff --git a/src/main/java/dinkplugin/notifiers/DeathNotifier.java b/src/main/java/dinkplugin/notifiers/DeathNotifier.java
index cea2101..d50ff9a 100644
--- a/src/main/java/dinkplugin/notifiers/DeathNotifier.java
+++ b/src/main/java/dinkplugin/notifiers/DeathNotifier.java
@@ -6,13 +6,14 @@ import dinkplugin.domain.ExceptionalDeath;
 import dinkplugin.message.Embed;
 import dinkplugin.message.templating.Replacements;
 import dinkplugin.message.templating.Template;
-import dinkplugin.util.ConfigUtil;
-import dinkplugin.util.ItemUtils;
-import dinkplugin.util.Utils;
 import dinkplugin.message.NotificationBody;
 import dinkplugin.message.NotificationType;
 import dinkplugin.notifiers.data.DeathNotificationData;
 import dinkplugin.notifiers.data.SerializedItemStack;
+import dinkplugin.util.ConfigUtil;
+import dinkplugin.util.ItemUtils;
+import dinkplugin.util.Region;
+import dinkplugin.util.Utils;
 import dinkplugin.util.WorldUtils;
 import lombok.Synchronized;
 import lombok.extern.slf4j.Slf4j;
@@ -234,7 +235,8 @@ public class DeathNotifier extends BaseNotifier {
             killerName,
             npc ? ((NPC) killer).getId() : null,
             keptStacks,
-            lostStacks
+            lostStacks,
+            Region.of(client, regionId)
         );
 
         createMessage(config.deathSendImage(), NotificationBody.builder()
diff --git a/src/main/java/dinkplugin/notifiers/data/DeathNotificationData.java b/src/main/java/dinkplugin/notifiers/data/DeathNotificationData.java
index a8774b2..958410f 100644
--- a/src/main/java/dinkplugin/notifiers/data/DeathNotificationData.java
+++ b/src/main/java/dinkplugin/notifiers/data/DeathNotificationData.java
@@ -1,5 +1,6 @@
 package dinkplugin.notifiers.data;
 
+import dinkplugin.util.Region;
 import lombok.EqualsAndHashCode;
 import lombok.Value;
 import org.jetbrains.annotations.NotNull;
@@ -34,4 +35,7 @@ public class DeathNotificationData extends NotificationData {
     @NotNull
     Collection<SerializedItemStack> lostItems;
 
+    @NotNull
+    Region location;
+
 }
diff --git a/src/main/java/dinkplugin/util/Region.java b/src/main/java/dinkplugin/util/Region.java
new file mode 100644
index 0000000..ccc0556
--- /dev/null
+++ b/src/main/java/dinkplugin/util/Region.java
@@ -0,0 +1,19 @@
+package dinkplugin.util;
+
+import lombok.Value;
+import net.runelite.api.Client;
+
+@Value
+public class Region {
+    int regionId;
+    int plane;
+    boolean instanced;
+
+    public static Region of(Client client, int regionId) {
+        return new Region(regionId, client.getPlane(), client.isInInstancedRegion());
+    }
+
+    public static Region of(Client client) {
+        return of(client, WorldUtils.getLocation(client).getRegionID());
+    }
+}


Test patch:
diff --git a/src/test/java/dinkplugin/notifiers/DeathNotifierTest.java b/src/test/java/dinkplugin/notifiers/DeathNotifierTest.java
index 9e6a7a7..450a735 100644
--- a/src/test/java/dinkplugin/notifiers/DeathNotifierTest.java
+++ b/src/test/java/dinkplugin/notifiers/DeathNotifierTest.java
@@ -11,6 +11,7 @@ import dinkplugin.message.templating.Template;
 import dinkplugin.notifiers.data.DeathNotificationData;
 import dinkplugin.notifiers.data.SerializedItemStack;
 import dinkplugin.util.ItemUtils;
+import dinkplugin.util.Region;
 import net.runelite.api.InventoryID;
 import net.runelite.api.Item;
 import net.runelite.api.ItemContainer;
@@ -102,7 +103,7 @@ class DeathNotifierTest extends MockedNotifierTest {
             false,
             NotificationBody.builder()
                 .text(buildTemplate(String.format("%s has died, losing %d gp", PLAYER_NAME, 0)))
-                .extra(new DeathNotificationData(0L, false, null, null, null, Collections.emptyList(), Collections.emptyList()))
+                .extra(new DeathNotificationData(0L, false, null, null, null, Collections.emptyList(), Collections.emptyList(), Region.of(client)))
                 .type(NotificationType.DEATH)
                 .build()
         );
@@ -147,7 +148,7 @@ class DeathNotifierTest extends MockedNotifierTest {
             false,
             NotificationBody.builder()
                 .text(buildTemplate(String.format("%s has died, losing %d gp", PLAYER_NAME, TUNA_PRICE)))
-                .extra(new DeathNotificationData(TUNA_PRICE, false, null, null, null, kept, lost))
+                .extra(new DeathNotificationData(TUNA_PRICE, false, null, null, null, kept, lost, Region.of(client)))
                 .type(NotificationType.DEATH)
                 .embeds(embeds)
                 .build()
@@ -173,7 +174,7 @@ class DeathNotifierTest extends MockedNotifierTest {
             false,
             NotificationBody.builder()
                 .text(buildTemplate(String.format("%s has been PKed by %s for %d gp", PLAYER_NAME, pker, 0)))
-                .extra(new DeathNotificationData(0L, true, pker, pker, null, Collections.emptyList(), Collections.emptyList()))
+                .extra(new DeathNotificationData(0L, true, pker, pker, null, Collections.emptyList(), Collections.emptyList(), Region.of(client)))
                 .type(NotificationType.DEATH)
                 .build()
         );
@@ -200,7 +201,7 @@ class DeathNotifierTest extends MockedNotifierTest {
             false,
             NotificationBody.builder()
                 .text(buildTemplate(String.format("%s has been PKed by %s for %d gp", PLAYER_NAME, pker, 0)))
-                .extra(new DeathNotificationData(0L, true, pker, pker, null, Collections.emptyList(), Collections.emptyList()))
+                .extra(new DeathNotificationData(0L, true, pker, pker, null, Collections.emptyList(), Collections.emptyList(), Region.of(client)))
                 .type(NotificationType.DEATH)
                 .build()
         );
@@ -224,7 +225,7 @@ class DeathNotifierTest extends MockedNotifierTest {
             false,
             NotificationBody.builder()
                 .text(buildTemplate(String.format("%s has died, losing %d gp", PLAYER_NAME, 0)))
-                .extra(new DeathNotificationData(0L, false, null, null, null, Collections.emptyList(), Collections.emptyList()))
+                .extra(new DeathNotificationData(0L, false, null, null, null, Collections.emptyList(), Collections.emptyList(), Region.of(client)))
                 .type(NotificationType.DEATH)
                 .build()
         );
@@ -272,7 +273,7 @@ class DeathNotifierTest extends MockedNotifierTest {
                         .replacement("{{npc}}", Replacements.ofWiki(name))
                         .build()
                 )
-                .extra(new DeathNotificationData(0L, false, null, name, NpcID.GUARD, Collections.emptyList(), Collections.emptyList()))
+                .extra(new DeathNotificationData(0L, false, null, name, NpcID.GUARD, Collections.emptyList(), Collections.emptyList(), Region.of(client)))
                 .type(NotificationType.DEATH)
                 .build()
         );
@@ -313,7 +314,7 @@ class DeathNotifierTest extends MockedNotifierTest {
             false,
             NotificationBody.builder()
                 .text(buildTemplate(String.format("%s has died, losing %d gp", PLAYER_NAME, TUNA_PRICE)))
-                .extra(new DeathNotificationData(TUNA_PRICE, false, null, null, null, kept, lost))
+                .extra(new DeathNotificationData(TUNA_PRICE, false, null, null, null, kept, lost, Region.of(client)))
                 .type(NotificationType.DEATH)
                 .build()
         );
@@ -353,7 +354,7 @@ class DeathNotifierTest extends MockedNotifierTest {
             false,
             NotificationBody.builder()
                 .text(buildTemplate(String.format("%s has died, losing %d gp", PLAYER_NAME, 0)))
-                .extra(new DeathNotificationData(0L, false, null, null, null, kept, Collections.emptyList()))
+                .extra(new DeathNotificationData(0L, false, null, null, null, kept, Collections.emptyList(), Region.of(client)))
                 .type(NotificationType.DEATH)
                 .build()
         );
@@ -394,7 +395,7 @@ class DeathNotifierTest extends MockedNotifierTest {
             false,
             NotificationBody.builder()
                 .text(buildTemplate(String.format("%s has died, losing %d gp", PLAYER_NAME, TUNA_PRICE)))
-                .extra(new DeathNotificationData(TUNA_PRICE, false, null, null, null, kept, lost))
+                .extra(new DeathNotificationData(TUNA_PRICE, false, null, null, null, kept, lost, Region.of(client)))
                 .type(NotificationType.DEATH)
                 .build()
         );
@@ -431,7 +432,7 @@ class DeathNotifierTest extends MockedNotifierTest {
             false,
             NotificationBody.builder()
                 .text(buildTemplate(String.format("%s has died, losing %d gp", PLAYER_NAME, 0)))
-                .extra(new DeathNotificationData(0L, false, null, null, null, kept, Collections.emptyList()))
+                .extra(new DeathNotificationData(0L, false, null, null, null, kept, Collections.emptyList(), Region.of(client)))
                 .type(NotificationType.DEATH)
                 .build()
         );
@@ -469,7 +470,7 @@ class DeathNotifierTest extends MockedNotifierTest {
             false,
             NotificationBody.builder()
                 .text(buildTemplate(String.format("%s has died, losing %d gp", PLAYER_NAME, 0)))
-                .extra(new DeathNotificationData(0L, false, null, null, null, kept, Collections.emptyList()))
+                .extra(new DeathNotificationData(0L, false, null, null, null, kept, Collections.emptyList(), Region.of(client)))
                 .type(NotificationType.DEATH)
                 .build()
         );
@@ -517,7 +518,7 @@ class DeathNotifierTest extends MockedNotifierTest {
             false,
             NotificationBody.builder()
                 .text(buildTemplate(String.format("%s has died, losing %d gp", PLAYER_NAME, TUNA_PRICE)))
-                .extra(new DeathNotificationData(TUNA_PRICE, false, null, null, null, kept, lost))
+                .extra(new DeathNotificationData(TUNA_PRICE, false, null, null, null, kept, lost, Region.of(client)))
                 .type(NotificationType.DEATH)
                 .build()
         );
@@ -581,7 +582,7 @@ class DeathNotifierTest extends MockedNotifierTest {
             false,
             NotificationBody.builder()
                 .text(buildTemplate(String.format("%s has died, losing %d gp", PLAYER_NAME, 0)))
-                .extra(new DeathNotificationData(0L, false, null, null, null, kept, Collections.emptyList()))
+                .extra(new DeathNotificationData(0L, false, null, null, null, kept, Collections.emptyList(), Region.of(client)))
                 .type(NotificationType.DEATH)
                 .build()
         );
@@ -621,7 +622,7 @@ class DeathNotifierTest extends MockedNotifierTest {
             false,
             NotificationBody.builder()
                 .text(buildTemplate(String.format("%s has died, losing %d gp", PLAYER_NAME, TUNA_PRICE)))
-                .extra(new DeathNotificationData(TUNA_PRICE, false, null, null, null, kept, lost))
+                .extra(new DeathNotificationData(TUNA_PRICE, false, null, null, null, kept, lost, Region.of(client)))
                 .type(NotificationType.DEATH)
                 .build()
         );