Jump to content

Just made my first script, but had a few problems


Cosine

Recommended Posts

I just made a Chicken Killer as my first OSBot script to learn the ropes. It works fine, but I had a few questions/problems...

package core;

import org.osbot.rs07.api.filter.Filter;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;

import java.awt.*;

@ScriptManifest(name = "ChickenKiller", author = "Cosine", version = 1.0, info = "", logo = "") 

public class Main extends Script {

    @Override
    public void onStart() {
        //Code here will execute before the loop is started
    }

    @Override
    public void onExit() {
        //Code here will execute after the script ends
    }

    @Override
    public int onLoop() {	
    	if (inCombat()) {
    		waitToAttack().sleep();
    	} else {
    		if (attackChicken()) {
    			waitToAttack().sleep();
    		}
    	}
    	return(random(200, 300));
    }
    
    @SuppressWarnings("unchecked")
	private boolean attackChicken() {
    	NPC chicken = getNpcs().closest(chickenFilter());
    	if (chicken != null) {
    		chicken.interact("Attack");
    		
    		new ConditionalSleep(random(300, 400)) { //cant get sleep(random(300, 400)) to work... wants to wrap in try/catch
        		@Override
                public boolean condition() {
                    return false;
                }
        	}.sleep();
    		
    		return true;
    	}
    	return false;
    }
    
    private ConditionalSleep waitToAttack() {
    	return new ConditionalSleep(random(1500, 2000)) {
    		@Override
            public boolean condition() {
                return !inCombat();
            }
    	};
    }
    
    private Filter<NPC> chickenFilter() {
    	return new Filter<NPC>() {
    		@Override
    		public boolean match(NPC npc) {
    			return npc.getName().startsWith("Chicken") && npc.isAttackable();
    		}
    	};
    }
    
    private boolean inCombat() {
    	return getCombat().isFighting() || myPlayer().isMoving();
    }
    
    
    @Override
    public void onPaint(Graphics2D g) {
        //This is where you will put your code for paint(s)
    }

}

My first issue is that for whatever reason, I can't get a standard sleep to work anywhere in my script. I wanted to use one in my attackChicken method, but it wants me to wrap it in a try/catch. I have no idea why this is happening as  Script already throws an InterruptedException. My workaround is to use a ConditionalSleep with a hardcoded false condition, but it's not practical... Does anything look wrong with my code that could be causing this?

My second issue is that whenever I make a change to my script, I need to close OSBot and then re-open it, login and run it again. Shouldn't I be able to run the script right from exporting it from my IDE?

The IDE I'm using is Eclipse.

Anyone know how to fix these issues?

Link to comment
Share on other sites

4 minutes ago, Cosine said:

I just made a Chicken Killer as my first OSBot script to learn the ropes. It works fine, but I had a few questions/problems...


package core;

import org.osbot.rs07.api.filter.Filter;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;

import java.awt.*;

@ScriptManifest(name = "ChickenKiller", author = "Cosine", version = 1.0, info = "", logo = "") 

public class Main extends Script {

    @Override
    public void onStart() {
        //Code here will execute before the loop is started
    }

    @Override
    public void onExit() {
        //Code here will execute after the script ends
    }

    @Override
    public int onLoop() {	
    	if (inCombat()) {
    		waitToAttack().sleep();
    	} else {
    		if (attackChicken()) {
    			waitToAttack().sleep();
    		}
    	}
    	return(random(200, 300));
    }
    
    @SuppressWarnings("unchecked")
	private boolean attackChicken() {
    	NPC chicken = getNpcs().closest(chickenFilter());
    	if (chicken != null) {
    		chicken.interact("Attack");
    		
    		new ConditionalSleep(random(300, 400)) { //cant get sleep(random(300, 400)) to work... wants to wrap in try/catch
        		@Override
                public boolean condition() {
                    return false;
                }
        	}.sleep();
    		
    		return true;
    	}
    	return false;
    }
    
