-
Notifications
You must be signed in to change notification settings - Fork 1
Events
In coding, an event is something that happens, and your program can choose to respond to it. Think of events like things happening in the real world: when you press a button, that’s an event. When you move your mouse, that’s another event. In code, events are things your program can “listen” for and do something when they happen.
Events in Housatic are used to detect regular occurrences in-game. For example, if you want the bot to detect a player sending a message, you can use the chat
event.
To register an event to Housatic, you can use the global register()
function. The event's name goes in the first argument, then a function containing code you want to run when the event is triggered goes in the second argument.
// Referencing a predefined chat() function
register("chat", onChat());
function onChat() {
// runs when the bot receives a chat message
}
// Using an anonymous arrow function
register("houseSpawn", () => {
// runs upon joining any house
});
Housatic has a few custom built-in events that mainly cater to Housing:
Event | Description |
---|---|
houseSpawn | triggers when the bot joins any house |
houseCrash | triggers when the bot detects a chat message associated with house crashes |
chat | triggers when the bot receives a chat message* |
All mineflayer events are supported in addition to Housatic's built-in events. You can view a full list of them here.
*this event supports criteria! When registering this event, you can use a third argument to enter a string that a chat message has to match to run the event.
register("chat", () => {
// Runs when the bot gets a message that is exactly "Hey there, bot!"
}, "Hey there, bot!");
You can use curly braces {}
to create placeholders for any characters and assign them to variables. The name between the braces defines the variable that will hold the matched content. For example:
register("chat", (name) => {
// Runs when the bot receives a message matching the pattern "* Say hello to ___!" exactly.
// The placeholder inside `{}` (in this case, 'name') will capture whatever text is in that spot, and you can use it in your code.
}, "* Say hello to {name}!");
Great! Now that we know how to work with events, let's move on to everything else. ➜