From b2475b98c9ebd5363e555b21b08cde27c02f4e89 Mon Sep 17 00:00:00 2001 From: BlayTheNinth <1933180+BlayTheNinth@users.noreply.github.com> Date: Fri, 24 May 2024 13:33:21 +0200 Subject: [PATCH] feat: Add entity tick handlers --- .../balm/api/event/EntityTickHandler.java | 7 ++ .../blay09/mods/balm/api/event/TickType.java | 1 + .../fabric/event/FabricBalmCommonEvents.java | 56 +++++++++-- .../forge/event/ForgeBalmCommonEvents.java | 95 +++++++++++++++---- .../event/NeoForgeBalmCommonEvents.java | 13 +++ 5 files changed, 144 insertions(+), 28 deletions(-) create mode 100644 common/src/main/java/net/blay09/mods/balm/api/event/EntityTickHandler.java diff --git a/common/src/main/java/net/blay09/mods/balm/api/event/EntityTickHandler.java b/common/src/main/java/net/blay09/mods/balm/api/event/EntityTickHandler.java new file mode 100644 index 00000000..984d4040 --- /dev/null +++ b/common/src/main/java/net/blay09/mods/balm/api/event/EntityTickHandler.java @@ -0,0 +1,7 @@ +package net.blay09.mods.balm.api.event; + +import net.minecraft.world.entity.Entity; + +public interface EntityTickHandler { + void handle(Entity entity); +} diff --git a/common/src/main/java/net/blay09/mods/balm/api/event/TickType.java b/common/src/main/java/net/blay09/mods/balm/api/event/TickType.java index 1c29cad2..6b66c6ec 100644 --- a/common/src/main/java/net/blay09/mods/balm/api/event/TickType.java +++ b/common/src/main/java/net/blay09/mods/balm/api/event/TickType.java @@ -9,4 +9,5 @@ public class TickType { public static final TickType Server = new TickType<>(); public static final TickType ServerLevel = new TickType<>(); public static final TickType ServerPlayer = new TickType<>(); + public static final TickType Entity = new TickType<>(); } 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 e55265e2..b3e0e23a 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 @@ -22,8 +22,12 @@ public class FabricBalmCommonEvents { private static final List playerTickStartHandlers = new ArrayList<>(); private static final List playerTickEndHandlers = new ArrayList<>(); - private static ServerTickEvents.StartTick serverTickStartListener = null; - private static ServerTickEvents.EndTick serverTickEndListener = null; + private static final List entityTickStartHandlers = new ArrayList<>(); + private static final List 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)); @@ -36,8 +40,8 @@ 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); @@ -45,15 +49,15 @@ public static void registerEvents(FabricBalmEvents events) { } }; - 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); @@ -61,12 +65,48 @@ public static void registerEvents(FabricBalmEvents events) { } }; - 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); 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 7f3ce02d..9c956783 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 @@ -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; @@ -69,6 +72,18 @@ 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()); @@ -76,14 +91,18 @@ public static void registerEvents(ForgeBalmEvents events) { }); }); - 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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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()); @@ -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()); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); 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 bb65747f..c4fd406b 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 @@ -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; @@ -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());