    private ConditionalSleep waitToAttack() {
    	return new ConditionalSleep(random(1500, 2000)) {
    		@Override
            public boolean condition() {
                return !inCombat();
            }
    	};
    }
    
    private Filter<NPC> chickenFilter() {
    	return new Filter<NPC>() {
    		@Override
    		public boolean match(NPC npc) {
    			return npc.getName().startsWith("Chicken") && npc.isAttackable();
    		}
    	};
    }
    
    private boolean inCombat() {
    	return getCombat().isFighting() || myPlayer().isMoving();
    }
    
    
    @Override
    public void onPaint(Graphics2D g) {
        //This is where you will put your code for paint(s)
    }

}

My first issue is that for whatever reason, I can't get a standard sleep to work anywhere in my script. I wanted to use one in my attackChicken method, but it wants me to wrap it in a try/catch. I have no idea why this is happening as  Script already throws an InterruptedException. My workaround is to use a ConditionalSleep with a hardcoded false condition, but it's not practical... Does anything look wrong with my code that could be causing this?

My second issue is that whenever I make a change to my script, I need to close OSBot and then re-open it, login and run it again. Shouldn't I be able to run the script right from exporting it from my IDE?

The IDE I'm using is Eclipse.

Anyone know how to fix these issues?

Can you give a code snippet of how you are trying to sleep?

 

As for the needing to close OSBot, no, you can simply click the play button to choose a script. When on that screen, hit the refresh button - then you can run your updated jar.

Link to comment
Share on other sites

27 minutes ago, Cosine said:

I just made a Chicken Killer as my first OSBot script to learn the ropes. It works fine, but I had a few questions/problems...



    @SuppressWarnings("unchecked")
	private boolean attackChicken() {
    	NPC chicken = getNpcs().closest(chickenFilter());
    	if (chicken != null) {
    		chicken.interact("Attack");
    		
    		new ConditionalSleep(random(300, 400)) { //cant get sleep(random(300, 400)) to work... wants to wrap in try/catch
        		@Override
                public boolean condition() {
                    return false;
                }
        	}.sleep();
    		
    		return true;
    	}
    	return false;
    }
    
    private ConditionalSleep waitToAttack() {
    	return new ConditionalSleep(random(1500, 2000)) {
    		@Override
            public boolean condition() {
                return !inCombat();
            }
    	};
    }
    
    private Filter<NPC> chickenFilter() {
    	return new Filter<NPC>() {
    		@Override
    		public boolean match(NPC npc) {
    			return npc.getName().startsWith("Chicken") && npc.isAttackable();
    		}
    	};
    }
    
    private boolean inCombat() {
    	return getCombat().isFighting() || myPlayer().isMoving();
    }
    
    
    @Override
    public void onPaint(Graphics2D g) {
        //This is where you will put your code for paint(s)
    }

}

 

its becuz ur not throwing InterruptedException from seeing in the code.
It's better to get used to conditional sleeps, u can chnge the return false to return myPlayer().isUnderAttack(). so it would sleep dynamically untill the player is underattack, no need to use random(300,400) instead use a value like 3000 or 4000, see which suits best for you.

Link to comment
Share on other sites

Firstly, don't even bother doing random sleep lengths it doesn't help you at all, secondly your conditional sleep logic is wrong.

You have to assign it a condition not just set it to false, so basically this code below I changed some stuff for you. It will sleep for 5 seconds or until your player is in combat. 

5000 = Length of sleep.

250 = How often it checks if the condition is met, if condition is met it will no longer sleep.

    private boolean inCombat() {
    	return getCombat().isFighting() || myPlayer().isUnderAttack() || myPlayer().isAnimating();
    }
    		new ConditionalSleep(5000, 250) {
        		@Override
                public boolean condition() {
                    return inCombat();
                }
        	}.sleep();

 

Edited by 01053
Link to comment
Share on other sites

