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

Add /plot visit [player] command #38

Merged
merged 2 commits into from
May 9, 2021
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
43 changes: 40 additions & 3 deletions src/plot/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::time::{Duration, Instant, SystemTime};

impl Plot {
/// Handles a command that starts with `/plot` or `/p`
fn handle_plot_command(&mut self, player: usize, command: &str, _args: Vec<&str>) {
fn handle_plot_command(&mut self, player: usize, command: &str, args: Vec<&str>) {
let plot_x = self.players[player].x as i32 >> 8;
let plot_z = self.players[player].z as i32 >> 8;
match command {
Expand Down Expand Up @@ -51,6 +51,19 @@ impl Plot {
let center = Plot::get_center(plot_x, plot_z);
self.players[player].teleport(center.0, 64.0, center.1);
}
"visit" | "v" => {
if args.is_empty() {
self.players[player].send_error_message("Invalid number of arguments!");
return;
}
let plot = database::get_owned_plot(args[0]);
if let Some((plot_x, plot_z)) = plot {
let center = Plot::get_center(plot_x, plot_z);
self.players[player].teleport(center.0, 64.0, center.1);
} else {
self.players[player].send_system_message(&format!("{} does not own any plots.", args[0]));
}
}
_ => self.players[player].send_error_message("Invalid argument for /plot"),
}
}
Expand Down Expand Up @@ -347,7 +360,7 @@ lazy_static! {
// 6: /plot
Node {
flags: (CommandFlags::LITERAL).bits() as i8,
children: vec![7, 8, 9, 10, 38, 39, 40],
children: vec![7, 8, 9, 10, 38, 39, 40, 41, 43],
redirect_node: None,
name: Some("plot"),
parser: None,
Expand Down Expand Up @@ -616,14 +629,38 @@ lazy_static! {
name: Some("a"),
parser: None,
},
// 40: /p auto
// 40: /p middle
Comment on lines -619 to +632
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of scope ;) I'll let it slide haha

Node {
flags: (CommandFlags::LITERAL | CommandFlags::EXECUTABLE).bits() as i8,
children: vec![],
redirect_node: None,
name: Some("middle"),
parser: None,
},
// 41: /p visit
Node {
flags: (CommandFlags::LITERAL).bits() as i8,
children: vec![42],
redirect_node: None,
name: Some("visit"),
parser: None,
},
// 42: /p visit [player]
Node {
flags: (CommandFlags::ARGUMENT | CommandFlags::EXECUTABLE).bits() as i8,
children: vec![],
redirect_node: None,
name: Some("player"),
parser: Some(Parser::Entity(3)),
},
// 43: /p v
Node {
flags: (CommandFlags::LITERAL | CommandFlags::REDIRECT).bits() as i8,
children: vec![],
redirect_node: Some(41),
name: Some("v"),
parser: None,
},
],
root_index: 0
}.encode();
Expand Down
20 changes: 20 additions & 0 deletions src/plot/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ pub fn get_cached_username(uuid: String) -> Option<String> {
.ok()
}

pub fn get_owned_plot(player: &str) -> Option<(i32, i32)> {
lock()
.query_row(
"SELECT
plot_x, plot_z
FROM
plot
JOIN
userplot ON userplot.plot_id = plot.id
JOIN
user ON user.id = userplot.user_id
WHERE
name=?1
AND is_owner=TRUE",
params![player],
|row| Ok((row.get(0)?, row.get(1)?)),
)
.ok()
}

pub fn is_claimed(plot_x: i32, plot_z: i32) -> Option<bool> {
lock()
.query_row(
Expand Down