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

Implement relative teleportation to blocks and plots #66

Merged
merged 2 commits into from
May 17, 2022
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ MCHPRS will generate a `Config.toml` file in the current working directory when

### LuckPerms

MCHPRS has basic support for LuckPerms with MySQL or MariaDB remote database storage. This implementation has no commands or interface and would have to be manged through LuckPerms running on a proxy (`/lpb`) or other server (`/lp`)
MCHPRS has basic support for LuckPerms with MySQL or MariaDB remote database storage. This implementation has no commands or interface and would have to be manged through LuckPerms running on a proxy (`/lpb`) or other server (`/lp`)

To use LuckPerms, append this to your `Config.toml`:

Expand All @@ -67,6 +67,7 @@ server_context = "global"
| `/rtps [rtps]` | None | Set the **redstone** ticks per second in the plot to `[rtps]`. (There are two game ticks in a redstone tick) |
| `/radvance [ticks]` | `/radv` | Advances the plot by `[ticks]` redstone ticks. |
| `/teleport [player]` | `/tp` | Teleports you to `[player]`. |
| `/teleport [x] [y] [z]` | `/tp` | Teleports you to `[x] [y] [z]`. Supports relative coordinates. Floats can be expressed as described [here](https://doc.rust-lang.org/std/primitive.f64.html#grammar). |
| `/speed [speed]` | None | Sets your flyspeed. |
| `/gamemode [mode]` | `/gmc`, `/gmsp` | Sets your gamemode. |
| `/container [type] [power]` | None | Gives you a container (e.g. barrel) which outputs a specified amount of power when used with a comparator. |
Expand All @@ -82,7 +83,7 @@ These are the commands that are currently implemented:
| `/plot auto` | `/p a` | Automatically finds an unclaimed plot and claims. |
| `/plot middle` | None | Teleports you to the center of the plot you are in. |
| `/plot visit [player]` | `/p v` | Teleports you to a player's plot. |
| `/plot tp [x] [z]` | `/p v` | Teleports you to a plot at (x, z). |
| `/plot tp [x] [z]` | `/p v` | Teleports you to the plot at `[x] [y]`. Supports relative coordinates. |
| `/plot lock` | None | Locks the player into the plot so moving outside of the plot bounds does not transfer you to other plots. |
| `/plot unlock` | None | Reverses the locking done by `/plot lock`. |

Expand Down
34 changes: 24 additions & 10 deletions src/plot/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ use bitflags::_core::i32::MAX;
use log::{debug, info, warn};
use std::lazy::SyncLazy;
use std::time::{Duration, Instant};
use std::str::FromStr;
use std::ops::Add;

// Parses a relative or absolute coordinate relative to a reference coordinate
fn parse_relative_coord<F: FromStr + Add + Add<Output = F>>(coord: &str, ref_coord: F) -> Result<F, <F as FromStr>::Err> {
if coord == "~" {
Ok(ref_coord)
} else if coord.starts_with("~") {
coord[1..].parse::<F>().map(|x| ref_coord + x)
} else {
coord.parse::<F>()
}
}

impl Plot {
/// Handles a command that starts with `/plot` or `/p`
Expand Down Expand Up @@ -112,22 +125,22 @@ impl Plot {
return;
}

let plot_x;
let plot_z;
if let Ok(x_arg) = args[0].parse() {
plot_x = x_arg;
let new_plot_x;
let new_plot_z;
if let Ok(x_arg) = parse_relative_coord(args[0], plot_x) {
new_plot_x = x_arg;
} else {
self.players[player].send_error_message("Unable to parse x coordinate!");
return;
}
if let Ok(z_arg) = args[1].parse() {
plot_z = z_arg;
if let Ok(z_arg) = parse_relative_coord(args[1], plot_z) {
new_plot_z = z_arg;
} else {
self.players[player].send_error_message("Unable to parse z coordinate!");
return;
}

let center = Plot::get_center(plot_x, plot_z);
let center = Plot::get_center(new_plot_x, new_plot_z);
self.players[player].teleport(PlayerPos::new(center.0, 64.0, center.1));
}
"lock" => {
Expand Down Expand Up @@ -321,22 +334,23 @@ impl Plot {
}
"/teleport" | "/tp" => {
if args.len() == 3 {
let player_pos = self.players[player].pos;
let x;
let y;
let z;
if let Ok(x_arg) = args[0].parse::<f64>() {
if let Ok(x_arg) = parse_relative_coord(args[0], player_pos.x) {
x = x_arg;
} else {
self.players[player].send_error_message("Unable to parse x coordinate!");
return false;
}
if let Ok(y_arg) = args[1].parse::<f64>() {
if let Ok(y_arg) = parse_relative_coord(args[1], player_pos.y) {
y = y_arg;
} else {
self.players[player].send_error_message("Unable to parse y coordinate!");
return false;
}
if let Ok(z_arg) = args[2].parse::<f64>() {
if let Ok(z_arg) = parse_relative_coord(args[2], player_pos.z) {
z = z_arg;
} else {
self.players[player].send_error_message("Unable to parse z coordinate!");
Expand Down