11package dev .diamond .luafy .registry ;
22
33import dev .diamond .luafy .Luafy ;
4+ import dev .diamond .luafy .autodoc .Argtypes ;
45import dev .diamond .luafy .script .event .ScriptEvent ;
6+ import net .fabricmc .fabric .api .entity .event .v1 .ServerLivingEntityEvents ;
57import net .fabricmc .fabric .api .event .lifecycle .v1 .ServerLifecycleEvents ;
68import net .fabricmc .fabric .api .event .lifecycle .v1 .ServerTickEvents ;
9+ import net .minecraft .commands .CommandSourceStack ;
710import net .minecraft .core .Registry ;
11+ import net .minecraft .core .registries .Registries ;
12+ import net .minecraft .world .damagesource .DamageSource ;
13+ import net .minecraft .world .entity .LivingEntity ;
814
915public class ScriptEvents {
1016
1117 public static ScriptEvent <Object > LOAD = new ScriptEvent <>("Executes after a reload." , b -> {
1218
13- }, (src , ctx ) -> {
19+ }, (b , ctx , script ) -> {
1420
1521 });
1622
1723 public static ScriptEvent <Object > TICK = new ScriptEvent <>("Executes every server tick." , b -> {
1824
19- }, (src , ctx ) -> {
25+ }, (b , ctx , script ) -> {});
26+
27+ public static ScriptEvent <EntityTakesDamage > ENTITY_TAKES_DAMAGE = new ScriptEvent <>("Executes after an entity takes damage." , b -> {
28+ b .add ("entity" , ScriptObjects .LIVING_ENTITY , "Living Entity that took damage." );
29+ b .add ("attacker" , ScriptObjects .ENTITY , "Entity that dealt damage." );
30+ b .add ("damage_taken" , Argtypes .NUMBER , "Damage taken." );
31+ b .add ("source" , Argtypes .STRING , "Identifier of Damage Source." );
32+ b .add ("was_blocked" , Argtypes .BOOLEAN , "If true, the damage was blocked." );
33+ }, (b , ctx , script ) -> {
34+ b .add ("entity" , ScriptObjects .LIVING_ENTITY .provideTable (ctx .e , script ));
35+ b .add ("attacker" , ScriptObjects .ENTITY .provideTable (ctx .src .getDirectEntity (), script ));
36+ b .add ("damage_taken" , ctx .damageTaken );
37+
38+ var v = script
39+ .getSource ()
40+ .getServer ()
41+ .registryAccess ()
42+ .get (Registries .DAMAGE_TYPE )
43+ .orElseThrow ()
44+ .value ()
45+ .getKey (ctx .src .type ());
46+
47+ b .add ("source" , v .toString ());
48+ b .add ("was_blocked" , ctx .damageTaken );
49+ });
2050
51+ public static ScriptEvent <EntityDies > ENTITY_DIES = new ScriptEvent <>("Executes after an entity dies." , b -> {
52+ b .add ("entity" , ScriptObjects .LIVING_ENTITY , "Living Entity that died." );
53+ b .add ("attacker" , ScriptObjects .ENTITY , "Entity that killed this one." );
54+ b .add ("source" , Argtypes .STRING , "Identifier of Damage Source." );
55+ }, (b , ctx , script ) -> {
56+ b .add ("entity" , ScriptObjects .LIVING_ENTITY .provideTable (ctx .e , script ));
57+ b .add ("attacker" , ScriptObjects .ENTITY .provideTable (ctx .src .getDirectEntity (), script ));
58+
59+ var v = script
60+ .getSource ()
61+ .getServer ()
62+ .registryAccess ()
63+ .get (Registries .DAMAGE_TYPE )
64+ .orElseThrow ()
65+ .value ()
66+ .getKey (ctx .src .type ());
67+
68+ b .add ("source" , v .toString ());
2169 });
2270
2371
2472
2573 public static void registerAll () {
2674 Registry .register (LuafyRegistries .SCRIPT_EVENTS , Luafy .id ("load" ), LOAD );
2775 Registry .register (LuafyRegistries .SCRIPT_EVENTS , Luafy .id ("tick" ), TICK );
76+ Registry .register (LuafyRegistries .SCRIPT_EVENTS , Luafy .id ("entity_takes_damage" ), ENTITY_TAKES_DAMAGE );
77+ Registry .register (LuafyRegistries .SCRIPT_EVENTS , Luafy .id ("entity_dies" ), ENTITY_DIES );
2878 }
2979
3080
@@ -43,7 +93,27 @@ public static void applyEvents() {
4393 });
4494
4595
96+ // damage
97+ ServerLivingEntityEvents .AFTER_DAMAGE .register ((e , src , baseDmgTaken , dmgTaken , isBlocked ) -> {
98+ ENTITY_TAKES_DAMAGE .trigger (getCommandSourceStack (e ), new ScriptEvents .EntityTakesDamage (e , src , dmgTaken , isBlocked ));
99+ });
100+
101+ ServerLivingEntityEvents .AFTER_DEATH .register ((e , src ) -> {
102+ ENTITY_DIES .trigger (getCommandSourceStack (e ), new ScriptEvents .EntityDies (e , src ));
103+ });
104+
46105
106+
107+ }
108+
109+ public static CommandSourceStack getCommandSourceStack (LivingEntity le ) {
110+ if (le .level ().getServer () == null ) {
111+ throw new RuntimeException ("Tried to get the server of an entity that is not in the server (??)" );
112+ }
113+ return le .level ().getServer ().createCommandSourceStack ();
47114 }
48115
116+ public record EntityTakesDamage (LivingEntity e , DamageSource src , float damageTaken , boolean blocked ) {}
117+ public record EntityDies (LivingEntity e , DamageSource src ) {}
118+
49119}
0 commit comments