|
4 | 4 | import dev.diamond.luafy.script.type.Argtypes; |
5 | 5 | import dev.diamond.luafy.script.event.ScriptEvent; |
6 | 6 | import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents; |
| 7 | +import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents; |
7 | 8 | import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; |
8 | 9 | import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; |
9 | 10 | import net.minecraft.core.Registry; |
| 11 | +import net.minecraft.server.level.ServerPlayer; |
10 | 12 | import net.minecraft.world.damagesource.DamageSource; |
11 | 13 | import net.minecraft.world.entity.LivingEntity; |
12 | 14 | import org.luaj.vm2.LuaValue; |
@@ -52,13 +54,27 @@ public class ScriptEvents { |
52 | 54 |
|
53 | 55 | }); |
54 | 56 |
|
| 57 | + public static ScriptEvent<ServerPlayer> PLAYER_JOINS_SERVER = new ScriptEvent<>("Executes when a player joins the server.", b -> { |
| 58 | + b.add("player", ScriptObjects.PLAYER, "Player that joined."); |
| 59 | + }, (b, ctx, script) -> { |
| 60 | + b.add("player", ScriptObjects.PLAYER.provideTable(ctx, script)); |
| 61 | + }); |
| 62 | + |
| 63 | + public static ScriptEvent<ServerPlayer> PLAYER_LEAVES_SERVER = new ScriptEvent<>("Executes when a player joins the server.", b -> { |
| 64 | + b.add("player", ScriptObjects.PLAYER, "Player that left."); |
| 65 | + }, (b, ctx, script) -> { |
| 66 | + b.add("player", ScriptObjects.PLAYER.provideTable(ctx, script)); |
| 67 | + }); |
| 68 | + |
55 | 69 |
|
56 | 70 |
|
57 | 71 | public static void registerAll() { |
58 | 72 | Registry.register(LuafyRegistries.SCRIPT_EVENTS, Luafy.id("load"), LOAD); |
59 | 73 | Registry.register(LuafyRegistries.SCRIPT_EVENTS, Luafy.id("tick"), TICK); |
60 | 74 | Registry.register(LuafyRegistries.SCRIPT_EVENTS, Luafy.id("entity_takes_damage"), ENTITY_TAKES_DAMAGE); |
61 | 75 | Registry.register(LuafyRegistries.SCRIPT_EVENTS, Luafy.id("entity_dies"), ENTITY_DIES); |
| 76 | + Registry.register(LuafyRegistries.SCRIPT_EVENTS, Luafy.id("player_joins_server"), PLAYER_JOINS_SERVER); |
| 77 | + Registry.register(LuafyRegistries.SCRIPT_EVENTS, Luafy.id("player_leaves_server"), PLAYER_LEAVES_SERVER); |
62 | 78 | } |
63 | 79 |
|
64 | 80 |
|
@@ -86,11 +102,16 @@ public static void applyEvents() { |
86 | 102 | ENTITY_DIES.trigger(e.level().getServer().createCommandSourceStack(), new ScriptEvents.EntityDies(e, src)); |
87 | 103 | }); |
88 | 104 |
|
89 | | - |
| 105 | + // join/leave server |
| 106 | + ServerPlayerEvents.JOIN.register(player -> { |
| 107 | + PLAYER_JOINS_SERVER.trigger(player.level().getServer().createCommandSourceStack(), player); |
| 108 | + }); |
| 109 | + ServerPlayerEvents.LEAVE.register(player -> { |
| 110 | + PLAYER_JOINS_SERVER.trigger(player.level().getServer().createCommandSourceStack(), player); |
| 111 | + }); |
90 | 112 |
|
91 | 113 | } |
92 | 114 |
|
93 | 115 | public record EntityTakesDamage(LivingEntity e, DamageSource src, float damageTaken, boolean blocked) {} |
94 | 116 | public record EntityDies(LivingEntity e, DamageSource src) {} |
95 | | - |
96 | 117 | } |
0 commit comments