Skip to content

Commit

Permalink
feat: Add entity tick handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed May 24, 2024
1 parent a7f4b67 commit b2475b9
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.blay09.mods.balm.api.event;

import net.minecraft.world.entity.Entity;

public interface EntityTickHandler {
void handle(Entity entity);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public class TickType<T> {
public static final TickType<ServerTickHandler> Server = new TickType<>();
public static final TickType<ServerLevelTickHandler> ServerLevel = new TickType<>();
public static final TickType<ServerPlayerTickHandler> ServerPlayer = new TickType<>();
public static final TickType<EntityTickHandler> Entity = new TickType<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ public class FabricBalmCommonEvents {

private static final List<ServerPlayerTickHandler> playerTickStartHandlers = new ArrayList<>();
private static final List<ServerPlayerTickHandler> playerTickEndHandlers = new ArrayList<>();
private static ServerTickEvents.StartTick serverTickStartListener = null;
private static ServerTickEvents.EndTick serverTickEndListener = null;
private static final List<EntityTickHandler> entityTickStartHandlers = new ArrayList<>();
private static final List<EntityTickHandler> entityTickEndHandlers = new ArrayList<>();
private static ServerTickEvents.StartTick serverTickPlayersStartListener = null;
private static ServerTickEvents.EndTick serverTickPlayersEndListener = null;
private static ServerTickEvents.StartTick serverTickEntitiesStartListener = null;
private static ServerTickEvents.EndTick serverTickEntitiesEndListener = null;

public static void registerEvents(FabricBalmEvents events) {
events.registerTickEvent(TickType.Server, TickPhase.Start, (ServerTickHandler handler) -> ServerTickEvents.START_SERVER_TICK.register(handler::handle));
Expand All @@ -36,37 +40,73 @@ public static void registerEvents(FabricBalmEvents events) {
(ServerLevelTickHandler handler) -> ServerTickEvents.END_WORLD_TICK.register(handler::handle));

events.registerTickEvent(TickType.ServerPlayer, TickPhase.Start, (ServerPlayerTickHandler handler) -> {
if (serverTickStartListener == null) {
serverTickStartListener = server -> {
if (serverTickPlayersStartListener == null) {
serverTickPlayersStartListener = server -> {
for (ServerPlayer player : server.getPlayerList().getPlayers()) {
for (ServerPlayerTickHandler playerTickHandler : playerTickStartHandlers) {
playerTickHandler.handle(player);
}
}
};

ServerTickEvents.START_SERVER_TICK.register(serverTickStartListener);
ServerTickEvents.START_SERVER_TICK.register(serverTickPlayersStartListener);
}

playerTickStartHandlers.add(handler);
});

events.registerTickEvent(TickType.ServerPlayer, TickPhase.End, (ServerPlayerTickHandler handler) -> {
if (serverTickEndListener == null) {
serverTickEndListener = server -> {
if (serverTickPlayersEndListener == null) {
serverTickPlayersEndListener = server -> {
for (ServerPlayer player : server.getPlayerList().getPlayers()) {
for (ServerPlayerTickHandler playerTickHandler : playerTickEndHandlers) {
playerTickHandler.handle(player);
}
}
};

ServerTickEvents.END_SERVER_TICK.register(serverTickEndListener);
ServerTickEvents.END_SERVER_TICK.register(serverTickPlayersEndListener);
}

playerTickEndHandlers.add(handler);
});

events.registerTickEvent(TickType.Entity, TickPhase.Start, (EntityTickHandler handler) -> {
if (serverTickEntitiesStartListener == null) {
serverTickEntitiesStartListener = server -> {
for (final var level : server.getAllLevels()) {
for (final var entity : level.getAllEntities()) {
for (final var entityTickHandler : entityTickStartHandlers) {
entityTickHandler.handle(entity);
}
}
}
};

ServerTickEvents.START_SERVER_TICK.register(serverTickEntitiesStartListener);
}

entityTickStartHandlers.add(handler);
});

events.registerTickEvent(TickType.Entity, TickPhase.End, (EntityTickHandler handler) -> {
if (serverTickEntitiesEndListener == null) {
serverTickEntitiesEndListener = server -> {
for (final var level : server.getAllLevels()) {
for (final var entity : level.getAllEntities()) {
for (final var entityTickHandler : entityTickEndHandlers) {
entityTickHandler.handle(entity);
}
}
}
};

ServerTickEvents.END_SERVER_TICK.register(serverTickEntitiesEndListener);
}

entityTickEndHandlers.add(handler);
});

events.registerEvent(ServerStartedEvent.class, () -> ServerLifecycleEvents.SERVER_STARTED.register(server -> {
final ServerStartedEvent event = new ServerStartedEvent(server);
events.fireEventHandlers(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
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.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.damagesource.DamageSources;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.item.ItemTossEvent;
import net.minecraftforge.event.entity.living.LivingEvent;
import net.minecraftforge.event.entity.player.AttackEntityEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
Expand Down Expand Up @@ -69,21 +72,37 @@ public static void registerEvents(ForgeBalmEvents events) {
});
});

events.registerTickEvent(TickType.Entity, TickPhase.Start, (EntityTickHandler handler) -> {
MinecraftForge.EVENT_BUS.addListener((LivingEvent.LivingTickEvent orig) -> { // TODO unlike Fabric and NeoForge, only ticks for living entities
handler.handle(orig.getEntity());
});
});

events.registerTickEvent(TickType.Entity, TickPhase.End, (EntityTickHandler handler) -> {
MinecraftForge.EVENT_BUS.addListener((LivingEvent.LivingTickEvent orig) -> { // TODO ticks at same time as START on Forge
handler.handle(orig.getEntity());
});
});

events.registerEvent(ServerStartedEvent.class, priority -> {
MinecraftForge.EVENT_BUS.addListener(ForgeBalmEvents.toForge(priority), (net.minecraftforge.event.server.ServerStartedEvent orig) -> {
final ServerStartedEvent event = new ServerStartedEvent(orig.getServer());
events.fireEventHandlers(priority, event);
});
});

events.registerEvent(ServerStoppedEvent.class, priority -> {
events.registerEvent(ServerStoppedEvent.class, priority ->

{
MinecraftForge.EVENT_BUS.addListener(ForgeBalmEvents.toForge(priority), (net.minecraftforge.event.server.ServerStoppedEvent orig) -> {
final ServerStoppedEvent event = new ServerStoppedEvent(orig.getServer());
events.fireEventHandlers(priority, event);
});
});

events.registerEvent(UseBlockEvent.class, priority -> {
events.registerEvent(UseBlockEvent.class, priority ->

{
MinecraftForge.EVENT_BUS.addListener(ForgeBalmEvents.toForge(priority), (PlayerInteractEvent.RightClickBlock orig) -> {
final UseBlockEvent event = new UseBlockEvent(orig.getEntity(), orig.getLevel(), orig.getHand(), orig.getHitVec());
events.fireEventHandlers(priority, event);
Expand All @@ -94,7 +113,9 @@ public static void registerEvents(ForgeBalmEvents events) {
});
});

events.registerEvent(UseItemEvent.class, priority -> {
events.registerEvent(UseItemEvent.class, priority ->

{
MinecraftForge.EVENT_BUS.addListener(ForgeBalmEvents.toForge(priority), (PlayerInteractEvent.RightClickItem orig) -> {
final UseItemEvent event = new UseItemEvent(orig.getEntity(), orig.getLevel(), orig.getHand());
events.fireEventHandlers(priority, event);
Expand All @@ -105,21 +126,27 @@ public static void registerEvents(ForgeBalmEvents events) {
});
});

events.registerEvent(PlayerLoginEvent.class, priority -> {
events.registerEvent(PlayerLoginEvent.class, priority ->

{
MinecraftForge.EVENT_BUS.addListener(ForgeBalmEvents.toForge(priority), (PlayerEvent.PlayerLoggedInEvent orig) -> {
final PlayerLoginEvent event = new PlayerLoginEvent((ServerPlayer) orig.getEntity());
events.fireEventHandlers(priority, event);
});
});

events.registerEvent(PlayerLogoutEvent.class, priority -> {
events.registerEvent(PlayerLogoutEvent.class, priority ->

{
MinecraftForge.EVENT_BUS.addListener(ForgeBalmEvents.toForge(priority), (PlayerEvent.PlayerLoggedOutEvent orig) -> {
final PlayerLogoutEvent event = new PlayerLogoutEvent((ServerPlayer) orig.getEntity());
events.fireEventHandlers(priority, event);
});
});

events.registerEvent(BreakBlockEvent.class, priority -> {
events.registerEvent(BreakBlockEvent.class, priority ->

{
MinecraftForge.EVENT_BUS.addListener(ForgeBalmEvents.toForge(priority), (BlockEvent.BreakEvent orig) -> {
BlockEntity blockEntity = orig.getLevel().getBlockEntity(orig.getPos());
final BreakBlockEvent event = new BreakBlockEvent((Level) orig.getLevel(), orig.getPlayer(), orig.getPos(), orig.getState(), blockEntity);
Expand All @@ -130,14 +157,18 @@ public static void registerEvents(ForgeBalmEvents events) {
});
});

events.registerEvent(PlayerRespawnEvent.class, priority -> {
events.registerEvent(PlayerRespawnEvent.class, priority ->

{
MinecraftForge.EVENT_BUS.addListener(ForgeBalmEvents.toForge(priority), (PlayerEvent.PlayerRespawnEvent orig) -> {
final PlayerRespawnEvent event = new PlayerRespawnEvent(((ServerPlayer) orig.getEntity()), (ServerPlayer) orig.getEntity());
events.fireEventHandlers(priority, event);
});
});

events.registerEvent(LivingFallEvent.class, priority -> {
events.registerEvent(LivingFallEvent.class, priority ->

{
MinecraftForge.EVENT_BUS.addListener(ForgeBalmEvents.toForge(priority), (net.minecraftforge.event.entity.living.LivingFallEvent orig) -> {
final LivingFallEvent event = new LivingFallEvent(orig.getEntity());
events.fireEventHandlers(priority, event);
Expand All @@ -153,7 +184,9 @@ public static void registerEvents(ForgeBalmEvents events) {
});
});

events.registerEvent(LivingDamageEvent.class, priority -> {
events.registerEvent(LivingDamageEvent.class, priority ->

{
MinecraftForge.EVENT_BUS.addListener(ForgeBalmEvents.toForge(priority), (net.minecraftforge.event.entity.living.LivingDamageEvent orig) -> {
final LivingDamageEvent event = new LivingDamageEvent(orig.getEntity(), orig.getSource(), orig.getAmount());
events.fireEventHandlers(priority, event);
Expand All @@ -164,7 +197,9 @@ public static void registerEvents(ForgeBalmEvents events) {
});
});

events.registerEvent(CropGrowEvent.Pre.class, priority -> {
events.registerEvent(CropGrowEvent.Pre.class, priority ->

{
MinecraftForge.EVENT_BUS.addListener(ForgeBalmEvents.toForge(priority), (BlockEvent.CropGrowEvent.Pre orig) -> {
if (orig.getLevel() instanceof Level level) {
final CropGrowEvent.Pre event = new CropGrowEvent.Pre(level, orig.getPos(), orig.getState());
Expand All @@ -176,7 +211,9 @@ public static void registerEvents(ForgeBalmEvents events) {
});
});

events.registerEvent(CropGrowEvent.Post.class, priority -> {
events.registerEvent(CropGrowEvent.Post.class, priority ->

{
MinecraftForge.EVENT_BUS.addListener(ForgeBalmEvents.toForge(priority), (BlockEvent.CropGrowEvent.Post orig) -> {
if (orig.getLevel() instanceof Level level) {
final CropGrowEvent.Post event = new CropGrowEvent.Post(level, orig.getPos(), orig.getState());
Expand All @@ -185,21 +222,27 @@ public static void registerEvents(ForgeBalmEvents events) {
});
});

events.registerEvent(ChunkTrackingEvent.Start.class, priority -> {
events.registerEvent(ChunkTrackingEvent.Start.class, priority ->

{
MinecraftForge.EVENT_BUS.addListener(ForgeBalmEvents.toForge(priority), (ChunkWatchEvent.Watch orig) -> {
final ChunkTrackingEvent.Start event = new ChunkTrackingEvent.Start(orig.getLevel(), orig.getPlayer(), orig.getPos());
events.fireEventHandlers(priority, event);
});
});

events.registerEvent(ChunkTrackingEvent.Stop.class, priority -> {
events.registerEvent(ChunkTrackingEvent.Stop.class, priority ->

{
MinecraftForge.EVENT_BUS.addListener(ForgeBalmEvents.toForge(priority), (ChunkWatchEvent.UnWatch orig) -> {
final ChunkTrackingEvent.Stop event = new ChunkTrackingEvent.Stop(orig.getLevel(), orig.getPlayer(), orig.getPos());
events.fireEventHandlers(priority, event);
});
});

events.registerEvent(TossItemEvent.class, priority -> {
events.registerEvent(TossItemEvent.class, priority ->

{
MinecraftForge.EVENT_BUS.addListener(ForgeBalmEvents.toForge(priority), (ItemTossEvent orig) -> {
final TossItemEvent event = new TossItemEvent(orig.getPlayer(), orig.getEntity().getItem());
events.fireEventHandlers(priority, event);
Expand All @@ -209,7 +252,9 @@ public static void registerEvents(ForgeBalmEvents events) {
});
});

events.registerEvent(PlayerAttackEvent.class, priority -> {
events.registerEvent(PlayerAttackEvent.class, priority ->

{
MinecraftForge.EVENT_BUS.addListener(ForgeBalmEvents.toForge(priority), (AttackEntityEvent orig) -> {
final PlayerAttackEvent event = new PlayerAttackEvent(orig.getEntity(), orig.getTarget());
events.fireEventHandlers(priority, event);
Expand All @@ -219,7 +264,9 @@ public static void registerEvents(ForgeBalmEvents events) {
});
});

events.registerEvent(LivingHealEvent.class, priority -> {
events.registerEvent(LivingHealEvent.class, priority ->

{
MinecraftForge.EVENT_BUS.addListener(ForgeBalmEvents.toForge(priority), (net.minecraftforge.event.entity.living.LivingHealEvent orig) -> {
final LivingHealEvent event = new LivingHealEvent(orig.getEntity(), orig.getAmount());
events.fireEventHandlers(priority, event);
Expand All @@ -229,7 +276,9 @@ public static void registerEvents(ForgeBalmEvents events) {
});
});

events.registerEvent(DigSpeedEvent.class, priority -> {
events.registerEvent(DigSpeedEvent.class, priority ->

{
MinecraftForge.EVENT_BUS.addListener(ForgeBalmEvents.toForge(priority), (PlayerEvent.BreakSpeed orig) -> {
final DigSpeedEvent event = new DigSpeedEvent(orig.getEntity(), orig.getState(), orig.getOriginalSpeed());
events.fireEventHandlers(priority, event);
Expand All @@ -242,21 +291,27 @@ public static void registerEvents(ForgeBalmEvents events) {
});
});

events.registerEvent(PlayerChangedDimensionEvent.class, priority -> {
events.registerEvent(PlayerChangedDimensionEvent.class, priority ->

{
MinecraftForge.EVENT_BUS.addListener(ForgeBalmEvents.toForge(priority), (PlayerEvent.PlayerChangedDimensionEvent orig) -> {
final PlayerChangedDimensionEvent event = new PlayerChangedDimensionEvent((ServerPlayer) orig.getEntity(), orig.getFrom(), orig.getTo());
events.fireEventHandlers(priority, event);
});
});

events.registerEvent(ItemCraftedEvent.class, priority -> {
events.registerEvent(ItemCraftedEvent.class, priority ->

{
MinecraftForge.EVENT_BUS.addListener(ForgeBalmEvents.toForge(priority), (PlayerEvent.ItemCraftedEvent orig) -> {
final ItemCraftedEvent event = new ItemCraftedEvent(orig.getEntity(), orig.getCrafting(), orig.getInventory());
events.fireEventHandlers(priority, event);
});
});

events.registerEvent(CommandEvent.class, priority -> {
events.registerEvent(CommandEvent.class, priority ->

{
MinecraftForge.EVENT_BUS.addListener(ForgeBalmEvents.toForge(priority), (net.minecraftforge.event.CommandEvent orig) -> {
final CommandEvent event = new CommandEvent(orig.getParseResults());
events.fireEventHandlers(priority, event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent;
import net.neoforged.neoforge.event.level.BlockEvent;
import net.neoforged.neoforge.event.level.ChunkWatchEvent;
import net.neoforged.neoforge.event.tick.EntityTickEvent;
import net.neoforged.neoforge.event.tick.LevelTickEvent;
import net.neoforged.neoforge.event.tick.PlayerTickEvent;
import net.neoforged.neoforge.event.tick.ServerTickEvent;
Expand Down Expand Up @@ -65,6 +66,18 @@ public static void registerEvents(NeoForgeBalmEvents events) {
});
});

events.registerTickEvent(TickType.Entity, TickPhase.Start, (EntityTickHandler handler) -> {
NeoForge.EVENT_BUS.addListener((EntityTickEvent.Pre orig) -> {
handler.handle(orig.getEntity());
});
});

events.registerTickEvent(TickType.Entity, TickPhase.End, (EntityTickHandler handler) -> {
NeoForge.EVENT_BUS.addListener((EntityTickEvent.Post orig) -> {
handler.handle(orig.getEntity());
});
});

events.registerEvent(ServerStartedEvent.class, priority -> {
NeoForge.EVENT_BUS.addListener(NeoForgeBalmEvents.toForge(priority), (net.neoforged.neoforge.event.server.ServerStartedEvent orig) -> {
final ServerStartedEvent event = new ServerStartedEvent(orig.getServer());
Expand Down

0 comments on commit b2475b9

Please sign in to comment.