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 13 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
6 changes: 5 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 @@ -66,6 +66,8 @@ impl Config {
view_distance: self.server.view_distance,
max_players: self.server.max_players,
default_gamemode: self.server.default_gamemode,
tablist_header: self.server.tablist_header.clone(),
tablist_footer: self.server.tablist_footer.clone(),
proxy_mode: match self.proxy.proxy_mode {
ProxyMode::None => None,
ProxyMode::Bungee => Some(crate::options::ProxyMode::Bungeecord),
Expand All @@ -90,6 +92,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
8 changes: 7 additions & 1 deletion feather/server/src/options.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use base::Gamemode;
use base::{Gamemode, Text};

use crate::favicon::Favicon;

Expand Down Expand Up @@ -28,6 +28,12 @@ pub struct Options {
/// The default gamemode for new players.
pub default_gamemode: Gamemode,

/// The default tablist header.
pub tablist_header: Text,

/// The default tablist footer.
pub tablist_footer: Text,

Copy link
Contributor

Choose a reason for hiding this comment

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

We have Config for server config and Options for Server options (mostly for initial connection handling, status, ping, working with proxies), so I think tablist values should only be in Config.

Copy link
Author

Choose a reason for hiding this comment

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

let server_options = game.resources.get::<Server>().unwrap().options.clone();

game.insert_resource(TablistHeaderFooter {
    header: server_options.tablist_header.clone(),
    footer: server_options.tablist_footer.clone(),
});

Then how do I get it from config to use it for TablistHeaderFooter in tablist.rs.

Copy link
Author

Choose a reason for hiding this comment

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

Nevermind, got it.

Copy link
Contributor

Choose a reason for hiding this comment

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

You can pass it to register(game) function, or even better, make Config (or even Rc<Config> to make it immutable) a resource and Options non-resource. Options is never used as a resource (just did a quick search on cs.github)

/// Proxy IP forwarding mode
pub proxy_mode: Option<ProxyMode>,
// HMAC key used with Velocity IP forwarding.
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
43 changes: 40 additions & 3 deletions feather/server/src/systems/tablist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@
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::{ClientId, Server};

pub fn register(systems: &mut SystemExecutor<Game>) {
pub fn register(game: &mut Game, systems: &mut SystemExecutor<Game>) {
let server_options = game.resources.get::<Server>().unwrap().options.clone();

game.insert_resource(TablistHeaderFooter {
header: server_options.tablist_header.clone(),
footer: server_options.tablist_footer.clone(),
});

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