dragonfly_plugin/event/
mutations.rs1#![allow(clippy::all)]
3use crate::event::{EventContext, EventResult};
4use crate::types;
5impl<'a> EventContext<'a, types::ChatEvent> {
6 fn ensure_mutation_exists(&mut self) {
7 if !matches!(
8 self.result,
9 EventResult::Mutated(types::EventResultUpdate::Chat(_))
10 ) {
11 self.result = EventResult::Mutated(types::EventResultUpdate::Chat(
12 types::ChatMutation::default(),
13 ));
14 }
15 }
16 pub fn set_message(&mut self, message: impl Into<Option<String>>) -> &mut Self {
18 self.ensure_mutation_exists();
19 if let EventResult::Mutated(types::EventResultUpdate::Chat(ref mut m)) = self.result {
20 m.message = message.into();
21 }
22 self
23 }
24}
25impl<'a> EventContext<'a, types::BlockBreakEvent> {
26 fn ensure_mutation_exists(&mut self) {
27 if !matches!(
28 self.result,
29 EventResult::Mutated(types::EventResultUpdate::BlockBreak(_))
30 ) {
31 self.result = EventResult::Mutated(types::EventResultUpdate::BlockBreak(
32 types::BlockBreakMutation::default(),
33 ));
34 }
35 }
36 pub fn set_drops(&mut self, drops: impl Into<Option<types::ItemStackList>>) -> &mut Self {
38 self.ensure_mutation_exists();
39 if let EventResult::Mutated(types::EventResultUpdate::BlockBreak(ref mut m)) = self.result {
40 m.drops = drops.into();
41 }
42 self
43 }
44 pub fn set_xp(&mut self, xp: impl Into<Option<i32>>) -> &mut Self {
46 self.ensure_mutation_exists();
47 if let EventResult::Mutated(types::EventResultUpdate::BlockBreak(ref mut m)) = self.result {
48 m.xp = xp.into();
49 }
50 self
51 }
52}
53impl<'a> EventContext<'a, types::PlayerFoodLossEvent> {
54 fn ensure_mutation_exists(&mut self) {
55 if !matches!(
56 self.result,
57 EventResult::Mutated(types::EventResultUpdate::PlayerFoodLoss(_))
58 ) {
59 self.result = EventResult::Mutated(types::EventResultUpdate::PlayerFoodLoss(
60 types::PlayerFoodLossMutation::default(),
61 ));
62 }
63 }
64 pub fn set_to(&mut self, to: impl Into<Option<i32>>) -> &mut Self {
66 self.ensure_mutation_exists();
67 if let EventResult::Mutated(types::EventResultUpdate::PlayerFoodLoss(ref mut m)) =
68 self.result
69 {
70 m.to = to.into();
71 }
72 self
73 }
74}
75impl<'a> EventContext<'a, types::PlayerHealEvent> {
76 fn ensure_mutation_exists(&mut self) {
77 if !matches!(
78 self.result,
79 EventResult::Mutated(types::EventResultUpdate::PlayerHeal(_))
80 ) {
81 self.result = EventResult::Mutated(types::EventResultUpdate::PlayerHeal(
82 types::PlayerHealMutation::default(),
83 ));
84 }
85 }
86 pub fn set_amount(&mut self, amount: impl Into<Option<f64>>) -> &mut Self {
88 self.ensure_mutation_exists();
89 if let EventResult::Mutated(types::EventResultUpdate::PlayerHeal(ref mut m)) = self.result {
90 m.amount = amount.into();
91 }
92 self
93 }
94}
95impl<'a> EventContext<'a, types::PlayerHurtEvent> {
96 fn ensure_mutation_exists(&mut self) {
97 if !matches!(
98 self.result,
99 EventResult::Mutated(types::EventResultUpdate::PlayerHurt(_))
100 ) {
101 self.result = EventResult::Mutated(types::EventResultUpdate::PlayerHurt(
102 types::PlayerHurtMutation::default(),
103 ));
104 }
105 }
106 pub fn set_damage(&mut self, damage: impl Into<Option<f64>>) -> &mut Self {
108 self.ensure_mutation_exists();
109 if let EventResult::Mutated(types::EventResultUpdate::PlayerHurt(ref mut m)) = self.result {
110 m.damage = damage.into();
111 }
112 self
113 }
114 pub fn set_attack_immunity_ms(
116 &mut self,
117 attack_immunity_ms: impl Into<Option<i64>>,
118 ) -> &mut Self {
119 self.ensure_mutation_exists();
120 if let EventResult::Mutated(types::EventResultUpdate::PlayerHurt(ref mut m)) = self.result {
121 m.attack_immunity_ms = attack_immunity_ms.into();
122 }
123 self
124 }
125}
126impl<'a> EventContext<'a, types::PlayerDeathEvent> {
127 fn ensure_mutation_exists(&mut self) {
128 if !matches!(
129 self.result,
130 EventResult::Mutated(types::EventResultUpdate::PlayerDeath(_))
131 ) {
132 self.result = EventResult::Mutated(types::EventResultUpdate::PlayerDeath(
133 types::PlayerDeathMutation::default(),
134 ));
135 }
136 }
137 pub fn set_keep_inventory(&mut self, keep_inventory: impl Into<Option<bool>>) -> &mut Self {
139 self.ensure_mutation_exists();
140 if let EventResult::Mutated(types::EventResultUpdate::PlayerDeath(ref mut m)) = self.result
141 {
142 m.keep_inventory = keep_inventory.into();
143 }
144 self
145 }
146}
147impl<'a> EventContext<'a, types::PlayerRespawnEvent> {
148 fn ensure_mutation_exists(&mut self) {
149 if !matches!(
150 self.result,
151 EventResult::Mutated(types::EventResultUpdate::PlayerRespawn(_))
152 ) {
153 self.result = EventResult::Mutated(types::EventResultUpdate::PlayerRespawn(
154 types::PlayerRespawnMutation::default(),
155 ));
156 }
157 }
158 pub fn set_position(&mut self, position: impl Into<Option<types::Vec3>>) -> &mut Self {
160 self.ensure_mutation_exists();
161 if let EventResult::Mutated(types::EventResultUpdate::PlayerRespawn(ref mut m)) =
162 self.result
163 {
164 m.position = position.into();
165 }
166 self
167 }
168 pub fn set_world(&mut self, world: impl Into<Option<types::WorldRef>>) -> &mut Self {
170 self.ensure_mutation_exists();
171 if let EventResult::Mutated(types::EventResultUpdate::PlayerRespawn(ref mut m)) =
172 self.result
173 {
174 m.world = world.into();
175 }
176 self
177 }
178}
179impl<'a> EventContext<'a, types::PlayerAttackEntityEvent> {
180 fn ensure_mutation_exists(&mut self) {
181 if !matches!(
182 self.result,
183 EventResult::Mutated(types::EventResultUpdate::PlayerAttackEntity(_))
184 ) {
185 self.result = EventResult::Mutated(types::EventResultUpdate::PlayerAttackEntity(
186 types::PlayerAttackEntityMutation::default(),
187 ));
188 }
189 }
190 pub fn set_force(&mut self, force: impl Into<Option<f64>>) -> &mut Self {
192 self.ensure_mutation_exists();
193 if let EventResult::Mutated(types::EventResultUpdate::PlayerAttackEntity(ref mut m)) =
194 self.result
195 {
196 m.force = force.into();
197 }
198 self
199 }
200 pub fn set_height(&mut self, height: impl Into<Option<f64>>) -> &mut Self {
202 self.ensure_mutation_exists();
203 if let EventResult::Mutated(types::EventResultUpdate::PlayerAttackEntity(ref mut m)) =
204 self.result
205 {
206 m.height = height.into();
207 }
208 self
209 }
210 pub fn set_critical(&mut self, critical: impl Into<Option<bool>>) -> &mut Self {
212 self.ensure_mutation_exists();
213 if let EventResult::Mutated(types::EventResultUpdate::PlayerAttackEntity(ref mut m)) =
214 self.result
215 {
216 m.critical = critical.into();
217 }
218 self
219 }
220}
221impl<'a> EventContext<'a, types::PlayerExperienceGainEvent> {
222 fn ensure_mutation_exists(&mut self) {
223 if !matches!(
224 self.result,
225 EventResult::Mutated(types::EventResultUpdate::PlayerExperienceGain(_))
226 ) {
227 self.result = EventResult::Mutated(types::EventResultUpdate::PlayerExperienceGain(
228 types::PlayerExperienceGainMutation::default(),
229 ));
230 }
231 }
232 pub fn set_amount(&mut self, amount: impl Into<Option<i32>>) -> &mut Self {
234 self.ensure_mutation_exists();
235 if let EventResult::Mutated(types::EventResultUpdate::PlayerExperienceGain(ref mut m)) =
236 self.result
237 {
238 m.amount = amount.into();
239 }
240 self
241 }
242}
243impl<'a> EventContext<'a, types::PlayerLecternPageTurnEvent> {
244 fn ensure_mutation_exists(&mut self) {
245 if !matches!(
246 self.result,
247 EventResult::Mutated(types::EventResultUpdate::PlayerLecternPageTurn(_))
248 ) {
249 self.result = EventResult::Mutated(types::EventResultUpdate::PlayerLecternPageTurn(
250 types::PlayerLecternPageTurnMutation::default(),
251 ));
252 }
253 }
254 pub fn set_new_page(&mut self, new_page: impl Into<Option<i32>>) -> &mut Self {
256 self.ensure_mutation_exists();
257 if let EventResult::Mutated(types::EventResultUpdate::PlayerLecternPageTurn(ref mut m)) =
258 self.result
259 {
260 m.new_page = new_page.into();
261 }
262 self
263 }
264}
265impl<'a> EventContext<'a, types::PlayerItemPickupEvent> {
266 fn ensure_mutation_exists(&mut self) {
267 if !matches!(
268 self.result,
269 EventResult::Mutated(types::EventResultUpdate::PlayerItemPickup(_))
270 ) {
271 self.result = EventResult::Mutated(types::EventResultUpdate::PlayerItemPickup(
272 types::PlayerItemPickupMutation::default(),
273 ));
274 }
275 }
276 pub fn set_item(&mut self, item: impl Into<Option<types::ItemStack>>) -> &mut Self {
278 self.ensure_mutation_exists();
279 if let EventResult::Mutated(types::EventResultUpdate::PlayerItemPickup(ref mut m)) =
280 self.result
281 {
282 m.item = item.into();
283 }
284 self
285 }
286}
287impl<'a> EventContext<'a, types::PlayerTransferEvent> {
288 fn ensure_mutation_exists(&mut self) {
289 if !matches!(
290 self.result,
291 EventResult::Mutated(types::EventResultUpdate::PlayerTransfer(_))
292 ) {
293 self.result = EventResult::Mutated(types::EventResultUpdate::PlayerTransfer(
294 types::PlayerTransferMutation::default(),
295 ));
296 }
297 }
298 pub fn set_address(&mut self, address: impl Into<Option<types::Address>>) -> &mut Self {
300 self.ensure_mutation_exists();
301 if let EventResult::Mutated(types::EventResultUpdate::PlayerTransfer(ref mut m)) =
302 self.result
303 {
304 m.address = address.into();
305 }
306 self
307 }
308}
309impl<'a> EventContext<'a, types::WorldExplosionEvent> {
310 fn ensure_mutation_exists(&mut self) {
311 if !matches!(
312 self.result,
313 EventResult::Mutated(types::EventResultUpdate::WorldExplosion(_))
314 ) {
315 self.result = EventResult::Mutated(types::EventResultUpdate::WorldExplosion(
316 types::WorldExplosionMutation::default(),
317 ));
318 }
319 }
320 pub fn set_entity_uuids(
322 &mut self,
323 entity_uuids: impl Into<Option<types::StringList>>,
324 ) -> &mut Self {
325 self.ensure_mutation_exists();
326 if let EventResult::Mutated(types::EventResultUpdate::WorldExplosion(ref mut m)) =
327 self.result
328 {
329 m.entity_uuids = entity_uuids.into();
330 }
331 self
332 }
333 pub fn set_blocks(&mut self, blocks: impl Into<Option<types::BlockPosList>>) -> &mut Self {
335 self.ensure_mutation_exists();
336 if let EventResult::Mutated(types::EventResultUpdate::WorldExplosion(ref mut m)) =
337 self.result
338 {
339 m.blocks = blocks.into();
340 }
341 self
342 }
343 pub fn set_item_drop_chance(&mut self, item_drop_chance: impl Into<Option<f64>>) -> &mut Self {
345 self.ensure_mutation_exists();
346 if let EventResult::Mutated(types::EventResultUpdate::WorldExplosion(ref mut m)) =
347 self.result
348 {
349 m.item_drop_chance = item_drop_chance.into();
350 }
351 self
352 }
353 pub fn set_spawn_fire(&mut self, spawn_fire: impl Into<Option<bool>>) -> &mut Self {
355 self.ensure_mutation_exists();
356 if let EventResult::Mutated(types::EventResultUpdate::WorldExplosion(ref mut m)) =
357 self.result
358 {
359 m.spawn_fire = spawn_fire.into();
360 }
361 self
362 }
363}