From 3d66a7e102c9f587a2549e38097a21850bf29940 Mon Sep 17 00:00:00 2001 From: Ganom Date: Sat, 25 Jan 2020 15:23:48 -0500 Subject: [PATCH] tmorph: use best of both worlds. --- .../runelite/client/plugins/tmorph/Parse.java | 47 +++++++++ .../client/plugins/tmorph/TMorph.java | 95 +++++++++++++++++-- .../client/plugins/tmorph/TMorphConfig.java | 80 ++++++++++++++++ .../client/plugins/tmorph/ui/TPanel.java | 38 +++++--- 4 files changed, 242 insertions(+), 18 deletions(-) create mode 100644 runelite-client/src/main/java/net/runelite/client/plugins/tmorph/Parse.java diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/tmorph/Parse.java b/runelite-client/src/main/java/net/runelite/client/plugins/tmorph/Parse.java new file mode 100644 index 0000000000..fa4ba34ec8 --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/plugins/tmorph/Parse.java @@ -0,0 +1,47 @@ +package net.runelite.client.plugins.tmorph; + +import java.util.Arrays; +import java.util.Map; +import javax.inject.Singleton; + +@Singleton +public class Parse +{ + public static boolean parse(String value) + { + try + { + final StringBuilder sb = new StringBuilder(); + + for (String str : value.split("\n")) + { + if (!str.startsWith("//")) + { + sb.append(str).append("\n"); + } + } + final Map tmp = TMorph.getNEWLINE_SPLITTER().withKeyValueSeparator(':').split(sb); + + for (Map.Entry entry : tmp.entrySet()) + { + if (!TMorph.getKit().containsKey(entry.getValue())) + { + return false; + } + + final int[] ints = Arrays.stream(entry.getKey().split(",")).map(String::trim).mapToInt(Integer::parseInt).toArray(); + + if (ints.length <= 1) + { + return false; + } + } + + return true; + } + catch (Exception ex) + { + return false; + } + } +} diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/tmorph/TMorph.java b/runelite-client/src/main/java/net/runelite/client/plugins/tmorph/TMorph.java index 165eb39f10..eaeb8d7e00 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/tmorph/TMorph.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/tmorph/TMorph.java @@ -26,6 +26,7 @@ package net.runelite.client.plugins.tmorph; import com.google.common.base.Splitter; import com.google.common.collect.ImmutableMap; import com.google.inject.Provides; +import java.awt.Color; import java.awt.image.BufferedImage; import java.util.Arrays; import java.util.HashMap; @@ -36,10 +37,12 @@ import lombok.AccessLevel; import lombok.Getter; import lombok.Setter; import net.runelite.api.Actor; +import net.runelite.api.ChatMessageType; import net.runelite.api.Client; import net.runelite.api.GameState; import net.runelite.api.Player; import net.runelite.api.events.AnimationChanged; +import net.runelite.api.events.CommandExecuted; import net.runelite.api.events.GameStateChanged; import net.runelite.api.events.GameTick; import net.runelite.api.events.SpotAnimationChanged; @@ -50,13 +53,14 @@ import net.runelite.client.config.ConfigManager; import net.runelite.client.eventbus.EventBus; import net.runelite.client.eventbus.Subscribe; import net.runelite.client.events.ConfigChanged; -import net.runelite.client.game.ItemManager; import net.runelite.client.plugins.Plugin; import net.runelite.client.plugins.PluginDescriptor; import net.runelite.client.plugins.PluginType; import net.runelite.client.plugins.tmorph.ui.TPanel; import net.runelite.client.ui.ClientToolbar; import net.runelite.client.ui.NavigationButton; +import net.runelite.client.util.Clipboard; +import net.runelite.client.util.ColorUtil; import net.runelite.client.util.ImageUtil; import org.apache.commons.lang3.ObjectUtils; @@ -76,10 +80,12 @@ public class TMorph extends Plugin static { final ImmutableMap.Builder builder = new ImmutableMap.Builder<>(); + for (KitType kit : KitType.values()) { builder.put(kit.getName(), kit); } + kit = builder.build(); } @@ -104,10 +110,6 @@ public class TMorph extends Plugin @Inject private ClientThread clientThread; - @Inject - private ItemManager itemManager; - - private TPanel panel; private NavigationButton navButton; private int animation; @@ -118,6 +120,9 @@ public class TMorph extends Plugin private int targetGraphic; @Setter private Map panelMorph = new HashMap<>(); + private Map set1; + private Map set2; + private Map set3; @Provides TMorphConfig provideConfig(ConfigManager configManager) @@ -150,6 +155,78 @@ public class TMorph extends Plugin eventBus.unregister(this); } + @Subscribe + public void onCommandExecuted(CommandExecuted event) + { + final String[] args = event.getArguments(); + + if (event.getCommand().equals("tmorph")) + { + try + { + if (args[0].equals("copy")) + { + final StringBuilder sb = new StringBuilder(); + final Player player = client.getLocalPlayer(); + + if (player == null + || player.getPlayerAppearance() == null + || client.getWidget(WidgetInfo.LOGIN_CLICK_TO_PLAY_SCREEN) != null + || client.getViewportWidget() == null) + { + return; + } + + for (KitType kitType : KitType.values()) + { + if (kitType.equals(KitType.RING) || kitType.equals(KitType.AMMUNITION)) + { + continue; + } + + final int id = player.getPlayerAppearance().getEquipmentId(kitType); + + if (id == -1) + { + continue; + } + + sb.append(id); + sb.append(",-1"); + sb.append(":"); + sb.append(kitType.getName()); + sb.append("\n"); + } + client.addChatMessage( + ChatMessageType.GAMEMESSAGE, + "TMorph", + ColorUtil.prependColorTag("Your current gear has been copied to your clipboard", Color.RED), + null + ); + Clipboard.store(sb.toString()); + } + else + { + client.addChatMessage( + ChatMessageType.GAMEMESSAGE, + "TMorph", + ColorUtil.prependColorTag("Invalid syntax, do ::tmorph copy", Color.RED), + null + ); + } + } + catch (Exception e) + { + client.addChatMessage( + ChatMessageType.GAMEMESSAGE, + "TMorph", + ColorUtil.prependColorTag("Invalid syntax, do ::tmorph copy", Color.RED), + null + ); + } + } + } + @Subscribe public void onGameStateChanged(GameStateChanged event) { @@ -233,11 +310,14 @@ public class TMorph extends Plugin } updateGear(panelMorph, player); + updateGear(set1, player); + updateGear(set2, player); + updateGear(set3, player); } public void updateGear(Map map, Player player) { - if (map == null || map.isEmpty()) + if (map == null || map.isEmpty() || player.getPlayerAppearance() == null) { return; } @@ -281,6 +361,9 @@ public class TMorph extends Plugin private void updateConfig() { + this.set1 = NEWLINE_SPLITTER.withKeyValueSeparator(':').split(config.set1()); + this.set2 = NEWLINE_SPLITTER.withKeyValueSeparator(':').split(config.set2()); + this.set3 = NEWLINE_SPLITTER.withKeyValueSeparator(':').split(config.set3()); this.animation = config.animationSwap(); this.globalAnimSwap = config.globalAnimSwap(); this.globalGraphicSwap = config.globalGraphicSwap(); diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/tmorph/TMorphConfig.java b/runelite-client/src/main/java/net/runelite/client/plugins/tmorph/TMorphConfig.java index c7b9c63e52..404fadf453 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/tmorph/TMorphConfig.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/tmorph/TMorphConfig.java @@ -27,10 +27,76 @@ import net.runelite.client.config.Config; import net.runelite.client.config.ConfigGroup; import net.runelite.client.config.ConfigItem; import net.runelite.client.config.ConfigSection; +import net.runelite.client.config.ConfigTitleSection; +import net.runelite.client.config.Title; @ConfigGroup("TMorph") public interface TMorphConfig extends Config { + @ConfigTitleSection( + keyName = "swaps", + name = "Morphers", + description = "", + position = 1 + ) + default Title swaps() + { + return new Title(); + } + + @ConfigItem( + keyName = "mageSwap", + name = "Swap Set 1", + description = "
Proper Format is id,id:Slot" + + "
For example: 6570,21295:Cape" + + "
Valid Slots: Helmet, Cape, Amulet, Weapon, Torso, Shield, Legs, Head, Hands, Boots, Jaw, Ring, Ammo
", + titleSection = "swaps", + position = 1, + parse = true, + clazz = Parse.class, + method = "parse" + ) + default String set1() + { + return ""; + } + + @ConfigItem( + keyName = "rangeSwap", + name = "Swap Set 2", + description = "
Proper Format is id,id:Slot" + + "
For example: 6570,21295:Cape" + + "
Valid Slots: Helmet, Cape, Amulet, Weapon, Torso, Shield, Legs, Head, Hands, Boots, Jaw, Ring, Ammo
", + titleSection = "swaps", + position = 2, + parse = true, + clazz = Parse.class, + method = "parse" + ) + default String set2() + { + return ""; + } + + @ConfigItem( + keyName = "meleeSwap", + name = "Swap Set 3", + description = "
Proper Format is id,id:Slot" + + "
For example: 6570,21295:Cape" + + "
Valid Slots: Helmet, Cape, Amulet, Weapon, Torso, Shield, Legs, Head, Hands, Boots, Jaw, Ring, Ammo
", + titleSection = "swaps", + position = 3, + parse = true, + clazz = Parse.class, + method = "parse" + ) + default String set3() + { + return ""; + } + + //////////////////Experimental Functions + @ConfigSection( position = 4, keyName = "experimentalSection", @@ -137,4 +203,18 @@ public interface TMorphConfig extends Config { return 0; } + + @ConfigTitleSection( + keyName = "copy", + name = "
If you would like to copy your equipped" + + "
gear, type \"::tmorph copy\" in chat." + + "
This will copy your gear to your" + + "
clipboard for easy copy paste.
", + description = "", + position = 50 + ) + default Title copy() + { + return new Title(); + } } diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/tmorph/ui/TPanel.java b/runelite-client/src/main/java/net/runelite/client/plugins/tmorph/ui/TPanel.java index 603d1d21a1..6c22369940 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/tmorph/ui/TPanel.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/tmorph/ui/TPanel.java @@ -63,6 +63,7 @@ import static net.runelite.api.kit.KitType.TORSO; import static net.runelite.api.kit.KitType.WEAPON; import net.runelite.api.widgets.Widget; import net.runelite.api.widgets.WidgetInfo; +import net.runelite.client.Notifier; import net.runelite.client.database.DatabaseManager; import static net.runelite.client.database.data.Tables.TMORPH_SETS; import net.runelite.client.database.data.tables.records.TmorphSetsRecord; @@ -86,6 +87,7 @@ public class TPanel extends PluginPanel private final DatabaseManager databaseManager; private final ItemManager itemManager; private final TMorph plugin; + private final Notifier notifier; private final JComboBox selector; private final Map equipSlots; @@ -99,7 +101,8 @@ public class TPanel extends PluginPanel final Client client, final DatabaseManager databaseManager, final ItemManager itemManager, - final TMorph plugin + final TMorph plugin, + final Notifier notifier ) { super(false); @@ -107,6 +110,7 @@ public class TPanel extends PluginPanel this.databaseManager = databaseManager; this.itemManager = itemManager; this.plugin = plugin; + this.notifier = notifier; this.equipSlots = new LinkedHashMap<>(); this.kitToId = new HashMap<>(); this.setMap = new HashMap<>(); @@ -117,7 +121,7 @@ public class TPanel extends PluginPanel private void init() { - selector.addItem(""); + selector.addItem("Populating fields..."); selector.setSelectedIndex(0); selector.addActionListener((e) -> { @@ -159,7 +163,7 @@ public class TPanel extends PluginPanel final JPanel containerPanel = new JPanel(); final JLabel caption = new JLabel(); - caption.setText("Current Morph"); + caption.setText("Morph Selector"); caption.setForeground(Color.WHITE); caption.setHorizontalAlignment(JLabel.CENTER); caption.setVerticalAlignment(JLabel.CENTER); @@ -290,7 +294,7 @@ public class TPanel extends PluginPanel if (client.getGameState() == GameState.LOGGED_IN) { - Map s = generate(); + Map s = generate(false); } } }); @@ -300,18 +304,23 @@ public class TPanel extends PluginPanel i++; } - final JButton setButton = new JButton("Set Active Morph"); - setButton.addActionListener((e) -> plugin.setPanelMorph(generate())); + final JButton setButton = new JButton("Set/Copy Active Morph"); + setButton.addActionListener((e) -> plugin.setPanelMorph(generate(true))); equipPanel.add(setButton); final JButton saveButton = new JButton("Save Active Morph"); saveButton.addActionListener((e) -> { - final String result = JOptionPane.showInputDialog(saveButton, "What would you like to name the set?"); + final String s = JOptionPane.showInputDialog(saveButton, "What would you like to name the set?"); + + if (s == null || s.isEmpty()) + { + return; + } Result records = databaseManager.getDsl() .selectFrom(TMORPH_SETS) - .where(TMORPH_SETS.SET_NAME.eq(result)) + .where(TMORPH_SETS.SET_NAME.eq(s)) .fetch(); boolean exists = records.isNotEmpty(); @@ -319,7 +328,7 @@ public class TPanel extends PluginPanel if (!exists) { databaseManager.getDsl().insertInto(TMORPH_SETS) - .set(TMORPH_SETS.SET_NAME, result) + .set(TMORPH_SETS.SET_NAME, s) .set(TMORPH_SETS.HELMET, kitToId.getOrDefault(HELMET, -1)) .set(TMORPH_SETS.CAPE, kitToId.getOrDefault(CAPE, -1)) .set(TMORPH_SETS.AMULET, kitToId.getOrDefault(AMULET, -1)) @@ -344,7 +353,7 @@ public class TPanel extends PluginPanel .set(TMORPH_SETS.LEGS, kitToId.getOrDefault(LEGS, -1)) .set(TMORPH_SETS.HANDS, kitToId.getOrDefault(HANDS, -1)) .set(TMORPH_SETS.BOOTS, kitToId.getOrDefault(BOOTS, -1)) - .where(TMORPH_SETS.SET_NAME.eq(result)) + .where(TMORPH_SETS.SET_NAME.eq(s)) .execute(); } }); @@ -359,7 +368,7 @@ public class TPanel extends PluginPanel } } - public Map generate() + public Map generate(boolean copy) { final StringBuilder sb = new StringBuilder(); final Player player = client.getLocalPlayer(); @@ -403,7 +412,12 @@ public class TPanel extends PluginPanel } final String s = sb.toString(); - Clipboard.store(s); + + if (copy) + { + Clipboard.store(s); + notifier.notify("Saved to clipboard."); + } return plugin.getNEWLINE_SPLITTER() .withKeyValueSeparator(":")