Jump to content

Feedback on First Script


CrashBandiboob

Recommended Posts

This is the first time I have ever really coded anything (apart from HTML for my Neopets guild about 20 years ago), thought I would give it a crack and have made a woodcutting script.

What this does at the moment is just powercuts the closest trees, I just wanted some feedback on where I'm going wrong/what could be improved/things I've missed. Most of what I have done has been guess work, trial & error and reading a lot of other peoples code, but it seems to be working alright!

j1m8AZ4.png cGgCt43.png

Some things I would like some help with:
1. I'd like to put something that enables run when energy goes beyond a certain value, can someone point me in the right direction for this?
2. My axe check needs to contain all of the axes not just bronze axe. How would I do this? I've tried doing a string array but EquipmentSlot doesn't seem to like this
3. Something probably a bit too advanced for me right now - How do I create a little pop up before the script kicks in so the user can choose a different tree type?

Here's the compiled (is that even the right term?) jar file:

https://www.mediafire.com/file/kpgbnkfnemx3u9d/FirstScript.jar/file



And here's the source code:

 https://pastebin.com/57neLNcJ
Edited by CrashBandiboob
Added images
  • Like 1
Link to comment
Share on other sites

1 hour ago, CrashBandiboob said:

This is the first time I have ever really coded anything (apart from HTML for my Neopets guild about 20 years ago), thought I would give it a crack and have made a woodcutting script.

What this does at the moment is just powercuts the closest trees, I just wanted some feedback on where I'm going wrong/what could be improved/things I've missed. Most of what I have done has been guess work, trial & error and reading a lot of other peoples code, but it seems to be working alright!

j1m8AZ4.png

Some things I would like some help with:
1. I'd like to put something that enables run when energy goes beyond a certain value, can someone point me in the right direction for this?
2. My axe check needs to contain all of the axes not just bronze axe. How would I do this? I've tried doing a string array but EquipmentSlot doesn't seem to like this
3. Something probably a bit too advanced for me right now - How do I create a little pop up before the script kicks in so the user can choose a different tree type?

Here's the compiled (is that even the right term?) jar file:


https://www.mediafire.com/file/kpgbnkfnemx3u9d/FirstScript.jar/file



And here's the source code:


 https://pastebin.com/57neLNcJ

1. Make a method for checking the value and enabling it using the API.
 

Spoiler

getSettings().getRunEnergy();

For checking the value of your run energy.


getSettings().isRunning();

I think to check if the running is enabled or disabled. If that doesn't work you might need to use Config values to check.

getSettings().setRunning(true or false);

To set the running to enable(true) or disabled(false). Only one can be in there example being


getSettings().setRunning(true);

getSettings().setRunning(false);

 

2. Try using this below instead of isWearingItem.

getEquipment().isWieldingWeaponThatContains(axe);

3. Actually number 16. Adding a GUI in this post is I think 95% copy and paste for your current situation lol.
 

Spoiler

 

 

Edited by Gunman
  • Like 1
  • Heart 1
Link to comment
Share on other sites

7 minutes ago, Gunman said:

1. Make a method for checking the value and enabling it using the API.
 

  Hide contents


getSettings().getRunEnergy();

For checking the value of your run energy.



getSettings().isRunning();

I think to check if the running is enabled or disabled. If that doesn't work you might need to use Config values to check.

getSettings().setRunning(true or false);

To set the running to enable(true) or disabled(false). Only one can be in there example being



getSettings().setRunning(true);

getSettings().setRunning(false);

 

2. Try using this below instead of isWearingItem.


getEquipment().isWieldingWeaponThatContains(axe);

3. Actually number 16. Adding a GUI in this post is I think 95% copy and paste for your current situation lol.
 

  Reveal hidden contents

 

 

Amazing! Thank you!

Link to comment
Share on other sites

Attempt at answering some of your questions. Feel free to pm me if you have any other questions 🙂

 

1. I'd like to put something that enables run when energy goes beyond a certain value, can someone point me in the right direction for this?

Spoiler

For this you can use an api call to check your current run energy. -  getSettings().getRunEnergy()

Than To set the running state you can use another api call - getSettings().setRunning() This takes in a boolean parameter || Example - getSettings().setRunning(true)

2. My axe check needs to contain all of the axes not just bronze axe. How would I do this? I've tried doing a string array but EquipmentSlot doesn't seem to like this

Spoiler

You are on the right track of using a String Array, but what you need to do is loop through it and see if the player has that axe in the inventory or if the player has the axe equipped.

Example

Spoiler


private boolean hasAxe(String[] axeNames) {
    for (String s : axeNames) {
        if (getInventory().contains(s) || getEquipment().getForNameThatContains(s) != null) {
            return true;
        }
    }
    return false;
}

 


3. Something probably a bit too advanced for me right now - How do I create a little pop up before the script kicks in so the user can choose a different tree type?

Spoiler

If you want something quick here is an example. All though I would recommend looking up tutorials on the use of the Java Swing Library.

Quick Method

Spoiler


import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

import javax.swing.*;
import java.awt.*;

@ScriptManifest(name = "@Test", author = "BravoTaco", version = 1.0, info = "", logo = "")
public class Main extends Script {

    private final String[] treeNames = new String[]{"Tree", "Oak tree", "Willow tree", "Maple tree", "Yew tree"};
    private boolean isStarted = false;
    private String treeName;