Thanks for the help all of you. I made a few changes to my script and for now I'm considering it finished as I'm going to move on to a Cow killer because I want my own scripts to test up accounts from scratch at each level :) 

If you guys like, you can have another look at my updated code and maybe see if I'm doing anything incorrect/doing any bad practices.

package core;

import org.osbot.rs07.api.filter.Filter;
import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.api.ui.Skill;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;

import java.awt.*;

@ScriptManifest(name = "ChickenKiller", author = "Cosine", version = 1.0, info = "", logo = "") 

public class Main extends Script {
	
	private final Area chickenCoop = new Area(3225, 3301, 3236, 3295);
	private final Area gateArea = new Area(3236, 3294, 3231, 3287);
	private long startTime;
	private long runTime;

    @Override
    public void onStart() {
        startTime = System.currentTimeMillis();
        getExperienceTracker().startAll();
    }

    @Override
    public void onExit() {
    }

    @Override
    public int onLoop() {
		if(atChickenPen()) {
	    	if (inCombat()) {
	    		waitToAttack().sleep();
	    	} else if (attackChicken()) {
	    			waitToAttack().sleep();
	    	}
		} else {
    		walkToChickens();
    	}
		
		return(random(300, 400));
    }
    
    private boolean atChickenPen() {
    	return chickenCoop.contains(myPlayer()) || gateArea.contains(myPlayer());
    }
    
    private boolean inCombat() {
    	return getCombat().isFighting() || myPlayer().isMoving();
    }
    
    private ConditionalSleep waitToAttack() {
    	return new ConditionalSleep(5000, 250) {
    		@Override
            public boolean condition() {
                return !inCombat();
            }
    	};
    }
    
	private boolean attackChicken() {
    	NPC chicken = getNpcs().closest(chickenFilter());
    	if (chicken != null) {
    		if (!chicken.isVisible()) {
    			camera.toEntity(chicken);
    		}
    		chicken.interact("Attack");		
    		return true;
    	}
    	return false;
    }
	
	private Filter<NPC> chickenFilter() {
    	return new Filter<NPC>() {
    		@Override
    		public boolean match(NPC npc) {
    			return npc.getName().startsWith("Chicken") && npc.isAttackable() && (chickenCoop.contains(npc) || gateArea.contains(npc)) && npc.isVisible();
    		}
    	};
    }
    
    private void walkToChickens() {
    	getWalking().walk(chickenCoop);
    }
    
    public final String formatTime(final long ms) {
    	long s = ms / 1000, m = s / 60, h = m / 60;
    	s %= 60; m %= 60; h %= 24;
    	return String.format("%02d:%02d:%02d", h, m, s);
    }
    
    @Override
    public void onPaint(Graphics2D g) {
    	runTime = System.currentTimeMillis() - startTime;
    	
    	g.setColor(new Color(252, 200, 20, 255));
    	g.fillRect(10, 220, 180, 110);
    	
    	g.setColor(Color.decode("#FFFFFF"));
        g.drawString("Runtime: " + formatTime(runTime), 17, 240);
        g.drawString("Attack XP: " + getExperienceTracker().getGainedXP(Skill.ATTACK) + " (" + getExperienceTracker().getGainedLevels(Skill.ATTACK)+ ")", 17, 260);
        g.drawString("Strength XP: " + getExperienceTracker().getGainedXP(Skill.STRENGTH) + " (" + getExperienceTracker().getGainedLevels(Skill.STRENGTH)+ ")", 17, 280);
        g.drawString("Defence XP: " + getExperienceTracker().getGainedXP(Skill.DEFENCE) + " (" + getExperienceTracker().getGainedLevels(Skill.DEFENCE)+ ")", 17, 300);
        g.drawString("Hitpoints XP: " + getExperienceTracker().getGainedXP(Skill.HITPOINTS) + " (" + getExperienceTracker().getGainedLevels(Skill.HITPOINTS)+ ")", 17, 320);
    }
    
}

Thanks again

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