Repository: pajlads/DinkPlugin
Pull request #34 (2022-10-19 23:20:18)

fix: ignore more world types
Addresses https://github.com/ATremonte/Discord-Level-Notifications/issues/5


Patch:
diff --git a/src/main/java/dinkplugin/ClueNotifier.java b/src/main/java/dinkplugin/ClueNotifier.java
index f3dc6c4..536075e 100644
--- a/src/main/java/dinkplugin/ClueNotifier.java
+++ b/src/main/java/dinkplugin/ClueNotifier.java
@@ -20,7 +20,7 @@ public class ClueNotifier extends BaseNotifier {
     }
 
     public void handleNotify(String numberCompleted, String clueType) {
-        if (plugin.isSpeedrunWorld()) return;
+        if (plugin.isIgnoredWorld()) return;
         messageBody = new NotificationBody<>();
 
         StringBuilder lootMessage = new StringBuilder();
diff --git a/src/main/java/dinkplugin/CollectionNotifier.java b/src/main/java/dinkplugin/CollectionNotifier.java
index dfe30c3..84e5f7a 100644
--- a/src/main/java/dinkplugin/CollectionNotifier.java
+++ b/src/main/java/dinkplugin/CollectionNotifier.java
@@ -10,7 +10,7 @@ public class CollectionNotifier extends BaseNotifier {
     }
 
     public void handleNotify(String itemName) {
-        if (plugin.isSpeedrunWorld()) return;
+        if (plugin.isIgnoredWorld()) return;
         String notifyMessage = plugin.config.collectionNotifyMessage()
             .replaceAll("%USERNAME%", Utils.getPlayerName())
             .replaceAll("%ITEM%", itemName);
diff --git a/src/main/java/dinkplugin/DeathNotifier.java b/src/main/java/dinkplugin/DeathNotifier.java
index c5c2543..3ca9c68 100644
--- a/src/main/java/dinkplugin/DeathNotifier.java
+++ b/src/main/java/dinkplugin/DeathNotifier.java
@@ -11,7 +11,7 @@ public class DeathNotifier extends BaseNotifier {
 
     @Override
     public void handleNotify() {
-        if (plugin.isSpeedrunWorld()) return;
+        if (plugin.isIgnoredWorld()) return;
         String notifyMessage = plugin.config.deathNotifyMessage()
             .replaceAll("%USERNAME%", Utils.getPlayerName());
         NotificationBody<Object> b = new NotificationBody<>();
diff --git a/src/main/java/dinkplugin/DinkPlugin.java b/src/main/java/dinkplugin/DinkPlugin.java
index 94d7013..882e42d 100644
--- a/src/main/java/dinkplugin/DinkPlugin.java
+++ b/src/main/java/dinkplugin/DinkPlugin.java
@@ -25,18 +25,18 @@ import net.runelite.client.plugins.loottracker.LootReceived;
 import net.runelite.client.ui.DrawManager;
 import net.runelite.client.util.Text;
 import net.runelite.http.api.loottracker.LootRecordType;
-import net.runelite.http.api.worlds.WorldResult;
 import okhttp3.OkHttpClient;
 
 import javax.inject.Inject;
 import java.util.Collection;
-import java.util.Objects;
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import static net.runelite.api.widgets.WidgetID.QUEST_COMPLETED_GROUP_ID;
 
-
 @Slf4j
 @PluginDescriptor(
     name = "Dink",
@@ -75,6 +75,8 @@ public class DinkPlugin extends Plugin {
     public static final Pattern COLLECTION_LOG_REGEX = Pattern.compile("New item added to your collection log: (?<itemName>(.*))");
     private static final Pattern PET_REGEX = Pattern.compile("You have a funny feeling like you.*");
 
+    private static final Set<WorldType> IGNORED_WORLDS = EnumSet.of(WorldType.PVP_ARENA, WorldType.QUEST_SPEEDRUNNING, WorldType.NOSAVE_MODE, WorldType.TOURNAMENT_WORLD);
+
     private final boolean questCompleted = false;
     private boolean clueCompleted = false;
     private String clueCount = "";
@@ -83,7 +85,6 @@ public class DinkPlugin extends Plugin {
     @Inject
     private WorldService worldService;
 
-
     @Override
     protected void startUp() throws Exception {
         Utils.client = client;
@@ -295,14 +296,11 @@ public class DinkPlugin extends Plugin {
         }
     }
 
-    final String SPEED_RUN_WORLD_ACTIVITY = "Speedrunning World";
-    public boolean isSpeedrunWorld() {
-        WorldResult worldresult = worldService.getWorlds();
-        if (worldresult == null) {
-            log.warn("Failed to get worlds, assuming non-speedrun world");
-            return false;
-        }
-        net.runelite.http.api.worlds.World w = worldresult.findWorld(client.getWorld());
-        return w.getActivity().equals(SPEED_RUN_WORLD_ACTIVITY);
+    public static boolean _isIgnoredWorld(Set<WorldType> worldType) {
+        return !Collections.disjoint(IGNORED_WORLDS, worldType);
+    }
+
+    public boolean isIgnoredWorld() {
+        return _isIgnoredWorld(client.getWorldType().clone());
     }
 }
diff --git a/src/main/java/dinkplugin/LevelNotifier.java b/src/main/java/dinkplugin/LevelNotifier.java
index bfb5a16..918f63d 100644
--- a/src/main/java/dinkplugin/LevelNotifier.java
+++ b/src/main/java/dinkplugin/LevelNotifier.java
@@ -81,7 +81,7 @@ public class LevelNotifier extends BaseNotifier {
     }
 
     public void handleLevelUp(String skill, int level, int xp) {
-        if (plugin.isSpeedrunWorld()) return;
+        if (plugin.isIgnoredWorld()) return;
 
         int virtualLevel = level < 99 ? level : Experience.getLevelForXp(xp); // avoid log(n) query when not needed
         Integer previousLevel = currentLevels.put(skill, virtualLevel);
diff --git a/src/main/java/dinkplugin/LootNotifier.java b/src/main/java/dinkplugin/LootNotifier.java
index 47dd8d0..cbebf01 100644
--- a/src/main/java/dinkplugin/LootNotifier.java
+++ b/src/main/java/dinkplugin/LootNotifier.java
@@ -19,7 +19,7 @@ public class LootNotifier extends BaseNotifier {
     }
 
     public void handleNotify(Collection<ItemStack> items, String dropper) {
-        if (plugin.isSpeedrunWorld()) return;
+        if (plugin.isIgnoredWorld()) return;
         NotificationBody<LootNotificationData> messageBody = new NotificationBody<>();
         StringBuilder lootMessage = new StringBuilder();
         int minValue = plugin.config.minLootValue();
diff --git a/src/main/java/dinkplugin/QuestNotifier.java b/src/main/java/dinkplugin/QuestNotifier.java
index 9a3d0a1..925368f 100644
--- a/src/main/java/dinkplugin/QuestNotifier.java
+++ b/src/main/java/dinkplugin/QuestNotifier.java
@@ -1,10 +1,5 @@
 package dinkplugin;
 
-import com.google.common.collect.ImmutableList;
-
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
 public class QuestNotifier extends BaseNotifier {
 
     public QuestNotifier(DinkPlugin plugin) {
@@ -12,7 +7,7 @@ public class QuestNotifier extends BaseNotifier {
     }
 
     public void handleNotify(String questText) {
-        if (plugin.isSpeedrunWorld()) return;
+        if (plugin.isIgnoredWorld()) return;
         String notifyMessage = plugin.config.questNotifyMessage()
             .replaceAll("%USERNAME%", Utils.getPlayerName())
             .replaceAll("%QUEST%", Utils.parseQuestWidget(questText));
diff --git a/src/main/java/dinkplugin/SlayerNotifier.java b/src/main/java/dinkplugin/SlayerNotifier.java
index c6aeb3c..4763af4 100644
--- a/src/main/java/dinkplugin/SlayerNotifier.java
+++ b/src/main/java/dinkplugin/SlayerNotifier.java
@@ -19,7 +19,7 @@ public class SlayerNotifier extends BaseNotifier {
 
     @Override
     public void handleNotify() {
-        if (plugin.isSpeedrunWorld()) return;
+        if (plugin.isIgnoredWorld()) return;
         // Little jank, but it's a bit cleaner than having bools and checking in the main plugin class
         if (slayerPoints.isEmpty() || slayerTask.isEmpty() || slayerCompleted.isEmpty()) {
             return;


Test patch:
diff --git a/src/test/java/dinkplugin/IgnoredWorldTest.kt b/src/test/java/dinkplugin/IgnoredWorldTest.kt
new file mode 100644
index 0000000..4cb88ff
--- /dev/null
+++ b/src/test/java/dinkplugin/IgnoredWorldTest.kt
@@ -0,0 +1,45 @@
+package dinkplugin
+
+import org.junit.jupiter.api.Assertions.*
+import org.junit.jupiter.api.extension.ExtensionContext
+import org.junit.jupiter.params.ParameterizedTest
+import org.junit.jupiter.params.provider.Arguments
+import org.junit.jupiter.params.provider.ArgumentsProvider
+import org.junit.jupiter.params.provider.ArgumentsSource
+import org.junit.jupiter.params.provider.ValueSource
+import java.util.stream.Stream
+import java.util.EnumSet;
+import net.runelite.api.WorldType;
+
+class IgnoredWorldTest {
+    @ParameterizedTest(name = "World should not be ignored {0}")
+    @ArgumentsSource(NonIgnoredWorldTypeProvider::class)
+    fun `World should not be ignored`(worldType: Set<WorldType>) {
+        assertFalse(DinkPlugin._isIgnoredWorld(worldType))
+    }
+
+    private class NonIgnoredWorldTypeProvider : ArgumentsProvider {
+        override fun provideArguments(context: ExtensionContext?): Stream<out Arguments> = Stream.of(
+            Arguments.of(EnumSet.of(WorldType.MEMBERS)),
+            Arguments.of(emptySet<WorldType>()),
+            Arguments.of(EnumSet.of(WorldType.SKILL_TOTAL)),
+            Arguments.of(EnumSet.of(WorldType.FRESH_START_WORLD)),
+        )
+    }
+
+    @ParameterizedTest(name = "World should be ignored {0}")
+    @ArgumentsSource(IgnoredWorldTypeProvider::class)
+    fun `World should be ignored`(worldType: Set<WorldType>) {
+        assertTrue(DinkPlugin._isIgnoredWorld(worldType))
+    }
+
+    private class IgnoredWorldTypeProvider : ArgumentsProvider {
+        override fun provideArguments(context: ExtensionContext?): Stream<out Arguments> = Stream.of(
+            Arguments.of(EnumSet.of(WorldType.PVP_ARENA)),
+            Arguments.of(EnumSet.of(WorldType.QUEST_SPEEDRUNNING)),
+            Arguments.of(EnumSet.of(WorldType.NOSAVE_MODE)),
+            Arguments.of(EnumSet.of(WorldType.TOURNAMENT_WORLD)),
+            Arguments.of(EnumSet.of(WorldType.FRESH_START_WORLD, WorldType.NOSAVE_MODE)),
+        )
+    }
+}