Jump to content
trickked

Few Questions

Recommended Posts

Hey fellas - so i'm working on a thieving script for fun (first time scripting) and for some reason my character clicks the master farmer and instantly logs out. Also - could someone explain to me how i would be able to make the script thieve other npc as well? Thanks. -- Here is my code so far:

 

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

import javax.swing.*;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.api.ui.Skill;
import java.awt.*;
import java.text.DecimalFormat;
import java.util.concurrent.TimeUnit;

@ScriptManifest(author = "Trickked", info = "My first script", name = "Master Farmer Thiever", version = 0, logo = "")
public class Main1 extends Script {

	// Holds time start of script and how long it ran
	private long timeBegan;
	private long timeRan;

	// Holds XP Gained from beginning/end
	private int beginningXP;
	private int currentXP;
	private int xpGained;

	// Hold Stat level start/current/amount gained
	private int currentLevel;
	private int beginningLevel;
	private int levelsGained;

	// Holds percentage till next level and current xp
	private double currentLevelXp;
	private double nextLevelXp;
	private double percentTNL;

	// Time until next level
	private int xpPerHour;
	private double xpTillNextLevel;
	private long timeTNL;

	private String selectedThiever = "Master Farmer";

	final int[] XP_TABLE = { 0, 0, 83, 174, 276, 388, 512, 650, 801, 969, 1154, 1358, 1584, 1833, 2107, 2411, 2746,
			3115, 3523, 3973, 4470, 5018, 5624, 6291, 7028, 7842, 8740, 9730, 10824, 12031, 13363, 14833, 16456, 18247,
			20224, 22406, 24815, 27473, 30408, 33648, 37224, 41171, 45529, 50339, 55649, 61512, 67983, 75127, 83014,
			91721, 101333, 111945, 123660, 136594, 150872, 166636, 184040, 203254, 224466, 247886, 273742, 302288,
			333804, 368599, 407015, 449428, 496254, 547953, 605032, 668051, 737627, 814445, 899257, 992895, 1096278,
			1210421, 1336443, 1475581, 1629200, 1798808, 1986068, 2192818, 2421087, 2673114, 2951373, 3258594, 3597792,
			3972294, 4385776, 4842295, 5346332, 5902831, 6517253, 7195629, 7944614, 8771558, 9684577, 10692629,
			11805606, 13034431, 200000000 };

	private JFrame gui;
	private boolean started = false;

	private void createGUI() {

		final int GUI_WIDTH = 400, GUI_HEIGHT = 500;

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

		final int gX = (int) (screenSize.getWidth() / 2) - (GUI_WIDTH / 2);
		final int gY = (int) (screenSize.getHeight() / 2) - (GUI_HEIGHT / 2);

		gui = new JFrame("GUI");

		gui.setBounds(gX, gY, GUI_WIDTH, GUI_HEIGHT);

		gui.setResizable(false);

		JPanel panel = new JPanel();
		gui.add(panel);

		JLabel label = new JLabel("Select what you would like to thieve:");
		label.setForeground(Color.white);
		panel.add(label);

		JComboBox<String> thieverSelector = new JComboBox<>(new String[] { "Master Farmer", "Men", "Guard" });
		thieverSelector.addActionListener(e -> selectedThiever = thieverSelector.getSelectedItem().toString());
		panel.add(thieverSelector);

		JButton startButton = new JButton("Start");
		startButton.addActionListener(e -> {
			started = true;
			gui.setVisible(false);
		});
		panel.add(startButton);
		gui.setVisible(true);
	}

	// Keeps track of the time from start to end
	@Override
	public void onStart() {
		timeBegan = System.currentTimeMillis();
		timeRan = System.currentTimeMillis() - this.timeBegan;
		beginningXP = skills.getExperience(Skill.THIEVING);
		beginningLevel = skills.getStatic(Skill.THIEVING);
		timeTNL = 0;
		getBank().withdraw("Lobster", 10);

		createGUI();

	}

	// Stores interactions
	private enum State {
		PICKPOCKET, BANK, WAIT, EAT
	};

