Class: CraftClient::Server
- Inherits:
-
Object
- Object
- CraftClient::Server
- Defined in:
- lib/craft_client/server.rb
Instance Attribute Summary collapse
-
#block_cache ⇒ Object
Returns the value of attribute block_cache.
-
#players ⇒ Object
readonly
Returns the value of attribute players.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #connect ⇒ Object
- #disconnect ⇒ Object
- #flush_buffer ⇒ Object
-
#get_block(x, y, z) ⇒ Object
get_block_at.
- #get_event ⇒ Object
-
#initialize(username, identity_token, server_name, server_port) ⇒ Server
constructor
A new instance of Server.
- #send_chat_message(message) ⇒ Object
- #send_private_message(player, message) ⇒ Object
-
#set_block(x, y, z, id) ⇒ Object
set_block.
- #set_light(x, y, z, light) ⇒ Object
- #set_position(x, y, z, rotate_x, rotate_y) ⇒ Object
- #set_sign(x, y, z, facing, text) ⇒ Object
Constructor Details
#initialize(username, identity_token, server_name, server_port) ⇒ Server
Returns a new instance of Server.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/craft_client/server.rb', line 6 def initialize(username, identity_token, server_name, server_port) @username = username @identity_token = identity_token @server_name = server_name @server_port = server_port @block_cache = {} @players = {} @write_mutex = Mutex.new @buffer = '' @tcp = nil end |
Instance Attribute Details
#block_cache ⇒ Object
Returns the value of attribute block_cache.
3 4 5 |
# File 'lib/craft_client/server.rb', line 3 def block_cache @block_cache end |
#players ⇒ Object (readonly)
Returns the value of attribute players.
4 5 6 |
# File 'lib/craft_client/server.rb', line 4 def players @players end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
4 5 6 |
# File 'lib/craft_client/server.rb', line 4 def username @username end |
Instance Method Details
#connect ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/craft_client/server.rb', line 22 def connect # Authenticate with craft.michaelfogleman.com uri = URI.parse('https://craft.michaelfogleman.com/api/1/identity') http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true response = http.post(uri.request_uri, "username=#{@username}&identity_token=#{@identity_token}") server_token = response.body.chomp fail 'Could not authenticate!' if server_token.length != 32 # Connect and authenticate with the server @tcp = TCPSocket.new(@server_name, @server_port) @tcp.puts("A,#{@username},#{server_token}") end |
#disconnect ⇒ Object
147 148 149 |
# File 'lib/craft_client/server.rb', line 147 def disconnect @tcp.close unless @tcp.nil? end |
#flush_buffer ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/craft_client/server.rb', line 105 def flush_buffer @write_mutex.synchronize do @tcp.write(@buffer) @tcp.flush @buffer = '' end end |
#get_block(x, y, z) ⇒ Object
get_block_at
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/craft_client/server.rb', line 73 def get_block(x, y, z) # get_block_at # Return block if it exists in cache return @block_cache[[x, y, z]] unless @block_cache[[x, y, z]].nil? # Otherwise, request the chunk the block is in. @write_mutex.synchronize do chunk_x = (x / CHUNK_SIZE).floor chunk_z = (z / CHUNK_SIZE).floor @tcp.puts("C,#{chunk_x},#{chunk_z}") @tcp.flush # Keep mutex locked to stop other threads requesting from server loop do data = @tcp.gets.chomp.split(',') if data[0] == 'B' @block_cache[[data[3].to_i, data[4].to_i, data[5].to_i]] = data[6].to_i elsif data[0] == 'C' break end end end @block_cache[[x, y, z]] end |
#get_event ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/craft_client/server.rb', line 37 def get_event # Blocks and returns an event hash data = @tcp.gets.chomp.split(',') case data[0] when 'T' # Chat message = data[1 .. -1].join(',') if !.split('>')[1].nil? # If message was said by player return { type: :chat_message, sender: .split('>')[0], message: .split('>')[1 .. -1].join('>').lstrip } else return { type: :chat_special, message: } end when 'B' # Block change pos = [data[3].to_i, data[4].to_i, data[5].to_i] # Create the position data return nil if data[1].to_i != pos[0] / CHUNK_SIZE || data[2].to_i != pos[2] / CHUNK_SIZE @block_cache[pos] = data[6].to_i return { type: :block_change, pos: pos, id: data[6].to_i } when 'S' # Sign change return { type: :sign_update, pos: [data[3].to_i, data[4].to_i, data[5].to_i], facing: data[6].to_i, text: (data[7..-1] || []).join(',') } when 'N' # Player join prepare_player(data[1].to_i) @players[data[1].to_i][:name] = data[2..-1].join(',') return { type: :player_join, id: data[1].to_i, name: data[2..-1].join(',') } when 'P' # Player position prepare_player(data[1].to_i) @players[data[1].to_i][:pos] = data[2..-1].map(&:to_f) return { type: :player_position, id: data[1].to_i, pos: @players[data[1].to_i][:pos] } when 'D' # Player leave event = { type: :player_leave, id: data[1].to_i, name: @players[data[1].to_i][:name] } @players.delete(data[1].to_i) return event else return nil end end |
#send_chat_message(message) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/craft_client/server.rb', line 113 def () @write_mutex.synchronize do .lines.each do |line| @buffer += "T,#{line.chomp}\n" end end end |
#send_private_message(player, message) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/craft_client/server.rb', line 121 def (player, ) @write_mutex.synchronize do .lines.each do |line| @buffer += "T,@#{player} #{line.chomp}\n" end end end |
#set_block(x, y, z, id) ⇒ Object
set_block
98 99 100 101 102 103 |
# File 'lib/craft_client/server.rb', line 98 def set_block(x, y, z, id) # set_block @block_cache[[x, y, z]] = id @write_mutex.synchronize do @buffer += "B,#{x},#{y},#{z},#{id}\n" end end |
#set_light(x, y, z, light) ⇒ Object
141 142 143 144 145 |
# File 'lib/craft_client/server.rb', line 141 def set_light(x, y, z, light) @write_mutex.synchronize do @buffer += "L,#{x},#{y},#{z},#{light}\n" end end |
#set_position(x, y, z, rotate_x, rotate_y) ⇒ Object
135 136 137 138 139 |
# File 'lib/craft_client/server.rb', line 135 def set_position(x, y, z, rotate_x, rotate_y) @write_mutex.synchronize do @buffer += "P,#{x.to_f},#{y.to_f},#{z.to_f},#{rotate_x.to_f},#{rotate_y.to_f}\n" end end |
#set_sign(x, y, z, facing, text) ⇒ Object
129 130 131 132 133 |
# File 'lib/craft_client/server.rb', line 129 def set_sign(x, y, z, facing, text) @write_mutex.synchronize do @buffer += "S,#{x},#{y},#{z},#{facing},#{text}\n" end end |