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 16 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"
3 changes: 3 additions & 0 deletions feather/common/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ pub struct ChunkLoadEvent {
pub struct ChunkLoadFailEvent {
pub position: ChunkPosition,
}

#[derive(Debug)]
pub struct TablistExtrasUpdateEvent;
Copy link
Contributor

Choose a reason for hiding this comment

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

#522 is now merged, so quill-compatible events should be in quill/common/srs/events/change.rs, implement Serialize, Deserialize, Clone, be defined in quill/common/src/component.rs in HostComponent enum and have bincode_component_impl! in the end.

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
47 changes: 44 additions & 3 deletions feather/server/src/systems/tablist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,31 @@
use uuid::Uuid;

use base::{Gamemode, ProfileProperty};
use common::Game;
use common::{events::TablistExtrasUpdateEvent, Game};
use ecs::{SysResult, SystemExecutor};
use quill_common::events::{EntityRemoveEvent, GamemodeEvent, PlayerJoinEvent};
use quill_common::{components::Name, entities::Player};
use quill_common::{components::Name, entities::Player, tablist::TablistHeaderFooter};

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 +76,34 @@ fn add_tablist_players(game: &mut Game, server: &mut Server) -> SysResult {
Ok(())
}

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(())
}

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
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,
}