	// Receives interactions made and checks inventory
	private State getState() {
		int HpPercent = (getSkills().getDynamic(Skill.HITPOINTS) * 100 / getSkills().getStatic(Skill.HITPOINTS));
		NPC farmer = getNpcs().closest("Master Farmer");
		if (getInventory().isFull() && !getInventory().contains("Lobster"))
			return State.BANK;
		if (farmer != null && !getInventory().isFull() && HpPercent > 50 && !myPlayer().isAnimating())

			return State.PICKPOCKET;
		if (HpPercent < 50 && !myPlayer().isAnimating() && getInventory().getItem("Shrimp").interact("Eat")
				|| getInventory().getItem("Trout").interact("Eat") || getInventory().getItem("Salmon").interact("Eat")
				|| getInventory().getItem("Tuna").interact("Eat") || getInventory().getItem("Lobster").interact("Eat"))
			return State.EAT;
		return State.WAIT;
	}

	// Continues script until out of food
	@Override
	public int onLoop() throws InterruptedException {
		if (started) {
			switch (getState()) {
			case PICKPOCKET:
				log("Pickpocket");
				NPC farmer = getNpcs().closest("Master Farmer");
				if (farmer != null) {
					getNpcs().closest("Master Farmer").interact("Pickpocket");
				} else {
					if (!myPlayer().isAnimating())
						;

				}
			case EAT:
				if (getInventory().contains("Shrimp") || getInventory().contains("Trout")
						|| getInventory().contains("Tuna") || getInventory().contains("Shrimp")
						|| getInventory().contains("Lobster")) {
					if (getInventory().getSelectedItemName() == null)
						;
					{
						getInventory().getItem("Lobster").interact("Eat");
					}
				} else {
					getInventory().deselectItem();

				}

			case BANK:
				if (getBank().isOpen()) {
					getBank().depositAll();
					getBank().withdraw("Lobster", 10);
				} else {
					RS2Object banker = getObjects().closest("Bank Booth");
					if (!myPlayer().isAnimating() && !getInventory().contains("Lobster") && banker != null
							&& banker.exists()) {
						banker.interact("Bank");
						sleep(5000); // Put a conditional sleep here!
					} else {
						stop(); // Stop if banker could not be found. You will
								// probably need to add walking here
					}
				}
				break;
			case WAIT:
				log("wait");
				sleep(random(500, 700));
				break;
			}
			return random(50, 400);
		}
		return 0;
	}

	// Sends message once script ends
	@Override
	public void onExit() {
		if (gui != null) {
			gui.setVisible(false);
			gui.dispose();
		}
	}

	// ONPAINT() METHOD
	public void onPaint(Graphics2D g) {
		if (started) {
			timeRan = System.currentTimeMillis() - this.timeBegan;
			g.drawString("Time Ran: " + ft(timeRan), 80, 75);

			currentXP = skills.getExperience(Skill.THIEVING);
			xpGained = currentXP - beginningXP;
			g.drawString("XP Gained: " + xpGained, 80, 114);

			currentLevelXp = XP_TABLE[currentLevel];
			nextLevelXp = XP_TABLE[currentLevel + 1];
			percentTNL = ((currentXP - currentLevelXp) / (nextLevelXp - currentLevelXp) * 100);
			DecimalFormat df = new DecimalFormat("#.#");
			g.drawString("Percent TNL: " + df.format(percentTNL), 80, 153);

			currentLevel = skills.getStatic(Skill.THIEVING);
			g.drawString("Beginning Level: " + beginningLevel, 80, 192);
			g.drawString("Current Level: " + currentLevel, 80, 231);

			levelsGained = currentLevel - beginningLevel;
			g.drawString("Levels Gained: " + levelsGained, 80, 270);

			xpPerHour = (int) (xpGained / ((System.currentTimeMillis() - this.timeBegan) / 3600000.0D));
			nextLevelXp = XP_TABLE[currentLevel + 1];
			xpTillNextLevel = nextLevelXp - currentXP;

			if (xpGained >= 1) {
				timeTNL = (long) ((xpTillNextLevel / xpPerHour) * 3600000);
				g.drawString("Time TNL: " + ft(timeTNL), 80, 309);
			}
		}
	}

	private String ft(long duration)

