Jump to content
Einstein

Einstein's Scripting Tutorial Part 2

Recommended Posts

Einstein's Scripting Tutorial [Part 2]

 

Target audience: Beginners

Level of expertise: Doesn't try to download RAM

Estimated duration: 20 minutes

 

The first tutorial focused on how to get a script started; the very basics.

The part 2 will focus on slightly more advanced topics such as:

  1. Paint
  2. Graphical User Interface
  3. Code structure
  4. Anti Ban
  5. Script arguments
  6. Debugging
  7. Concurrency

Let's get started!

 

1. Paint

In order to add a paint to your script, you need to make the main class implement the Painting interface.

qDXjDPO.png

Nice! Now you can use the g object to draw images and text on the screen.

Let's use the getImage() method to cache two images. One stored on the computer and one from the web.

As always, use your IDE's keyboard shortcut to handle imports. (Eclipse users: Ctrl + Shift + O)

p0EQvJR.png

Notes:

  • https://imgur.com/ offers free and reliable image hosting.
  • file:// is the prefix for the file protocol, the extra / points to the root directory of the current drive.

 

Using the g object to draw a string and an image:

MMXuxYN.png

 

Results:

 

tGiVcZf.png

 

The string is now displayed at the specified screen location, in the top left corner. The image can be seen in the center.

I've also included a visual representation of the X-Y axis.

In order to accurately determine the coordinates you need to use TRiBot's debugger:

1PxqEYR.png

 

 

2. Graphical User Interface

By adding a GUI you won't have to rewrite and re-compile the code every time you want to change the script's settings (for example: the name of the NPC a combat script will attack, or the type of tree a Woodcutting script will chop down).

1. You first need to create a class that extends JFrame.

QYO0HTz.png

 

2. Initialize the GUI. You can do this in the run() method, but I like to initialize the GUI and handle other script configurations in the onStart() method. (You will need to make your class implement Starting).

cy9IJGR.png

 

3. Implement an action listener: upon clicking "Start" on your GUI, cache all the settings in your variables class and dispose() the GUI.

Do not: use System.exit(0)

Do: setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) or setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE)

I highly recommend using NetBeans for fast designing and a professionally looking GUI.

 

 

 

3. Code Structure

Any kind of non-trivial script requires some sort of framework. Otherwise the code will become unmanageable very quickly.

 

A. Binary:

It looks something like this:7AgZNbt.png

 

 

 

 

B. Task/Node framework:

Basically the idea is this:

1. You have an abstract class or an interface called Node that defines two methods: shouldExecute() and execute().

2. All the script actions are split into different classes that inherit/implement the Node class/interface, therefore inheriting both methods.

  • shouldExecute() defines when the action should happen. For example consider a class called DropLogs. The shouldExecute() method will return TRUE if the inventory is full.
  • execute() method is the action itself. Using the same Woodcutting example, execute() will run the code that actually drops the logs.

3. Main class creates a Node object from each Node subclass, groups all the created objects in a list, and iterates through the list members with the following logic:

 if ( shouldExecute() returns TRUE )

  then execute();

 

Detailed guides on the task/node framework in your script:

Props to @Worthy

https://tribot.org/forums/topic/20101-node-framework-tutorial-increase-efficiency-and-clarity/

 

Props to @Encoded

https://tribot.org/forums/topic/46989-encodeds-task-framework/

 

 

 

C. State based script

soon™

 

 

 

 

 

4. Anti-Ban

A. TRiBot's AI Anti-Ban

By default, TRiBot has an AI Anti-Ban enabled.

  • Script#getAIAntibanState() returns its current state
  • Script#setAIAntibanState() activates/deactivates the anti-ban, however only users with Scripter rank can use this method to disable it.

I would advise you to leave it as it is. The only exception is when you have a script that is supposed to AFK. In this case, the anti ban is defeating its own purpose. Use this code to get the thread and store it in a variable. After that you will be able to activate/deactivate the anti-ban as needed, by performing actions on the variable.

ocyQ4VS.png

Note: After suspending the thread's execution, Script#getAIAntibanState will still return TRUE because its internal flag was never changed. However even if the returned value of that method suggests otherwise, the AI anti-ban is no longer executing.

 

 

B. ABC2

ABC2 is designed to use real human playing data instead of pseudo-randomization for deciding:

  If / When the bot should:

  • check xp
  • examine entity
  • move mouse
  • rotate camera
  • move to anticipated location etc.

When the bot should:

  • eat
  • activate run etc.

How the bot should:

  • open bank
  • open tabs
  • walk etc.

And how long the bot should wait before reacting to an event.

I can't explain it better than TRiLeZ, so here is the link to the official ABC2 implementing guide. https://tribot.org/forums/topic/60720-guide-to-implementing-abc2/

However, I would like to clarify one single aspect (has been asked many times / some users are confused):

ABC2: Anti-Ban Compliance utility version 2

ABCL: Anti-Ban Compliance Level, ranging from 0 to 10, based on a point system explained in the tutorial I linked above.

A script will either "have" both ABC2 and ABCL level 0-10, or it will "have" none of them.

 

C. Your custom anti-ban

The only thing I have to say is be careful.

Basing your custom anti-ban on a random number generator will most likely get you banned even faster. Humans are unable to perform random actions consistently. More information on this subject can be found here:   https://tribot.org/forums/topic/60719-tribot-release-9300_0-abc2/

 

 

 

 

5. Script arguments

A handy alternative to GUI.

