Skip to content

Commit e8f3a43

Browse files
committed
Reduce proxies to one struct.
Make proxy methods return opaque RPIT types that are implemented by Proxy.
1 parent e9dfa13 commit e8f3a43

File tree

9 files changed

+292
-247
lines changed

9 files changed

+292
-247
lines changed

src/minecraft.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ mod game_mode;
3030
#[cfg(feature = "minecraft-java-edition")]
3131
pub mod java_edition;
3232
mod negate;
33+
mod proxy;
3334
mod range;
3435
mod resource_location;
3536
mod serialize;

src/minecraft/bedrock_edition.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ mod camera;
44

55
use std::future::Future;
66

7+
use camera::Camera;
78
pub use camera::Target;
89

910
use crate::minecraft::java_edition::TargetSelector;
11+
use crate::minecraft::proxy::Proxy;
1012
use crate::minecraft::{parse_response, Entity, Error, Serialize};
1113
use crate::Minecraft;
1214

@@ -19,7 +21,7 @@ pub trait BedrockEdition: Minecraft {
1921
fn always_day(&mut self, lock: bool) -> impl Future<Output = Result<String, Error>> + Send;
2022

2123
/// Modify the player's camera view.
22-
fn camera(&mut self, target: Entity<TargetSelector>) -> camera::Proxy<'_, Self>
24+
fn camera(&mut self, target: Entity<TargetSelector>) -> impl Camera<'_>
2325
where
2426
Self: Sized + Send;
2527
}
@@ -42,7 +44,7 @@ where
4244
.and_then(parse_response)
4345
}
4446

45-
fn camera(&mut self, target: Entity<TargetSelector>) -> camera::Proxy<'_, Self> {
46-
camera::Proxy::new(self, vec![target.serialize()])
47+
fn camera(&mut self, target: Entity<TargetSelector>) -> impl Camera<'_> {
48+
Proxy::new(self, vec![target.serialize()])
4749
}
4850
}
Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,55 @@
11
use std::borrow::Cow;
2+
use std::future::Future;
23

34
pub use color::Color;
5+
use set::Set;
46
pub use set::Target;
57
pub use time::Time;
68

9+
use crate::minecraft::proxy::Proxy;
710
use crate::minecraft::{Error, Serialize};
811
use crate::RCon;
912

1013
mod color;
1114
mod set;
1215
mod time;
1316

