Jump to content

  • Curse Sites
Become a Premium Member! Help
Latest News Article

Minecraft Bot API


  • Please log in to reply
42 replies to this topic

#1

Daviidi
    Daviidi

    Coal Miner

  • Members
  • 122 posts
  • Minecraft: Daviidi

Posted 14 August 2011 - 10:19 PM

Minecraft Bot API


Version 0.1

This API should make it easier for programmers to make bots for minecraft - it handles all the protocol, and provides methods for making the character perform actions. It does not require minecraft to be installed.

Note that this still new and under development, and although the sample bot worked fine on my machine connecting to a local unsecured server, it can only do a small number of things. It may have bugs, and your bots will undoubtedly need updating as later versions are released.

Links
Minecraft Bot API v0.1 (jar)
Source on Github
Making a bot
Example Bot
API Website (Maintained by PrivateAlpha)

Feel free to join us in #minecraftbot at irc.esper.net

License: Completely public domain - You can use it, distribute it, re-use it, sell it, do whatever you want with it.

Any suggestions are greatly appreciated!

If you have any problems using this API, post in this thread and I will be happy to help you.

Edit - This API is deprecated and I will probably never pick it up again, sorry. :(
KeonCraft- A factions PVP Survival server.

Register or log in to remove.

#2

Commander Keen

Posted 15 August 2011 - 12:01 AM

Very interesting. How hard would it be to make a simple archer bot that stands on one place and shoots at near enemy mobs?

#3

Alamillin

Posted 17 August 2011 - 04:36 PM

WoW, this is awesome!! I love it!! Congratulations for your idea!!

I have been doing some work with your API, and i think i found some errors in the code (i have downloaded source version (the fourth link, MainTest, doesn't work)).

I started with the simplest bot:

import com.daviidi.minecraftbotapi.BotPlayer;
import com.daviidi.minecraftbotapi.GameConnection;
import com.daviidi.minecraftbotapi.RecieveListener;
import com.daviidi.minecraftbotapi.protocol.server.AbstractResponse;
import com.daviidi.minecraftbotapi.protocol.server.LoginResponse;

public class MyBot implements RecieveListener {

	public static void main(String[] args) throws Throwable {
		new MyBot();
	}
	private BotPlayer player;
	private GameConnection g; //you can't assign a value to a final variable

	//you can write...
	/*
	String username = "MinecraftBot";
	String server = "127.0.0.1";
	int port = 25565;
	private final GameConnection g = new GameConnection(server, port, username);
	 */
	//...and remove "g = new GameConnection(server, port, username);" from constructor
	
	public MyBot() throws Throwable {
		String username = "MinecraftBot";
		String server = "127.0.0.1";
		int port = 25565;
		g = new GameConnection(server, port, username);
		g.connect();
		g.addListener(this);
	}

	public void recieved(AbstractResponse packet) {
		if (packet instanceof LoginResponse) {
			System.out.println("Logged in!");
			player = g.getPlayer();
		}
	}
}

I changed the modifiers for g variable, but all other code is copied from "Making a bot" link.

When I run it, it prints the trace of a NullPointerException.

java.lang.NullPointerException
		at com.daviidi.minecraftbotapi.GameConnection.recieved(GameConnection.java:197)
		at com.daviidi.minecraftbotapi.GameListener.recieved(GameListener.java:14)
		at com.daviidi.minecraftbotapi.RecieverThread.send(RecieverThread.java:49)
		at com.daviidi.minecraftbotapi.RecieverThread.run(RecieverThread.java:519)

null value is w.getItems() (package com.daviidi.minecraftbotapi.GameConnection, line 197, in "for" condition).

To solve this, as an experimental solution, i write next just above "for":

if(w.getItems() == null){
	w.setItems(new ItemStack[45]);// i have seen 45 slots somewhere
}

Now, it logs in with no problems, but if I hit the bot, or if I break a block, or I wait for 10 seconds, it logs out.


It will be a pleasure to help you (or to test) with this API ;)

Sorry for my english :(

#4

PrivateAlpha

Posted 17 August 2011 - 08:18 PM

I'll be watching you 0_0

Also, please continue with this :P I really want to see whats going to happen with this :P

#5

PrivateAlpha

Posted 18 August 2011 - 12:18 PM

hmmm,
java.lang.NullPointerException
	at com.daviidi.minecraftbotapi.GameConnection.recieved(GameConnection.java:197)
	at com.daviidi.minecraftbotapi.GameListener.recieved(GameListener.java:14)
	at com.daviidi.minecraftbotapi.RecieverThread.send(RecieverThread.java:49)
	at com.daviidi.minecraftbotapi.RecieverThread.run(RecieverThread.java:519)

not sure why this happened but it should be on my side :/

Edit:
for (int j = 0; j < w.getItems().length; j++) {
						i.set(j, w.getItems()[j]);
you need to check if w.getItems() returns null(Because it does) so just add an if in there or w/e

#6

PrivateAlpha

Posted 18 August 2011 - 09:13 PM

Also the bot needs to send a keepAlive packet every 60 secs although i just do it every 30 seconds.

#7

Alamillin

Posted 18 August 2011 - 09:25 PM

View PostPrivateAlpha, on 18 August 2011 - 09:13 PM, said:

Also the bot needs to send a keepAlive packet every 60 secs although i just do it every 30 seconds.

Aham... this should be why it logs out automatically...

#8

Commander Keen

Posted 18 August 2011 - 09:32 PM

Try posting it at the API's Github site, Davidii seems to be subscribed to it.

BTW, any chance to see your code? I have just utterly failed at following the tutorial, I always liked more to learn from existing code.

#9

PrivateAlpha

Posted 18 August 2011 - 09:50 PM

View PostCommander Keen, on 18 August 2011 - 09:32 PM, said:

Try posting it at the API's Github site, Davidii seems to be subscribed to it.

BTW, any chance to see your code? I have just utterly failed at following the tutorial, I always liked more to learn from existing code.

Well i would give you my code but its partially reliant on the changes I've made to the api myself. I'll give OP a day or two to fix the bugs. I'll put out a post my code then ;)

#10

Alamillin

Posted 18 August 2011 - 09:55 PM

here is my simple bot:

import com.daviidi.minecraftbotapi.BotPlayer;
import com.daviidi.minecraftbotapi.GameConnection;
import com.daviidi.minecraftbotapi.RecieveListener;
import com.daviidi.minecraftbotapi.protocol.client.KeepAliveRequest;
import com.daviidi.minecraftbotapi.protocol.server.AbstractResponse;
import com.daviidi.minecraftbotapi.protocol.server.LoginResponse;

public class MyBot implements RecieveListener {

	public static void main(String[] args) throws Throwable {
		new MyBot();
	}
	private BotPlayer player;
	private GameConnection g; //you can't assign a value to a final variable

	//you can do...
	/*
	String username = "MinecraftBot";
	String server = "127.0.0.1";
	int port = 25565;
	private final GameConnection g = new GameConnection(server, port, username);
	 */
	//...and remove "g = new GameConnection(server, port, username);" from constructor
	public MyBot() throws Throwable {
		String username = "Minecraft Bot";
		String server = "localhost";
		int port = 25565;
		g = new GameConnection(server, port, username);
		g.connect();
		g.addListener(this);

		new KeepAliveThread().run();
	}

	public void recieved(AbstractResponse packet) {
		if (packet instanceof LoginResponse) {
			System.out.println("Logged in!");
			player = g.getPlayer();
		}
	}

	//this class send a KeepAliveRequest every 30 seconds, as PrivateAlpha tells
	private class KeepAliveThread extends Thread {

		KeepAliveRequest kar;

		public KeepAliveThread() {
			kar = new KeepAliveRequest();
		}

		public void run() {
			try {
				while (true) {
					Thread.sleep(30000);
					kar.send(g.out); //change visibility of GameConnection.out to public
					System.out.println("KeepAliveRquest send!!");
				}
			} catch (Exception e) {
				System.out.println("KeepAliveThread fails!!");
			}
		}
	}
}

it doesn't anything, just logs in.
if seems to have some bugs at login, like it stay floating in the mid of the air, and if i hit the bot or break a block near it, it logs out ("invalid package <package id>" appears in console).

About post this in Github, Daviidi asks us to post here... so post here.

#11

Commander Keen

Posted 19 August 2011 - 07:51 AM

Thank you both!

Quote

About post this in Github, Daviidi asks us to post here... so post here.

He does not seem to be answering your problems or bug reports here, so I just figured he forgot about this thread.

#12

ColdIce
    ColdIce

    Tree Puncher

  • Members
  • 17 posts

Posted 19 August 2011 - 02:59 PM

Well, I'm getting invalid packet ID all over. Is this because it's an Bukkit Server I'm trying to connect to?

Invalid Packet:
INVALID PACKET: -27/0xffffffe5
INVALID PACKET: -48/0xffffffd0
INVALID PACKET: 75/0x4b
INVALID PACKET: 91/0x5b
INVALID PACKET: -2/0xfffffffe
INVALID PACKET: -126/0xffffff82
INVALID PACKET: -95/0xffffffa1
INVALID PACKET: 115/0x73
INVALID PACKET: -25/0xffffffe7
INVALID PACKET: -88/0xffffffa8
INVALID PACKET: -21/0xffffffeb
INVALID PACKET: -71/0xffffffb9
INVALID PACKET: -67/0xffffffbd
INVALID PACKET: 87/0x57
INVALID PACKET: 111/0x6f
INVALID PACKET: -86/0xffffffaa
INVALID PACKET: 126/0x7e
INVALID PACKET: -12/0xfffffff4
INVALID PACKET: 98/0x62
INVALID PACKET: -40/0xffffffd8
INVALID PACKET: -8/0xfffffff8
INVALID PACKET: 93/0x5d
INVALID PACKET: -5/0xfffffffb


#13

PrivateAlpha

Posted 19 August 2011 - 07:21 PM

A few things in ReceiverThread had to change because of java using signed bytes :/
if (packetId == 0x82|| packetId == -126) {
					UpdateSignResponse u = new UpdateSignResponse();
					-snip-
					continue;
				}
				if (packetId == 0x83|| packetId == -125) {
					MapDataResponse m = new MapDataResponse();
					-snip-
					continue;
				}
				if (packetId == 0xC8|| packetId == -56) {
					IncrementStatisticResponse i = new IncrementStatisticResponse();
					-snip-
					continue;
				}
				if (packetId == 0xFF || packetId == -1) {
					DisconnectResponse d = new DisconnectResponse();
					-snip-
					continue;
				}


Notice the ||'s that use ints to do the checks

Also I'm hangin in #minecraftbot on irc.esper.net if anyone wants to come chat ;)

#14

Daviidi
    Daviidi

    Coal Miner

  • Members
  • 122 posts
  • Minecraft: Daviidi

Posted 19 August 2011 - 11:06 PM

View PostPrivateAlpha, on 18 August 2011 - 12:18 PM, said:

hmmm,
java.lang.NullPointerException
	at com.daviidi.minecraftbotapi.GameConnection.recieved(GameConnection.java:197)
	at com.daviidi.minecraftbotapi.GameListener.recieved(GameListener.java:14)
	at com.daviidi.minecraftbotapi.RecieverThread.send(RecieverThread.java:49)
	at com.daviidi.minecraftbotapi.RecieverThread.run(RecieverThread.java:519)

not sure why this happened but it should be on my side :/

Edit:
for (int j = 0; j < w.getItems().length; j++) {
						i.set(j, w.getItems()[j]);
you need to check if w.getItems() returns null(Because it does) so just add an if in there or w/e

Oh, it wasn't setting the items in the response class. Fixed that in the latest commit, thanks.


View PostPrivateAlpha, on 18 August 2011 - 09:13 PM, said:

Also the bot needs to send a keepAlive packet every 60 secs although i just do it every 30 seconds.

Ah, I made a thread to do that. Only it appears I forgot to start the thread. Whoops! I'll fix that.


View PostCommander Keen, on 19 August 2011 - 07:51 AM, said:

Thank you both!



He does not seem to be answering your problems or bug reports here, so I just figured he forgot about this thread.

I kind of forgot, sorry. :/


View PostPrivateAlpha, on 19 August 2011 - 07:21 PM, said:

A few things in ReceiverThread had to change because of java using signed bytes :/
if (packetId == 0x82|| packetId == -126) {
					UpdateSignResponse u = new UpdateSignResponse();
					-snip-
					continue;
				}
				if (packetId == 0x83|| packetId == -125) {
					MapDataResponse m = new MapDataResponse();
					-snip-
					continue;
				}
				if (packetId == 0xC8|| packetId == -56) {
					IncrementStatisticResponse i = new IncrementStatisticResponse();
					-snip-
					continue;
				}
				if (packetId == 0xFF || packetId == -1) {
					DisconnectResponse d = new DisconnectResponse();
					-snip-
					continue;
				}


Notice the ||'s that use ints to do the checks

Also I'm hangin in #minecraftbot on irc.esper.net if anyone wants to come chat ;)

Ah, I could probably use read() instead of readByte(), I think that's unsigned.

Thanks all of you!
KeonCraft- A factions PVP Survival server.

#15

redpois0n
    redpois0n

    Stone Miner

  • Curse Premium
  • Curse Premium
  • 88 posts
  • Location: In a chair

Posted 20 August 2011 - 06:43 AM

hahahah this could be fun

#16

321olos
    321olos

    Tree Puncher

  • Members
  • 26 posts

Posted 20 August 2011 - 10:56 AM

Hey, this seems like some great work. Im currently trying to add in following of a player and following commands. E.g. if the user says "mine (block)" the bot will mine the block... I will let you know how i go with it.

#17

PrivateAlpha

Posted 20 August 2011 - 01:01 PM

View PostDaviidi, on 19 August 2011 - 11:06 PM, said:

-snip-
I kind of forgot, sorry. :/

Now now dont you dare forget about us! :P

#18

PrivateAlpha

Posted 20 August 2011 - 04:24 PM

Also Would be nice if you could join us in #minecraftbot on irc.esper.net . Would be great to discuss the api with you ;)

#19

Daviidi
    Daviidi

    Coal Miner

  • Members
  • 122 posts
  • Minecraft: Daviidi

Posted 20 August 2011 - 05:46 PM

View Post321olos, on 20 August 2011 - 10:56 AM, said:

Hey, this seems like some great work. Im currently trying to add in following of a player and following commands. E.g. if the user says "mine (block)" the bot will mine the block... I will let you know how i go with it.

You won't be able to do that with just the API, yet. Adding the mine block method and place block method will be the next thing I do, along with better inventory management. Then it will be possible, but to get the bot to the block will require pathfinding.

View PostPrivateAlpha, on 20 August 2011 - 04:24 PM, said:

Also Would be nice if you could join us in #minecraftbot on irc.esper.net . Would be great to discuss the api with you ;)

Sure, I'm there. :)
KeonCraft- A factions PVP Survival server.

#20

Commander Keen

Posted 20 August 2011 - 08:41 PM

Re-asking:

View PostCommander Keen, on 15 August 2011 - 12:01 AM, said:

Very interesting. How hard would it be to make a simple archer bot that stands on one place and shoots at near enemy mobs?