In order to use script arguments, you need to implement the Arguments interface.

Su6vl6u.png

 

All arguments written in the bottom left box of the Script Manager will be passed to the overridden method in a form of a hash map. The script can then set variables based on the values of the hash map.

4xJpi1I.png

 

 

 

 

6. Debugging

Regardless of your level of expertise, bugs will remain a fact of life. Debugging your scripts can be done in multiple ways. One of the easiest ways is to print messages in the console as the code executes:

WYT4sDs.png

 

Methods that can be used to achieve this:

  • Script.println() - prints to the Client Debug
  • General.println() - prints to the Client Debug
  • System.out.println() - prints to the Bot Debug

5oCj3Pk.png

 

 

 

 

7.Concurrency

All methods from the interfaces implemented by your script will be concurrent (will run at the same time) with the main thread. Example of concurrent methods:

CR5g1xq.png

Ideally, you would want to have all in-game actions performed in the main thread.

All other threads should be used to manipulate data.

 

 

The end

Thank you for reading part II of my scripting tutorial.

I hope you enjoyed it and you learned something.

 

Edited by Einstein
  • Like 4

Share this post


Link to post
Share on other sites
3 minutes ago, Einstein said:

TzhaqAQ.png

A method named getX would assume a return value. Something more appropriate would be this.

 

private Thread getAntibanThread() {
	for (Thread t : Thread.getAllStackTraces().keySet())
		if (thread.getName().contains("Antiban))
			return t;

	return null;
}

antiBanThread = getAntibanThread();

 

  • Like 2

Share this post


Link to post
Share on other sites
Just now, Blastois3 said:

A method named getX would assume a return value. Something more appropriate would be this.

 

private Thread getAntibanThread() {
	for (Thread t : Thread.getAllStackTraces().keySet())
		if (thread.getName().contains("Antiban))
			return t;

	return null;
}

antiBanThread = getAntibanThread();

 

You're right.

Thanks!

Share this post


Link to post
Share on other sites
Just now, lets be friends said:

Good stuff. Wish you did more with the antiban instead of just linking to a thread but everything else was great.

Regarding ABC2 implementation, it simply can't be explained better than it already is in the official guide. Your best bet would be to check out some open source scripts that implement the utility.

 

I'm glad you liked the tutorial. :)

Share this post


Link to post
Share on other sites
2 minutes ago, lets be friends said:

Good stuff. Wish you did more with the antiban instead of just linking to a thread but everything else was great.

ABC2 tutorial by TRiLeZ, what more could you ask for :)

Thanks for taking the time to write this and spreading the (Tai Lopez voice) KNOWLEDGE. 

  • Haha 1

Share this post


Link to post
Share on other sites

Nice tutorial.

I find it funny how you use a control-flow diagram to illustrate your code structure. However, the Task/Node frameworks do not allow this type of structure. 

 

Your diagram illustrates a Binary Decision Tree of Decisions and Processes. I am cooking up a framework now that allows for that exact node structure! Stay tuned.

  • Like 1

Share this post


Link to post
Share on other sites
5 minutes ago, YoHoJo said:

ABC2 tutorial by TRiLeZ, what more could you ask for :)

Thanks for taking the time to write this and spreading the (Tai Lopez voice) KNOWLEDGE. 

Thanks!

It's much easier to learn scripting when you have a MENTOR.

EhwMsFG.jpg

 

Just now, wastedbro said:

Nice tutorial.

I find it funny how you use a control-flow diagram to illustrate your code structure. However, the Task/Node frameworks do not allow this type of structure. 

 

Your diagram illustrates a Binary Decision Tree of Decisions and Processes. I am cooking up a framework now that allows for that exact node structure! Stay tuned.

Thanks!

Looking forward to seeing your new framework. :shy:

  • Haha 1

Share this post


Link to post
Share on other sites
12 minutes ago, wastedbro said:

Nice tutorial.

I find it funny how you use a control-flow diagram to illustrate your code structure. However, the Task/Node frameworks do not allow this type of structure. 

 

Your diagram illustrates a Binary Decision Tree of Decisions and Processes. I am cooking up a framework now that allows for that exact node structure! Stay tuned.

Can't edit the comment above.

Well, the diagram illustrates the binary tree (point A).

 

The node framework (point B) has no diagram, it's explained with text only. I've edited the thread and added spacing to prevent confusion. :lol:

Thanks for pointing it out!

  • Like 1

Share this post


Link to post
Share on other sites
On 26.11.2017 at 10:37 PM, Einstein said:

5. Script arguments

A handy alternative to GUI.

In order to use script arguments, you need to implement the Arguments interface.

Su6vl6u.png

 

All arguments written in the bottom left box of the Script Manager will be passed to the overridden method in a form of a hash map. The script can then set variables based on the values of the hash map.

4xJpi1I.png

 

wot m8?

Share this post


Link to post
Share on other sites
50 minutes ago, rstaiger said:

arguments like, do they change variables? or they are like a method

They change a variable.

The script you are using has to support arguments. Let's say you have a woodcutting script and you have 3 bots. One bot is just starting out so your argument for that client or script instance is PROGRESSIVE and you want it to POWERCHOP for example. The next two bots have high wc level so you could leave on PROGRESSIVE or have them cut a specific tree in a specific area and you want them to bank instead of powerchop and you want it to stop after 4 hours. So your argument might look like: YEW, WC_GUILD, BANK , 4.  You can save a profile under your script queue that has everything set up for you. All you need to do is start the queue. Similar for the client starter.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×