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 Jul 3, 2024
1 parent 9d2ccdf commit cb79ab1
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
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;
}
}
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 @@ -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
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
});
});
}

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

}

0 comments on commit cb79ab1

Please sign in to comment.