14-
/// Camera actions proxy.
15-
#[derive(Debug)]
16-
pub struct Proxy<'client, T> {
17-
client: &'client mut T,
18-
args: Vec<Cow<'client, str>>,
19-
}
17+
/// Camera-related operations.
18+
pub trait Camera<'client> {
19+
/// Return a proxy with available set options.
20+
fn set(self, preset: Cow<'client, str>) -> impl Set;
2021

21-
impl<'client, T> Proxy<'client, T> {
22-
pub(crate) const fn new(client: &'client mut T, args: Vec<Cow<'client, str>>) -> Self {
23-
Self { client, args }
24-
}
22+
/// Clear the camera view.
23+
fn clear(&mut self) -> impl Future<Output = Result<String, Error>> + Send;
2524

26-
/// Return a proxy with available set options.
27-
pub fn set(mut self, preset: Cow<'client, str>) -> set::Proxy<'client, T> {
28-
self.args.extend([Cow::Borrowed("set"), preset]);
29-
set::Proxy::new(self.client, self.args)
30-
}
25+
/// Fade camera view.
26+
fn fade(
27+
&mut self,
28+
color: Color,
29+
time: Option<Time>,
30+
) -> impl Future<Output = Result<String, Error>> + Send;
3131
}
3232

33-
impl<T> Proxy<'_, T>
33+
impl<'client, T> Camera<'client> for Proxy<'client, T>
3434
where
3535
T: RCon + Send,
3636
{
37-
/// Clear the camera view.
38-
pub async fn clear(mut self) -> Result<String, Error> {
39-
self.args.push(Cow::Borrowed("clear"));
40-
self.client
41-
.run_utf8(self.args.join(" "))
42-
.await
43-
.map_err(Into::into)
37+
fn set(self, preset: Cow<'client, str>) -> impl Set {
38+
self.delegate(&[Cow::Borrowed("set"), preset])
4439
}
4540

46-
/// Fade camera view.
47-
pub async fn fade(self, color: Color, time: Option<Time>) -> Result<String, Error> {
41+
async fn clear(&mut self) -> Result<String, Error> {
42+
self.run_utf8(&["clear".into()]).await
43+
}
44+
45+
async fn fade(&mut self, color: Color, time: Option<Time>) -> Result<String, Error> {
4846
let mut args = vec![Cow::Borrowed("fade")];
4947

5048
if let Some(time) = time {
5149
args.push(time.serialize());
5250
}
5351

5452
args.push(color.serialize());
55-
self.client
56-
.run_utf8(args.join(" "))
57-
.await
58-
.map_err(Into::into)
53+
self.run_utf8(&args).await
5954
}
6055
}
Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,49 @@
11
use std::borrow::Cow;
2+
use std::future::Future;
23

34
pub use target::Target;
45

6+
use crate::minecraft::proxy::Proxy;
57
use crate::minecraft::util::EscapeString;
68
use crate::minecraft::{Error, Serialize};
79
use crate::RCon;
810

911
mod target;
1012

11-
/// Camera actions proxy.
12-
#[derive(Debug)]
13-
pub struct Proxy<'client, T> {
14-
client: &'client mut T,
15-
args: Vec<Cow<'client, str>>,
16-
}
13+
/// Set camera settings.
14+
pub trait Set {
15+
/// Set the default value.
16+
///
17+
/// TODO: investigate what this actually is. The wiki is quite sparse on this.
18+
///
19+
/// See: <https://minecraft.fandom.com/wiki/Commands/camera>
20+
fn default(
21+
&mut self,
22+
default: Option<Cow<'_, str>>,
23+
) -> impl Future<Output = Result<String, Error>>;
1724

18-
impl<'client, T> Proxy<'client, T> {
19-
pub(crate) const fn new(client: &'client mut T, args: Vec<Cow<'client, str>>) -> Self {
20-
Self { client, args }
21-
}
25+
/// Set camera facing.
26+
fn facing(&mut self, target: Target) -> impl Future<Output = Result<String, Error>>;
27+
28+
// TODO: Implement further sub-commands.
2229
}
2330

24-
impl<T> Proxy<'_, T>
31+
impl<T> Set for Proxy<'_, T>
2532
where
2633
T: RCon + Send,
2734
{
28-
/// Set the default value.
29-
///
30-
/// TODO: investigate what this actually is. The wiki is quite sparse on this.
31-
///
32-
/// See: <https://minecraft.fandom.com/wiki/Commands/camera>
33-
pub async fn default(mut self, default: Option<Cow<'_, str>>) -> Result<String, Error> {
34-
self.args.push(Cow::Borrowed("default"));
35+
async fn default(&mut self, default: Option<Cow<'_, str>>) -> Result<String, Error> {
36+
let mut args = vec![Cow::Borrowed("default")];
3537

3638
if let Some(default) = default {
37-
self.args.push(default.quote().into());
39+
args.push(default.quote().into());
3840
}
3941

40-
self.client
41-
.run_utf8(self.args.join(" "))
42-
.await
43-
.map_err(Into::into)
42+
self.run_utf8(&args).await
4443
}
4544

46-
/// Set camera facing.
47-
pub async fn facing(mut self, target: Target) -> Result<String, Error> {
48-
self.args
49-
.extend([Cow::Borrowed("facing"), target.serialize()]);
50-
self.client
51-
.run_utf8(self.args.join(" "))
45+
async fn facing(&mut self, target: Target) -> Result<String, Error> {
46+
self.run_utf8(&[Cow::Borrowed("facing"), target.serialize()])
5247
.await
53-
.map_err(Into::into)
5448
}
55-
56-
// TODO: Implement further sub-commands.
5749
}

src/minecraft/java_edition.rs

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
use std::borrow::Cow;
44
use std::future::Future;
55

6+
use advancement::Advancement;
67
pub use advancement::Grant;
8+
use attribute::Attribute;
9+
use bossbar::Bossbar;
710
pub use target_selector::{Argument, Sort, TargetSelector};
811

12+
use crate::minecraft::proxy::Proxy;
913
use crate::minecraft::{Entity, ResourceLocation, Serialize};
1014
use crate::Minecraft;
1115

@@ -23,38 +27,21 @@ pub trait JavaEdition: Minecraft {
2327
///
2428
/// # Returns
2529
///
26-
/// Returns an [`advancement::Proxy`] which can be used to execute
30+
/// Returns an [`Proxy`] which can be used to execute
2731
/// advancement-related commands pertaining to the `target`.
28-
fn advancement(&mut self, target: Entity<TargetSelector>) -> advancement::Proxy<'_, Self>
29-
where
30-
Self: Sized,
31-
{
32-
advancement::Proxy::new(self, vec!["advancement".into(), target.serialize()])
33-
}
32+
fn advancement(&mut self, target: Entity<TargetSelector>) -> impl Advancement;
3433

3534
/// Manage a target's attribute.
3635
///
3736
/// # Returns
3837
///
39-
/// Returns an [`attribute::Proxy`] which can be used to execute
38+
/// Returns an [`Proxy`] which can be used to execute
4039
/// advancement-related commands pertaining to the `target`.
4140
fn attribute(
4241
&mut self,
4342
target: Entity<TargetSelector>,
4443
attribute: ResourceLocation,
45-
) -> attribute::Proxy<'_, Self>
46-
where
47-
Self: Sized,
48-
{
49-
attribute::Proxy::new(
50-
self,
51-
vec![
52-
"attribute".into(),
53-
target.serialize(),
54-
attribute.serialize(),
55-
],
56-
)
57-
}
44+
) -> impl Attribute;
5845

5946
/// Adds player to banlist.
6047
///
@@ -93,24 +80,38 @@ pub trait JavaEdition: Minecraft {
9380
entry_type: Option<banlist::EntryType>,
9481
) -> impl Future<Output = Result<Vec<banlist::Entry>, banlist::Error>> + Send;
9582

96-
/// Creates, modifies and lists bossbars.
97-
fn bossbar(&mut self) -> bossbar::Proxy<'_, Self>
98-
where
99-
Self: Sized,
100-
{
101-
bossbar::Proxy::new(self, vec!["bossbar".into()])
102-
}
83+
/// Manage bossbars.
84+
///
85+
/// # Returns
86+
///
87+
/// Returns an [`Proxy`] which can be used to execute
88+
/// bossbar-related commands.
89+
fn bossbar(&mut self) -> impl Bossbar;
10390
}
10491

10592
impl<T> JavaEdition for T
10693
where
10794
T: Minecraft + Send,
10895
{
109-
/// Adds player to banlist.
110-
///
111-
/// # Errors
112-
///
113-
/// Returns an [`ban::Error`] on errors.
96+
fn advancement(&mut self, target: Entity<TargetSelector>) -> impl Advancement {
97+
Proxy::new(self, vec!["advancement".into(), target.serialize()])
98+
}
99+
100+
fn attribute(
101+
&mut self,
102+
target: Entity<TargetSelector>,
103+
attribute: ResourceLocation,
104+
) -> impl Attribute {
105+
Proxy::new(
106+
self,
107+
vec![
108+
"attribute".into(),
109+
target.serialize(),
110+
attribute.serialize(),
111+
],
112+
)
113+
}
114+
114115
async fn ban(
115116
&mut self,
116117
target: Entity<TargetSelector>,
@@ -125,14 +126,6 @@ where
125126
ban::parse_response(&self.run_utf8(args.join(" ")).await?)
126127
}
127128

128-
/// Adds IP address to banlist.
129-
///
130-
/// Specifies the IP address to be added to the blacklist.
131-
/// Can also be a name of an online player, which represents the IP of that player.
132-
///
133-
/// # Errors
134-
///
135-
/// Returns an [`ban_ip::Error`] on errors.
136129
async fn ban_ip<S>(
137130
&mut self,
138131
target: ban_ip::Target,
@@ -150,11 +143,6 @@ where
150143
ban_ip::parse_response(&self.run_utf8(args.join(" ")).await?)
151144
}
152145

153-
/// Return the entries from the ban list.
154-
///
155-
/// # Errors
156-
///
157-
/// Returns an [`banlist::Error`] on errors.
158146
async fn banlist(
159147
&mut self,
160148
entry_type: Option<banlist::EntryType>,
@@ -167,4 +155,8 @@ where
167155

168156
banlist::parse_response(&self.run_utf8(args.join(" ")).await?)
169157
}
158+
159+
fn bossbar(&mut self) -> impl Bossbar {
160+
Proxy::new(self, vec!["bossbar".into()])
161+
}
170162
}

0 commit comments

Comments
 (0)