-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added `/vanish` command for admins. --------- Co-authored-by: Andrew Gazelka <andrew.gazelka@gmail.com>
- Loading branch information
1 parent
e272fa0
commit 37ad0fd
Showing
10 changed files
with
164 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use clap::Parser; | ||
use flecs_ecs::{ | ||
core::{Entity, EntityView, EntityViewGet, WorldProvider}, | ||
prelude::*, | ||
}; | ||
use hyperion::net::{Compose, ConnectionId}; | ||
use hyperion_clap::{CommandPermission, MinecraftCommand}; | ||
|
||
use crate::module::vanish::Vanished; | ||
|
||
#[derive(Parser, CommandPermission, Debug)] | ||
#[command(name = "vanish")] | ||
#[command_permission(group = "Admin")] | ||
pub struct VanishCommand; | ||
|
||
impl MinecraftCommand for VanishCommand { | ||
fn execute(self, system: EntityView<'_>, caller: Entity) { | ||
let world = system.world(); | ||
|
||
world.get::<&Compose>(|compose| { | ||
caller.entity_view(world).get::<( | ||
Option<&Vanished>, | ||
&ConnectionId, | ||
&hyperion::simulation::Name, | ||
)>(|(vanished, stream, name)| { | ||
let is_vanished = vanished.is_some_and(Vanished::is_vanished); | ||
let caller = caller.entity_view(world); | ||
if is_vanished { | ||
caller.set(Vanished::new(false)); | ||
let packet = hyperion::net::agnostic::chat(format!( | ||
"§7[Admin] §f{name} §7is now visible", | ||
)); | ||
compose.unicast(&packet, *stream, system).unwrap(); | ||
} else { | ||
caller.set(Vanished::new(true)); | ||
let packet = hyperion::net::agnostic::chat(format!( | ||
"§7[Admin] §f{name} §7is now vanished", | ||
)); | ||
compose.unicast(&packet, *stream, system).unwrap(); | ||
} | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ pub mod level; | |
pub mod regeneration; | ||
pub mod spawn; | ||
pub mod stats; | ||
pub mod vanish; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use flecs_ecs::{core::World, prelude::*}; | ||
use hyperion::{ | ||
net::{Compose, ConnectionId}, | ||
simulation::{Uuid, metadata::entity::EntityFlags}, | ||
}; | ||
use valence_protocol::packets::play::{self, player_list_s2c::PlayerListActions}; | ||
use valence_server::GameMode; | ||
|
||
#[derive(Component)] | ||
pub struct VanishModule; | ||
|
||
#[derive(Default, Component, Debug)] | ||
pub struct Vanished(pub bool); | ||
|
||
impl Vanished { | ||
#[must_use] | ||
pub const fn new(is_vanished: bool) -> Self { | ||
Self(is_vanished) | ||
} | ||
|
||
#[must_use] | ||
pub const fn is_vanished(&self) -> bool { | ||
self.0 | ||
} | ||
} | ||
|
||
impl Module for VanishModule { | ||
fn module(world: &World) { | ||
world.component::<Vanished>(); | ||
|
||
system!( | ||
"vanish_sync", | ||
world, | ||
&Compose($), | ||
&ConnectionId, | ||
&Vanished, | ||
&Uuid, | ||
) | ||
.multi_threaded() | ||
.kind::<flecs::pipeline::PreStore>() | ||
.each_iter(move |it, row, (compose, _connection_id, vanished, uuid)| { | ||
let entity = it.entity(row); | ||
let system = it.system(); | ||
let world = it.world(); | ||
|
||
if vanished.is_vanished() { | ||
// Remove from player list and make them invisible | ||
let remove_packet = play::PlayerListS2c { | ||
actions: PlayerListActions::new() | ||
.with_update_listed(true) | ||
.with_update_game_mode(true), | ||
entries: vec![play::player_list_s2c::PlayerListEntry { | ||
player_uuid: uuid.0, | ||
listed: false, | ||
game_mode: GameMode::Survival, | ||
..Default::default() | ||
}] | ||
.into(), | ||
}; | ||
compose.broadcast(&remove_packet, system).send().unwrap(); | ||
|
||
// Set entity flags to make them invisible | ||
let flags = EntityFlags::INVISIBLE; | ||
entity.entity_view(world).set(flags); | ||
} else { | ||
// Add back to player list and make them visible | ||
let add_packet = play::PlayerListS2c { | ||
actions: PlayerListActions::new() | ||
.with_update_listed(true) | ||
.with_update_game_mode(true), | ||
entries: vec![play::player_list_s2c::PlayerListEntry { | ||
player_uuid: uuid.0, | ||
listed: true, | ||
game_mode: GameMode::Survival, | ||
..Default::default() | ||
}] | ||
.into(), | ||
}; | ||
compose.broadcast(&add_packet, system).send().unwrap(); | ||
|
||
// Clear invisible flag | ||
let flags = EntityFlags::default(); | ||
entity.entity_view(world).set(flags); | ||
} | ||
}); | ||
} | ||
} |