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

WIP: VERY EARLY PROXY EFFORTS #142

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions spec/integration/bot_spec.cr
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ Spectator.describe Rosegold::Bot do
it "should fall due to gravity" do
client.join_game do |client|
Rosegold::Bot.new(client).try do |bot|
sleep 50
sleep 2 # load chunks
bot.chat "/tp 1 -58 1"
sleep 1 # teleport
18 changes: 18 additions & 0 deletions src/rosegold/client.cr
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ class Rosegold::Client < Rosegold::EventEmitter

property host : String, port : Int32
property connection : Connection::Client?
property proxy : TCPServer?

property \
online_players : Hash(UUID, PlayerList::Entry) = Hash(UUID, PlayerList::Entry).new,
@@ -44,6 +45,7 @@ class Rosegold::Client < Rosegold::EventEmitter
@physics = Physics.new self
@inventory = PlayerWindow.new self
@window = @inventory
start_proxy
end

def connection? : Connection::Client?
@@ -57,6 +59,20 @@ class Rosegold::Client < Rosegold::EventEmitter
conn
end

def start_proxy : TCPServer
proxy = @proxy ||= TCPServer.new "localhost", 1234
spawn do
while socket = proxy.accept?
puts "WHOA"
spawn do
server = Connection::Server.new Minecraft::IO::Wrap.new(socket), ProtocolState::LOGIN.serverbound
server.state = ProtocolState::LOGIN.serverbound
server.send_packet Clientbound::LoginStart.new player.username.not_nil! # ameba:disable Lint/NotNil
end
end
end
end

def connected?
@connection.try &.open?
end
@@ -72,6 +88,8 @@ class Rosegold::Client < Rosegold::EventEmitter
# Waits for the client to be fully spawned, ie. physics and inventory being ready.
def join_game(timeout_ticks = 1200)
connect
start_proxy
Log.info { "Connect to localhost:1234 to spectate Rosegold bot" }
until spawned?
sleep 1/20
timeout_ticks -= 1