classicube_helpers\tab_list/
entry.rs

1#![allow(clippy::used_underscore_binding)]
2
3use std::os::raw::c_int;
4
5use classicube_sys::{StringsBuffer_UNSAFE_Get, TabList};
6
7#[derive(Debug)]
8pub struct TabListEntry {
9    id: u8,
10    name_offset: &'static u16,
11    group_rank: &'static u8,
12}
13
14impl TabListEntry {
15    /// # Safety
16    ///
17    /// `id` must exist.
18    ///
19    /// `Entity` cannot outlive the entity in-game.
20    ///
21    /// `Entities` will use `Weak` to make sure this dies when the entity is removed.
22    #[must_use]
23    pub unsafe fn from_id(id: u8) -> Option<Self> {
24        let name_offset = &TabList.NameOffsets[id as usize];
25        let group_rank = &TabList.GroupRanks[id as usize];
26
27        if *name_offset == 0 || *group_rank == 0 || TabList._buffer.count == 0 {
28            return None;
29        }
30
31        Some(Self {
32            id,
33            name_offset,
34            group_rank,
35        })
36    }
37
38    #[must_use]
39    pub fn get_id(&self) -> u8 {
40        self.id
41    }
42
43    /// or "Player"
44    pub fn get_real_name(&self) -> String {
45        unsafe {
46            StringsBuffer_UNSAFE_Get(&mut TabList._buffer, c_int::from(*self.name_offset - 3))
47        }
48        .to_string()
49    }
50
51    /// or "Text" or "list"
52    pub fn get_nick_name(&self) -> String {
53        unsafe {
54            StringsBuffer_UNSAFE_Get(&mut TabList._buffer, c_int::from(*self.name_offset - 2))
55        }
56        .to_string()
57    }
58
59    pub fn get_group(&self) -> String {
60        unsafe {
61            StringsBuffer_UNSAFE_Get(&mut TabList._buffer, c_int::from(*self.name_offset - 1))
62        }
63        .to_string()
64    }
65
66    #[must_use]
67    pub fn get_rank(&self) -> u8 {
68        *self.group_rank
69    }
70}