From cb79ab1b86b71371ca0fc917470577f4656650f7 Mon Sep 17 00:00:00 2001 From: BlayTheNinth <1933180+BlayTheNinth@users.noreply.github.com> Date: Wed, 28 Feb 2024 21:17:50 +0100 Subject: [PATCH] feat: Add LivingDeathEvent and EntityAddedEvent --- .../mods/balm/api/event/EntityAddedEvent.java | 22 +++++++++++++++++++ .../mods/balm/api/event/LivingDeathEvent.java | 22 +++++++++++++++++++ .../fabric/event/FabricBalmCommonEvents.java | 14 ++++++++++++ .../forge/event/ForgeBalmCommonEvents.java | 21 ++++++++++++++++++ .../event/NeoForgeBalmCommonEvents.java | 21 ++++++++++++++++++ 5 files changed, 100 insertions(+) create mode 100644 common/src/main/java/net/blay09/mods/balm/api/event/EntityAddedEvent.java create mode 100644 common/src/main/java/net/blay09/mods/balm/api/event/LivingDeathEvent.java diff --git a/common/src/main/java/net/blay09/mods/balm/api/event/EntityAddedEvent.java b/common/src/main/java/net/blay09/mods/balm/api/event/EntityAddedEvent.java new file mode 100644 index 00000000..1a38df2c --- /dev/null +++ b/common/src/main/java/net/blay09/mods/balm/api/event/EntityAddedEvent.java @@ -0,0 +1,22 @@ +package net.blay09.mods.balm.api.event; + +import net.minecraft.world.entity.Entity; +import net.minecraft.world.level.Level; + +public class EntityAddedEvent extends BalmEvent { + private final Entity entity; + private final Level level; + + public EntityAddedEvent(Entity entity, Level level) { + this.entity = entity; + this.level = level; + } + + public Entity getEntity() { + return entity; + } + + public Level getLevel() { + return level; + } +} diff --git a/common/src/main/java/net/blay09/mods/balm/api/event/LivingDeathEvent.java b/common/src/main/java/net/blay09/mods/balm/api/event/LivingDeathEvent.java new file mode 100644 index 00000000..d2f612c4 --- /dev/null +++ b/common/src/main/java/net/blay09/mods/balm/api/event/LivingDeathEvent.java @@ -0,0 +1,22 @@ +package net.blay09.mods.balm.api.event; + +import net.minecraft.world.damagesource.DamageSource; +import net.minecraft.world.entity.LivingEntity; + +public class LivingDeathEvent extends BalmEvent { + private final LivingEntity entity; + private final DamageSource damageSource; + + public LivingDeathEvent(LivingEntity entity, DamageSource damageSource) { + this.entity = entity; + this.damageSource = damageSource; + } + + public LivingEntity getEntity() { + return entity; + } + + public DamageSource getDamageSource() { + return damageSource; + } +} diff --git a/fabric/src/main/java/net/blay09/mods/balm/fabric/event/FabricBalmCommonEvents.java b/fabric/src/main/java/net/blay09/mods/balm/fabric/event/FabricBalmCommonEvents.java index b3e0e23a..86ef3b54 100644 --- a/fabric/src/main/java/net/blay09/mods/balm/fabric/event/FabricBalmCommonEvents.java +++ b/fabric/src/main/java/net/blay09/mods/balm/fabric/event/FabricBalmCommonEvents.java @@ -4,7 +4,9 @@ import net.blay09.mods.balm.api.event.*; import net.blay09.mods.balm.api.event.server.ServerStartedEvent; import net.blay09.mods.balm.api.event.server.ServerStoppedEvent; +import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents; import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents; @@ -159,5 +161,17 @@ public static void registerEvents(FabricBalmEvents events) { final PlayerRespawnEvent event = new PlayerRespawnEvent(oldPlayer, newPlayer); events.fireEventHandlers(event); })); + + events.registerEvent(EntityAddedEvent.class, () -> ServerLivingEntityEvents.ALLOW_DEATH.register((entity, damageSource, damageAmount) -> { + final var event = new LivingDeathEvent(entity, damageSource); + events.fireEventHandlers(event); + return !event.isCanceled(); + })); + + events.registerEvent(EntityAddedEvent.class, () -> ServerEntityEvents.ENTITY_LOAD.register((entity, level) -> { + final EntityAddedEvent event = new EntityAddedEvent(entity, level); + events.fireEventHandlers(event); + // TODO cannot cancel on fabric + })); } } diff --git a/forge/src/main/java/net/blay09/mods/balm/forge/event/ForgeBalmCommonEvents.java b/forge/src/main/java/net/blay09/mods/balm/forge/event/ForgeBalmCommonEvents.java index 9c956783..39bdef0f 100644 --- a/forge/src/main/java/net/blay09/mods/balm/forge/event/ForgeBalmCommonEvents.java +++ b/forge/src/main/java/net/blay09/mods/balm/forge/event/ForgeBalmCommonEvents.java @@ -13,6 +13,7 @@ import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.TickEvent; +import net.minecraftforge.event.entity.EntityJoinLevelEvent; import net.minecraftforge.event.entity.item.ItemTossEvent; import net.minecraftforge.event.entity.living.LivingEvent; import net.minecraftforge.event.entity.player.AttackEntityEvent; @@ -320,6 +321,26 @@ public static void registerEvents(ForgeBalmEvents events) { } }); }); + + events.registerEvent(LivingDeathEvent.class, priority -> { + MinecraftForge.EVENT_BUS.addListener(ForgeBalmEvents.toForge(priority), (net.minecraftforge.event.entity.living.LivingDeathEvent orig) -> { + final LivingDeathEvent event = new LivingDeathEvent(orig.getEntity(), orig.getSource()); + events.fireEventHandlers(priority, event); + if (event.isCanceled()) { + orig.setCanceled(true); + } + }); + }); + + events.registerEvent(EntityAddedEvent.class, priority -> { + MinecraftForge.EVENT_BUS.addListener(ForgeBalmEvents.toForge(priority), (EntityJoinLevelEvent orig) -> { + final EntityAddedEvent event = new EntityAddedEvent(orig.getEntity(), orig.getLevel()); + events.fireEventHandlers(priority, event); + if (event.isCanceled()) { + orig.setCanceled(true); + } + }); + }); } } diff --git a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/event/NeoForgeBalmCommonEvents.java b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/event/NeoForgeBalmCommonEvents.java index 8239dcba..cfef7be8 100644 --- a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/event/NeoForgeBalmCommonEvents.java +++ b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/event/NeoForgeBalmCommonEvents.java @@ -11,6 +11,7 @@ import net.neoforged.bus.api.Event; import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.common.damagesource.DamageContainer; +import net.neoforged.neoforge.event.entity.EntityJoinLevelEvent; import net.neoforged.neoforge.event.entity.item.ItemTossEvent; import net.neoforged.neoforge.event.entity.player.AttackEntityEvent; import net.neoforged.neoforge.event.entity.player.PlayerEvent; @@ -276,6 +277,26 @@ public static void registerEvents(NeoForgeBalmEvents events) { } }); }); + + events.registerEvent(LivingDeathEvent.class, priority -> { + NeoForge.EVENT_BUS.addListener(NeoForgeBalmEvents.toForge(priority), (net.neoforged.neoforge.event.entity.living.LivingDeathEvent orig) -> { + final LivingDeathEvent event = new LivingDeathEvent(orig.getEntity(), orig.getSource()); + events.fireEventHandlers(priority, event); + if (event.isCanceled()) { + orig.setCanceled(true); + } + }); + }); + + events.registerEvent(EntityAddedEvent.class, priority -> { + NeoForge.EVENT_BUS.addListener(NeoForgeBalmEvents.toForge(priority), (EntityJoinLevelEvent orig) -> { + final EntityAddedEvent event = new EntityAddedEvent(orig.getEntity(), orig.getLevel()); + events.fireEventHandlers(priority, event); + if (event.isCanceled()) { + orig.setCanceled(true); + } + }); + }); } }