Skip to main content

yog_event/
events.rs

1//! Event types passed from Minecraft (through the Java host) into Rust mods.
2
3use yog_core::BlockPos;
4
5/// Whether a handler is running before or after the action.
6///
7/// In `Pre` phase the handler's return value may cancel the action.
8/// In `Post` phase the return value is ignored.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum EventPhase {
11    Pre,
12    Post,
13}
14
15/// Fired when a player breaks a block (server side).
16#[derive(Debug, Clone)]
17pub struct BlockBreakEvent {
18    pub player_name: String,
19    /// Registry id of the block, e.g. `minecraft:stone`.
20    pub block_id: String,
21    pub pos: BlockPos,
22    /// Dimension the block was broken in, e.g. `minecraft:overworld`.
23    pub dimension: String,
24}
25
26/// Fired when a player sends a chat message.
27#[derive(Debug, Clone)]
28pub struct ChatEvent {
29    pub player_name: String,
30    pub message: String,
31    pub dimension: String,
32}
33
34/// Fired when a player joins the server.
35#[derive(Debug, Clone)]
36pub struct PlayerJoinEvent {
37    pub player_name: String,
38    /// Player UUID as a string, e.g. `069a79f4-44e9-4726-a5be-fca90e38aaf5`.
39    pub uuid: String,
40    pub dimension: String,
41}
42
43/// Fired when a player leaves the server.
44#[derive(Debug, Clone)]
45pub struct PlayerLeaveEvent {
46    pub player_name: String,
47    pub uuid: String,
48    pub dimension: String,
49}
50
51/// Fired when a player right-clicks with an item (server side).
52#[derive(Debug, Clone)]
53pub struct UseItemEvent {
54    pub player_name: String,
55    /// Registry id of the held item, e.g. `minecraft:stick`.
56    pub item_id: String,
57    /// Whether the player was sneaking (shift) during use.
58    pub sneaking: bool,
59    pub dimension: String,
60}
61
62/// Fired when a player right-clicks a block (server side).
63#[derive(Debug, Clone)]
64pub struct UseBlockEvent {
65    pub player_name: String,
66    /// Registry id of the targeted block, e.g. `minecraft:chest`.
67    pub block_id: String,
68    pub pos: BlockPos,
69    pub dimension: String,
70    /// Whether the player was sneaking (shift).
71    pub sneaking: bool,
72}
73
74/// Fired when a player attacks (left-clicks) an entity (server side).
75#[derive(Debug, Clone)]
76pub struct AttackEntityEvent {
77    pub player_name: String,
78    /// Registry id of the target, e.g. `minecraft:zombie`.
79    pub target_type: String,
80    /// Target entity UUID as a string.
81    pub target_uuid: String,
82    pub dimension: String,
83}
84
85/// Fired after a living entity takes damage (server side).
86#[derive(Debug, Clone)]
87pub struct EntityDamageEvent {
88    /// Registry id of the entity, e.g. `minecraft:zombie`.
89    pub entity_type: String,
90    pub uuid: String,
91    /// Amount of damage dealt (hit points).
92    pub amount: f32,
93    /// Identifier of the damage source, e.g. `minecraft:player`, `fall`.
94    pub source: String,
95    pub dimension: String,
96}
97
98/// Fired after a living entity dies (server side).
99#[derive(Debug, Clone)]
100pub struct EntityDeathEvent {
101    /// Registry id of the entity, e.g. `minecraft:zombie`.
102    pub entity_type: String,
103    pub uuid: String,
104    /// Identifier of the killing damage source, e.g. `minecraft:player`.
105    pub source: String,
106    pub dimension: String,
107}
108
109/// Fired when a player places a block (server side).
110///
111/// Passed to handlers registered via `Registry::on_player_place_block` with an
112/// [`EventPhase`] argument:
113/// - `Pre`  — fires before placement; return `false` to cancel.
114/// - `Post` — fires after the block has been placed.
115#[derive(Debug, Clone)]
116pub struct PlaceBlockEvent {
117    pub player_name: String,
118    /// Registry id of the block being placed, e.g. `minecraft:stone`.
119    pub block_id: String,
120    pub pos: BlockPos,
121    pub dimension: String,
122    /// Whether the player was sneaking (shift).
123    pub sneaking: bool,
124}
125
126/// Fired when any entity is loaded into a world (server side).
127///
128/// Also used for `on_entity_spawn_pre` (cancellable): return `false` to
129/// discard the entity immediately after loading (effective spawn cancellation).
130#[derive(Debug, Clone)]
131pub struct EntitySpawnEvent {
132    /// Registry id of the entity, e.g. `minecraft:zombie`.
133    pub entity_type: String,
134    pub uuid: String,
135    /// Dimension the entity was added to, e.g. `minecraft:overworld`.
136    pub dimension: String,
137}
138
139/// Fired when a player dies.
140///
141/// - `Pre`  — fires before death is processed; return `false` to prevent death
142///   (entity survives at 0.5 HP).
143/// - `Post` — fires after the player has died.
144#[derive(Debug, Clone)]
145pub struct PlayerDeathEvent {
146    pub player_name: String,
147    pub uuid: String,
148    /// Damage source identifier, e.g. `"player"`, `"fall"`.
149    pub source: String,
150    pub dimension: String,
151}
152
153/// Fired when a player respawns after death (Post only).
154#[derive(Debug, Clone)]
155pub struct PlayerRespawnEvent {
156    pub player_name: String,
157    pub uuid: String,
158    /// True if respawning at a bed or respawn anchor; false for world spawn.
159    pub at_anchor: bool,
160    /// Dimension respawned into (may differ from where the player died).
161    pub dimension: String,
162}
163
164/// Fired when a player earns an advancement (Post only).
165#[derive(Debug, Clone)]
166pub struct AdvancementEvent {
167    pub player_name: String,
168    pub uuid: String,
169    /// Namespaced id of the advancement, e.g. `"minecraft:story/mine_stone"`.
170    pub advancement_id: String,
171    pub dimension: String,
172}
173
174/// Fired when a player right-clicks (interacts with) an entity (server side).
175///
176/// - `Pre`  — fires before the interaction; return `false` to cancel.
177/// - `Post` — fires after the interaction.
178#[derive(Debug, Clone)]
179pub struct EntityInteractEvent {
180    pub player_name: String,
181    pub player_uuid: String,
182    /// Registry id of the interacted entity, e.g. `"minecraft:villager"`.
183    pub entity_type: String,
184    pub entity_uuid: String,
185    /// `"main_hand"` or `"off_hand"`.
186    pub hand: String,
187    pub dimension: String,
188}
189
190/// Fired when a player takes a crafted item from a crafting output slot (Post only).
191#[derive(Debug, Clone)]
192pub struct CraftEvent {
193    pub player_name: String,
194    pub player_uuid: String,
195    /// Registry id of the crafted item, e.g. `"minecraft:stick"`.
196    pub result_item: String,
197    pub result_count: u32,
198    pub dimension: String,
199}
200
201/// Fired when an explosion occurs in a world.
202///
203/// - `Pre`  — fires before block destruction; return `false` to cancel
204///   (blocks and entities are unaffected).
205/// - `Post` — fires after the explosion has taken effect.
206#[derive(Debug, Clone)]
207pub struct ExplosionEvent {
208    pub dimension: String,
209    pub x: f64,
210    pub y: f64,
211    pub z: f64,
212    pub power: f32,
213    /// UUID of the entity that caused the explosion, or empty string if none.
214    pub cause_uuid: String,
215}
216
217// ── ABI minor 9 event types ───────────────────────────────────────────────────
218
219/// A hopper or dropper puts an item into a block that accepts items
220/// (`block.accepts_items`).
221///
222/// - `Pre`  — return whether the block takes it (nothing changes yet).
223/// - `Post` — it takes it: do what taking it means.
224#[derive(Debug, Clone)]
225pub struct ItemInsertEvent {
226    /// Registry id of the block.
227    pub block_id: String,
228    pub pos: BlockPos,
229    pub dimension: String,
230    pub item_id: String,
231    pub count: u32,
232    /// The item's data as SNBT (`{}` for none).
233    pub nbt: String,
234}
235
236/// Fired when a player picks up an item entity.
237///
238/// - `Pre`  — return `false` to prevent the pickup.
239/// - `Post` — item was successfully picked up.
240#[derive(Debug, Clone)]
241pub struct ItemPickupEvent {
242    pub player_name: String,
243    pub player_uuid: String,
244    /// Registry id of the item, e.g. `"minecraft:diamond"`.
245    pub item_id: String,
246    pub item_count: u32,
247    /// UUID of the item entity that was picked up.
248    pub entity_uuid: String,
249    pub dimension: String,
250}
251
252/// Fired every time a player sends a movement packet (very high frequency).
253///
254/// Post-phase only. The fields reflect the *new* position the client claims.
255#[derive(Debug, Clone)]
256pub struct PlayerMoveEvent {
257    pub player_name: String,
258    pub player_uuid: String,
259    pub x: f64,
260    pub y: f64,
261    pub z: f64,
262    pub yaw: f32,
263    pub pitch: f32,
264    pub dimension: String,
265}
266
267/// Fired when a player opens a container screen.
268///
269/// - `Pre`  — return `false` to prevent the screen from opening.
270/// - `Post` — screen opened; `container_type` is set.
271#[derive(Debug, Clone)]
272pub struct ContainerOpenEvent {
273    pub player_name: String,
274    pub player_uuid: String,
275    /// Screen handler registry id, e.g. `"minecraft:chest"`.
276    /// Empty string for screens not in the registry (e.g. the player inventory).
277    pub container_type: String,
278    pub dimension: String,
279}
280
281/// Fired when a player closes a container screen (Post only).
282#[derive(Debug, Clone)]
283pub struct ContainerCloseEvent {
284    pub player_name: String,
285    pub player_uuid: String,
286    pub dimension: String,
287}
288
289// ── ABI minor 10 — client-side events ────────────────────────────────────────
290
291/// Fired every client tick on the render thread.
292#[derive(Debug, Clone)]
293pub struct ClientTickEvent {}
294
295/// Fired every frame when the HUD is rendered.
296/// `delta_tick` is the partial-tick interpolation factor (0.0–1.0).
297#[derive(Debug, Clone)]
298pub struct HudRenderEvent {
299    pub delta_tick: f32,
300}
301
302/// Fired on every key press, release, or repeat (client-side).
303///
304/// Return `false` in the handler to prevent Minecraft from processing the key.
305#[derive(Debug, Clone)]
306pub struct KeyPressEvent {
307    /// GLFW key code (e.g. 69 = E). See `org.lwjgl.glfw.GLFW`.
308    pub key_code: i32,
309    pub scan_code: i32,
310    /// 0 = release, 1 = press, 2 = repeat.
311    pub action: i32,
312    /// Modifier bitmask: 1=Shift, 2=Ctrl, 4=Alt, 8=Super.
313    pub modifiers: i32,
314}
315
316/// Fired when a GUI screen opens or closes.
317#[derive(Debug, Clone)]
318pub struct ScreenEvent {
319    /// Simple class name of the screen, e.g. `"InventoryScreen"`, `"ChestScreen"`.
320    pub screen_class: String,
321}
322
323/// Fired when a persistent projectile (arrow, trident, etc.) hits a target.
324///
325/// - `Pre`  — return `false` to cancel the hit (projectile passes through).
326/// - `Post` — hit was processed.
327#[derive(Debug, Clone)]
328pub struct ProjectileHitEvent {
329    /// Registry id of the projectile, e.g. `"minecraft:arrow"`.
330    pub projectile_type: String,
331    pub projectile_uuid: String,
332    /// UUID of the entity that fired the projectile, or empty string.
333    pub shooter_uuid: String,
334    /// `"block"` or `"entity"`.
335    pub hit_type: String,
336    /// UUID of the entity that was hit (empty for block hits).
337    pub hit_entity_uuid: String,
338    pub x: f64,
339    pub y: f64,
340    pub z: f64,
341    pub dimension: String,
342}