yog_event/wire/
handles.rs1use yog_core::{BlockPos, join_state, split_state};
14use yog_ids::sep::{PROPERTY_IS, PROPERTY_SEP};
15
16pub const SUBJECTS: &[(&str, &str)] = &[
22 (stringify!(uuid), stringify!(Entity)),
23 (stringify!(player), stringify!(Player)),
24 (stringify!(dim), stringify!(World)),
25];
26
27#[derive(Debug, Clone, PartialEq, Eq, Hash)]
29pub struct Entity {
30 pub uuid: String,
31}
32
33impl Entity {
34 pub fn new(uuid: impl Into<String>) -> Self {
35 Self { uuid: uuid.into() }
36 }
37}
38
39#[derive(Debug, Clone, PartialEq, Eq, Hash)]
41pub struct Player {
42 pub name: String,
43}
44
45impl Player {
46 pub fn new(name: impl Into<String>) -> Self {
47 Self { name: name.into() }
48 }
49
50 pub fn entity(&self) -> Option<Entity> {
52 self.info().map(|i| Entity::new(i.uuid))
53 }
54}
55
56#[derive(Debug, Clone, PartialEq, Eq, Hash)]
58pub struct World {
59 pub dim: String,
60}
61
62impl World {
63 pub fn new(dim: impl Into<String>) -> Self {
64 Self { dim: dim.into() }
65 }
66
67 pub fn overworld() -> Self {
68 Self::new(yog_ids::game::OVERWORLD)
69 }
70
71 pub fn property(&self, pos: BlockPos, name: &str) -> Option<String> {
73 let state = self.state(pos)?;
74 split_state(&state)
75 .1
76 .into_iter()
77 .find(|(k, _)| *k == name)
78 .map(|(_, v)| v.to_owned())
79 }
80
81 pub fn set_properties(&self, pos: BlockPos, properties: &str) -> bool {
84 let Some(state) = self.state(pos) else {
85 return false;
86 };
87 let (id, mut props) = split_state(&state);
88 for pair in properties.split(PROPERTY_SEP).filter(|p| !p.is_empty()) {
89 let Some((k, v)) = pair.split_once(PROPERTY_IS) else {
90 return false;
91 };
92 match props.iter_mut().find(|(n, _)| *n == k) {
93 Some(p) => p.1 = v,
94 None => props.push((k, v)),
95 }
96 }
97 self.set_state(pos, join_state(id, &props))
98 }
99}