forked from valence-rs/valence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld_border.rs
200 lines (156 loc) · 4.69 KB
/
world_border.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use crate::protocol::packets::play::{
WorldBorderCenterChangedS2c, WorldBorderInitializeS2c, WorldBorderInterpolateSizeS2c,
WorldBorderSizeChangedS2c, WorldBorderWarningBlocksChangedS2c,
WorldBorderWarningTimeChangedS2c,
};
use crate::testing::*;
use crate::world_border::{
WorldBorderBundle, WorldBorderCenter, WorldBorderLerp, WorldBorderPortalTpBoundary,
WorldBorderWarnBlocks, WorldBorderWarnTime,
};
#[test]
fn test_initialize_on_join() {
let ScenarioSingleClient {
mut app,
client: _,
mut helper,
layer: _,
} = prepare();
app.update();
// Check if a world border initialize packet was sent
let frames = helper.collect_received();
frames.assert_count::<WorldBorderInitializeS2c>(1);
}
#[test]
fn test_center_change() {
let ScenarioSingleClient {
mut app,
client: _,
mut helper,
layer,
} = prepare();
app.update();
helper.clear_received();
// Change the center
let mut center = app.world.get_mut::<WorldBorderCenter>(layer).unwrap();
center.x = 10.0;
app.update();
// Check if a world border center changed packet was sent
let frames = helper.collect_received();
frames.assert_count::<WorldBorderCenterChangedS2c>(1);
}
#[test]
fn test_diameter_change() {
let ScenarioSingleClient {
mut app,
client: _,
mut helper,
layer,
} = prepare();
app.update();
helper.clear_received();
// Change the diameter
let mut lerp = app.world.get_mut::<WorldBorderLerp>(layer).unwrap();
lerp.target_diameter = 20.0;
app.update();
// Check if a world border size changed packet was sent
let frames = helper.collect_received();
frames.assert_count::<WorldBorderSizeChangedS2c>(1);
}
#[test]
fn test_interpolation() {
let ScenarioSingleClient {
mut app,
client: _,
mut helper,
layer,
} = prepare();
app.update();
helper.clear_received();
// Change the diameter and start interpolation to it over 20 ticks
let mut lerp = app.world.get_mut::<WorldBorderLerp>(layer).unwrap();
lerp.target_diameter = 20.0;
lerp.remaining_ticks = 20;
// Tick 20 times
for _ in 0..20 {
app.update();
}
// Check if a world border interpolate size packet was sent
let frames = helper.collect_received();
frames.assert_count::<WorldBorderInterpolateSizeS2c>(1);
// Check if the interpolation is finished
let lerp = app.world.get_mut::<WorldBorderLerp>(layer).unwrap();
assert_eq!(lerp.current_diameter, 20.0);
assert_eq!(lerp.remaining_ticks, 0);
}
#[test]
fn test_warning_blocks_change() {
let ScenarioSingleClient {
mut app,
client: _,
mut helper,
layer,
} = prepare();
app.update();
helper.clear_received();
// Change the warning blocks
let mut warn_blocks = app.world.get_mut::<WorldBorderWarnBlocks>(layer).unwrap();
warn_blocks.0 = 10;
app.update();
// Check if a world border warning blocks changed packet was sent
let frames = helper.collect_received();
frames.assert_count::<WorldBorderWarningBlocksChangedS2c>(1);
}
#[test]
fn test_warning_time_change() {
let ScenarioSingleClient {
mut app,
client: _,
mut helper,
layer,
} = prepare();
app.update();
helper.clear_received();
// Change the warning time
let mut warn_time = app.world.get_mut::<WorldBorderWarnTime>(layer).unwrap();
warn_time.0 = 10;
app.update();
// Check if a world border warning time changed packet was sent
let frames = helper.collect_received();
frames.assert_count::<WorldBorderWarningTimeChangedS2c>(1);
}
#[test]
fn test_portal_tp_boundary_change() {
let ScenarioSingleClient {
mut app,
client: _,
mut helper,
layer,
} = prepare();
app.update();
helper.clear_received();
// Change the portal tp boundary
let mut portal_tp_boundary = app
.world
.get_mut::<WorldBorderPortalTpBoundary>(layer)
.unwrap();
portal_tp_boundary.0 = 10;
app.update();
// Check if a world border initialize packet was sent
let frames = helper.collect_received();
frames.assert_count::<WorldBorderInitializeS2c>(1);
}
fn prepare() -> ScenarioSingleClient {
let mut s = ScenarioSingleClient::new();
// Process a tick to get past the "on join" logic.
s.app.update();
// Attach the world border bundle to the chunk layer
s.app.world.entity_mut(s.layer).insert(WorldBorderBundle {
lerp: WorldBorderLerp {
target_diameter: 10.0,
..Default::default()
},
..Default::default()
});
s
}