Jump to content
Sign in to follow this  
GiimpeN

My first java project (Rock Crab killer)

Recommended Posts

Hi I got some problem with my script and i don't know whats wrong I got it to work for aggro the rockcrabs (wake them up) then when im trying to add an attack method its not working please help me. And im really nooby on java as i sad my first real project.

package killer;

import java.awt.Graphics;

import org.osbot.script.Script;
import org.osbot.script.ScriptManifest;
import org.osbot.script.rs2.model.Entity;
import org.osbot.script.rs2.model.Player;


@ScriptManifest(author = "GiimpeN", info = "Kills RockCrabs", name = "CrabKiller", version = 0.1)
public class CrabKiller extends Script {


	public void onStart() {
		log("Script Started.");
	}

	public void onExit() {

	}

	public int onLoop() throws InterruptedException {
		Player player = client.getMyPlayer();

		Entity Rocks = closestNPC(73, 71);

		if (Rocks != null) {
			if (!Rocks.isVisible()) {
				client.moveCameraToEntity(Rocks);
				if (!player.isMoving()) {
					if (!player.isAnimating()) {
						Rocks.interact("Walk here");
						sleep(random(700, 800));
					}

				}
			}

		}

		return 50;
	}

	public int onLoop1() throws InterruptedException {
		Entity rockcrab = closestAttackableNPCForName("Rock Crab");
		Player player = client.getMyPlayer();
		if (player.isUnderAttack()) {
			rockcrab.interact("Attack");
			if (!rockcrab.isVisible()) {
				client.moveCameraToEntity(rockcrab);

			}
		}
		return 50;
	}

	public void onPaint(Graphics g) {

	}

}

Share this post


Link to post
Share on other sites

You have two onLoop methods but ones called onLoop1....how does the script know to run that code? The bot is looking for a method called onLoop to start the regular loop, onLoop and onLoop1 are two completely different things. If you want you could set the script to call your onLoop1 method once it completes the original code (getting the player to wake up the crab) like this:

public int onLoop() throws InterruptedException {
        Player player = client.getMyPlayer();

        Entity rockcrab = closestAttackableNPCForName("Rock Crab");
        Entity Rocks = closestNPC(73, 71);

        if (Rocks != null) {
            if (!Rocks.isVisible()) {
                client.moveCameraToEntity(Rocks);
                if (!player.isMoving()) {
                    if (!player.isAnimating()) {
                        Rocks.interact("Walk here");
                        sleep(random(700, 800));
                        if (rockcrab.exists()) {
                            attackCrab();
                        }
                    }

                }
            }

        }

        return 50;
    }
And change your onLoop1 to attackCrab so it makes more sense.
 
The final code would then be:
package killer;

import java.awt.Graphics;

import org.osbot.script.Script;
import org.osbot.script.ScriptManifest;
import org.osbot.script.rs2.model.Entity;
import org.osbot.script.rs2.model.Player;


@ScriptManifest(author = "GiimpeN", info = "Kills RockCrabs", name = "CrabKiller", version = 0.1)
public class CrabKiller extends Script {


    public void onStart() {
        log("Script Started.");
    }

    public void onExit() {

    }

    public int onLoop() throws InterruptedException {
        Player player = client.getMyPlayer();

        Entity rockcrab = closestAttackableNPCForName("Rock Crab");
        Entity Rocks = closestNPC(73, 71);

        if (Rocks != null) {
            if (!Rocks.isVisible()) {
                client.moveCameraToEntity(Rocks);
                if (!player.isMoving()) {
                    if (!player.isAnimating()) {
                        Rocks.interact("Walk here");
                        sleep(random(700, 800));
                        if (rockcrab.exists()) {
                            attackCrab();
                        }
                    }

                }
            }

        }

        return 50;
    }

    public int attackCrab() throws InterruptedException {
        Entity rockcrab = closestAttackableNPCForName("Rock Crab");
        Player player = client.getMyPlayer();
        if (player.isUnderAttack()) {
            rockcrab.interact("Attack");
            if (!rockcrab.isVisible()) {
                client.moveCameraToEntity(rockcrab);

            }
        }
        return 50;
    }

    public void onPaint(Graphics g) {

    }

}

Only change I did was rename your onLoop1 method to attackCrab, add an if statement to only call attackCrab if a rockcrab exists, and if it doesn't it will go and repeat the main loop again.

Edited by Swizzbeat

Share this post


Link to post
Share on other sites

