I wrote this object loader class to make a personal aio woodcutting script. I thought i'd share it with you guys
Hope something can be learnt from this
ObjectLoader class:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.DefaultListModel;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JList;
import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.script.Script;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.JTextField;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class ObjectLoader extends JFrame {
/**
* @author Apaec
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private String interactionString;
private int radius = 10;
private final DefaultListModel<String> modelDesired = new DefaultListModel<String>();
private final DefaultListModel<String> modelLoaded = new DefaultListModel<String>();
private ArrayList<RS2Object> objectListDesired = new ArrayList<RS2Object>();
private ArrayList<RS2Object> objectListLoaded = new ArrayList<RS2Object>();
private ArrayList<Position> controlPos = new ArrayList<Position>();
private boolean isVisible = false;
public ObjectLoader(final Script script, String defaultSearchString,
int defaultRadius) {
this.interactionString = defaultSearchString;
this.radius = defaultRadius;
}
public void openLoader(final Script script) {
setTitle("Object Loader");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 305);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
setAlwaysOnTop(true);
setResizable(false);
JLabel lblLoadObjectsWithin = new JLabel("Load objects within radius:");
JLabel lblLoadObjectsWith = new JLabel("Load objects with action:\r\n");
final JSpinner spinner = new JSpinner();
final JTextField txtChopDown = new JTextField(interactionString);
JScrollPane scrollPane = new JScrollPane();
final JList<String> desiredtemsList = new JList<String>(modelDesired);
JLabel lblDesiredObjects = new JLabel("Desired Objects");
JScrollPane scrollPane_1 = new JScrollPane();
final JList<String> loadedItemsList = new JList<String>(modelLoaded);
JLabel lblNewLabel = new JLabel("Loaded Local Objects");
JButton btnNewButton = new JButton("<<");
JButton button = new JButton(">>");
JButton btnRefresh = new JButton("Refresh");
JButton btnSave = new JButton("Save");
lblLoadObjectsWithin.setBounds(10, 11, 193, 14);
lblLoadObjectsWith.setBounds(10, 36, 193, 14);
txtChopDown.setBounds(213, 33, 215, 20);
btnNewButton.setBounds(192, 61, 54, 69);
button.setBounds(192, 134, 54, 69);
btnRefresh.setBounds(10, 214, 89, 42);
btnSave.setBounds(109, 214, 319, 42);
spinner.setBounds(213, 8, 215, 20);
scrollPane.setBounds(10, 61, 178, 142);
scrollPane_1.setBounds(250, 61, 178, 142);
txtChopDown.setHorizontalAlignment(SwingConstants.CENTER);
txtChopDown.setColumns(10);
lblDesiredObjects.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
scrollPane.setViewportView(desiredtemsList);
scrollPane.setColumnHeaderView(lblDesiredObjects);
scrollPane_1.setViewportView(loadedItemsList);
scrollPane_1.setColumnHeaderView(lblNewLabel);
spinner.setModel(new SpinnerNumberModel(radius, 1, 30, 1));
contentPane.add(lblLoadObjectsWithin);
contentPane.add(spinner);
contentPane.add(lblLoadObjectsWith);
contentPane.add(txtChopDown);
contentPane.add(scrollPane);
contentPane.add(scrollPane_1);
contentPane.add(btnNewButton);
contentPane.add(button);
contentPane.add(btnRefresh);
contentPane.add(btnSave);
setVisible(true);
isVisible = true;
spinner.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent arg0) {
radius = (int) spinner.getValue();
}
});
txtChopDown.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
interactionString = txtChopDown.getText();
}
public void insertUpdate(DocumentEvent arg0) {
interactionString = txtChopDown.getText();
}
public void removeUpdate(DocumentEvent arg0) {
interactionString = txtChopDown.getText();
}
});
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (loadedItemsList.getSelectedIndex() != -1) {
modelDesired
.addElement(convertObjectToString(objectListLoaded
.get(loadedItemsList.getSelectedIndex())));
objectListDesired.add(objectListLoaded.get(loadedItemsList
.getSelectedIndex()));
objectListLoaded.remove(loadedItemsList.getSelectedIndex());
modelLoaded.removeElementAt(loadedItemsList
.getSelectedIndex());
}
}
});
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (desiredtemsList.getSelectedIndex() != -1) {
modelLoaded
.addElement(convertObjectToString(objectListDesired
.get(desiredtemsList.getSelectedIndex())));
objectListLoaded.add(objectListDesired.get(desiredtemsList
.getSelectedIndex()));
objectListDesired.remove(desiredtemsList.getSelectedIndex());
modelDesired.removeElementAt(desiredtemsList
.getSelectedIndex());
}
}
});
btnRefresh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
clearListsAndModels();
ArrayList<RS2Object> objectsAll = null;
objectsAll = loadNearbyObjectsForActionAsList(script,
getRadius(), getInteractionString(), true);
if (objectsAll != null) {
for (RS2Object o : objectsAll)
modelLoaded.addElement(convertObjectToString(o));
objectListLoaded = objectsAll;
}
}
});
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
setVisible(false);
isVisible = false;
}
});
}
public String convertObjectToString(RS2Object o) {
return o.getName() + " (" + o.getPosition().getX() + ","
+ o.getPosition().getY() + "," + o.getPosition().getZ() + ")";
}
public String getInteractionString() {
return interactionString;
}
public int getRadius() {
return radius;
}
public void clearListsAndModels() {
modelLoaded.clear();
modelDesired.clear();
objectListDesired.clear();
objectListLoaded.clear();
controlPos.clear();
}
public boolean isOpen() {
return isVisible;
}
public RS2Object[] getAllObjects() {
if (objectListLoaded.size() > 0) {
RS2Object[] objects = new RS2Object[objectListLoaded.size()];
for (RS2Object o : objectListLoaded)
objects[objectListLoaded.indexOf(o)] = o;
return objects;
} else {
return null;
}
}
public RS2Object[] getDesiredObjects() {
if (objectListDesired.size() > 0) {
RS2Object[] objects = new RS2Object[objectListDesired.size()];
for (RS2Object o : objectListDesired)
objects[objectListDesired.indexOf(o)] = o;
return objects;
} else {
return null;
}
}
private ArrayList<RS2Object> loadNearbyObjectsForActionAsList(
Script script, int radius, String action, boolean clearLists) {
if (clearLists)
clearListsAndModels();
ArrayList<RS2Object> objects = new ArrayList<RS2Object>();
for (RS2Object r : script.objects.getAll()) {
if (r != null && r.exists() && !r.getName().equals("null")
&& script.map.distance(r) <= radius
&& !controlPos.contains(r.getPosition())) {
if (action.equals("")) {
controlPos.add(r.getPosition());
objects.add(r);
} else if (r.hasAction(action)) {
controlPos.add(r.getPosition());
objects.add(r);
}
}
}
return objects.size() > 0 ? objects : null;
}
}
>>apologies for the poor jframe code, used a gui builder. Did my best to tidy it up somewhat but I understand it's still a little messy.
How to use it in an example script:
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
@ScriptManifest(name = "Object loader demo", author = "Apaec", version = 1.0, info = "Demo displayin object loader", logo = "")
public class ObjectLoaderDemo extends Script {
ObjectLoader loader;
RS2Object[] desiredObjects = null;
public void onStart() {
loader = new ObjectLoader(this, "", 10);
if (!loader.isOpen())
loader.openLoader(this);
}
public int onLoop() throws InterruptedException {
while (loader.isOpen()) {
sleep(400);
desiredObjects = loader.getDesiredObjects();
}
// visual debug:
if (desiredObjects.length > 0)
for (RS2Object o : desiredObjects)
if (o != null && o.exists())
log(o.getName());
return 10;
}
public void onExit() {
if (loader.isOpen())
loader.dispose();
}
}
Where desiredObjects is your array containing the objects configured by the user.
Screenshots:
Gif:
If you're commenting on my code, please be constructive, and I understand it's a bit below-par in places. I know how it is tho when posting snippets x)
how it helps
apa