From 7536ae4db3ba451b9ddbb8dabe5968976dd714f7 Mon Sep 17 00:00:00 2001 From: Yuri Moens Date: Thu, 28 Oct 2021 16:30:07 +0200 Subject: [PATCH] Add GUI support --- .../io/reisub/dreambot/cshopper/CShopper.java | 36 ++- .../dreambot/cshopper/CShopperOptions.java | 16 ++ .../reisub/dreambot/cshopper/ShopperItem.java | 31 +++ .../io/reisub/dreambot/cshopper/Trader.java | 49 +++++ Util/src/io/reisub/dreambot/util/ui/GUI.java | 208 ++++++++++++++++++ .../io/reisub/dreambot/util/ui/Option.java | 55 +++++ .../io/reisub/dreambot/util/ui/Parameter.java | 29 +++ 7 files changed, 421 insertions(+), 3 deletions(-) create mode 100644 CShopper/src/io/reisub/dreambot/cshopper/CShopperOptions.java create mode 100644 CShopper/src/io/reisub/dreambot/cshopper/ShopperItem.java create mode 100644 CShopper/src/io/reisub/dreambot/cshopper/Trader.java create mode 100644 Util/src/io/reisub/dreambot/util/ui/GUI.java create mode 100644 Util/src/io/reisub/dreambot/util/ui/Option.java create mode 100644 Util/src/io/reisub/dreambot/util/ui/Parameter.java diff --git a/CShopper/src/io/reisub/dreambot/cshopper/CShopper.java b/CShopper/src/io/reisub/dreambot/cshopper/CShopper.java index 49c4a8e..0b5b711 100644 --- a/CShopper/src/io/reisub/dreambot/cshopper/CShopper.java +++ b/CShopper/src/io/reisub/dreambot/cshopper/CShopper.java @@ -4,19 +4,26 @@ import io.reisub.dreambot.cshopper.tasks.*; import io.reisub.dreambot.util.CTaskScript; import io.reisub.dreambot.util.Constants; import io.reisub.dreambot.util.tasks.Run; +import io.reisub.dreambot.util.ui.GUI; +import io.reisub.dreambot.util.ui.Parameter; +import org.dreambot.api.methods.MethodProvider; import org.dreambot.api.methods.container.impl.Inventory; +import org.dreambot.api.randoms.RandomEvent; import org.dreambot.api.script.Category; import org.dreambot.api.script.ScriptManifest; import org.dreambot.api.wrappers.items.Item; +import javax.swing.*; import java.awt.*; +import java.lang.reflect.InvocationTargetException; @SuppressWarnings("unused") @ScriptManifest(category = Category.MISC, name = "CShopper", description = "Shops like a woman with an unlimited Visa card", author = Constants.AUTHOR, version = 1.0) public class CShopper extends CTaskScript { private static final Configuration configuration = new Configuration( - "Ordan", - new ItemWrapper("Gold ore", 2600, 73, false) + Constants.LUNDAIL, + new ItemWrapper("Nature rune", 5000) + //new ItemWrapper("Gold ore", 2600, 73, false) //new ItemWrapper("Hammerstone seed", -1), //new ItemWrapper("Asgarnian seed", -1), //new ItemWrapper("Jute seed", -1), @@ -26,6 +33,7 @@ public class CShopper extends CTaskScript { ); private Buy buyTask; + private GUI gui; public static Configuration getConfiguration() { return configuration; @@ -34,11 +42,33 @@ public class CShopper extends CTaskScript { @Override public void onStart() { getUI().setCustomLines(configuration.getItems().length); + getRandomManager().disableSolver(RandomEvent.BREAK); + getRandomManager().disableSolver(RandomEvent.LOGIN); buyTask = new Buy(); + try { + SwingUtilities.invokeAndWait(() -> { + gui = new GUI(getSDNName()); + gui.addComboBox("Trader", CShopperOptions.traders, new Parameter("Custom")); + gui.addComboBox("Items", + CShopperOptions.items, + true, + new Parameter("Custom"), + new Parameter("Buy total"), + new Parameter("Minimum in shop"), + new Parameter("Stackable", new JCheckBox())); + gui.init(); + }); + } catch (Exception ignored) {} + + while (gui == null || !gui.isReady()) { + MethodProvider.sleep(500); + } + + MethodProvider.log(gui.getOption("Trader").getName()); + addNodes( - new Deposit(), new Stop(), new Run(), new Open(), diff --git a/CShopper/src/io/reisub/dreambot/cshopper/CShopperOptions.java b/CShopper/src/io/reisub/dreambot/cshopper/CShopperOptions.java new file mode 100644 index 0000000..548e405 --- /dev/null +++ b/CShopper/src/io/reisub/dreambot/cshopper/CShopperOptions.java @@ -0,0 +1,16 @@ +package io.reisub.dreambot.cshopper; + +import io.reisub.dreambot.util.Constants; +import io.reisub.dreambot.util.ui.Option; + +public class CShopperOptions { + public final static Option[] traders = new Option[]{ + new Option("custom"), + new Option("Lundail", "mage arena rune shop") + }; + + public final static Option[] items = new Option[]{ + new Option("custom"), + new Option("Nature rune") + }; +} diff --git a/CShopper/src/io/reisub/dreambot/cshopper/ShopperItem.java b/CShopper/src/io/reisub/dreambot/cshopper/ShopperItem.java new file mode 100644 index 0000000..4e10c7a --- /dev/null +++ b/CShopper/src/io/reisub/dreambot/cshopper/ShopperItem.java @@ -0,0 +1,31 @@ +package io.reisub.dreambot.cshopper; + +public enum ShopperItem { + CUSTOM("custom..."), + NATURE_RUNE("Nature rune"); + + private final String name; + + private String customName; + + ShopperItem(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + @Override + public String toString() { + return name; + } + + public void setCustomName(String customName) { + this.customName = customName; + } + + public String getCustomName() { + return customName; + } +} diff --git a/CShopper/src/io/reisub/dreambot/cshopper/Trader.java b/CShopper/src/io/reisub/dreambot/cshopper/Trader.java new file mode 100644 index 0000000..8443a5d --- /dev/null +++ b/CShopper/src/io/reisub/dreambot/cshopper/Trader.java @@ -0,0 +1,49 @@ +package io.reisub.dreambot.cshopper; + +public enum Trader { + CUSTOM("custom..."), + LUNDAIL("Lundail", "mage arena rune shop"); + + private final String name; + private final String info; + + private String customName; + + Trader(String name) { + this(name, ""); + } + + Trader(String name, String info) { + this.name = name; + this.info = info; + } + + public String getName() { + if (customName == null || customName.equals("")) { + return name; + } else { + return customName; + } + } + + public String getInfo() { + return info; + } + + @Override + public String toString() { + if (info.equals("")) { + return name; + } else { + return name + " (" + info + ")"; + } + } + + public String getCustomName() { + return customName; + } + + public void setCustomName(String customName) { + this.customName = customName; + } +} diff --git a/Util/src/io/reisub/dreambot/util/ui/GUI.java b/Util/src/io/reisub/dreambot/util/ui/GUI.java new file mode 100644 index 0000000..f90e78b --- /dev/null +++ b/Util/src/io/reisub/dreambot/util/ui/GUI.java @@ -0,0 +1,208 @@ +package io.reisub.dreambot.util.ui; + +import org.dreambot.api.methods.MethodProvider; + +import javax.swing.*; +import java.awt.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +public class GUI extends JFrame { + private final GridBagLayout layout; + private final GridBagConstraints constraints; + private final Map componentMap; + private final Map> optionMap; + + private volatile boolean ready; + + public GUI(String title) { + super(title); + layout = new GridBagLayout(); + constraints = new GridBagConstraints(); + constraints.insets = new Insets(6, 6, 6, 6); + + componentMap = new HashMap<>(); + optionMap = new HashMap<>(); + } + + public void init() { + setUndecorated(true); + + Container contentPane = getContentPane(); + contentPane.setLayout(layout); + addStart(); + + pack(); + setLocationRelativeTo(getOwner()); + setVisible(true); + } + + private void addStart() { + JButton start = new JButton("Start"); + start.setPreferredSize(new Dimension(100, 40)); + + constraints.fill = GridBagConstraints.VERTICAL; + constraints.weighty = 2.0; + layout.setConstraints(start, constraints); + getContentPane().add(start); + + start.addActionListener((e) -> { + for (String key : componentMap.keySet()) { + Component component = componentMap.get(key); + java.util.List