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

Support for custom tablist header and footer #513

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions feather/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ uuid = { version = "0.8", features = [ "v4" ] }
libcraft-core = { path = "../../libcraft/core" }
libcraft-inventory = { path = "../../libcraft/inventory" }
libcraft-items = { path = "../../libcraft/items" }
libcraft-text = { path = "../../libcraft/text" }
rayon = "1.5"
worldgen = { path = "../worldgen", package = "feather-worldgen" }
rand = "0.8"
1 change: 1 addition & 0 deletions feather/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ uuid = "0.8"
slab = "0.4"
libcraft-core = { path = "../../libcraft/core" }
libcraft-items = { path = "../../libcraft/items" }
libcraft-text = { path = "../../libcraft/text" }
worldgen = { path = "../worldgen", package = "feather-worldgen" }

[features]
Expand Down
2 changes: 2 additions & 0 deletions feather/server/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ motd = "A Feather server"
max_players = 16
default_gamemode = "creative"
view_distance = 12
tablist_header = "A Feather Server"
tablist_footer = ""

[log]
# If you prefer less verbose logs, switch this to "info".
Expand Down
13 changes: 12 additions & 1 deletion feather/server/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ use common::{
Window,
};
use libcraft_items::InventorySlot;
use packets::server::{Particle, SetSlot, SpawnLivingEntity, UpdateLight, WindowConfirmation};
use packets::server::{
Particle, PlayerListHeaderAndFooter, SetSlot, SpawnLivingEntity, UpdateLight,
WindowConfirmation,
};
use protocol::packets::server::{
ChangeGameState, EntityPosition, EntityPositionAndRotation, EntityTeleport, GameStateChange,
HeldItemChange, PlayerAbilities,
Expand Down Expand Up @@ -332,6 +335,14 @@ impl Client {
self.send_packet(PlayerInfo::RemovePlayers(vec![uuid]));
}

pub fn send_tablist_header_footer(&self, header: &str, footer: &str) {
log::trace!("Sending PlayerListHeaderAndFooter ({},{})", header, footer);
self.send_packet(PlayerListHeaderAndFooter {
header: header.to_string(),
footer: footer.to_string(),
})
}

pub fn change_player_tablist_gamemode(&self, uuid: Uuid, gamemode: Gamemode) {
self.send_packet(PlayerInfo::UpdateGamemodes(vec![(uuid, gamemode)]));
}
Expand Down
4 changes: 3 additions & 1 deletion feather/server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::{fs, net::IpAddr, path::Path, str::FromStr};

use anyhow::Context;
use base::Gamemode;
use base::{Gamemode, Text};
use serde::{Deserialize, Deserializer};

use crate::{favicon::Favicon, Options};
Expand Down Expand Up @@ -90,6 +90,8 @@ pub struct ServerConfig {
pub max_players: u32,
pub default_gamemode: Gamemode,
pub view_distance: u32,
pub tablist_header: Text,
pub tablist_footer: Text,
}

#[derive(Debug, Deserialize)]
Expand Down
12 changes: 7 additions & 5 deletions feather/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,26 @@ async fn main() -> anyhow::Result<()> {
let options = config.to_options();
let server = Server::bind(options).await?;

let game = init_game(server, &config)?;
let game = init_game(server, config)?;

run(game);

Ok(())
}

fn init_game(server: Server, config: &Config) -> anyhow::Result<Game> {
fn init_game(server: Server, config: Config) -> anyhow::Result<Game> {
let mut game = Game::new();
init_systems(&mut game, server);
init_world_source(&mut game, config);
init_world_source(&mut game, &config);
init_systems(&mut game, server, config);
init_plugin_manager(&mut game)?;
Ok(game)
}

fn init_systems(game: &mut Game, server: Server) {
fn init_systems(game: &mut Game, server: Server, config: Config) {
let mut systems = SystemExecutor::new();

game.insert_resource(config);

// Register common before server code, so
// that packet broadcasting happens after
// gameplay actions.
Expand Down
2 changes: 1 addition & 1 deletion feather/server/src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn register(server: Server, game: &mut Game, systems: &mut SystemExecutor<Ga
view::register(game, systems);
crate::chunk_subscriptions::register(systems);
player_leave::register(systems);
tablist::register(systems);
tablist::register(game, systems);
block::register(systems);
entity::register(game, systems);
chat::register(game, systems);
Expand Down
50 changes: 48 additions & 2 deletions feather/server/src/systems/tablist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,32 @@ use uuid::Uuid;
use base::{Gamemode, ProfileProperty};
use common::Game;
use ecs::{SysResult, SystemExecutor};
use quill_common::events::{EntityRemoveEvent, GamemodeEvent, PlayerJoinEvent};
use quill_common::events::{
EntityRemoveEvent, GamemodeEvent, PlayerJoinEvent, TablistExtrasUpdateEvent,
};
use quill_common::tablist::TablistHeaderFooter;
use quill_common::{components::Name, entities::Player};

use crate::config::Config;
use crate::{ClientId, Server};

pub fn register(systems: &mut SystemExecutor<Game>) {
pub fn register(game: &mut Game, systems: &mut SystemExecutor<Game>) {
let (header, footer) = {
let server_config = &game.resources.get::<Config>().unwrap().server;
(
server_config.tablist_header.clone(),
server_config.tablist_footer.clone(),
)
};

game.insert_resource(TablistHeaderFooter { header, footer });

systems
.group::<Server>()
.add_system(remove_tablist_players)
.add_system(add_tablist_players)
.add_system(update_tablist_header_footer)
.add_system(send_tablist_header_footer_on_join)
.add_system(change_tablist_player_gamemode);
}

Expand Down Expand Up @@ -63,6 +79,36 @@ fn add_tablist_players(game: &mut Game, server: &mut Server) -> SysResult {
Ok(())
}

/// Updates tablist header and footer when [TablistExtrasUpdateEvent] is raised
fn update_tablist_header_footer(game: &mut Game, server: &mut Server) -> SysResult {
for _ in game.ecs.query::<&TablistExtrasUpdateEvent>().iter() {
let header_footer = game.resources.get::<TablistHeaderFooter>()?;
server.broadcast_with(|client| {
client.send_tablist_header_footer(
&header_footer.header.to_string(),
&header_footer.footer.to_string(),
)
});
}
Ok(())
}

/// Sends tablist header and footer to players who join
fn send_tablist_header_footer_on_join(game: &mut Game, server: &mut Server) -> SysResult {
for (_, (_, &client_id)) in game.ecs.query::<(&PlayerJoinEvent, &ClientId)>().iter() {
let header_footer = game.resources.get::<TablistHeaderFooter>()?;
server
.clients
.get(client_id)
.unwrap()
.send_tablist_header_footer(
&header_footer.header.to_string(),
&header_footer.footer.to_string(),
);
}
Ok(())
}

fn change_tablist_player_gamemode(game: &mut Game, server: &mut Server) -> SysResult {
for (_, (event, &uuid)) in game.ecs.query::<(&GamemodeEvent, &Uuid)>().iter() {
// Change this player's gamemode in players' tablists
Expand Down
2 changes: 2 additions & 0 deletions quill/common/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ host_component_enum! {
FlyingAbilityEvent = 1028,
BuildingAbilityEvent = 1029,
InvulnerabilityEvent = 1030,
TablistExtrasUpdateEvent = 1031,
}
}

Expand Down Expand Up @@ -377,3 +378,4 @@ bincode_component_impl!(InstabreakEvent);
bincode_component_impl!(FlyingAbilityEvent);
bincode_component_impl!(BuildingAbilityEvent);
bincode_component_impl!(InvulnerabilityEvent);
bincode_component_impl!(TablistExtrasUpdateEvent);
2 changes: 1 addition & 1 deletion quill/common/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub use block_interact::{BlockInteractEvent, BlockPlacementEvent};
pub use change::{
BuildingAbilityEvent, CreativeFlyingEvent, FlyingAbilityEvent, GamemodeEvent, InstabreakEvent,
InvulnerabilityEvent, SneakEvent, SprintEvent,
InvulnerabilityEvent, SneakEvent, SprintEvent, TablistExtrasUpdateEvent,
};
pub use entity::{EntityCreateEvent, EntityRemoveEvent, PlayerJoinEvent};
pub use interact_entity::InteractEntityEvent;
Expand Down
4 changes: 4 additions & 0 deletions quill/common/src/events/change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ pub struct BuildingAbilityEvent(pub bool);
/// This event is called when player's invulnerability property changes.
#[derive(Debug, Serialize, Deserialize, Clone, Deref)]
pub struct InvulnerabilityEvent(pub bool);

/// Raise this event to resend tablist extras to all players
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct TablistExtrasUpdateEvent;
1 change: 1 addition & 0 deletions quill/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod entities;
pub mod entity;
pub mod entity_init;
pub mod events;
pub mod tablist;

use std::marker::PhantomData;

Expand Down
7 changes: 7 additions & 0 deletions quill/common/src/tablist.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use libcraft_text::Text;

#[derive(Debug, Clone)]
pub struct TablistHeaderFooter {
pub header: Text,
pub footer: Text,
}