Skip to content

Commit

Permalink
feat: Add LivingDeathEvent and EntityAddedEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Feb 28, 2024
1 parent 1d7cf6a commit 2516eae
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -119,5 +121,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
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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.player.AttackEntityEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
Expand Down Expand Up @@ -255,6 +256,26 @@ public static void registerEvents(ForgeBalmEvents events) {
events.fireEventHandlers(priority, event);
});
});

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);
}
});
});
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 2516eae

Please sign in to comment.