	{
		String res = "";
		long days = TimeUnit.MILLISECONDS.toDays(duration);
		long hours = TimeUnit.MILLISECONDS.toHours(duration)
				- TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(duration));
		long minutes = TimeUnit.MILLISECONDS.toMinutes(duration)
				- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(duration));
		long seconds = TimeUnit.MILLISECONDS.toSeconds(duration)
				- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration));
		if (days == 0) {
			res = (hours + ":" + minutes + ":" + seconds);
		} else {
			res = (days + ":" + hours + ":" + minutes + ":" + seconds);
		}
		return res;

	}

}

 

Link to comment
Share on other sites

			case BANK:

				if (getBank().isOpen()) {
					getBank().depositAll();
					if(getBank().contains("Lobster"){
						getBank().withdraw("Lobster", 10);
					}else {
						stop();
					}
				} else {
					RS2Object banker = getObjects().closest("Bank Booth");
					if (!myPlayer().isAnimating() && !getInventory().contains("Lobster") && banker != null
							&& banker.exists()) {
						banker.interact("Bank");
						sleep(5000); // Put a conditional sleep here!
					}
					if (bankker == null{
					stop();
}
				}
				break;

 

Link to comment
Share on other sites

I dont know much about java or state programming but it looks like you aren't looping through your script it runs once and ends.

	} else {
					if (!myPlayer().isAnimating())
						;

I mean you called an if statement and checked if your player is doing an animation, but don't want to do anything if true, what's the point of doing this..

Link to comment
Share on other sites

 

2 hours ago, raijin said:

you should look at the ExperienceTracker api, it will reduce your code a lot :)

 

1 hour ago, Shudsy said:

Half of your script could of been removed using the experiencetracker and skills api

Sadly i'm not that familiar with what you mentioned above, as this is my first time scripting. I am still learning on how to use a majority of the things. :/

Link to comment
Share on other sites

12 minutes ago, trickked said:

 

 

Sadly i'm not that familiar with what you mentioned above, as this is my first time scripting. I am still learning on how to use a majority of the things. :/

It sounds like you've just found this code online somewhere and don't actually understand most of what it's doing. If you want to actually learn how to script look through the tutorials on this forum, this one is a good place to start: 

 

Not many people are willing to spoonfeed others

Link to comment
Share on other sites

6 minutes ago, d0zza said:

It sounds like you've just found this code online somewhere and don't actually understand most of what it's doing. If you want to actually learn how to script look through the tutorials on this forum, this one is a good place to start: 

 

Not many people are willing to spoonfeed others

I didn't find this online actually. I went through apaecs tutorial/other tutorials on here and learned from that.  The comments on the side you see like //conditonal here is from Apaec helping me fix an error i made.

Edited by trickked
Link to comment
Share on other sites

3 minutes ago, trickked said:

I didn't find this online actually. I went through apaecs tutorial/other tutorials on here and learned from that.  The comments on the side you see like //conditonal here is from Apaec helping me fix an error i made.

Well read through your code again then. Switch cases need a break at the end of them otherwise they'll just go into the next case and perform those actions too

Link to comment
Share on other sites

5 minutes ago, d0zza said:

Well read through your code again then. Switch cases need a break at the end of them otherwise they'll just go into the next case and perform those actions too

thats the whole point of me making a thread in help.. not to be accused of copy and pasting code mate, just saying.. lmao.. I learn from receiving help/looking at tutorials.

Link to comment
Share on other sites

3 hours ago, trickked said:

thats the whole point of me making a thread in help.. not to be accused of copy and pasting code mate, just saying.. lmao.. I learn from receiving help/looking at tutorials.

If you don't know what api is (as you mentioned above) then you're not gonna get very far with scripting. I'm not trying to be a dick I'm just telling the truth.

If you did understand what the code is doing then it would be clear that you would need to modify this part of your program to change which npc to steal from

case PICKPOCKET:
				log("Pickpocket");
				NPC farmer = getNpcs().closest("Master Farmer");
				if (farmer != null) {
					getNpcs().closest("Master Farmer").interact("Pickpocket");
				} else {
					if (!myPlayer().isAnimating())
						;

				}

 

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.

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