    @Override
    public void onStart() {
        showTreeSelectionDialog();
    }

    @Override
    public int onLoop() {

        if (treeName != null && !isStarted) {
            log("Started!");
            isStarted = true;
        } else if (!isStarted) {
            log("Not Started!");
            stop(false);
        }

        return random(1300, 2300);

    }

    private void showTreeSelectionDialog() {
        //Initialize Dialog
        JDialog jDialog = new JDialog();
        jDialog.setModal(true);
        jDialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
        jDialog.setMinimumSize(new Dimension(250, 300));
        jDialog.setLocationRelativeTo(null);

        //Initialize mainPanel and add it to the dialog box
        JPanel mainPanel = new JPanel(new BorderLayout());
        jDialog.add(mainPanel);

        //Initialize infoPanel and add it to the mainPanel at the center
        JPanel infoPanel = new JPanel();
        mainPanel.add(infoPanel, BorderLayout.CENTER);

        //Initialize treeNameLabel and add it to the infoPanel
        JLabel treeNameLabel = new JLabel("Choose A Tree: ");
        infoPanel.add(treeNameLabel);

        //Initialize treeNameCB and add it to the infoPanel
        JComboBox<String> treeNameCB = new JComboBox<>(treeNames);
        infoPanel.add(treeNameCB);

        //Initialize startButton with text of Start
        JButton startButton = new JButton("Start");

        //Add an action listener to the start button to close the gui and start the script.
        startButton.addActionListener(e -> {
            treeName = (String) treeNameCB.getSelectedItem();
            jDialog.dispose();
        });

        //Add the start button to the main panel at the bottom
        mainPanel.add(startButton, BorderLayout.SOUTH);

        jDialog.pack();
        jDialog.setVisible(true);
    }
}

 

 

  • Heart 1
Link to comment
Share on other sites

3 hours ago, BravoTaco said:

Attempt at answering some of your questions. Feel free to pm me if you have any other questions 🙂

 

1. I'd like to put something that enables run when energy goes beyond a certain value, can someone point me in the right direction for this?

  Reveal hidden contents

For this you can use an api call to check your current run energy. -  getSettings().getRunEnergy()

Than To set the running state you can use another api call - getSettings().setRunning() This takes in a boolean parameter || Example - getSettings().setRunning(true)

2. My axe check needs to contain all of the axes not just bronze axe. How would I do this? I've tried doing a string array but EquipmentSlot doesn't seem to like this

  Reveal hidden contents

You are on the right track of using a String Array, but what you need to do is loop through it and see if the player has that axe in the inventory or if the player has the axe equipped.

Example

  Reveal hidden contents



private boolean hasAxe(String[] axeNames) {
    for (String s : axeNames) {
        if (getInventory().contains(s) || getEquipment().getForNameThatContains(s) != null) {
            return true;
        }
    }
    return false;
}

 


3. Something probably a bit too advanced for me right now - How do I create a little pop up before the script kicks in so the user can choose a different tree type?

  Reveal hidden contents

If you want something quick here is an example. All though I would recommend looking up tutorials on the use of the Java Swing Library.

Quick Method

  Reveal hidden contents



import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

import javax.swing.*;
import java.awt.*;

@ScriptManifest(name = "@Test", author = "BravoTaco", version = 1.0, info = "", logo = "")
public class Main extends Script {

    private final String[] treeNames = new String[]{"Tree", "Oak tree", "Willow tree", "Maple tree", "Yew tree"};
    private boolean isStarted = false;
    private String treeName;

    @Override
    public void onStart() {
        showTreeSelectionDialog();
    }

    @Override
    public int onLoop() {

        if (treeName != null && !isStarted) {
            log("Started!");
            isStarted = true;
        } else if (!isStarted) {
            log("Not Started!");
            stop(false);
        }

        return random(1300, 2300);

    }

    private void showTreeSelectionDialog() {
        //Initialize Dialog
        JDialog jDialog = new JDialog();
        jDialog.setModal(true);
        jDialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
        jDialog.setMinimumSize(new Dimension(250, 300));
        jDialog.setLocationRelativeTo(null);

        //Initialize mainPanel and add it to the dialog box
        JPanel mainPanel = new JPanel(new BorderLayout());
        jDialog.add(mainPanel);

        //Initialize infoPanel and add it to the mainPanel at the center
        JPanel infoPanel = new JPanel();
        mainPanel.add(infoPanel, BorderLayout.CENTER);

        //Initialize treeNameLabel and add it to the infoPanel
        JLabel treeNameLabel = new JLabel("Choose A Tree: ");
        infoPanel.add(treeNameLabel);

        //Initialize treeNameCB and add it to the infoPanel
        JComboBox<String> treeNameCB = new JComboBox<>(treeNames);
        infoPanel.add(treeNameCB);

        //Initialize startButton with text of Start
        JButton startButton = new JButton("Start");

        //Add an action listener to the start button to close the gui and start the script.
        startButton.addActionListener(e -> {
            treeName = (String) treeNameCB.getSelectedItem();
            jDialog.dispose();
        });

        //Add the start button to the main panel at the bottom
        mainPanel.add(startButton, BorderLayout.SOUTH);

        jDialog.pack();
        jDialog.setVisible(true);
    }
}

 

 

This is all golden, thank you!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...