Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit the chat event via bot #153

Merged
merged 1 commit into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions spec/integration/bot_event_emitter_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "../spec_helper"

Spectator.describe "Rosegold::Bot event emitter" do
it "should emit chat events" do
client.join_game do |client|
Rosegold::Bot.new(client).try do |bot|
test = false
bot.on Rosegold::Clientbound::ChatMessage do |event|
if event.message.to_s == "<#{client.player.username}> Hello, world!"
test = true
end
end
bot.chat "Hello, world!"
bot.wait_tick
expect(test).to be true
end
end
end
end
9 changes: 6 additions & 3 deletions src/rosegold/bot.cr
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
require "../rosegold"
require "./control/*"

class Rosegold::Bot
class Rosegold::Bot < Rosegold::EventEmitter
private getter client : Client

getter inventory : Inventory

def initialize(@client)
@inventory = Inventory.new client
@interact = Interactions.new client
client.on(Rosegold::Clientbound::ChatMessage) do |packet|
emit_event packet
end
end

# Does not connect immediately.
Expand All @@ -18,10 +21,10 @@ class Rosegold::Bot

# Connects to the server and waits for being ingame.
def self.join_game(address : String, timeout_ticks = 1200)
new Client.new(address).tap &.join_game(timeout_ticks)
new Client.new(address).join_game(timeout_ticks)
end

delegate host, port, connect, connected?, disconnect, join_game, spawned?, online_players, on, to: client
delegate host, port, connect, connected?, disconnect, join_game, spawned?, online_players, to: client
delegate uuid, username, feet, eyes, health, food, saturation, gamemode, sneaking?, sprinting?, to: client.player
delegate sneak, sprint, to: client.physics
delegate main_hand, to: inventory
Expand Down