1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
use std::fmt;
use std::io::{self, Write};
use crate::argument::Argument;
use crate::chunk::ChunkStream;
use crate::heights::HeightsStream;
use crate::response::{BufReader, ResponseStream};
use crate::{Block, Chunk, Coordinate, Coordinate2D, Heights, Result};
#[cfg(not(feature = "uds"))]
use std::net::TcpStream;
#[cfg(feature = "uds")]
use std::os::unix::net::UnixStream;
// `pub(crate)` so that `ResponseStream` can use it without being generic
#[cfg(not(feature = "uds"))]
pub(crate) type Stream = TcpStream;
#[cfg(feature = "uds")]
pub(crate) type Stream = UnixStream;
/// Connection for Minecraft server.
#[derive(Debug)]
pub struct Connection {
stream: Stream,
reader: BufReader<Stream>,
}
// TODO(feat): Add context to errors?
// TODO(doc): Rewrite documentation for functionality methods
impl Connection {
/// Default address and port for [ELCI] server.
///
/// [ELCI]: https://github.com/rozukke/elci
#[cfg(not(feature = "uds"))]
pub const DEFAULT_ADDRESS: &str = "127.0.0.1:4711";
/// Default unix socket path for [ELCI] server.
///
/// [ELCI]: https://github.com/rozukke/elci
#[cfg(feature = "uds")]
pub const DEFAULT_PATH: &str = "/tmp/elci-proxy";
/// Create a new connection with the ([default server address]).
///
/// [default server address]: Self::DEFAULT_ADDRESS
#[cfg(not(feature = "uds"))]
pub fn new() -> io::Result<Self> {
Self::from_stream(TcpStream::connect(Self::DEFAULT_ADDRESS)?)
}
/// Create a new connection with the ([default server path]).
///
/// [default server path]: Self::DEFAULT_PATH
#[cfg(feature = "uds")]
pub fn new() -> io::Result<Self> {
Self::from_stream(UnixStream::connect(Self::DEFAULT_PATH)?)
}
/// Create a new connection with a specified server address.
#[cfg(not(feature = "uds"))]
pub fn with_address(addr: impl std::net::ToSocketAddrs) -> io::Result<Self> {
Self::from_stream(TcpStream::connect(addr)?)
}
/// Create a new connection with a specified server path.
#[cfg(feature = "uds")]
pub fn with_path(path: impl AsRef<std::path::Path>) -> io::Result<Self> {
Self::from_stream(UnixStream::connect(path)?)
}
/// Create a new connection from a given [`Stream`].
fn from_stream(stream: Stream) -> io::Result<Self> {
let reader = BufReader::new(stream.try_clone()?);
Ok(Self { stream, reader })
}
/// Serialize and send a command to the server.
fn send<'a>(
&mut self,
command: &'static str,
arguments: impl AsRef<[Argument<'a>]>,
) -> Result<()> {
self.stream.write_fmt(format_args!("{}(", command))?;
for (i, arg) in arguments.as_ref().iter().enumerate() {
if i > 0 {
self.stream.write_fmt(format_args!(","))?;
}
self.stream.write_fmt(format_args!("{}", arg))?;
}
self.stream.write_fmt(format_args!(")\n"))?;
Ok(())
}
/// Creates a [`ResponseStream`] to read from the server.
fn recv(&mut self) -> Result<ResponseStream<'_>> {
ResponseStream::new(&mut self.reader)
}
/// Sends a message to the in-game chat.
///
/// Does **not** require that a player has joined.
pub fn post_to_chat(&mut self, message: impl fmt::Display) -> Result<()> {
self.send("chat.post", [Argument::Format(format_args!("{}", message))])
}
/// Performs an in-game Minecraft command.
///
/// Players have to exist on the server and should be server operators (default with [ELCI]).
///
/// [ELCI]: https://github.com/rozukke/elci
pub fn do_command(&mut self, command: impl fmt::Display) -> Result<()> {
self.send(
"player.doCommand",
[Argument::Format(format_args!("{}", command))],
)
}
/// Returns a [`Coordinate`] representing player position (block position of lower half of
/// playermodel).
pub fn get_player_position(&mut self) -> Result<Coordinate> {
self.send("player.getPos", [])?;
let mut response = self.recv()?;
let coord = response.final_coordinate()?;
Ok(coord)
}
/// Returns the coordinate location of the block the player is standing on (i.e. tile).
pub fn get_player_tile_position(&mut self) -> Result<Coordinate> {
let mut coord = self.get_player_position()?;
coord.y -= 1;
Ok(coord)
}
/// Sets player position (block position of lower half of playermodel) to specified
/// [`Coordinate`].
pub fn set_player_position(&mut self, position: impl Into<Coordinate>) -> Result<()> {
self.send("player.setPos", [Argument::Coordinate(position.into())])
}
/// Sets player position to be one above specified tile (i.e. tile = block player is standing
/// on).
pub fn set_player_tile_position(&mut self, position: impl Into<Coordinate>) -> Result<()> {
let mut position = position.into();
position.y += 1;
self.set_player_position(position)
}
/// Returns [`Block`] object from specified [`Coordinate`].
pub fn get_block(&mut self, location: impl Into<Coordinate>) -> Result<Block> {
self.send(
"world.getBlockWithData",
[Argument::Coordinate(location.into())],
)?;
let mut response = self.recv()?;
let block = response.final_block()?;
Ok(block)
}
/// Sets block at [`Coordinate`] to specified [`Block`].
pub fn set_block(
&mut self,
location: impl Into<Coordinate>,
block: impl Into<Block>,
) -> Result<()> {
self.send(
"world.setBlock",
[
Argument::Coordinate(location.into()),
Argument::Block(block.into()),
],
)
}
/// Returns the `y`-value of the highest solid block at the specified `x` and `z` coordinate
///
/// **DO NOT USE FOR LARGE AREAS, IT WILL BE VERY SLOW** -- use [`get_heights`] instead.
///
/// [`get_heights`]: Connection::get_heights
pub fn get_height(&mut self, location: impl Into<Coordinate2D>) -> Result<i32> {
self.send("world.getHeight", [Argument::Coordinate2D(location.into())])?;
let mut response = self.recv()?;
let height = response.final_i32()?;
Ok(height)
}
/// Sets a cuboid of blocks to all be the specified [`Block`], with the corners of the cuboid
/// specified by [`Coordinate`]s `corner_a` and `corner_b` (in any order).
pub fn set_blocks(
&mut self,
corner_a: impl Into<Coordinate>,
corner_b: impl Into<Coordinate>,
block: impl Into<Block>,
) -> Result<()> {
self.send(
"world.setBlocks",
[
Argument::Coordinate(corner_a.into()),
Argument::Coordinate(corner_b.into()),
Argument::Block(block.into()),
],
)
}
/// Returns a [`Chunk`] structure of the [`Block`]s in cuboid specified by [`Coordinate`]s
/// `corner_a` and `corner_b` (in any order).
///
/// Reads entire response and allocates [`Chunk`] structure. To read response as a stream, use
/// [`get_blocks_stream`] instead.
///
/// [`get_blocks_stream`]: Connection::get_blocks_stream
// TODO(rename): get_chunk
pub fn get_blocks(
&mut self,
corner_a: impl Into<Coordinate>,
corner_b: impl Into<Coordinate>,
) -> Result<Chunk> {
self.get_blocks_stream(corner_a, corner_b)?.collect()
}
/// Returns a [`Chunk`] structure of the [`Block`]s in cuboid specified by [`Coordinate`]s
/// `corner_a` and `corner_b` (in any order).
///
/// Reads response as a stream to avoid unneccessary allocation. See also: [`get_blocks`].
///
/// [`get_blocks`]: Connection::get_blocks
// TODO(rename): get_chunk_stream
pub fn get_blocks_stream(
&mut self,
corner_a: impl Into<Coordinate>,
corner_b: impl Into<Coordinate>,
) -> Result<ChunkStream<'_>> {
let corner_a = corner_a.into();
let corner_b = corner_b.into();
self.send(
"world.getBlocksWithData",
[
Argument::Coordinate(corner_a),
Argument::Coordinate(corner_b),
],
)?;
let response = self.recv()?;
let chunk = ChunkStream::new(corner_a, corner_b, response);
Ok(chunk)
}
/// Returns a [`Heights`] structure of y-values in rectangle specified by [`Coordinate2D`]s
/// `corner_a` and `corner_b` (in any order).
///
/// Reads entire response and allocates [`Heights`] structure. To read response as a stream, use
/// [`get_heights_stream`] instead.
///
/// [`get_heights_stream`]: Connection::get_heights_stream
pub fn get_heights(
&mut self,
corner_a: impl Into<Coordinate2D>,
corner_b: impl Into<Coordinate2D>,
) -> Result<Heights> {
self.get_heights_stream(corner_a, corner_b)?.collect()
}
/// Returns a [`Heights`] structure of y-values in rectangle specified by [`Coordinate2D`]s
/// `corner_a` and `corner_b` (in any order).
///
/// Reads response as a stream to avoid unneccessary allocation. See also: [`get_heights`].
///
/// [`get_heights`]: Connection::get_heights
pub fn get_heights_stream(
&mut self,
corner_a: impl Into<Coordinate2D>,
corner_b: impl Into<Coordinate2D>,
) -> Result<HeightsStream<'_>> {
let corner_a = corner_a.into();
let corner_b = corner_b.into();
self.send(
"world.getHeights",
[
Argument::Coordinate2D(corner_a),
Argument::Coordinate2D(corner_b),
],
)?;
let response = self.recv()?;
let heights = HeightsStream::new(corner_a, corner_b, response);
Ok(heights)
}
}