Jump to content

Learning Java - Need a bit of help


barnold

Recommended Posts

Hi, I'm learning Java and trying to apply what I'm learning to scripting on OSBot.

I'm struggling a little bit on the best way to do things.

For example when should I create a class for something?

I tried to create a class for my stats but I couldn't get it working for some reason. But I'm not sure if I need to be doing that in the first place

I've read a lot of the basic tutorials on the site which I understand but couldn't find much else. I'm currently reading through the Oracle Java Docs.

I feel like I have all these ideas for scripts but I don't know how to properly write them. I hear about "task systems" and "node systems" but I'm not really too sure what the differnce is between them.

If anything can help out or point me in the right direction for some good resources I'd appreciate it!

 

Thanks

 

Edited by barnold
Link to comment
Share on other sites

Classes are only really needed if you want to represent an Object. For instance we have a class called Dog.

Dog has the following properties:
-Barks
-Love
-Paws

With the constructor: Dog(int barks, int love, int paws)

Would could do something like:

Dog dog1 = new Dog(4,2,9);
Dog dog2 = new Dog(5,5,8);
Dog dog3 = new Dog(9,9,9);
List<Dog> doggos = new ArrayList<>();
doggos.addAll(dog1, dog2, dog3);


Now try doing this without using a new class of Dog.

You would have something like

int dog1Barks = 4;
int dog1Love = 2;
int dog1Paws = 9;
int dog2Barks = 5;
int dog2Love= 5;
... etc etc

 

  • Like 1
Link to comment
Share on other sites

I'm not sure if this will helps, but anyways, here's we go. I'll keep this scripting-related.

When I started learning about programming, I used to think I would have to do everything by myself. Everything from scratch. That is NOT the case in programming, especially java. In java, there are always classes already created by other people that you can import and use whatever "tools" there is in that class. When I started programming I always thought of myself making scripts for runescape, but I thought it was something hard to do, because I kept thinking about "How am I going to make the character walk?" , "How am I going to make the character attack, eat, teleport, etc". That's when I found out about the OsBot API. The definition of API is the following:

Quote

In computer programming, an application programming interface (API) is a set of subroutine definitions, protocols, and tools for building application software.

The API will provide you with tools (methods, actions) that you can use to make your scripts.

With the API, I found the solution for my questions mentioned above.

How to walk? - Use the WebWalking class from the API 

walking.webWalk(Location);

 

How to check if my inventory is full? - Use the inventory class from the API

getInventory().isFull();

 

How do I check my magic level?

getSkills().getDynamic(Skill.MAGIC);

 

And so on...

What I'm trying to say is, someone else have made the tools for you, you don't have to worry about making them. Now you have to think about how your script is going to run. What should it do? What should it check? This is where logic comes in. This is where pseudo-code is useful.

For a simple tree cutting script, this is what I think of my character to do:

 

If I have the wc lvl, go to next if. Else, log out.

If I am wielding the axe, go to the next if. Else, wield the axe.

If my inventory is not full, cut the trees. Else, clear my inventory.

 

Turning that into pseudo-code (basically the same thing but with the correct structure):

Check if I have the woodcutting level needed 

     Check if I am wielding the axe 

            Check if my inventory is not full 

                Cut the trees

                                                
             else
             Clear inventory (bank or drop items)     

        else
        Wield the axe

else
Log out because have no woodcutting level


And turning that into actual java code

private void woodcuttingTest() throws  InterruptedException{
        if(getSkills().getDynamic(Skill.WOODCUTTING) > 1) {
            if (equipment.isWearingItem(EquipmentSlot.WEAPON, "Bronze Axe")){
                if (!getInventory().isFull()){
                    Entity tree = getObjects().closest("Tree");
                    tree.interact("Chop down");
                }
                else{
                    getInventory().dropAll();
                }
            }
            else{
                inventory.getItem("Bronze Axe").interact("Wield");
            }
        }
        else{
            logoutTab.logOut();
        }
    }

 

I used to be very confused when I first started, so hopefully this clear your mind out.

This post is more related to scripting, if you want to learn more about java I suggest reading books, looking up JAVA guides on youtube to learn how the language works.

This tutorial will help you A LOT in your scripting questions: 

 

 

 

 

 

 

 

 

Edited by Ragboys is back
  • Like 1
Link to comment
Share on other sites

1 hour ago, barnold said:

Thanks for the helpful replies.

So I was trying to create a class that I was going to use to track stats and other things, should I not bother with that and just have it in my main script class?

 

Thanks

It's not so much a matter of 'should'. Do you think that data will be useful later? Is it worth the investment to learn how to write something like that? Will it benefit you?

 

Get creative, the devs here have created a wonderful API with plenty of functions to help you write just about any kind of script that's osrs related.

Edited by Chikan
Link to comment
Share on other sites

9 hours ago, barnold said:

Thanks for the helpful replies.

So I was trying to create a class that I was going to use to track stats and other things, should I not bother with that and just have it in my main script class?

 

Thanks

Before creating something you need to use on any of your script, make sure to search it in the API. Mostly like what you need will be there and it'll save you a lot of time. However, if you wanna go the hard & long way, you could make your own methods, which personally will just make you lose time.

 

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...