You have two onLoop methods but ones called onLoop1....how does the script know to run that code? The bot is looking for a method called onLoop to start the regular loop, onLoop and onLoop1 are two completely different things. If you want you could set the script to call your onLoop1 method once it completes the original code (getting the player to wake up the crab) like this:

public int onLoop() throws InterruptedException {
        Player player = client.getMyPlayer();

        Entity rockcrab = closestAttackableNPCForName("Rock Crab");
        Entity Rocks = closestNPC(73, 71);

        if (Rocks != null) {
            if (!Rocks.isVisible()) {
                client.moveCameraToEntity(Rocks);
                if (!player.isMoving()) {
                    if (!player.isAnimating()) {
                        Rocks.interact("Walk here");
                        sleep(random(700, 800));
                        if (rockcrab.exists()) {
                            attackCrab();
                        }
                    }

                }
            }

        }

        return 50;
    }
And change your onLoop1 to attackCrab so it makes more sense.
 
The final code would then be:
package killer;

import java.awt.Graphics;

import org.osbot.script.Script;
import org.osbot.script.ScriptManifest;
import org.osbot.script.rs2.model.Entity;
import org.osbot.script.rs2.model.Player;


@ScriptManifest(author = "GiimpeN", info = "Kills RockCrabs", name = "CrabKiller", version = 0.1)
public class CrabKiller extends Script {


    public void onStart() {
        log("Script Started.");
    }

    public void onExit() {

    }

    public int onLoop() throws InterruptedException {
        Player player = client.getMyPlayer();

        Entity rockcrab = closestAttackableNPCForName("Rock Crab");
        Entity Rocks = closestNPC(73, 71);

        if (Rocks != null) {
            if (!Rocks.isVisible()) {
                client.moveCameraToEntity(Rocks);
                if (!player.isMoving()) {
                    if (!player.isAnimating()) {
                        Rocks.interact("Walk here");
                        sleep(random(700, 800));
                        if (rockcrab.exists()) {
                            attackCrab();
                        }
                    }

                }
            }

        }

        return 50;
    }

    public int attackCrab() throws InterruptedException {
        Entity rockcrab = closestAttackableNPCForName("Rock Crab");
        Player player = client.getMyPlayer();
        if (player.isUnderAttack()) {
            rockcrab.interact("Attack");
            if (!rockcrab.isVisible()) {
                client.moveCameraToEntity(rockcrab);

            }
        }
        return 50;
    }

    public void onPaint(Graphics g) {

    }

}

Only change I did was rename your onLoop1 method to attackCrab, add an if statement to only call attackCrab if a rockcrab exists, and if it doesn't it will go and repeat the main loop again.

Thx man, It make it clear now when you sad it but couldnt get is self :D

But thx really much. And btw is there anyhting other stupid with my code or is it okey for be my first?

Share this post


Link to post
Share on other sites

I'm new to scripting myself so I can't really offer to much advice! But one thing I noticed is that you don't have a check to see if the rock crab still exists or not. I'm working on a cow script and it consistently was messing up and the mouse would hover over the same spot even though the cow was dead. Not sure if its a client or script issue, but to combat it I added a while loop to check for if the crab still existed. Here's what I would do:

    public int attackCrab() throws InterruptedException {
        Entity rockcrab = closestAttackableNPCForName("Rock Crab");
        Player player = client.getMyPlayer();

        while (rockcrab.exists()) {
            if (!rockcrab.isVisible()) {
                 client.moveCameraToEntity(rockcrab);
             }
            if (!player.isUnderAttack()) {
                rockcrab.interact("Attack");
                sleep(random(1000, 2000));
            }
        }
        return 50;
    }

What I did here was add your code to a while loop to constantly check if the rock crab is still a thing or not. What if you hit a lag spike and the crab dies during that lag? Your script would still be stuck looking for the crab even though it isn't there anymore. With the while loop if that crab ever does not exist then it won't execute any of the code in its brackets. The next thing I did was add your if-visible check to BEFORE the attack statement. This way the crab is definitely on screen before it tries to click anything! and lastly I added a "!" to before the player.isUnderAttack() to check for if the player IS NOT under attack, then ATTACK and sleep for a random amount of time between 1000 and 2000 milliseconds (or 1 and 2 seconds).

 

Honestly the best way to become better is to practice. Your script won't work the first time you try it, you have to keep changing things up and from making mistakes you learn quite a lot. Just keep trying and with enough persistence you'll complete a successful script in no time :)

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...