|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use pumpkin_core::text::TextComponent; |
| 4 | +use pumpkin_protocol::{ |
| 5 | + client::play::{CDisplayObjective, CUpdateObjectives, CUpdateScore, RenderType}, |
| 6 | + NumberFormat, VarInt, |
| 7 | +}; |
| 8 | + |
| 9 | +use super::World; |
| 10 | + |
| 11 | +pub struct Scoreboard { |
| 12 | + objectives: HashMap<String, ScoreboardObjective<'static>>, |
| 13 | + // teams: HashMap<String, Team>, |
| 14 | +} |
| 15 | + |
| 16 | +impl Default for Scoreboard { |
| 17 | + fn default() -> Self { |
| 18 | + Self::new() |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +impl Scoreboard { |
| 23 | + pub fn new() -> Self { |
| 24 | + Self { |
| 25 | + objectives: HashMap::new(), |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + pub fn add_objective(&mut self, world: &World, objective: ScoreboardObjective) { |
| 30 | + if self.objectives.contains_key(objective.name) { |
| 31 | + // Maybe make this an error ? |
| 32 | + log::warn!( |
| 33 | + "Tried to create Objective which does already exist, {}", |
| 34 | + &objective.name |
| 35 | + ); |
| 36 | + return; |
| 37 | + } |
| 38 | + world.broadcast_packet_all(&CUpdateObjectives::new( |
| 39 | + objective.name, |
| 40 | + pumpkin_protocol::client::play::Mode::Add, |
| 41 | + objective.display_name, |
| 42 | + objective.render_type, |
| 43 | + objective.number_format, |
| 44 | + )); |
| 45 | + world.broadcast_packet_all(&CDisplayObjective::new( |
| 46 | + pumpkin_protocol::client::play::DisplaySlot::Sidebar, |
| 47 | + "test", |
| 48 | + )); |
| 49 | + } |
| 50 | + |
| 51 | + pub fn update_score(&self, world: &World, score: ScoreboardScore) { |
| 52 | + if self.objectives.contains_key(score.objective_name) { |
| 53 | + log::warn!( |
| 54 | + "Tried to place a score into a Objective which does not exist, {}", |
| 55 | + &score.objective_name |
| 56 | + ); |
| 57 | + return; |
| 58 | + } |
| 59 | + world.broadcast_packet_all(&CUpdateScore::new( |
| 60 | + score.entity_name, |
| 61 | + score.objective_name, |
| 62 | + score.value, |
| 63 | + score.display_name, |
| 64 | + score.number_format, |
| 65 | + )); |
| 66 | + } |
| 67 | + |
| 68 | + // pub fn add_team(&mut self, name: String) { |
| 69 | + // if self.teams.contains_key(&name) { |
| 70 | + // // Maybe make this an error ? |
| 71 | + // log::warn!("Tried to create Team which does already exist, {}", name); |
| 72 | + // } |
| 73 | + // } |
| 74 | +} |
| 75 | + |
| 76 | +pub struct ScoreboardObjective<'a> { |
| 77 | + name: &'a str, |
| 78 | + display_name: TextComponent<'a>, |
| 79 | + render_type: RenderType, |
| 80 | + number_format: Option<NumberFormat<'a>>, |
| 81 | +} |
| 82 | + |
| 83 | +impl<'a> ScoreboardObjective<'a> { |
| 84 | + pub const fn new( |
| 85 | + name: &'a str, |
| 86 | + display_name: TextComponent<'a>, |
| 87 | + render_type: RenderType, |
| 88 | + number_format: Option<NumberFormat<'a>>, |
| 89 | + ) -> Self { |
| 90 | + Self { |
| 91 | + name, |
| 92 | + display_name, |
| 93 | + render_type, |
| 94 | + number_format, |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +pub struct ScoreboardScore<'a> { |
| 100 | + entity_name: &'a str, |
| 101 | + objective_name: &'a str, |
| 102 | + value: VarInt, |
| 103 | + display_name: Option<TextComponent<'a>>, |
| 104 | + number_format: Option<NumberFormat<'a>>, |
| 105 | +} |
| 106 | + |
| 107 | +impl<'a> ScoreboardScore<'a> { |
| 108 | + pub const fn new( |
| 109 | + entity_name: &'a str, |
| 110 | + objective_name: &'a str, |
| 111 | + value: VarInt, |
| 112 | + display_name: Option<TextComponent<'a>>, |
| 113 | + number_format: Option<NumberFormat<'a>>, |
| 114 | + ) -> Self { |
| 115 | + Self { |
| 116 | + entity_name, |
| 117 | + objective_name, |
| 118 | + value, |
| 119 | + display_name, |
| 120 | + number_format, |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments