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