From 9baab7a49917cf578202dd4514fee0b15262d2be Mon Sep 17 00:00:00 2001 From: Im2be Date: Thu, 16 Jan 2020 21:08:38 +0100 Subject: [PATCH] inventorysetups: Fixed old setups not being imported on boot (#2257) * raids: Fix !layout command & update RaidOverlay * raids: Fixed an uncommon ArrayOutOfBoundsException in RotationSolver * raids: Refactored the RotationSolver logic * inventorysetups: Fixed old setups not being imported (blame Kyle) --- .../inventorysetups/InventorySetupOld.java | 15 +++++ .../inventorysetups/InventorySetupPlugin.java | 38 +++++++++++ .../plugins/raids/solver/RotationSolver.java | 64 +++++++++---------- 3 files changed, 84 insertions(+), 33 deletions(-) create mode 100644 runelite-client/src/main/java/net/runelite/client/plugins/inventorysetups/InventorySetupOld.java diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/inventorysetups/InventorySetupOld.java b/runelite-client/src/main/java/net/runelite/client/plugins/inventorysetups/InventorySetupOld.java new file mode 100644 index 0000000000..7adf1254c5 --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/plugins/inventorysetups/InventorySetupOld.java @@ -0,0 +1,15 @@ +package net.runelite.client.plugins.inventorysetups; + +import java.util.List; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@AllArgsConstructor +public class InventorySetupOld +{ +@Getter(AccessLevel.PUBLIC) +private List inventory; +@Getter(AccessLevel.PUBLIC) +private List equipment; +} diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/inventorysetups/InventorySetupPlugin.java b/runelite-client/src/main/java/net/runelite/client/plugins/inventorysetups/InventorySetupPlugin.java index 9a7452e0a0..5302b0703a 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/inventorysetups/InventorySetupPlugin.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/inventorysetups/InventorySetupPlugin.java @@ -77,6 +77,9 @@ import java.awt.datatransfer.StringSelection; import java.awt.image.BufferedImage; import java.lang.reflect.Type; import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.stream.Collectors; @PluginDescriptor( name = "Inventory Setups", @@ -712,7 +715,38 @@ public class InventorySetupPlugin extends Plugin catch (Exception e) { inventorySetups = new ArrayList<>(); + + //Populate with old inventorysetups + final Gson gson = new Gson(); + Type type = new TypeToken>() + { + }.getType(); + + HashMap oldSetups = new HashMap<>(); + oldSetups.putAll(gson.fromJson(json, type)); + + for (String name : oldSetups.keySet()) + { + InventorySetup newSetup = new InventorySetup( + new ArrayList<>(oldSetups.get(name).getInventory()), + new ArrayList<>(oldSetups.get(name).getEquipment()), + null, + name, + this.highlightColor, + this.highlightStackDifference, + this.highlightVariationDifference, + this.highlightDifference, + this.bankFilter, + this.highlightUnorderedDifference); + + inventorySetups.add(newSetup); + } + } + + inventorySetups = new ArrayList<>(inventorySetups.stream() + .sorted(Comparator.comparing(InventorySetup::getName, String::compareToIgnoreCase)) + .collect(Collectors.toList())); } } @@ -721,6 +755,10 @@ public class InventorySetupPlugin extends Plugin SwingUtilities.invokeLater(() -> { inventorySetups.add(newSetup); + inventorySetups = new ArrayList<>(inventorySetups.stream() + .sorted(Comparator.comparing(InventorySetup::getName, String::compareToIgnoreCase)) + .collect(Collectors.toList())); + panel.rebuild(); updateJsonConfig(); diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/raids/solver/RotationSolver.java b/runelite-client/src/main/java/net/runelite/client/plugins/raids/solver/RotationSolver.java index b3c796e6b1..97fad21eaf 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/raids/solver/RotationSolver.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/raids/solver/RotationSolver.java @@ -27,6 +27,7 @@ package net.runelite.client.plugins.raids.solver; import com.google.common.collect.ImmutableList; import net.runelite.client.plugins.raids.RaidRoom; import net.runelite.client.plugins.raids.RoomType; +import java.util.ArrayList; import java.util.List; import static net.runelite.client.plugins.raids.RaidRoom.GUARDIANS; import static net.runelite.client.plugins.raids.RaidRoom.MUTTADILES; @@ -55,7 +56,7 @@ public class RotationSolver return false; } - List match = null; + List> matches = new ArrayList<>(); Integer start = null; Integer index = null; int known = 0; @@ -85,52 +86,49 @@ public class RotationSolver return true; } - for (List rotation : ROTATIONS) + //Iterate over each rotation + for (ImmutableList rotation : ROTATIONS) { - COMPARE: - for (int i = 0; i < rotation.size(); i++) + //Determine the index of the first known combat room in this rotation + int rotationStart = rotation.indexOf(rooms[start]); + + //Iterate over each room (except the starting room) and determine whether or not the rotation still matches + int roomStep = 1; + boolean doesNotMatch = false; + while (roomStep < rooms.length && !doesNotMatch) { - if (rooms[start] == rotation.get(i)) + var roomIndex = (start + roomStep) % rooms.length; + var rotationRoomIndex = (rotationStart + roomStep) % rotation.size(); + if (rooms[roomIndex] != UNKNOWN_COMBAT && rooms[roomIndex] != rotation.get(rotationRoomIndex)) { - for (int j = start + 1; j < rooms.length; j++) - { - if (rooms[j].getType() != RoomType.COMBAT || rooms[j] == UNKNOWN_COMBAT) - { - continue; - } - - if (rooms[j] != rotation.get((i + j - start) % rotation.size())) - { - break COMPARE; - } - } - - if (match != null && match.equals(rotation)) - { - return false; - } - - index = i - start; - match = rotation; + doesNotMatch = true; } + + ++roomStep; + } + + if (!doesNotMatch) + { + //Found a matching rotation! + matches.add(rotation); + index = rotationStart - start; } } - if (match == null) + if (matches.size() != 1) { + //Could not find a unique match! return false; } + List match = matches.get(0); + + for (int i = 0; i < rooms.length; i++) { - if (rooms[i] == null) + if (rooms[i].getType() != RoomType.COMBAT || rooms[i] == UNKNOWN_COMBAT) { - continue; - } - - if ((rooms[i].getType() != RoomType.COMBAT || rooms[i] == UNKNOWN_COMBAT)) - { - rooms[i] = match.get((index + i + match.size()) % match.size()); + rooms[i] = match.get(Math.floorMod((index + i), match.size())); } } return true;