Skip to content

Javascript API

Michael Zangl edited this page Dec 17, 2015 · 6 revisions

With /minebot js, a custom javascript file can be executed. This is the documentation of this feature.

Basic principle

The Javascript file is executed independently from the main game loop. That means that you could compute pi and the game would still continue to run as normal. But as soon as you want to influence the game state, the game is paused for it, so that you do not need to care about that. The game stays paused and you can do whatever, like scanning for entities, but you need to call a resume operation after you are finished so that the game may continue.

Mind that you can freeze Minecraft by adding an infinite loop to your Javascript.

Please only use the Javascript API as intended. If you break this and e.g. access Minecraft methods while the game loop is not paused, you might end up having big problems.

Events

Still to decide how this is done. Events that should be handled:

  • A generic Tick-Event (called every tick)
  • A death event
  • Hurt event
  • Bot errors (like not standing on ...)

Strategies

Minebot works with strategies. A strategy tells the bot what to do until the strategy states that it is done. Strategies can be stacked, in which case the strategies higher up the stack act as dominant strategies. Lower strategies get can work as soon as the higher strategies are done. The higher ones can take over any time, but most of them let the lower strategies finish the current piece of work before they take over.

Higher strategies can also force the lower ones to abort. This can be used to stop strategies.

Command list

All commands are accessible using the minescript-Object. You can use a with-Statement if you want to, mind that there might also be other, undocumented functions.

Strategies

minescript.safeStrategy(String...): ScriptStrategy

Evaluates a given minebot command and returns the strategy that it would result it. This strategy is added with some safeguards according to the settings.

minescript.strategy(String, String[]): ScriptStrategy

This gives you the single strategy that command would result in. For mining this would be the mine strategy, without any protective strategies. The mine command would look like: strategy("minebot", ["mine", "coal_ore"]);

minescript.strategyWalkTowards(double, double): ScriptStrategy

A fast way to create a walk towards strategy. You can directly pass double values.

minescript.stack(ScriptStrategy, String[]): ScriptStrategy

Stacks multiple strategies. The first one is the most important one, and so on.

minescript.doStrategy(ScriptStrategy)

Starts executing the strategy. Your script will pause until the strategy is done.

minescript.doNothing()

Continue the game. Call this every time you accessed some minescript functions and you want the player to keep playing. This will let the game loop continue to run. Mind that your next minescript call is delayed until the next game tick and will then pause the game loop again.

Chat

minescript.serverCommand(String)

Send a command or a chat message to the server as if the user typed it. This will also let the game loop continue.

minescript.displayChat(String)

Display a message in the (local) player chat.

minescript.getChatMessages(): ChatMessage[]

Gets an array of all chat messages received since the client was started. This also includes chat messages from previous games.

New messages are appended, so you can simply remember the length of this array to filter for new messages.

Player

minescript.getPlayer(): FoundPlayer

Get the player object to access position, ...

minescript.getExperienceLevel(): int

Gets the experience level (the green number at the bottom of the screen)

minescript.isAlive(): bool

Checks whether the player is still living. Returns false when the respawn screen is shown.

minescript.getInventory(): InventoryDefinition

Get the current player inventory.

minescript.getPlayerBlockPosition(): Pos

Returns a position object for the player position.

World

minescript.getEntities(class, range): FoundEntity[]

Returns an array of entities with the given class in the given range (rectangular). Use the full class path as class (net.minecraft...)

minescript.getCurrentTime(): int

Gets the current time of day. 0 <= time < 24000.

Persistence

minescript.getString(name[, default=null]): String

The name is global. On windows, it is not case sensitive.

minescript.storeString(name, value)

Store a string to retrive it later using getString.

MISC

minescript.setDescription(String)

Sets the description that is displayed in the top right hand corner of the screen.

Found entity objects (FoundEntity)

There is no direct entity

getPos(): Pos

Get the position of the entity.

getType()

Gets the class of the entity.

getTypeName()

Gets the simple class name of the entity.

Position Objects (EntityPos, FoundEntity)

For other methods, see the javadoc to Pos. All methods can be called from Javascript.

x, y, z

The position of the entity.

motionX, motionY, motionZ

The speed vector of the entity.

speed

The absolute speed of the entity

distance(other): double

Computes the distance between the centers of two entities.

distance(x, y, z): double

Compute the distance to that position.

Chat messages (ChatMessage)

time

A java date

text

The chat message that was sent.

textFormatted

The chat message including the §x-format flags

isChat

True if this is a real chat message.

Clone this wiki locally