From 7c56748377f0aa4a796b77a99d3f7bcfc4f844d0 Mon Sep 17 00:00:00 2001 From: Su-Shing Chen Date: Sat, 8 Jun 2019 16:09:50 +1200 Subject: [PATCH 1/2] Sort worlds in world hopper using Guava Use Guava to abstract the world hopper sorting code. This fixes secondary sorting when sorting by activity, e.g. when sorting by "Ping" and then by "Activity". --- .../worldhopper/WorldSwitcherPanel.java | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/worldhopper/WorldSwitcherPanel.java b/runelite-client/src/main/java/net/runelite/client/plugins/worldhopper/WorldSwitcherPanel.java index b27f356dac..df32b03ee4 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/worldhopper/WorldSwitcherPanel.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/worldhopper/WorldSwitcherPanel.java @@ -24,6 +24,7 @@ */ package net.runelite.client.plugins.worldhopper; +import com.google.common.collect.Ordering; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; @@ -33,6 +34,7 @@ import java.awt.event.MouseEvent; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.function.Function; import javax.swing.JPanel; import javax.swing.SwingUtilities; import lombok.AccessLevel; @@ -159,24 +161,23 @@ class WorldSwitcherPanel extends PluginPanel switch (orderIndex) { case PING: - return Integer.compare(r1.getPing(), r2.getPing()) * (ascendingOrder ? 1 : -1); + return getCompareValue(r1, r2, WorldTableRow::getPing); case WORLD: - return Integer.compare(r1.getWorld().getId(), r2.getWorld().getId()) * (ascendingOrder ? 1 : -1); + return getCompareValue(r1, r2, row -> row.getWorld().getId()); case PLAYERS: - return Integer.compare(r1.getUpdatedPlayerCount(), r2.getUpdatedPlayerCount()) * (ascendingOrder ? 1 : -1); + return getCompareValue(r1, r2, WorldTableRow::getUpdatedPlayerCount); case ACTIVITY: - return r1.getWorld().getActivity().compareTo(r2.getWorld().getActivity()) * -1 * (ascendingOrder ? 1 : -1); + // Leave empty activity worlds on the bottom of the list + return getCompareValue(r1, r2, row -> + { + String activity = row.getWorld().getActivity(); + return !activity.equals("-") ? activity : null; + }); default: return 0; } }); - // Leave empty activity worlds on the bottom of the list - if (orderIndex == WorldOrder.ACTIVITY) - { - rows.sort((r1, r2) -> r1.getWorld().getActivity().equals("-") ? 1 : -1); - } - rows.sort((r1, r2) -> { boolean b1 = plugin.isFavorite(r1.getWorld()); @@ -197,6 +198,17 @@ class WorldSwitcherPanel extends PluginPanel listContainer.repaint(); } + private int getCompareValue(WorldTableRow row1, WorldTableRow row2, Function compareByFn) + { + Ordering ordering = Ordering.natural(); + if (!ascendingOrder) + { + ordering = ordering.reverse(); + } + ordering = ordering.nullsLast(); + return ordering.compare(compareByFn.apply(row1), compareByFn.apply(row2)); + } + void updateFavoriteMenu(int world, boolean favorite) { for (WorldTableRow row : rows) From 799158bacd6c55ec7658d76b0f1e7e835833cc58 Mon Sep 17 00:00:00 2001 From: Su-Shing Chen Date: Sat, 8 Jun 2019 16:11:41 +1200 Subject: [PATCH 2/2] Put worlds with unknown ping at the bottom of the world hopper list Closes #8176 --- .../client/plugins/worldhopper/WorldSwitcherPanel.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/worldhopper/WorldSwitcherPanel.java b/runelite-client/src/main/java/net/runelite/client/plugins/worldhopper/WorldSwitcherPanel.java index df32b03ee4..937a9e74d2 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/worldhopper/WorldSwitcherPanel.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/worldhopper/WorldSwitcherPanel.java @@ -161,7 +161,12 @@ class WorldSwitcherPanel extends PluginPanel switch (orderIndex) { case PING: - return getCompareValue(r1, r2, WorldTableRow::getPing); + // Leave worlds with unknown ping at the bottom + return getCompareValue(r1, r2, row -> + { + int ping = row.getPing(); + return ping > 0 ? ping : null; + }); case WORLD: return getCompareValue(r1, r2, row -> row.getWorld().getId()); case PLAYERS: